X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FScalar%2FLICM.cpp;h=40af0a811d103650c5417abfd6874636472d4f62;hb=9289ae85b41eef62ae1fadb93e99d33f723fb682;hp=3eee4a27f7d6eda3bd1ebbd58563454c08b2df5f;hpb=ae73dc1448d25b02cabc7c64c86c64371453dda8;p=oota-llvm.git diff --git a/lib/Transforms/Scalar/LICM.cpp b/lib/Transforms/Scalar/LICM.cpp index 3eee4a27f7d..40af0a811d1 100644 --- a/lib/Transforms/Scalar/LICM.cpp +++ b/lib/Transforms/Scalar/LICM.cpp @@ -35,6 +35,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" +#include "llvm/IntrinsicInst.h" #include "llvm/Instructions.h" #include "llvm/Target/TargetData.h" #include "llvm/Analysis/LoopInfo.h" @@ -45,8 +46,8 @@ #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Transforms/Utils/PromoteMemToReg.h" #include "llvm/Support/CFG.h" -#include "llvm/Support/Compiler.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Support/Debug.h" #include "llvm/ADT/Statistic.h" #include @@ -62,9 +63,9 @@ static cl::opt DisablePromotion("disable-licm-promotion", cl::Hidden, cl::desc("Disable memory promotion in LICM pass")); -// This feature is currently disabled by default because CodeGen is not yet capable -// of rematerializing these constants in PIC mode, so it can lead to degraded -// performance. Compile test/CodeGen/X86/remat-constant.ll with +// This feature is currently disabled by default because CodeGen is not yet +// capable of rematerializing these constants in PIC mode, so it can lead to +// degraded performance. Compile test/CodeGen/X86/remat-constant.ll with // -relocation-model=pic to see an example of this. static cl::opt EnableLICMConstantMotion("enable-licm-constant-variables", cl::Hidden, @@ -72,7 +73,7 @@ EnableLICMConstantMotion("enable-licm-constant-variables", cl::Hidden, "global variables")); namespace { - struct VISIBILITY_HIDDEN LICM : public LoopPass { + struct LICM : public LoopPass { static char ID; // Pass identification, replacement for typeid LICM() : LoopPass(&ID) {} @@ -90,6 +91,7 @@ namespace { AU.addRequired(); AU.addPreserved(); AU.addPreserved(); + AU.addPreservedID(LoopSimplifyID); } bool doFinalization() { @@ -139,6 +141,10 @@ namespace { /// void HoistRegion(DomTreeNode *N); + // Cleanup debug information (remove stoppoints with no coressponding + // instructions). + void CleanupDbgInfoRegion(DomTreeNode *N); + /// inSubLoop - Little predicate that returns true if the specified basic /// block is in a subloop of the current one, not the current one itself. /// @@ -230,7 +236,7 @@ namespace { char LICM::ID = 0; static RegisterPass X("licm", "Loop Invariant Code Motion"); -LoopPass *llvm::createLICMPass() { return new LICM(); } +Pass *llvm::createLICMPass() { return new LICM(); } /// Hoist expressions out of the specified loop. Note, alias info for inner /// loop is not preserved so it is not a good idea to run LICM multiple @@ -286,6 +292,7 @@ bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) { // SinkRegion(DT->getNode(L->getHeader())); HoistRegion(DT->getNode(L->getHeader())); + CleanupDbgInfoRegion(DT->getNode(L->getHeader())); // Now that all loop invariants have been removed from the loop, promote any // memory references to scalars that we can... @@ -337,6 +344,35 @@ void LICM::SinkRegion(DomTreeNode *N) { } } +void LICM::CleanupDbgInfoRegion(DomTreeNode *N) { + BasicBlock *BB = N->getBlock(); + + // If this subregion is not in the top level loop at all, exit. + if (!CurLoop->contains(BB)) return; + + // We are processing blocks in reverse dfo, so process children first... + const std::vector &Children = N->getChildren(); + for (unsigned i = 0, e = Children.size(); i != e; ++i) + CleanupDbgInfoRegion(Children[i]); + + // Only need to process the contents of this block if it is not part of a + // subloop (which would already have been processed). + if (inSubLoop(BB)) return; + + // We modify the basicblock, so don't cache end() + for (BasicBlock::iterator I=BB->begin(); I != BB->end();) { + Instruction *Last = 0; + // Remove consecutive dbgstoppoints, leave only last + do { + if (Last) { + Last->eraseFromParent(); + Changed = true; + } + Last = I; + ++I; + } while (isa(Last) && isa(I)); + } +} /// HoistRegion - Walk the specified region of the CFG (defined by all blocks /// dominated by the specified block, and that are in the current loop) in depth @@ -388,9 +424,13 @@ bool LICM::canSinkOrHoistInst(Instruction &I) { // Don't hoist loads which have may-aliased stores in loop. unsigned Size = 0; if (LI->getType()->isSized()) - Size = AA->getTargetData().getTypeStoreSize(LI->getType()); + Size = AA->getTypeStoreSize(LI->getType()); return !pointerInvalidatedByLoop(LI->getOperand(0), Size); } else if (CallInst *CI = dyn_cast(&I)) { + if (isa(CI)) { + // Don't hoist/sink dbgstoppoints, we handle them separately + return false; + } // Handle obvious cases efficiently. AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI); if (Behavior == AliasAnalysis::DoesNotAccessMemory) @@ -464,7 +504,7 @@ bool LICM::isLoopInvariantInst(Instruction &I) { /// position, and may either delete it or move it to outside of the loop. /// void LICM::sink(Instruction &I) { - DOUT << "LICM sinking instruction: " << I; + DEBUG(errs() << "LICM sinking instruction: " << I); SmallVector ExitBlocks; CurLoop->getExitBlocks(ExitBlocks); @@ -488,7 +528,6 @@ void LICM::sink(Instruction &I) { // Move the instruction to the start of the exit block, after any PHI // nodes in it. I.removeFromParent(); - BasicBlock::iterator InsertPt = ExitBlocks[0]->getFirstNonPHI(); ExitBlocks[0]->getInstList().insert(InsertPt, &I); } @@ -506,7 +545,7 @@ void LICM::sink(Instruction &I) { // Firstly, we create a stack object to hold the value... AllocaInst *AI = 0; - if (I.getType() != Type::VoidTy) { + if (I.getType() != Type::getVoidTy(I.getContext())) { AI = new AllocaInst(I.getType(), 0, I.getName(), I.getParent()->getParent()->getEntryBlock().begin()); CurAST->add(AI); @@ -592,7 +631,7 @@ void LICM::sink(Instruction &I) { if (AI) { std::vector Allocas; Allocas.push_back(AI); - PromoteMemToReg(Allocas, *DT, *DF, CurAST); + PromoteMemToReg(Allocas, *DT, *DF, AI->getContext(), CurAST); } } } @@ -601,7 +640,7 @@ void LICM::sink(Instruction &I) { /// that is safe to hoist, this instruction is called to do the dirty work. /// void LICM::hoist(Instruction &I) { - DOUT << "LICM hoisting to " << Preheader->getName() << ": " << I; + DEBUG(errs() << "LICM hoisting to " << Preheader->getName() << ": " << I); // Remove the instruction from its current basic block... but don't delete the // instruction. @@ -622,7 +661,8 @@ void LICM::hoist(Instruction &I) { /// bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) { // If it is not a trapping instruction, it is always safe to hoist. - if (!Inst.isTrapping()) return true; + if (Inst.isSafeToSpeculativelyExecute()) + return true; // Otherwise we have to check to make sure that the instruction dominates all // of the exit blocks. If it doesn't, then there is a path out of the loop @@ -634,12 +674,6 @@ bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) { if (Inst.getParent() == CurLoop->getHeader()) return true; - // It's always safe to load from a global or alloca. - if (isa(Inst)) - if (isa(Inst.getOperand(0)) || - isa(Inst.getOperand(0))) - return true; - // Get the exit blocks for the current loop. SmallVector ExitBlocks; CurLoop->getExitBlocks(ExitBlocks); @@ -772,7 +806,7 @@ void LICM::PromoteValuesInLoop() { PromotedAllocas.reserve(PromotedValues.size()); for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) PromotedAllocas.push_back(PromotedValues[i].first); - PromoteMemToReg(PromotedAllocas, *DT, *DF, CurAST); + PromoteMemToReg(PromotedAllocas, *DT, *DF, Preheader->getContext(), CurAST); } /// FindPromotableValuesInLoop - Check the current loop for stores to definite @@ -785,9 +819,6 @@ void LICM::FindPromotableValuesInLoop( std::map &ValueToAllocaMap) { Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin(); - SmallVector ExitingBlocks; - CurLoop->getExitingBlocks(ExitingBlocks); - // Loop over all of the alias sets in the tracker object. for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); I != E; ++I) { @@ -796,12 +827,12 @@ void LICM::FindPromotableValuesInLoop( // set, if the pointer is loop invariant, and if we are not eliminating any // volatile loads or stores. if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() || - AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->first)) + AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue())) continue; assert(!AS.empty() && "Must alias set should have at least one pointer element in it!"); - Value *V = AS.begin()->first; + Value *V = AS.begin()->getValue(); // Check that all of the pointers in the alias set have the same type. We // cannot (yet) promote a memory location that is loaded and stored in @@ -809,7 +840,7 @@ void LICM::FindPromotableValuesInLoop( { bool PointerOk = true; for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) - if (V->getType() != I->first->getType()) { + if (V->getType() != I->getValue()->getType()) { PointerOk = false; break; } @@ -862,9 +893,9 @@ void LICM::FindPromotableValuesInLoop( CurAST->copyValue(V, AI); for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) - ValueToAllocaMap.insert(std::make_pair(I->first, AI)); + ValueToAllocaMap.insert(std::make_pair(I->getValue(), AI)); - DOUT << "LICM: Promoting value: " << *V << "\n"; + DEBUG(errs() << "LICM: Promoting value: " << *V << "\n"); } }