#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
+#include "llvm/LLVMContext.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
-#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
if (SI->isVolatile())
return false;
} else if (const BitCastInst *BC = dyn_cast<BitCastInst>(*UI)) {
- // Uses by dbg info shouldn't inhibit promotion.
+ // A bitcast that does not feed into debug info inhibits promotion.
if (!BC->hasOneUse() || !isa<DbgInfoIntrinsic>(*BC->use_begin()))
return false;
+ // If the only use is by debug info, this alloca will not exist in
+ // non-debug code, so don't try to promote; this ensures the same
+ // codegen with debug info. Otherwise, debug info should not
+ // inhibit promotion (but we must examine other uses).
+ if (AI->hasOneUse())
+ return false;
} else {
return false;
}
/// AST - An AliasSetTracker object to update. If null, don't update it.
///
AliasSetTracker *AST;
+
+ LLVMContext &Context;
/// AllocaLookup - Reverse mapping of Allocas.
///
DenseMap<const BasicBlock*, unsigned> BBNumPreds;
public:
PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt,
- DominanceFrontier &df, AliasSetTracker *ast)
- : Allocas(A), DT(dt), DF(df), AST(ast) {}
+ DominanceFrontier &df, AliasSetTracker *ast,
+ LLVMContext &C)
+ : Allocas(A), DT(dt), DF(df), AST(ast), Context(C) {}
void run();
AllocaPointerVal = 0;
}
- /// RemoveDebugUses - Remove uses of the alloca in DbgInfoInstrinsics.
- void RemoveDebugUses(AllocaInst *AI) {
- for (Value::use_iterator U = AI->use_begin(), E = AI->use_end();
- U != E;) {
- Instruction *User = cast<Instruction>(*U);
- ++U;
- if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) {
- assert(BC->hasOneUse() && "Unexpected alloca uses!");
- DbgInfoIntrinsic *DI = cast<DbgInfoIntrinsic>(*BC->use_begin());
- DI->eraseFromParent();
- BC->eraseFromParent();
- }
- }
- }
-
/// AnalyzeAlloca - Scan the uses of the specified alloca, filling in our
/// ivars.
void AnalyzeAlloca(AllocaInst *AI) {
// and decide whether all of the loads and stores to the alloca are within
// the same basic block.
for (Value::use_iterator U = AI->use_begin(), E = AI->use_end();
- U != E; ++U) {
+ U != E;) {
Instruction *User = cast<Instruction>(*U);
- if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
+ ++U;
+ if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) {
+ // Remove any uses of this alloca in DbgInfoInstrinsics.
+ assert(BC->hasOneUse() && "Unexpected alloca uses!");
+ DbgInfoIntrinsic *DI = cast<DbgInfoIntrinsic>(*BC->use_begin());
+ DI->eraseFromParent();
+ BC->eraseFromParent();
+ continue;
+ }
+ else if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
// Remember the basic blocks which define new values for the alloca
DefiningBlocks.push_back(SI->getParent());
AllocaPointerVal = SI->getOperand(0);
assert(AI->getParent()->getParent() == &F &&
"All allocas should be in the same function, which is same as DF!");
- // Remove any uses of this alloca in DbgInfoInstrinsics.
- Info.RemoveDebugUses(AI);
-
if (AI->use_empty()) {
// If there are no uses of the alloca, just delete it now.
if (AST) AST->deleteValue(AI);
//
RenamePassData::ValVector Values(Allocas.size());
for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
- Values[i] = UndefValue::get(Allocas[i]->getAllocatedType());
+ Values[i] = Context.getUndef(Allocas[i]->getAllocatedType());
// Walks all basic blocks in the function performing the SSA rename algorithm
// and inserting the phi nodes we marked as necessary
// Just delete the users now.
//
if (!A->use_empty())
- A->replaceAllUsesWith(UndefValue::get(A->getType()));
+ A->replaceAllUsesWith(Context.getUndef(A->getType()));
if (AST) AST->deleteValue(A);
A->eraseFromParent();
}
BasicBlock::iterator BBI = BB->begin();
while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
SomePHI->getNumIncomingValues() == NumBadPreds) {
- Value *UndefVal = UndefValue::get(SomePHI->getType());
+ Value *UndefVal = Context.getUndef(SomePHI->getType());
for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
SomePHI->addIncoming(UndefVal, Preds[pred]);
}
// 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()) {
- BasicBlock *BB = LiveInBlockWorklist.back();
- LiveInBlockWorklist.pop_back();
+ BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
// The block really is live in here, insert it into the set. If already in
// the set, then it has already been processed.
if (StoresByIndex.empty()) {
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
- LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
+ LI->replaceAllUsesWith(Context.getUndef(LI->getType()));
if (AST && isa<PointerType>(LI->getType()))
AST->deleteValue(LI);
LBI.deleteValue(LI);
// Create a PhiNode using the dereferenced type... and add the phi-node to the
// BasicBlock.
PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(),
- Allocas[AllocaNo]->getName() + "." +
- utostr(Version++), BB->begin());
+ Allocas[AllocaNo]->getName() + "." + Version++,
+ BB->begin());
++NumPHIInsert;
PhiToAllocaMap[PN] = AllocaNo;
PN->reserveOperandSpace(getNumPreds(BB));
// 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())) {
- // Pred may have multiple edges to BB. If so, we want to add N incoming
- // values to each PHI we are inserting on the first time we see the edge.
- // Check to see if APN already has incoming values from Pred. This also
- // prevents us from modifying PHI nodes that are not currently being
- // inserted.
- bool HasPredEntries = false;
- for (unsigned i = 0, e = APN->getNumIncomingValues(); i != e; ++i) {
- if (APN->getIncomingBlock(i) == Pred) {
- HasPredEntries = true;
- break;
- }
- }
-
// If we have PHI nodes to update, compute the number of edges from Pred to
// BB.
- if (!HasPredEntries) {
+ if (PhiToAllocaMap.count(APN)) {
// 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
succ_iterator I = succ_begin(BB), E = succ_end(BB);
if (I == E) return;
- // Handle the last successor without using the worklist. This allows us to
- // handle unconditional branches directly, for example.
- --E;
- for (; I != E; ++I)
- Worklist.push_back(RenamePassData(*I, BB, IncomingVals));
+ // Keep track of the successors so we don't visit the same successor twice
+ SmallPtrSet<BasicBlock*, 8> VisitedSuccs;
+ // Handle the first successor without using the worklist.
+ VisitedSuccs.insert(*I);
Pred = BB;
BB = *I;
+ ++I;
+
+ for (; I != E; ++I)
+ if (VisitedSuccs.insert(*I))
+ Worklist.push_back(RenamePassData(*I, Pred, IncomingVals));
+
goto NextIteration;
}
///
void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
DominatorTree &DT, DominanceFrontier &DF,
- AliasSetTracker *AST) {
+ LLVMContext &Context, AliasSetTracker *AST) {
// If there is nothing to do, bail out...
if (Allocas.empty()) return;
- PromoteMem2Reg(Allocas, DT, DF, AST).run();
+ PromoteMem2Reg(Allocas, DT, DF, AST, Context).run();
}