Insert new instructions in AliasSet.
[oota-llvm.git] / lib / Transforms / Scalar / LICM.cpp
index 4ef43f8e9351a5af31c98625f485a3e0915b1195..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
@@ -73,6 +76,7 @@ namespace {
       AU.addRequiredID(LoopSimplifyID);
       AU.addRequired<LoopInfo>();
       AU.addRequired<DominatorTree>();
+      AU.addRequired<ETForest>();
       AU.addRequired<DominanceFrontier>();  // For scalar promotion (mem2reg)
       AU.addRequired<AliasAnalysis>();
     }
@@ -86,6 +90,7 @@ namespace {
     // Various analyses that we use...
     AliasAnalysis *AA;       // Current AliasAnalysis information
     LoopInfo      *LI;       // Current LoopInfo
+    ETForest      *ET;       // ETForest for the current loop..
     DominatorTree *DT;       // Dominator Tree for the current Loop...
     DominanceFrontier *DF;   // Current Dominance Frontier
 
@@ -199,6 +204,7 @@ namespace {
                                     std::map<Value*, AllocaInst*> &Val2AlMap);
   };
 
+  char LICM::ID = 0;
   RegisterPass<LICM> X("licm", "Loop Invariant Code Motion");
 }
 
@@ -214,9 +220,10 @@ bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
   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;
@@ -469,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.
@@ -492,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);
@@ -500,6 +510,7 @@ void LICM::sink(Instruction &I) {
       } else {
         LoadInst *L = new LoadInst(AI, "", U);
         U->replaceUsesOfWith(&I, L);
+        CurAST->add(L);
       }
     }
 
@@ -552,7 +563,7 @@ void LICM::sink(Instruction &I) {
     if (AI) {
       std::vector<AllocaInst*> Allocas;
       Allocas.push_back(AI);
-      PromoteMemToReg(Allocas, *DT, *DF, AA->getTargetData(), CurAST);
+      PromoteMemToReg(Allocas, *ET, *DF, CurAST);
     }
   }
 }
@@ -733,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, *DT, *DF, AA->getTargetData(), CurAST);
+  PromoteMemToReg(PromotedAllocas, *ET, *DF, CurAST);
 }
 
 /// FindPromotableValuesInLoop - Check the current loop for stores to definite