2003-10-05 04:26:39 +00:00
|
|
|
//===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-12 17:16:22 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// This file promotes memory references to be register references. It promotes
|
2004-09-19 18:51:51 +00:00
|
|
|
// alloca instructions which only have loads and stores as uses. An alloca is
|
2011-01-17 17:38:41 +00:00
|
|
|
// transformed by using iterated dominator frontiers to place PHI nodes, then
|
|
|
|
// traversing the function in depth-first order to rewrite loads and stores as
|
|
|
|
// appropriate.
|
|
|
|
//
|
|
|
|
// The algorithm used here is based on:
|
|
|
|
//
|
|
|
|
// Sreedhar and Gao. A linear time algorithm for placing phi-nodes.
|
|
|
|
// In Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of
|
|
|
|
// Programming Languages
|
|
|
|
// POPL '95. ACM, New York, NY, 62-73.
|
|
|
|
//
|
|
|
|
// It has been modified to not explicitly use the DJ graph data structure and to
|
|
|
|
// directly compute pruned SSA using per-variable liveness information.
|
2002-02-12 17:16:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-08-04 01:41:18 +00:00
|
|
|
#define DEBUG_TYPE "mem2reg"
|
2003-02-22 23:57:48 +00:00
|
|
|
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
|
2004-10-16 18:10:06 +00:00
|
|
|
#include "llvm/Constants.h"
|
2004-09-15 01:02:54 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2008-11-07 01:30:07 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2010-01-26 02:42:15 +00:00
|
|
|
#include "llvm/Metadata.h"
|
2010-11-16 17:41:24 +00:00
|
|
|
#include "llvm/Analysis/AliasSetTracker.h"
|
2010-01-26 02:42:15 +00:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2011-02-24 18:49:55 +00:00
|
|
|
#include "llvm/Analysis/DIBuilder.h"
|
2011-01-18 03:53:26 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2010-11-16 17:41:24 +00:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2011-06-27 04:20:45 +00:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2011-03-17 21:58:19 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2007-02-05 23:37:20 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-03-05 11:29:56 +00:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2007-02-05 22:13:11 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-02-05 21:58:48 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-08-04 01:41:18 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-11-07 01:59:41 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2003-04-18 19:25:22 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2004-09-03 18:19:51 +00:00
|
|
|
#include <algorithm>
|
2011-01-17 17:38:41 +00:00
|
|
|
#include <queue>
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2007-08-04 01:41:18 +00:00
|
|
|
STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block");
|
|
|
|
STATISTIC(NumSingleStore, "Number of alloca's promoted with a single store");
|
|
|
|
STATISTIC(NumDeadAlloca, "Number of dead alloca's removed");
|
2007-08-04 22:50:14 +00:00
|
|
|
STATISTIC(NumPHIInsert, "Number of PHI nodes inserted");
|
2007-08-04 01:41:18 +00:00
|
|
|
|
2007-02-07 01:15:04 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
2007-09-17 18:34:04 +00:00
|
|
|
struct DenseMapInfo<std::pair<BasicBlock*, unsigned> > {
|
|
|
|
typedef std::pair<BasicBlock*, unsigned> EltTy;
|
|
|
|
static inline EltTy getEmptyKey() {
|
|
|
|
return EltTy(reinterpret_cast<BasicBlock*>(-1), ~0U);
|
2007-02-07 01:15:04 +00:00
|
|
|
}
|
2007-09-17 18:34:04 +00:00
|
|
|
static inline EltTy getTombstoneKey() {
|
|
|
|
return EltTy(reinterpret_cast<BasicBlock*>(-2), 0U);
|
2007-02-07 01:15:04 +00:00
|
|
|
}
|
|
|
|
static unsigned getHashValue(const std::pair<BasicBlock*, unsigned> &Val) {
|
2012-03-05 11:29:56 +00:00
|
|
|
using llvm::hash_value;
|
|
|
|
return static_cast<unsigned>(hash_value(Val));
|
2007-09-17 18:34:04 +00:00
|
|
|
}
|
|
|
|
static bool isEqual(const EltTy &LHS, const EltTy &RHS) {
|
|
|
|
return LHS == RHS;
|
2007-02-07 01:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-02-22 23:57:48 +00:00
|
|
|
/// isAllocaPromotable - Return true if this alloca is legal for promotion.
|
2004-09-19 18:51:51 +00:00
|
|
|
/// This is true if there are only loads and stores to the alloca.
|
2003-02-22 23:57:48 +00:00
|
|
|
///
|
2007-04-25 17:15:20 +00:00
|
|
|
bool llvm::isAllocaPromotable(const AllocaInst *AI) {
|
2003-03-03 17:25:18 +00:00
|
|
|
// FIXME: If the memory unit is of pointer or integer type, we can permit
|
|
|
|
// assignments to subsections of the memory unit.
|
|
|
|
|
2007-08-26 21:43:30 +00:00
|
|
|
// Only allow direct and non-volatile loads and stores...
|
2010-03-25 23:06:16 +00:00
|
|
|
for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end();
|
2010-07-09 14:18:23 +00:00
|
|
|
UI != UE; ++UI) { // Loop over all of the uses of the alloca
|
|
|
|
const User *U = *UI;
|
|
|
|
if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
|
2011-08-15 23:55:52 +00:00
|
|
|
// Note that atomic loads can be transformed; atomic semantics do
|
|
|
|
// not have any meaning for a local alloca.
|
2007-08-26 21:43:30 +00:00
|
|
|
if (LI->isVolatile())
|
|
|
|
return false;
|
2010-07-09 14:18:23 +00:00
|
|
|
} else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
|
2004-01-12 01:18:32 +00:00
|
|
|
if (SI->getOperand(0) == AI)
|
|
|
|
return false; // Don't allow a store OF the AI, only INTO the AI.
|
2011-08-15 23:55:52 +00:00
|
|
|
// Note that atomic stores can be transformed; atomic semantics do
|
|
|
|
// not have any meaning for a local alloca.
|
2007-08-26 21:43:30 +00:00
|
|
|
if (SI->isVolatile())
|
|
|
|
return false;
|
2011-06-17 10:09:00 +00:00
|
|
|
} else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
|
|
|
|
if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
|
|
|
|
II->getIntrinsicID() != Intrinsic::lifetime_end)
|
|
|
|
return false;
|
|
|
|
} else if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
|
|
|
|
if (BCI->getType() != Type::getInt8PtrTy(U->getContext()))
|
|
|
|
return false;
|
|
|
|
if (!onlyUsedByLifetimeMarkers(BCI))
|
|
|
|
return false;
|
|
|
|
} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
|
|
|
|
if (GEPI->getType() != Type::getInt8PtrTy(U->getContext()))
|
|
|
|
return false;
|
|
|
|
if (!GEPI->hasAllZeroIndices())
|
|
|
|
return false;
|
|
|
|
if (!onlyUsedByLifetimeMarkers(GEPI))
|
|
|
|
return false;
|
2004-01-12 01:18:32 +00:00
|
|
|
} else {
|
2008-11-08 04:12:17 +00:00
|
|
|
return false;
|
2004-01-12 01:18:32 +00:00
|
|
|
}
|
2010-07-09 14:18:23 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-02-22 23:57:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-03-27 23:28:40 +00:00
|
|
|
namespace {
|
2007-08-04 01:47:41 +00:00
|
|
|
struct AllocaInfo;
|
2007-03-09 23:39:14 +00:00
|
|
|
|
|
|
|
// Data package used by RenamePass()
|
2009-10-25 06:33:48 +00:00
|
|
|
class RenamePassData {
|
2007-03-09 23:39:14 +00:00
|
|
|
public:
|
2007-08-04 01:19:38 +00:00
|
|
|
typedef std::vector<Value *> ValVector;
|
|
|
|
|
2009-12-13 07:04:45 +00:00
|
|
|
RenamePassData() : BB(NULL), Pred(NULL), Values() {}
|
2007-03-09 23:39:14 +00:00
|
|
|
RenamePassData(BasicBlock *B, BasicBlock *P,
|
2007-08-04 01:19:38 +00:00
|
|
|
const ValVector &V) : BB(B), Pred(P), Values(V) {}
|
2007-03-09 23:39:14 +00:00
|
|
|
BasicBlock *BB;
|
|
|
|
BasicBlock *Pred;
|
2007-08-04 01:19:38 +00:00
|
|
|
ValVector Values;
|
2007-08-04 01:07:49 +00:00
|
|
|
|
|
|
|
void swap(RenamePassData &RHS) {
|
|
|
|
std::swap(BB, RHS.BB);
|
|
|
|
std::swap(Pred, RHS.Pred);
|
|
|
|
Values.swap(RHS.Values);
|
|
|
|
}
|
2007-03-09 23:39:14 +00:00
|
|
|
};
|
2008-10-27 06:05:26 +00:00
|
|
|
|
|
|
|
/// LargeBlockInfo - This assigns and keeps a per-bb relative ordering of
|
|
|
|
/// load/store instructions in the block that directly load or store an alloca.
|
|
|
|
///
|
|
|
|
/// This functionality is important because it avoids scanning large basic
|
|
|
|
/// blocks multiple times when promoting many allocas in the same block.
|
2009-10-25 06:33:48 +00:00
|
|
|
class LargeBlockInfo {
|
2008-10-27 06:05:26 +00:00
|
|
|
/// InstNumbers - For each instruction that we track, keep the index of the
|
|
|
|
/// instruction. The index starts out as the number of the instruction from
|
|
|
|
/// the start of the block.
|
|
|
|
DenseMap<const Instruction *, unsigned> InstNumbers;
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// isInterestingInstruction - This code only looks at accesses to allocas.
|
|
|
|
static bool isInterestingInstruction(const Instruction *I) {
|
|
|
|
return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) ||
|
|
|
|
(isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getInstructionIndex - Get or calculate the index of the specified
|
|
|
|
/// instruction.
|
|
|
|
unsigned getInstructionIndex(const Instruction *I) {
|
|
|
|
assert(isInterestingInstruction(I) &&
|
|
|
|
"Not a load/store to/from an alloca?");
|
|
|
|
|
|
|
|
// If we already have this instruction number, return it.
|
|
|
|
DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I);
|
|
|
|
if (It != InstNumbers.end()) return It->second;
|
|
|
|
|
|
|
|
// Scan the whole block to get the instruction. This accumulates
|
|
|
|
// information for every interesting instruction in the block, in order to
|
|
|
|
// avoid gratuitus rescans.
|
|
|
|
const BasicBlock *BB = I->getParent();
|
|
|
|
unsigned InstNo = 0;
|
|
|
|
for (BasicBlock::const_iterator BBI = BB->begin(), E = BB->end();
|
|
|
|
BBI != E; ++BBI)
|
|
|
|
if (isInterestingInstruction(BBI))
|
|
|
|
InstNumbers[BBI] = InstNo++;
|
|
|
|
It = InstNumbers.find(I);
|
|
|
|
|
|
|
|
assert(It != InstNumbers.end() && "Didn't insert instruction?");
|
|
|
|
return It->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteValue(const Instruction *I) {
|
|
|
|
InstNumbers.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
InstNumbers.clear();
|
|
|
|
}
|
|
|
|
};
|
2007-03-09 23:39:14 +00:00
|
|
|
|
2009-10-25 06:33:48 +00:00
|
|
|
struct PromoteMem2Reg {
|
2004-09-15 01:02:54 +00:00
|
|
|
/// Allocas - The alloca instructions being promoted.
|
|
|
|
///
|
2003-10-05 20:54:03 +00:00
|
|
|
std::vector<AllocaInst*> Allocas;
|
2007-06-07 21:57:03 +00:00
|
|
|
DominatorTree &DT;
|
2011-02-24 18:49:55 +00:00
|
|
|
DIBuilder *DIB;
|
2002-10-01 22:38:41 +00:00
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
/// AST - An AliasSetTracker object to update. If null, don't update it.
|
|
|
|
///
|
|
|
|
AliasSetTracker *AST;
|
2009-07-05 22:41:43 +00:00
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
/// AllocaLookup - Reverse mapping of Allocas.
|
|
|
|
///
|
2011-01-18 04:50:38 +00:00
|
|
|
DenseMap<AllocaInst*, unsigned> AllocaLookup;
|
2003-10-05 03:16:07 +00:00
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
/// NewPhiNodes - The PhiNodes we're adding.
|
|
|
|
///
|
2007-02-07 01:15:04 +00:00
|
|
|
DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*> NewPhiNodes;
|
|
|
|
|
|
|
|
/// PhiToAllocaMap - For each PHI node, keep track of which entry in Allocas
|
|
|
|
/// it corresponds to.
|
|
|
|
DenseMap<PHINode*, unsigned> PhiToAllocaMap;
|
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
/// PointerAllocaValues - If we are updating an AliasSetTracker, then for
|
|
|
|
/// each alloca that is of pointer type, we keep track of what to copyValue
|
|
|
|
/// to the inserted PHI nodes here.
|
|
|
|
///
|
|
|
|
std::vector<Value*> PointerAllocaValues;
|
|
|
|
|
2010-01-26 02:42:15 +00:00
|
|
|
/// AllocaDbgDeclares - For each alloca, we keep track of the dbg.declare
|
|
|
|
/// intrinsic that describes it, if any, so that we can convert it to a
|
|
|
|
/// dbg.value intrinsic if the alloca gets promoted.
|
2010-01-26 18:57:53 +00:00
|
|
|
SmallVector<DbgDeclareInst*, 8> AllocaDbgDeclares;
|
2010-01-26 02:42:15 +00:00
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
/// Visited - The set of basic blocks the renamer has already visited.
|
|
|
|
///
|
2007-02-05 22:15:21 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 16> Visited;
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
/// BBNumbers - Contains a stable numbering of basic blocks to avoid
|
|
|
|
/// non-determinstic behavior.
|
2007-02-05 23:37:20 +00:00
|
|
|
DenseMap<BasicBlock*, unsigned> BBNumbers;
|
2004-06-19 07:40:14 +00:00
|
|
|
|
2011-01-17 17:38:41 +00:00
|
|
|
/// DomLevels - Maps DomTreeNodes to their level in the dominator tree.
|
|
|
|
DenseMap<DomTreeNode*, unsigned> DomLevels;
|
|
|
|
|
2007-08-04 20:24:50 +00:00
|
|
|
/// BBNumPreds - Lazily compute the number of predecessors a block has.
|
|
|
|
DenseMap<const BasicBlock*, unsigned> BBNumPreds;
|
2002-04-28 19:12:38 +00:00
|
|
|
public:
|
2008-10-27 07:05:53 +00:00
|
|
|
PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt,
|
2011-01-17 17:38:41 +00:00
|
|
|
AliasSetTracker *ast)
|
2011-02-24 18:49:55 +00:00
|
|
|
: Allocas(A), DT(dt), DIB(0), AST(ast) {}
|
2010-01-26 18:57:53 +00:00
|
|
|
~PromoteMem2Reg() {
|
2011-02-24 18:49:55 +00:00
|
|
|
delete DIB;
|
2010-01-26 18:57:53 +00:00
|
|
|
}
|
2002-04-28 19:12:38 +00:00
|
|
|
|
2003-02-22 23:57:48 +00:00
|
|
|
void run();
|
2002-04-28 19:12:38 +00:00
|
|
|
|
2007-06-07 21:57:03 +00:00
|
|
|
/// dominates - Return true if BB1 dominates BB2 using the DominatorTree.
|
2005-11-18 07:29:44 +00:00
|
|
|
///
|
|
|
|
bool dominates(BasicBlock *BB1, BasicBlock *BB2) const {
|
2007-06-07 21:57:03 +00:00
|
|
|
return DT.dominates(BB1, BB2);
|
2004-10-17 21:25:56 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 19:12:38 +00:00
|
|
|
private:
|
2007-08-04 01:41:18 +00:00
|
|
|
void RemoveFromAllocasList(unsigned &AllocaIdx) {
|
|
|
|
Allocas[AllocaIdx] = Allocas.back();
|
|
|
|
Allocas.pop_back();
|
|
|
|
--AllocaIdx;
|
|
|
|
}
|
2007-08-04 20:24:50 +00:00
|
|
|
|
|
|
|
unsigned getNumPreds(const BasicBlock *BB) {
|
|
|
|
unsigned &NP = BBNumPreds[BB];
|
|
|
|
if (NP == 0)
|
|
|
|
NP = std::distance(pred_begin(BB), pred_end(BB))+1;
|
|
|
|
return NP-1;
|
|
|
|
}
|
|
|
|
|
2007-08-04 21:14:29 +00:00
|
|
|
void DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum,
|
|
|
|
AllocaInfo &Info);
|
2007-08-04 22:50:14 +00:00
|
|
|
void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
|
|
|
|
const SmallPtrSet<BasicBlock*, 32> &DefBlocks,
|
|
|
|
SmallPtrSet<BasicBlock*, 32> &LiveInBlocks);
|
2007-08-04 01:41:18 +00:00
|
|
|
|
2008-10-27 06:05:26 +00:00
|
|
|
void RewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
|
|
|
|
LargeBlockInfo &LBI);
|
2008-10-27 07:05:53 +00:00
|
|
|
void PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
|
2008-10-27 06:05:26 +00:00
|
|
|
LargeBlockInfo &LBI);
|
2010-01-25 17:52:13 +00:00
|
|
|
|
2003-02-22 22:25:17 +00:00
|
|
|
void RenamePass(BasicBlock *BB, BasicBlock *Pred,
|
2007-08-04 01:19:38 +00:00
|
|
|
RenamePassData::ValVector &IncVals,
|
2007-08-04 01:04:40 +00:00
|
|
|
std::vector<RenamePassData> &Worklist);
|
2010-11-22 09:41:24 +00:00
|
|
|
bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version);
|
2002-04-28 19:12:38 +00:00
|
|
|
};
|
2007-08-04 01:41:18 +00:00
|
|
|
|
|
|
|
struct AllocaInfo {
|
2011-01-23 08:03:04 +00:00
|
|
|
SmallVector<BasicBlock*, 32> DefiningBlocks;
|
|
|
|
SmallVector<BasicBlock*, 32> UsingBlocks;
|
2007-08-04 01:41:18 +00:00
|
|
|
|
|
|
|
StoreInst *OnlyStore;
|
|
|
|
BasicBlock *OnlyBlock;
|
|
|
|
bool OnlyUsedInOneBlock;
|
|
|
|
|
|
|
|
Value *AllocaPointerVal;
|
2010-01-26 02:42:15 +00:00
|
|
|
DbgDeclareInst *DbgDeclare;
|
2007-08-04 01:41:18 +00:00
|
|
|
|
|
|
|
void clear() {
|
|
|
|
DefiningBlocks.clear();
|
|
|
|
UsingBlocks.clear();
|
|
|
|
OnlyStore = 0;
|
|
|
|
OnlyBlock = 0;
|
|
|
|
OnlyUsedInOneBlock = true;
|
|
|
|
AllocaPointerVal = 0;
|
2010-01-26 02:42:15 +00:00
|
|
|
DbgDeclare = 0;
|
2007-08-04 01:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AnalyzeAlloca - Scan the uses of the specified alloca, filling in our
|
|
|
|
/// ivars.
|
|
|
|
void AnalyzeAlloca(AllocaInst *AI) {
|
|
|
|
clear();
|
2008-11-07 01:30:07 +00:00
|
|
|
|
2007-08-04 01:41:18 +00:00
|
|
|
// As we scan the uses of the alloca instruction, keep track of stores,
|
|
|
|
// and decide whether all of the loads and stores to the alloca are within
|
|
|
|
// the same basic block.
|
2009-09-21 22:26:02 +00:00
|
|
|
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
|
|
|
|
UI != E;) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI++);
|
2010-01-21 23:08:36 +00:00
|
|
|
|
2009-09-21 22:26:02 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
|
2007-08-04 01:41:18 +00:00
|
|
|
// Remember the basic blocks which define new values for the alloca
|
|
|
|
DefiningBlocks.push_back(SI->getParent());
|
|
|
|
AllocaPointerVal = SI->getOperand(0);
|
|
|
|
OnlyStore = SI;
|
|
|
|
} else {
|
|
|
|
LoadInst *LI = cast<LoadInst>(User);
|
2007-08-04 20:24:50 +00:00
|
|
|
// Otherwise it must be a load instruction, keep track of variable
|
|
|
|
// reads.
|
2007-08-04 01:41:18 +00:00
|
|
|
UsingBlocks.push_back(LI->getParent());
|
|
|
|
AllocaPointerVal = LI;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OnlyUsedInOneBlock) {
|
|
|
|
if (OnlyBlock == 0)
|
|
|
|
OnlyBlock = User->getParent();
|
|
|
|
else if (OnlyBlock != User->getParent())
|
|
|
|
OnlyUsedInOneBlock = false;
|
|
|
|
}
|
|
|
|
}
|
2010-01-26 02:42:15 +00:00
|
|
|
|
2010-01-27 02:12:20 +00:00
|
|
|
DbgDeclare = FindAllocaDbgDeclare(AI);
|
2007-08-04 01:41:18 +00:00
|
|
|
}
|
|
|
|
};
|
2011-01-17 17:38:41 +00:00
|
|
|
|
|
|
|
typedef std::pair<DomTreeNode*, unsigned> DomTreeNodePair;
|
|
|
|
|
|
|
|
struct DomTreeNodeCompare {
|
|
|
|
bool operator()(const DomTreeNodePair &LHS, const DomTreeNodePair &RHS) {
|
|
|
|
return LHS.second < RHS.second;
|
|
|
|
}
|
|
|
|
};
|
2002-03-27 23:28:40 +00:00
|
|
|
} // end of anonymous namespace
|
2002-02-12 17:16:22 +00:00
|
|
|
|
2011-06-17 10:09:00 +00:00
|
|
|
static void removeLifetimeIntrinsicUsers(AllocaInst *AI) {
|
|
|
|
// Knowing that this alloca is promotable, we know that it's safe to kill all
|
|
|
|
// instructions except for load and store.
|
|
|
|
|
|
|
|
for (Value::use_iterator UI = AI->use_begin(), UE = AI->use_end();
|
|
|
|
UI != UE;) {
|
|
|
|
Instruction *I = cast<Instruction>(*UI);
|
|
|
|
++UI;
|
|
|
|
if (isa<LoadInst>(I) || isa<StoreInst>(I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!I->getType()->isVoidTy()) {
|
|
|
|
// The only users of this bitcast/GEP instruction are lifetime intrinsics.
|
|
|
|
// Follow the use/def chain to erase them now instead of leaving it for
|
|
|
|
// dead code elimination later.
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
|
|
|
|
UI != UE;) {
|
|
|
|
Instruction *Inst = cast<Instruction>(*UI);
|
|
|
|
++UI;
|
|
|
|
Inst->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
2007-08-04 01:47:41 +00:00
|
|
|
|
2003-02-22 23:57:48 +00:00
|
|
|
void PromoteMem2Reg::run() {
|
2011-01-17 17:38:41 +00:00
|
|
|
Function &F = *DT.getRoot()->getParent();
|
2003-10-05 01:52:53 +00:00
|
|
|
|
2004-09-15 01:02:54 +00:00
|
|
|
if (AST) PointerAllocaValues.resize(Allocas.size());
|
2010-01-26 02:42:15 +00:00
|
|
|
AllocaDbgDeclares.resize(Allocas.size());
|
2004-06-19 07:40:14 +00:00
|
|
|
|
2007-08-04 01:41:18 +00:00
|
|
|
AllocaInfo Info;
|
2008-10-27 06:05:26 +00:00
|
|
|
LargeBlockInfo LBI;
|
2007-08-04 01:41:18 +00:00
|
|
|
|
2003-10-05 22:19:20 +00:00
|
|
|
for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) {
|
|
|
|
AllocaInst *AI = Allocas[AllocaNum];
|
2003-10-05 03:16:07 +00:00
|
|
|
|
2007-04-25 17:15:20 +00:00
|
|
|
assert(isAllocaPromotable(AI) &&
|
2003-02-22 23:57:48 +00:00
|
|
|
"Cannot promote non-promotable alloca!");
|
2003-10-05 22:19:20 +00:00
|
|
|
assert(AI->getParent()->getParent() == &F &&
|
2003-02-22 23:57:48 +00:00
|
|
|
"All allocas should be in the same function, which is same as DF!");
|
2003-10-05 02:37:36 +00:00
|
|
|
|
2011-06-17 10:09:00 +00:00
|
|
|
removeLifetimeIntrinsicUsers(AI);
|
|
|
|
|
2003-10-05 20:54:03 +00:00
|
|
|
if (AI->use_empty()) {
|
|
|
|
// If there are no uses of the alloca, just delete it now.
|
2004-09-15 01:02:54 +00:00
|
|
|
if (AST) AST->deleteValue(AI);
|
2006-04-27 01:14:43 +00:00
|
|
|
AI->eraseFromParent();
|
2003-10-05 20:54:03 +00:00
|
|
|
|
|
|
|
// Remove the alloca from the Allocas list, since it has been processed
|
2007-08-04 01:41:18 +00:00
|
|
|
RemoveFromAllocasList(AllocaNum);
|
|
|
|
++NumDeadAlloca;
|
2003-10-05 20:54:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-04 01:41:18 +00:00
|
|
|
|
2003-10-05 22:19:20 +00:00
|
|
|
// Calculate the set of read and write-locations for each alloca. This is
|
2004-01-12 01:18:32 +00:00
|
|
|
// analogous to finding the 'uses' and 'definitions' of each variable.
|
2007-08-04 01:41:18 +00:00
|
|
|
Info.AnalyzeAlloca(AI);
|
2003-10-05 20:54:03 +00:00
|
|
|
|
2005-11-18 07:31:42 +00:00
|
|
|
// If there is only a single store to this value, replace any loads of
|
|
|
|
// it that are directly dominated by the definition with the value stored.
|
2007-08-04 01:41:18 +00:00
|
|
|
if (Info.DefiningBlocks.size() == 1) {
|
2008-10-27 06:05:26 +00:00
|
|
|
RewriteSingleStoreAlloca(AI, Info, LBI);
|
2005-11-18 07:31:42 +00:00
|
|
|
|
|
|
|
// Finally, after the scan, check to see if the store is all that is left.
|
2007-08-04 01:41:18 +00:00
|
|
|
if (Info.UsingBlocks.empty()) {
|
2012-02-20 23:13:17 +00:00
|
|
|
// Record debuginfo for the store and remove the declaration's
|
|
|
|
// debuginfo.
|
2010-01-29 00:01:35 +00:00
|
|
|
if (DbgDeclareInst *DDI = Info.DbgDeclare) {
|
2011-03-17 21:58:19 +00:00
|
|
|
if (!DIB)
|
|
|
|
DIB = new DIBuilder(*DDI->getParent()->getParent()->getParent());
|
|
|
|
ConvertDebugDeclareToDebugValue(DDI, Info.OnlyStore, *DIB);
|
2010-01-29 00:01:35 +00:00
|
|
|
DDI->eraseFromParent();
|
|
|
|
}
|
2007-08-04 02:38:38 +00:00
|
|
|
// Remove the (now dead) store and alloca.
|
|
|
|
Info.OnlyStore->eraseFromParent();
|
2008-10-27 06:05:26 +00:00
|
|
|
LBI.deleteValue(Info.OnlyStore);
|
|
|
|
|
2007-08-04 02:38:38 +00:00
|
|
|
if (AST) AST->deleteValue(AI);
|
|
|
|
AI->eraseFromParent();
|
2008-10-27 06:05:26 +00:00
|
|
|
LBI.deleteValue(AI);
|
2007-08-04 02:38:38 +00:00
|
|
|
|
2005-11-18 07:31:42 +00:00
|
|
|
// The alloca has been processed, move on.
|
2007-08-04 01:41:18 +00:00
|
|
|
RemoveFromAllocasList(AllocaNum);
|
2007-08-04 02:38:38 +00:00
|
|
|
|
|
|
|
++NumSingleStore;
|
2005-11-18 07:31:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-04 20:03:23 +00:00
|
|
|
// If the alloca is only read and written in one basic block, just perform a
|
|
|
|
// linear sweep over the block to eliminate it.
|
|
|
|
if (Info.OnlyUsedInOneBlock) {
|
2008-10-27 07:05:53 +00:00
|
|
|
PromoteSingleBlockAlloca(AI, Info, LBI);
|
2007-08-04 20:03:23 +00:00
|
|
|
|
2008-10-27 07:05:53 +00:00
|
|
|
// Finally, after the scan, check to see if the stores are all that is
|
|
|
|
// left.
|
|
|
|
if (Info.UsingBlocks.empty()) {
|
|
|
|
|
|
|
|
// Remove the (now dead) stores and alloca.
|
|
|
|
while (!AI->use_empty()) {
|
|
|
|
StoreInst *SI = cast<StoreInst>(AI->use_back());
|
2010-01-26 02:42:15 +00:00
|
|
|
// Record debuginfo for the store before removing it.
|
2011-03-17 21:58:19 +00:00
|
|
|
if (DbgDeclareInst *DDI = Info.DbgDeclare) {
|
|
|
|
if (!DIB)
|
|
|
|
DIB = new DIBuilder(*SI->getParent()->getParent()->getParent());
|
|
|
|
ConvertDebugDeclareToDebugValue(DDI, SI, *DIB);
|
|
|
|
}
|
2008-10-27 07:05:53 +00:00
|
|
|
SI->eraseFromParent();
|
|
|
|
LBI.deleteValue(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AST) AST->deleteValue(AI);
|
|
|
|
AI->eraseFromParent();
|
|
|
|
LBI.deleteValue(AI);
|
|
|
|
|
|
|
|
// The alloca has been processed, move on.
|
|
|
|
RemoveFromAllocasList(AllocaNum);
|
|
|
|
|
2010-01-29 00:01:35 +00:00
|
|
|
// The alloca's debuginfo can be removed as well.
|
|
|
|
if (DbgDeclareInst *DDI = Info.DbgDeclare)
|
|
|
|
DDI->eraseFromParent();
|
|
|
|
|
2008-10-27 07:05:53 +00:00
|
|
|
++NumLocalPromoted;
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-04 20:03:23 +00:00
|
|
|
}
|
2011-01-17 17:38:41 +00:00
|
|
|
|
|
|
|
// If we haven't computed dominator tree levels, do so now.
|
|
|
|
if (DomLevels.empty()) {
|
|
|
|
SmallVector<DomTreeNode*, 32> Worklist;
|
|
|
|
|
|
|
|
DomTreeNode *Root = DT.getRootNode();
|
|
|
|
DomLevels[Root] = 0;
|
|
|
|
Worklist.push_back(Root);
|
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
DomTreeNode *Node = Worklist.pop_back_val();
|
|
|
|
unsigned ChildLevel = DomLevels[Node] + 1;
|
|
|
|
for (DomTreeNode::iterator CI = Node->begin(), CE = Node->end();
|
|
|
|
CI != CE; ++CI) {
|
|
|
|
DomLevels[*CI] = ChildLevel;
|
|
|
|
Worklist.push_back(*CI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-19 07:40:14 +00:00
|
|
|
// If we haven't computed a numbering for the BB's in the function, do so
|
|
|
|
// now.
|
2007-02-05 23:37:20 +00:00
|
|
|
if (BBNumbers.empty()) {
|
|
|
|
unsigned ID = 0;
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
|
|
|
BBNumbers[I] = ID++;
|
|
|
|
}
|
2004-06-19 07:40:14 +00:00
|
|
|
|
2007-08-04 21:14:29 +00:00
|
|
|
// If we have an AST to keep updated, remember some pointer value that is
|
|
|
|
// stored into the alloca.
|
|
|
|
if (AST)
|
|
|
|
PointerAllocaValues[AllocaNum] = Info.AllocaPointerVal;
|
2010-01-26 02:42:15 +00:00
|
|
|
|
|
|
|
// Remember the dbg.declare intrinsic describing this alloca, if any.
|
|
|
|
if (Info.DbgDeclare) AllocaDbgDeclares[AllocaNum] = Info.DbgDeclare;
|
2007-08-04 21:14:29 +00:00
|
|
|
|
|
|
|
// Keep the reverse mapping of the 'Allocas' array for the rename pass.
|
2003-10-05 22:19:20 +00:00
|
|
|
AllocaLookup[Allocas[AllocaNum]] = AllocaNum;
|
2007-08-04 21:14:29 +00:00
|
|
|
|
|
|
|
// At this point, we're committed to promoting the alloca using IDF's, and
|
2008-10-27 06:05:26 +00:00
|
|
|
// the standard SSA construction algorithm. Determine which blocks need PHI
|
2007-08-04 21:14:29 +00:00
|
|
|
// nodes and see if we can optimize out some work by avoiding insertion of
|
|
|
|
// dead phi nodes.
|
|
|
|
DetermineInsertionPoint(AI, AllocaNum, Info);
|
2002-04-28 18:27:55 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-10-05 20:54:03 +00:00
|
|
|
if (Allocas.empty())
|
|
|
|
return; // All of the allocas must have been trivial!
|
2002-04-28 18:27:55 +00:00
|
|
|
|
2008-10-27 06:05:26 +00:00
|
|
|
LBI.clear();
|
|
|
|
|
|
|
|
|
2002-04-28 18:39:46 +00:00
|
|
|
// Set the incoming values for the basic block to be null values for all of
|
|
|
|
// the alloca's. We do this in case there is a load of a value that has not
|
|
|
|
// been stored yet. In this case, it will get this null value.
|
|
|
|
//
|
2007-08-04 01:19:38 +00:00
|
|
|
RenamePassData::ValVector Values(Allocas.size());
|
2002-04-28 18:39:46 +00:00
|
|
|
for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
|
2009-07-30 23:03:37 +00:00
|
|
|
Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
|
2002-04-28 18:39:46 +00:00
|
|
|
|
2002-04-28 18:27:55 +00:00
|
|
|
// Walks all basic blocks in the function performing the SSA rename algorithm
|
|
|
|
// and inserting the phi nodes we marked as necessary
|
|
|
|
//
|
2007-08-04 01:04:40 +00:00
|
|
|
std::vector<RenamePassData> RenamePassWorkList;
|
2007-03-26 23:19:29 +00:00
|
|
|
RenamePassWorkList.push_back(RenamePassData(F.begin(), 0, Values));
|
2010-01-05 16:27:25 +00:00
|
|
|
do {
|
2007-08-04 01:07:49 +00:00
|
|
|
RenamePassData RPD;
|
|
|
|
RPD.swap(RenamePassWorkList.back());
|
2007-03-26 23:19:29 +00:00
|
|
|
RenamePassWorkList.pop_back();
|
2007-03-09 23:39:14 +00:00
|
|
|
// RenamePass may add new worklist entries.
|
2007-08-04 01:04:40 +00:00
|
|
|
RenamePass(RPD.BB, RPD.Pred, RPD.Values, RenamePassWorkList);
|
2010-01-05 16:27:25 +00:00
|
|
|
} while (!RenamePassWorkList.empty());
|
2007-03-09 23:39:14 +00:00
|
|
|
|
2003-10-05 04:26:39 +00:00
|
|
|
// The renamer uses the Visited set to avoid infinite loops. Clear it now.
|
2003-10-05 01:52:53 +00:00
|
|
|
Visited.clear();
|
2002-04-28 18:27:55 +00:00
|
|
|
|
2006-04-27 01:14:43 +00:00
|
|
|
// Remove the allocas themselves from the function.
|
2003-10-05 01:52:53 +00:00
|
|
|
for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
|
|
|
|
Instruction *A = Allocas[i];
|
2002-04-28 18:27:55 +00:00
|
|
|
|
2003-10-05 01:52:53 +00:00
|
|
|
// If there are any uses of the alloca instructions left, they must be in
|
2011-01-18 03:53:26 +00:00
|
|
|
// unreachable basic blocks that were not processed by walking the dominator
|
|
|
|
// tree. Just delete the users now.
|
2003-10-05 01:52:53 +00:00
|
|
|
if (!A->use_empty())
|
2009-07-30 23:03:37 +00:00
|
|
|
A->replaceAllUsesWith(UndefValue::get(A->getType()));
|
2004-09-15 01:02:54 +00:00
|
|
|
if (AST) AST->deleteValue(A);
|
2006-04-27 01:14:43 +00:00
|
|
|
A->eraseFromParent();
|
2002-04-28 18:27:55 +00:00
|
|
|
}
|
2003-10-05 04:26:39 +00:00
|
|
|
|
2010-01-29 00:01:35 +00:00
|
|
|
// Remove alloca's dbg.declare instrinsics from the function.
|
|
|
|
for (unsigned i = 0, e = AllocaDbgDeclares.size(); i != e; ++i)
|
|
|
|
if (DbgDeclareInst *DDI = AllocaDbgDeclares[i])
|
|
|
|
DDI->eraseFromParent();
|
|
|
|
|
2006-04-27 01:14:43 +00:00
|
|
|
// Loop over all of the PHI nodes and see if there are any that we can get
|
|
|
|
// rid of because they merge all of the same incoming values. This can
|
|
|
|
// happen due to undef values coming into the PHI nodes. This process is
|
|
|
|
// iterative, because eliminating one PHI node can cause others to be removed.
|
|
|
|
bool EliminatedAPHI = true;
|
|
|
|
while (EliminatedAPHI) {
|
|
|
|
EliminatedAPHI = false;
|
|
|
|
|
2007-02-07 01:15:04 +00:00
|
|
|
for (DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*>::iterator I =
|
|
|
|
NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E;) {
|
|
|
|
PHINode *PN = I->second;
|
2010-11-16 17:41:24 +00:00
|
|
|
|
2007-02-07 01:15:04 +00:00
|
|
|
// If this PHI node merges one value and/or undefs, get the value.
|
2011-12-01 03:08:23 +00:00
|
|
|
if (Value *V = SimplifyInstruction(PN, 0, 0, &DT)) {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (AST && PN->getType()->isPointerTy())
|
2009-09-03 15:34:35 +00:00
|
|
|
AST->deleteValue(PN);
|
|
|
|
PN->replaceAllUsesWith(V);
|
|
|
|
PN->eraseFromParent();
|
|
|
|
NewPhiNodes.erase(I++);
|
|
|
|
EliminatedAPHI = true;
|
|
|
|
continue;
|
2006-04-27 01:14:43 +00:00
|
|
|
}
|
2007-02-07 01:15:04 +00:00
|
|
|
++I;
|
2006-04-27 01:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-05 04:26:39 +00:00
|
|
|
// At this point, the renamer has added entries to PHI nodes for all reachable
|
2007-02-05 22:13:11 +00:00
|
|
|
// code. Unfortunately, there may be unreachable blocks which the renamer
|
|
|
|
// hasn't traversed. If this is the case, the PHI nodes may not
|
2003-10-05 04:26:39 +00:00
|
|
|
// have incoming values for all predecessors. Loop over all PHI nodes we have
|
2004-10-17 21:25:56 +00:00
|
|
|
// created, inserting undef values if they are missing any incoming values.
|
2003-10-05 04:26:39 +00:00
|
|
|
//
|
2007-02-07 01:15:04 +00:00
|
|
|
for (DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*>::iterator I =
|
2003-10-05 04:26:39 +00:00
|
|
|
NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) {
|
2007-02-07 01:15:04 +00:00
|
|
|
// We want to do this once per basic block. As such, only process a block
|
|
|
|
// when we find the PHI that is the first entry in the block.
|
|
|
|
PHINode *SomePHI = I->second;
|
|
|
|
BasicBlock *BB = SomePHI->getParent();
|
|
|
|
if (&BB->front() != SomePHI)
|
|
|
|
continue;
|
2003-10-05 04:26:39 +00:00
|
|
|
|
|
|
|
// Only do work here if there the PHI nodes are missing incoming values. We
|
|
|
|
// know that all PHI nodes that were inserted in a block will have the same
|
2007-02-07 01:15:04 +00:00
|
|
|
// number of incoming values, so we can just check any of them.
|
2007-08-04 21:06:15 +00:00
|
|
|
if (SomePHI->getNumIncomingValues() == getNumPreds(BB))
|
2007-02-07 01:15:04 +00:00
|
|
|
continue;
|
2007-08-04 21:06:15 +00:00
|
|
|
|
|
|
|
// Get the preds for BB.
|
|
|
|
SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));
|
2007-02-07 01:15:04 +00:00
|
|
|
|
|
|
|
// Ok, now we know that all of the PHI nodes are missing entries for some
|
|
|
|
// basic blocks. Start by sorting the incoming predecessors for efficient
|
|
|
|
// access.
|
|
|
|
std::sort(Preds.begin(), Preds.end());
|
|
|
|
|
|
|
|
// Now we loop through all BB's which have entries in SomePHI and remove
|
|
|
|
// them from the Preds list.
|
|
|
|
for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
// Do a log(n) search of the Preds list for the entry we want.
|
|
|
|
SmallVector<BasicBlock*, 16>::iterator EntIt =
|
|
|
|
std::lower_bound(Preds.begin(), Preds.end(),
|
|
|
|
SomePHI->getIncomingBlock(i));
|
|
|
|
assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i)&&
|
|
|
|
"PHI node has entry for a block which is not a predecessor!");
|
|
|
|
|
|
|
|
// Remove the entry
|
|
|
|
Preds.erase(EntIt);
|
|
|
|
}
|
2003-10-05 04:26:39 +00:00
|
|
|
|
2007-02-07 01:15:04 +00:00
|
|
|
// At this point, the blocks left in the preds list must have dummy
|
|
|
|
// entries inserted into every PHI nodes for the block. Update all the phi
|
|
|
|
// nodes in this block that we are inserting (there could be phis before
|
|
|
|
// mem2reg runs).
|
|
|
|
unsigned NumBadPreds = SomePHI->getNumIncomingValues();
|
|
|
|
BasicBlock::iterator BBI = BB->begin();
|
|
|
|
while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
|
|
|
|
SomePHI->getNumIncomingValues() == NumBadPreds) {
|
2009-07-30 23:03:37 +00:00
|
|
|
Value *UndefVal = UndefValue::get(SomePHI->getType());
|
2007-02-07 01:15:04 +00:00
|
|
|
for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
|
|
|
|
SomePHI->addIncoming(UndefVal, Preds[pred]);
|
2003-10-05 04:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-07 01:15:04 +00:00
|
|
|
|
|
|
|
NewPhiNodes.clear();
|
2002-04-28 18:27:55 +00:00
|
|
|
}
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2007-08-04 01:47:41 +00:00
|
|
|
|
2007-08-04 22:50:14 +00:00
|
|
|
/// ComputeLiveInBlocks - Determine which blocks the value is live in. These
|
|
|
|
/// are blocks which lead to uses. Knowing this allows us to avoid inserting
|
|
|
|
/// PHI nodes into blocks which don't lead to uses (thus, the inserted phi nodes
|
|
|
|
/// would be dead).
|
|
|
|
void PromoteMem2Reg::
|
|
|
|
ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
|
|
|
|
const SmallPtrSet<BasicBlock*, 32> &DefBlocks,
|
|
|
|
SmallPtrSet<BasicBlock*, 32> &LiveInBlocks) {
|
|
|
|
|
|
|
|
// To determine liveness, we must iterate through the predecessors of blocks
|
|
|
|
// where the def is live. Blocks are added to the worklist if we need to
|
|
|
|
// check their predecessors. Start with all the using blocks.
|
2010-06-21 19:47:52 +00:00
|
|
|
SmallVector<BasicBlock*, 64> LiveInBlockWorklist(Info.UsingBlocks.begin(),
|
|
|
|
Info.UsingBlocks.end());
|
2007-08-04 22:50:14 +00:00
|
|
|
|
|
|
|
// If any of the using blocks is also a definition block, check to see if the
|
|
|
|
// definition occurs before or after the use. If it happens before the use,
|
|
|
|
// the value isn't really live-in.
|
|
|
|
for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) {
|
|
|
|
BasicBlock *BB = LiveInBlockWorklist[i];
|
|
|
|
if (!DefBlocks.count(BB)) continue;
|
|
|
|
|
|
|
|
// Okay, this is a block that both uses and defines the value. If the first
|
|
|
|
// reference to the alloca is a def (store), then we know it isn't live-in.
|
|
|
|
for (BasicBlock::iterator I = BB->begin(); ; ++I) {
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
|
|
|
if (SI->getOperand(1) != AI) continue;
|
|
|
|
|
|
|
|
// We found a store to the alloca before a load. The alloca is not
|
|
|
|
// actually live-in here.
|
|
|
|
LiveInBlockWorklist[i] = LiveInBlockWorklist.back();
|
|
|
|
LiveInBlockWorklist.pop_back();
|
|
|
|
--i, --e;
|
|
|
|
break;
|
2009-09-21 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
2007-08-04 22:50:14 +00:00
|
|
|
if (LI->getOperand(0) != AI) continue;
|
|
|
|
|
|
|
|
// Okay, we found a load before a store to the alloca. It is actually
|
|
|
|
// live into this block.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have a set of blocks where the phi is live-in, recursively add
|
|
|
|
// their predecessors until we find the full region the value is live.
|
|
|
|
while (!LiveInBlockWorklist.empty()) {
|
2009-05-06 17:22:41 +00:00
|
|
|
BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
|
2007-08-04 22:50:14 +00:00
|
|
|
|
|
|
|
// The block really is live in here, insert it into the set. If already in
|
|
|
|
// the set, then it has already been processed.
|
|
|
|
if (!LiveInBlocks.insert(BB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Since the value is live into BB, it is either defined in a predecessor or
|
|
|
|
// live into it to. Add the preds to the worklist unless they are a
|
|
|
|
// defining block.
|
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
|
|
|
|
BasicBlock *P = *PI;
|
|
|
|
|
|
|
|
// The value is not live into a predecessor if it defines the value.
|
|
|
|
if (DefBlocks.count(P))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Otherwise it is, add to the worklist.
|
|
|
|
LiveInBlockWorklist.push_back(P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-04 21:14:29 +00:00
|
|
|
/// DetermineInsertionPoint - At this point, we're committed to promoting the
|
|
|
|
/// alloca using IDF's, and the standard SSA construction algorithm. Determine
|
|
|
|
/// which blocks need phi nodes and see if we can optimize out some work by
|
|
|
|
/// avoiding insertion of dead phi nodes.
|
|
|
|
void PromoteMem2Reg::DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum,
|
|
|
|
AllocaInfo &Info) {
|
2007-08-04 22:50:14 +00:00
|
|
|
// Unique the set of defining blocks for efficient lookup.
|
|
|
|
SmallPtrSet<BasicBlock*, 32> DefBlocks;
|
|
|
|
DefBlocks.insert(Info.DefiningBlocks.begin(), Info.DefiningBlocks.end());
|
|
|
|
|
|
|
|
// Determine which blocks the value is live in. These are blocks which lead
|
|
|
|
// to uses.
|
|
|
|
SmallPtrSet<BasicBlock*, 32> LiveInBlocks;
|
|
|
|
ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks);
|
|
|
|
|
2011-01-17 17:38:41 +00:00
|
|
|
// Use a priority queue keyed on dominator tree level so that inserted nodes
|
|
|
|
// are handled from the bottom of the dominator tree upwards.
|
|
|
|
typedef std::priority_queue<DomTreeNodePair, SmallVector<DomTreeNodePair, 32>,
|
|
|
|
DomTreeNodeCompare> IDFPriorityQueue;
|
|
|
|
IDFPriorityQueue PQ;
|
|
|
|
|
|
|
|
for (SmallPtrSet<BasicBlock*, 32>::const_iterator I = DefBlocks.begin(),
|
|
|
|
E = DefBlocks.end(); I != E; ++I) {
|
|
|
|
if (DomTreeNode *Node = DT.getNode(*I))
|
|
|
|
PQ.push(std::make_pair(Node, DomLevels[Node]));
|
|
|
|
}
|
|
|
|
|
2011-01-18 04:41:32 +00:00
|
|
|
SmallVector<std::pair<unsigned, BasicBlock*>, 32> DFBlocks;
|
2011-01-17 17:38:41 +00:00
|
|
|
SmallPtrSet<DomTreeNode*, 32> Visited;
|
|
|
|
SmallVector<DomTreeNode*, 32> Worklist;
|
|
|
|
while (!PQ.empty()) {
|
|
|
|
DomTreeNodePair RootPair = PQ.top();
|
|
|
|
PQ.pop();
|
|
|
|
DomTreeNode *Root = RootPair.first;
|
|
|
|
unsigned RootLevel = RootPair.second;
|
|
|
|
|
|
|
|
// Walk all dominator tree children of Root, inspecting their CFG edges with
|
|
|
|
// targets elsewhere on the dominator tree. Only targets whose level is at
|
|
|
|
// most Root's level are added to the iterated dominance frontier of the
|
|
|
|
// definition set.
|
|
|
|
|
|
|
|
Worklist.clear();
|
|
|
|
Worklist.push_back(Root);
|
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
DomTreeNode *Node = Worklist.pop_back_val();
|
|
|
|
BasicBlock *BB = Node->getBlock();
|
|
|
|
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
|
|
|
|
++SI) {
|
|
|
|
DomTreeNode *SuccNode = DT.getNode(*SI);
|
|
|
|
|
|
|
|
// Quickly skip all CFG edges that are also dominator tree edges instead
|
|
|
|
// of catching them below.
|
|
|
|
if (SuccNode->getIDom() == Node)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned SuccLevel = DomLevels[SuccNode];
|
|
|
|
if (SuccLevel > RootLevel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Visited.insert(SuccNode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BasicBlock *SuccBB = SuccNode->getBlock();
|
|
|
|
if (!LiveInBlocks.count(SuccBB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DFBlocks.push_back(std::make_pair(BBNumbers[SuccBB], SuccBB));
|
|
|
|
if (!DefBlocks.count(SuccBB))
|
|
|
|
PQ.push(std::make_pair(SuccNode, SuccLevel));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DomTreeNode::iterator CI = Node->begin(), CE = Node->end(); CI != CE;
|
|
|
|
++CI) {
|
|
|
|
if (!Visited.count(*CI))
|
|
|
|
Worklist.push_back(*CI);
|
|
|
|
}
|
2007-08-04 22:50:14 +00:00
|
|
|
}
|
2007-08-04 21:14:29 +00:00
|
|
|
}
|
2011-01-17 17:38:41 +00:00
|
|
|
|
|
|
|
if (DFBlocks.size() > 1)
|
|
|
|
std::sort(DFBlocks.begin(), DFBlocks.end());
|
|
|
|
|
|
|
|
unsigned CurrentVersion = 0;
|
|
|
|
for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i)
|
|
|
|
QueuePhiNode(DFBlocks[i].second, AllocaNum, CurrentVersion);
|
2007-08-04 21:14:29 +00:00
|
|
|
}
|
|
|
|
|
2007-08-04 01:47:41 +00:00
|
|
|
/// RewriteSingleStoreAlloca - If there is only a single store to this value,
|
|
|
|
/// replace any loads of it that are directly dominated by the definition with
|
|
|
|
/// the value stored.
|
|
|
|
void PromoteMem2Reg::RewriteSingleStoreAlloca(AllocaInst *AI,
|
2008-10-27 06:05:26 +00:00
|
|
|
AllocaInfo &Info,
|
|
|
|
LargeBlockInfo &LBI) {
|
2007-08-04 02:15:24 +00:00
|
|
|
StoreInst *OnlyStore = Info.OnlyStore;
|
2007-08-04 02:32:22 +00:00
|
|
|
bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
|
2008-10-27 06:05:26 +00:00
|
|
|
BasicBlock *StoreBB = OnlyStore->getParent();
|
|
|
|
int StoreIndex = -1;
|
|
|
|
|
|
|
|
// Clear out UsingBlocks. We will reconstruct it here if needed.
|
|
|
|
Info.UsingBlocks.clear();
|
2007-08-04 02:15:24 +00:00
|
|
|
|
2008-10-27 06:05:26 +00:00
|
|
|
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E; ) {
|
|
|
|
Instruction *UserInst = cast<Instruction>(*UI++);
|
|
|
|
if (!isa<LoadInst>(UserInst)) {
|
|
|
|
assert(UserInst == OnlyStore && "Should only have load/stores");
|
2007-08-04 01:47:41 +00:00
|
|
|
continue;
|
2007-08-04 02:32:22 +00:00
|
|
|
}
|
2008-10-27 06:05:26 +00:00
|
|
|
LoadInst *LI = cast<LoadInst>(UserInst);
|
2007-08-04 01:47:41 +00:00
|
|
|
|
2008-10-27 06:05:26 +00:00
|
|
|
// Okay, if we have a load from the alloca, we want to replace it with the
|
|
|
|
// only value stored to the alloca. We can do this if the value is
|
|
|
|
// dominated by the store. If not, we use the rest of the mem2reg machinery
|
|
|
|
// to insert the phi nodes as needed.
|
|
|
|
if (!StoringGlobalVal) { // Non-instructions are always dominated.
|
|
|
|
if (LI->getParent() == StoreBB) {
|
|
|
|
// If we have a use that is in the same block as the store, compare the
|
|
|
|
// indices of the two instructions to see which one came first. If the
|
|
|
|
// load came before the store, we can't handle it.
|
|
|
|
if (StoreIndex == -1)
|
|
|
|
StoreIndex = LBI.getInstructionIndex(OnlyStore);
|
|
|
|
|
|
|
|
if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
|
|
|
|
// Can't handle this load, bail out.
|
|
|
|
Info.UsingBlocks.push_back(StoreBB);
|
|
|
|
continue;
|
2007-08-04 01:47:41 +00:00
|
|
|
}
|
2008-10-27 06:05:26 +00:00
|
|
|
|
|
|
|
} else if (LI->getParent() != StoreBB &&
|
|
|
|
!dominates(StoreBB, LI->getParent())) {
|
|
|
|
// If the load and store are in different blocks, use BB dominance to
|
|
|
|
// check their relationships. If the store doesn't dom the use, bail
|
|
|
|
// out.
|
|
|
|
Info.UsingBlocks.push_back(LI->getParent());
|
|
|
|
continue;
|
2007-08-04 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-27 06:05:26 +00:00
|
|
|
// Otherwise, we *can* safely rewrite this load.
|
2009-11-02 04:37:17 +00:00
|
|
|
Value *ReplVal = OnlyStore->getOperand(0);
|
|
|
|
// If the replacement value is the load, this must occur in unreachable
|
|
|
|
// code.
|
|
|
|
if (ReplVal == LI)
|
|
|
|
ReplVal = UndefValue::get(LI->getType());
|
|
|
|
LI->replaceAllUsesWith(ReplVal);
|
2010-02-16 11:11:14 +00:00
|
|
|
if (AST && LI->getType()->isPointerTy())
|
2008-10-27 06:05:26 +00:00
|
|
|
AST->deleteValue(LI);
|
|
|
|
LI->eraseFromParent();
|
|
|
|
LBI.deleteValue(LI);
|
2007-08-04 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 01:32:21 +00:00
|
|
|
namespace {
|
2007-08-04 01:47:41 +00:00
|
|
|
|
2008-10-27 07:05:53 +00:00
|
|
|
/// StoreIndexSearchPredicate - This is a helper predicate used to search by the
|
|
|
|
/// first element of a pair.
|
|
|
|
struct StoreIndexSearchPredicate {
|
|
|
|
bool operator()(const std::pair<unsigned, StoreInst*> &LHS,
|
|
|
|
const std::pair<unsigned, StoreInst*> &RHS) {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-08-07 01:32:21 +00:00
|
|
|
}
|
|
|
|
|
2008-10-27 07:05:53 +00:00
|
|
|
/// PromoteSingleBlockAlloca - Many allocas are only used within a single basic
|
2004-02-03 22:34:12 +00:00
|
|
|
/// block. If this is the case, avoid traversing the CFG and inserting a lot of
|
|
|
|
/// potentially useless PHI nodes by just performing a single linear pass over
|
|
|
|
/// the basic block using the Alloca.
|
|
|
|
///
|
2005-06-30 07:29:44 +00:00
|
|
|
/// If we cannot promote this alloca (because it is read before it is written),
|
|
|
|
/// return true. This is necessary in cases where, due to control flow, the
|
|
|
|
/// alloca is potentially undefined on some control flow paths. e.g. code like
|
|
|
|
/// this is potentially correct:
|
|
|
|
///
|
|
|
|
/// for (...) { if (c) { A = undef; undef = B; } }
|
|
|
|
///
|
|
|
|
/// ... so long as A is not used before undef is set.
|
|
|
|
///
|
2008-10-27 07:05:53 +00:00
|
|
|
void PromoteMem2Reg::PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
|
2008-10-27 06:05:26 +00:00
|
|
|
LargeBlockInfo &LBI) {
|
2008-10-27 07:05:53 +00:00
|
|
|
// The trickiest case to handle is when we have large blocks. Because of this,
|
|
|
|
// this code is optimized assuming that large blocks happen. This does not
|
|
|
|
// significantly pessimize the small block case. This uses LargeBlockInfo to
|
|
|
|
// make it efficient to get the index of various operations in the block.
|
|
|
|
|
|
|
|
// Clear out UsingBlocks. We will reconstruct it here if needed.
|
|
|
|
Info.UsingBlocks.clear();
|
|
|
|
|
|
|
|
// Walk the use-def list of the alloca, getting the locations of all stores.
|
|
|
|
typedef SmallVector<std::pair<unsigned, StoreInst*>, 64> StoresByIndexTy;
|
|
|
|
StoresByIndexTy StoresByIndex;
|
|
|
|
|
|
|
|
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
|
|
|
|
StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
|
|
|
|
|
|
|
|
// If there are no stores to the alloca, just replace any loads with undef.
|
|
|
|
if (StoresByIndex.empty()) {
|
|
|
|
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
|
2009-07-30 23:03:37 +00:00
|
|
|
LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
|
2010-02-16 11:11:14 +00:00
|
|
|
if (AST && LI->getType()->isPointerTy())
|
2008-10-27 07:05:53 +00:00
|
|
|
AST->deleteValue(LI);
|
|
|
|
LBI.deleteValue(LI);
|
|
|
|
LI->eraseFromParent();
|
2004-02-03 22:34:12 +00:00
|
|
|
}
|
2008-10-27 07:05:53 +00:00
|
|
|
return;
|
2004-02-03 22:34:12 +00:00
|
|
|
}
|
2007-08-04 20:01:43 +00:00
|
|
|
|
2008-10-27 07:05:53 +00:00
|
|
|
// Sort the stores by their index, making it efficient to do a lookup with a
|
|
|
|
// binary search.
|
|
|
|
std::sort(StoresByIndex.begin(), StoresByIndex.end());
|
|
|
|
|
|
|
|
// Walk all of the loads from this alloca, replacing them with the nearest
|
|
|
|
// store above them, if any.
|
|
|
|
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
|
|
|
|
LoadInst *LI = dyn_cast<LoadInst>(*UI++);
|
|
|
|
if (!LI) continue;
|
|
|
|
|
|
|
|
unsigned LoadIdx = LBI.getInstructionIndex(LI);
|
|
|
|
|
|
|
|
// Find the nearest store that has a lower than this load.
|
|
|
|
StoresByIndexTy::iterator I =
|
|
|
|
std::lower_bound(StoresByIndex.begin(), StoresByIndex.end(),
|
2010-05-11 06:17:44 +00:00
|
|
|
std::pair<unsigned, StoreInst*>(LoadIdx, static_cast<StoreInst*>(0)),
|
2008-10-27 07:05:53 +00:00
|
|
|
StoreIndexSearchPredicate());
|
|
|
|
|
|
|
|
// If there is no store before this load, then we can't promote this load.
|
|
|
|
if (I == StoresByIndex.begin()) {
|
|
|
|
// Can't handle this load, bail out.
|
|
|
|
Info.UsingBlocks.push_back(LI->getParent());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, there was a store before this load, the load takes its value.
|
|
|
|
--I;
|
|
|
|
LI->replaceAllUsesWith(I->second->getOperand(0));
|
2010-02-16 11:11:14 +00:00
|
|
|
if (AST && LI->getType()->isPointerTy())
|
2008-10-27 07:05:53 +00:00
|
|
|
AST->deleteValue(LI);
|
|
|
|
LI->eraseFromParent();
|
|
|
|
LBI.deleteValue(LI);
|
2007-08-04 20:01:43 +00:00
|
|
|
}
|
2004-02-03 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 18:27:55 +00:00
|
|
|
// QueuePhiNode - queues a phi-node to be added to a basic-block for a specific
|
|
|
|
// Alloca returns true if there wasn't already a phi-node for that variable
|
|
|
|
//
|
2003-10-05 04:33:22 +00:00
|
|
|
bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
|
2010-11-22 09:41:24 +00:00
|
|
|
unsigned &Version) {
|
2004-09-15 01:02:54 +00:00
|
|
|
// Look up the basic-block in question.
|
2007-02-07 01:15:04 +00:00
|
|
|
PHINode *&PN = NewPhiNodes[std::make_pair(BB, AllocaNo)];
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2002-04-28 18:27:55 +00:00
|
|
|
// If the BB already has a phi node added for the i'th alloca then we're done!
|
2007-02-07 01:15:04 +00:00
|
|
|
if (PN) return false;
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2002-09-10 22:38:47 +00:00
|
|
|
// Create a PhiNode using the dereferenced type... and add the phi-node to the
|
2003-04-18 19:25:22 +00:00
|
|
|
// BasicBlock.
|
2011-03-30 11:28:46 +00:00
|
|
|
PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB),
|
2009-07-30 17:37:43 +00:00
|
|
|
Allocas[AllocaNo]->getName() + "." + Twine(Version++),
|
2009-07-30 04:20:37 +00:00
|
|
|
BB->begin());
|
2007-08-04 22:50:14 +00:00
|
|
|
++NumPHIInsert;
|
2007-02-07 01:15:04 +00:00
|
|
|
PhiToAllocaMap[PN] = AllocaNo;
|
2004-09-15 01:02:54 +00:00
|
|
|
|
2010-02-16 11:11:14 +00:00
|
|
|
if (AST && PN->getType()->isPointerTy())
|
2004-09-15 01:02:54 +00:00
|
|
|
AST->copyValue(PointerAllocaValues[AllocaNo], PN);
|
|
|
|
|
2002-04-28 18:27:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2003-10-05 22:19:20 +00:00
|
|
|
// RenamePass - Recursively traverse the CFG of the function, renaming loads and
|
|
|
|
// stores to the allocas which we are promoting. IncomingVals indicates what
|
|
|
|
// value each Alloca contains on exit from the predecessor block Pred.
|
|
|
|
//
|
2003-02-22 23:57:48 +00:00
|
|
|
void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
|
2007-08-04 01:19:38 +00:00
|
|
|
RenamePassData::ValVector &IncomingVals,
|
2007-08-04 01:04:40 +00:00
|
|
|
std::vector<RenamePassData> &Worklist) {
|
2007-08-04 20:40:27 +00:00
|
|
|
NextIteration:
|
2007-02-07 01:15:04 +00:00
|
|
|
// If we are inserting any phi nodes into this BB, they will already be in the
|
|
|
|
// block.
|
|
|
|
if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) {
|
|
|
|
// If we have PHI nodes to update, compute the number of edges from Pred to
|
|
|
|
// BB.
|
2009-04-16 21:40:28 +00:00
|
|
|
if (PhiToAllocaMap.count(APN)) {
|
2008-02-05 21:26:23 +00:00
|
|
|
// We want to be able to distinguish between PHI nodes being inserted by
|
|
|
|
// this invocation of mem2reg from those phi nodes that already existed in
|
|
|
|
// the IR before mem2reg was run. We determine that APN is being inserted
|
|
|
|
// because it is missing incoming edges. All other PHI nodes being
|
|
|
|
// inserted by this pass of mem2reg will have the same number of incoming
|
|
|
|
// operands so far. Remember this count.
|
|
|
|
unsigned NewPHINumOperands = APN->getNumOperands();
|
|
|
|
|
2007-02-07 01:15:04 +00:00
|
|
|
unsigned NumEdges = 0;
|
2008-03-13 02:42:41 +00:00
|
|
|
for (succ_iterator I = succ_begin(Pred), E = succ_end(Pred); I != E; ++I)
|
|
|
|
if (*I == BB)
|
2007-02-07 01:15:04 +00:00
|
|
|
++NumEdges;
|
|
|
|
assert(NumEdges && "Must be at least one edge from Pred to BB!");
|
|
|
|
|
|
|
|
// Add entries for all the phis.
|
|
|
|
BasicBlock::iterator PNI = BB->begin();
|
|
|
|
do {
|
|
|
|
unsigned AllocaNo = PhiToAllocaMap[APN];
|
|
|
|
|
|
|
|
// Add N incoming values to the PHI node.
|
|
|
|
for (unsigned i = 0; i != NumEdges; ++i)
|
|
|
|
APN->addIncoming(IncomingVals[AllocaNo], Pred);
|
|
|
|
|
|
|
|
// The currently active variable for this block is now the PHI.
|
|
|
|
IncomingVals[AllocaNo] = APN;
|
|
|
|
|
|
|
|
// Get the next phi node.
|
|
|
|
++PNI;
|
|
|
|
APN = dyn_cast<PHINode>(PNI);
|
|
|
|
if (APN == 0) break;
|
|
|
|
|
2008-02-05 21:26:23 +00:00
|
|
|
// Verify that it is missing entries. If not, it is not being inserted
|
|
|
|
// by this mem2reg invocation so we want to ignore it.
|
|
|
|
} while (APN->getNumOperands() == NewPHINumOperands);
|
2007-02-07 01:15:04 +00:00
|
|
|
}
|
2003-10-05 02:37:36 +00:00
|
|
|
}
|
2007-02-07 01:15:04 +00:00
|
|
|
|
|
|
|
// Don't revisit blocks.
|
|
|
|
if (!Visited.insert(BB)) return;
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2003-10-05 03:45:44 +00:00
|
|
|
for (BasicBlock::iterator II = BB->begin(); !isa<TerminatorInst>(II); ) {
|
2003-10-05 01:52:53 +00:00
|
|
|
Instruction *I = II++; // get the instruction, increment iterator
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2002-04-28 18:27:55 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
2007-08-04 20:40:27 +00:00
|
|
|
AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand());
|
|
|
|
if (!Src) continue;
|
|
|
|
|
2011-01-18 04:50:38 +00:00
|
|
|
DenseMap<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src);
|
2007-08-04 20:40:27 +00:00
|
|
|
if (AI == AllocaLookup.end()) continue;
|
2002-03-27 23:17:37 +00:00
|
|
|
|
2007-08-04 20:40:27 +00:00
|
|
|
Value *V = IncomingVals[AI->second];
|
|
|
|
|
|
|
|
// Anything using the load now uses the current value.
|
|
|
|
LI->replaceAllUsesWith(V);
|
2010-02-16 11:11:14 +00:00
|
|
|
if (AST && LI->getType()->isPointerTy())
|
2007-08-04 20:40:27 +00:00
|
|
|
AST->deleteValue(LI);
|
|
|
|
BB->getInstList().erase(LI);
|
2002-04-28 18:27:55 +00:00
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
2003-02-22 22:25:17 +00:00
|
|
|
// Delete this instruction and mark the name as the current holder of the
|
2002-04-28 18:27:55 +00:00
|
|
|
// value
|
2007-08-04 20:40:27 +00:00
|
|
|
AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand());
|
|
|
|
if (!Dest) continue;
|
|
|
|
|
2011-01-18 04:50:38 +00:00
|
|
|
DenseMap<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest);
|
2007-08-04 20:40:27 +00:00
|
|
|
if (ai == AllocaLookup.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// what value were we writing?
|
|
|
|
IncomingVals[ai->second] = SI->getOperand(0);
|
2010-01-26 02:42:15 +00:00
|
|
|
// Record debuginfo for the store before removing it.
|
2011-03-17 21:58:19 +00:00
|
|
|
if (DbgDeclareInst *DDI = AllocaDbgDeclares[ai->second]) {
|
|
|
|
if (!DIB)
|
|
|
|
DIB = new DIBuilder(*SI->getParent()->getParent()->getParent());
|
|
|
|
ConvertDebugDeclareToDebugValue(DDI, SI, *DIB);
|
|
|
|
}
|
2007-08-04 20:40:27 +00:00
|
|
|
BB->getInstList().erase(SI);
|
2002-04-28 18:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-05 03:45:44 +00:00
|
|
|
|
2007-08-04 20:40:27 +00:00
|
|
|
// 'Recurse' to our successors.
|
2008-03-13 02:42:41 +00:00
|
|
|
succ_iterator I = succ_begin(BB), E = succ_end(BB);
|
|
|
|
if (I == E) return;
|
|
|
|
|
2009-04-16 21:40:28 +00:00
|
|
|
// Keep track of the successors so we don't visit the same successor twice
|
|
|
|
SmallPtrSet<BasicBlock*, 8> VisitedSuccs;
|
2008-03-13 02:42:41 +00:00
|
|
|
|
2009-04-16 21:40:28 +00:00
|
|
|
// Handle the first successor without using the worklist.
|
|
|
|
VisitedSuccs.insert(*I);
|
2007-08-04 20:40:27 +00:00
|
|
|
Pred = BB;
|
2008-03-13 02:42:41 +00:00
|
|
|
BB = *I;
|
2009-04-16 21:40:28 +00:00
|
|
|
++I;
|
|
|
|
|
|
|
|
for (; I != E; ++I)
|
|
|
|
if (VisitedSuccs.insert(*I))
|
|
|
|
Worklist.push_back(RenamePassData(*I, Pred, IncomingVals));
|
|
|
|
|
2007-08-04 20:40:27 +00:00
|
|
|
goto NextIteration;
|
2002-02-12 17:16:22 +00:00
|
|
|
}
|
2002-03-27 23:28:40 +00:00
|
|
|
|
2003-02-22 23:57:48 +00:00
|
|
|
/// PromoteMemToReg - Promote the specified list of alloca instructions into
|
2011-01-18 03:53:26 +00:00
|
|
|
/// scalar registers, inserting PHI nodes as appropriate. This function does
|
|
|
|
/// not modify the CFG of the function at all. All allocas must be from the
|
|
|
|
/// same function.
|
2003-02-22 23:57:48 +00:00
|
|
|
///
|
2004-09-15 01:02:54 +00:00
|
|
|
/// If AST is specified, the specified tracker is updated to reflect changes
|
|
|
|
/// made to the IR.
|
|
|
|
///
|
2004-01-09 06:12:26 +00:00
|
|
|
void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
|
2011-01-17 17:38:41 +00:00
|
|
|
DominatorTree &DT, AliasSetTracker *AST) {
|
2003-10-05 01:52:53 +00:00
|
|
|
// If there is nothing to do, bail out...
|
|
|
|
if (Allocas.empty()) return;
|
2005-06-30 07:29:44 +00:00
|
|
|
|
2011-01-17 17:38:41 +00:00
|
|
|
PromoteMem2Reg(Allocas, DT, AST).run();
|
2002-03-27 23:28:40 +00:00
|
|
|
}
|