Insert new instructions in AliasSet.
[oota-llvm.git] / lib / Transforms / Scalar / LICM.cpp
index 1ed490d294db74114e717b3c75413a97227d0081..3b16814fe86d07b93608e5a2b52f7f49f9403983 100644 (file)
@@ -63,6 +63,9 @@ namespace {
                    cl::desc("Disable memory promotion in LICM pass"));
 
   struct VISIBILITY_HIDDEN LICM : public LoopPass {
+    static char ID; // Pass identification, replacement for typeid
+    LICM() : LoopPass((intptr_t)&ID) {}
+
     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
 
     /// This transformation requires natural loop information & requires that
@@ -72,6 +75,7 @@ namespace {
       AU.setPreservesCFG();
       AU.addRequiredID(LoopSimplifyID);
       AU.addRequired<LoopInfo>();
+      AU.addRequired<DominatorTree>();
       AU.addRequired<ETForest>();
       AU.addRequired<DominanceFrontier>();  // For scalar promotion (mem2reg)
       AU.addRequired<AliasAnalysis>();
@@ -86,7 +90,8 @@ namespace {
     // Various analyses that we use...
     AliasAnalysis *AA;       // Current AliasAnalysis information
     LoopInfo      *LI;       // Current LoopInfo
-    ETForest *ET;       // ETForest for the current Loop...
+    ETForest      *ET;       // ETForest for the current loop..
+    DominatorTree *DT;       // Dominator Tree for the current Loop...
     DominanceFrontier *DF;   // Current Dominance Frontier
 
     // State that is updated as we process loops
@@ -98,19 +103,19 @@ namespace {
 
     /// SinkRegion - Walk the specified region of the CFG (defined by all blocks
     /// dominated by the specified block, and that are in the current loop) in
-    /// reverse depth first order w.r.t the ETForest.  This allows us to
+    /// reverse depth first order w.r.t the DominatorTree.  This allows us to
     /// visit uses before definitions, allowing us to sink a loop body in one
     /// pass without iteration.
     ///
-    void SinkRegion(BasicBlock *BB);
+    void SinkRegion(DominatorTree::Node *N);
 
     /// 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 first order w.r.t the ETForest.  This allows us to
+    /// loop) in depth first order w.r.t the DominatorTree.  This allows us to
     /// visit definitions before uses, allowing us to hoist a loop body in one
     /// pass without iteration.
     ///
-    void HoistRegion(BasicBlock *BB);
+    void HoistRegion(DominatorTree::Node *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.
@@ -135,20 +140,21 @@ namespace {
       if (BlockInLoop == LoopHeader)
         return true;
 
-      BasicBlock *IDom = ExitBlock;
+      DominatorTree::Node *BlockInLoopNode = DT->getNode(BlockInLoop);
+      DominatorTree::Node *IDom            = DT->getNode(ExitBlock);
 
       // Because the exit block is not in the loop, we know we have to get _at
       // least_ its immediate dominator.
       do {
         // Get next Immediate Dominator.
-        IDom = ET->getIDom(IDom);
+        IDom = IDom->getIDom();
 
         // If we have got to the header of the loop, then the instructions block
         // did not dominate the exit node, so we can't hoist it.
-        if (IDom == LoopHeader)
+        if (IDom->getBlock() == LoopHeader)
           return false;
 
-      } while (IDom != BlockInLoop);
+      } while (IDom != BlockInLoopNode);
 
       return true;
     }
@@ -198,6 +204,7 @@ namespace {
                                     std::map<Value*, AllocaInst*> &Val2AlMap);
   };
 
+  char LICM::ID = 0;
   RegisterPass<LICM> X("licm", "Loop Invariant Code Motion");
 }
 
@@ -212,10 +219,11 @@ bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
   LI = &getAnalysis<LoopInfo>();
   AA = &getAnalysis<AliasAnalysis>();
   DF = &getAnalysis<DominanceFrontier>();
+  DT = &getAnalysis<DominatorTree>();
   ET = &getAnalysis<ETForest>();
 
   CurAST = new AliasSetTracker(*AA);
-  // Collect Alias info frmo subloops
+  // Collect Alias info from subloops
   for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end();
        LoopItr != LoopItrE; ++LoopItr) {
     Loop *InnerL = *LoopItr;
@@ -251,8 +259,8 @@ bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
   // us to sink instructions in one pass, without iteration.  AFter sinking
   // instructions, we perform another pass to hoist them out of the loop.
   //
-  SinkRegion(L->getHeader());
-  HoistRegion(L->getHeader());
+  SinkRegion(DT->getNode(L->getHeader()));
+  HoistRegion(DT->getNode(L->getHeader()));
 
   // Now that all loop invariants have been removed from the loop, promote any
   // memory references to scalars that we can...
@@ -269,19 +277,19 @@ bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
 
 /// SinkRegion - Walk the specified region of the CFG (defined by all blocks
 /// dominated by the specified block, and that are in the current loop) in
-/// reverse depth first order w.r.t the ETForest.  This allows us to visit
+/// reverse depth first order w.r.t the DominatorTree.  This allows us to visit
 /// uses before definitions, allowing us to sink a loop body in one pass without
 /// iteration.
 ///
-void LICM::SinkRegion(BasicBlock *BB) {
-  assert(BB != 0 && "Null sink block?");
+void LICM::SinkRegion(DominatorTree::Node *N) {
+  assert(N != 0 && "Null dominator tree node?");
+  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...
-  std::vector<BasicBlock*> Children;
-  ET->getChildren(BB, Children);
+  const std::vector<DominatorTree::Node*> &Children = N->getChildren();
   for (unsigned i = 0, e = Children.size(); i != e; ++i)
     SinkRegion(Children[i]);
 
@@ -307,11 +315,12 @@ void LICM::SinkRegion(BasicBlock *BB) {
 
 /// 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
-/// first order w.r.t the ETForest.  This allows us to visit definitions
+/// first order w.r.t the DominatorTree.  This allows us to visit definitions
 /// before uses, allowing us to hoist a loop body in one pass without iteration.
 ///
-void LICM::HoistRegion(BasicBlock *BB) {
-  assert(BB != 0 && "Null hoist block?");
+void LICM::HoistRegion(DominatorTree::Node *N) {
+  assert(N != 0 && "Null dominator tree node?");
+  BasicBlock *BB = N->getBlock();
 
   // If this subregion is not in the top level loop at all, exit.
   if (!CurLoop->contains(BB)) return;
@@ -331,8 +340,7 @@ void LICM::HoistRegion(BasicBlock *BB) {
         hoist(I);
       }
 
-  std::vector<BasicBlock*> Children;
-  ET->getChildren(BB, Children);    
+  const std::vector<DominatorTree::Node*> &Children = N->getChildren();
   for (unsigned i = 0, e = Children.size(); i != e; ++i)
     HoistRegion(Children[i]);
 }
@@ -468,9 +476,11 @@ 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::VoidTy) {
       AI = new AllocaInst(I.getType(), 0, I.getName(),
                           I.getParent()->getParent()->getEntryBlock().begin());
+      CurAST->add(AI);
+    }
 
     // Secondly, insert load instructions for each use of the instruction
     // outside of the loop.
@@ -491,6 +501,7 @@ void LICM::sink(Instruction &I) {
               // Insert a new load instruction right before the terminator in
               // the predecessor block.
               PredVal = new LoadInst(AI, "", Pred->getTerminator());
+              CurAST->add(cast<LoadInst>(PredVal));
             }
 
             UPN->setIncomingValue(i, PredVal);
@@ -499,6 +510,7 @@ void LICM::sink(Instruction &I) {
       } else {
         LoadInst *L = new LoadInst(AI, "", U);
         U->replaceUsesOfWith(&I, L);
+        CurAST->add(L);
       }
     }
 
@@ -551,7 +563,7 @@ void LICM::sink(Instruction &I) {
     if (AI) {
       std::vector<AllocaInst*> Allocas;
       Allocas.push_back(AI);
-      PromoteMemToReg(Allocas, *ET, *DF, AA->getTargetData(), CurAST);
+      PromoteMemToReg(Allocas, *ET, *DF, CurAST);
     }
   }
 }
@@ -603,7 +615,7 @@ bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) {
   std::vector<BasicBlock*> ExitBlocks;
   CurLoop->getExitBlocks(ExitBlocks);
 
-  // For each exit block, walk up the ET until the
+  // For each exit block, get the DT node and walk up the DT until the
   // instruction's basic block is found or we exit the loop.
   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
     if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[i], Inst.getParent()))
@@ -732,7 +744,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, *ET, *DF, AA->getTargetData(), CurAST);
+  PromoteMemToReg(PromotedAllocas, *ET, *DF, CurAST);
 }
 
 /// FindPromotableValuesInLoop - Check the current loop for stores to definite