Finish LLVMContext-ing lib/Analysis. This required pushing LLVMContext's through...
[oota-llvm.git] / lib / Transforms / Scalar / TailDuplication.cpp
index 4e0949a2b4712c4cd9e17bf27e30f8faffe0c6d3..06815d29b2d8bee6d516d4c2e213e59deace8252 100644 (file)
 #include "llvm/Pass.h"
 #include "llvm/Type.h"
 #include "llvm/Support/CFG.h"
+#include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/SmallPtrSet.h"
-#include "llvm/Analysis/LoopInfo.h"
 #include <map>
 using namespace llvm;
 
@@ -49,14 +49,12 @@ namespace {
     bool runOnFunction(Function &F);
   public:
     static char ID; // Pass identification, replacement for typeid
-    TailDup() : FunctionPass((intptr_t)&ID) {}
+    TailDup() : FunctionPass(&ID) {}
 
-    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
   private:
     inline bool shouldEliminateUnconditionalBranch(TerminatorInst *, unsigned);
     inline void eliminateUnconditionalBranch(BranchInst *BI);
     SmallPtrSet<BasicBlock*, 4> CycleDetector;
-    LoopInfo *LI;  // The current loop information
   };
 }
 
@@ -72,9 +70,6 @@ FunctionPass *llvm::createTailDuplicationPass() { return new TailDup(); }
 /// a place it already pointed to earlier; see PR 2323.
 bool TailDup::runOnFunction(Function &F) {
   bool Changed = false;
-
-  LI = &getAnalysis<LoopInfo>();
-
   CycleDetector.clear();
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
     if (shouldEliminateUnconditionalBranch(I->getTerminator(),
@@ -89,10 +84,6 @@ bool TailDup::runOnFunction(Function &F) {
   return Changed;
 }
 
-void TailDup::getAnalysisUsage(AnalysisUsage &AU) const {
-  AU.addRequired<LoopInfo>();
-}
-
 /// shouldEliminateUnconditionalBranch - Return true if this branch looks
 /// attractive to eliminate.  We eliminate the branch if the destination basic
 /// block has <= 5 instructions in it, not counting PHI nodes.  In practice,
@@ -196,14 +187,6 @@ bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI,
   if (!CycleDetector.insert(Dest))
     return false;
 
-  // Avoid non-natural loops:
-  // If a loop header is duplicated, the former natural loop will contain two
-  // paths into the loop --> the loop it not natural anymore. We want to avoid
-  // this, because other optimizaions may fail to improve the loop because of 
-  // this.
-  if (LI->isLoopHeader(Dest))
-    return false;
-
   return true;
 }
 
@@ -275,7 +258,7 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) {
     while (!isa<TerminatorInst>(BBI)) {
       Instruction *I = BBI++;
 
-      bool CanHoist = !I->isTrapping() && !I->mayWriteToMemory();
+      bool CanHoist = !I->isTrapping() && !I->mayHaveSideEffects();
       if (CanHoist) {
         for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
           if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(op)))
@@ -334,9 +317,12 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) {
   //
   BI = Branch; ++BI;  // Get an iterator to the first new instruction
   for (; BI != SourceBlock->end(); ++BI)
-    for (unsigned i = 0, e = BI->getNumOperands(); i != e; ++i)
-      if (Value *Remapped = ValueMapping[BI->getOperand(i)])
-        BI->setOperand(i, Remapped);
+    for (unsigned i = 0, e = BI->getNumOperands(); i != e; ++i) {
+      std::map<Value*, Value*>::const_iterator I =
+        ValueMapping.find(BI->getOperand(i));
+      if (I != ValueMapping.end())
+        BI->setOperand(i, I->second);
+    }
 
   // Next we check to see if any of the successors of DestBlock had PHI nodes.
   // If so, we need to add entries to the PHI nodes for SourceBlock now.
@@ -350,8 +336,9 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) {
       Value *IV = PN->getIncomingValueForBlock(DestBlock);
 
       // Remap the value if necessary...
-      if (Value *MappedIV = ValueMapping[IV])
-        IV = MappedIV;
+      std::map<Value*, Value*>::const_iterator I = ValueMapping.find(IV);
+      if (I != ValueMapping.end())
+        IV = I->second;
       PN->addIncoming(IV, SourceBlock);
     }
   }
@@ -366,10 +353,17 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) {
   // instructions one last time, constant propagating and DCE'ing them, because
   // they may not be needed anymore.
   //
-  if (HadPHINodes)
-    while (BI != SourceBlock->end())
-      if (!dceInstruction(BI) && !doConstantPropagation(BI))
-        ++BI;
+  if (HadPHINodes) {
+    while (BI != SourceBlock->end()) {
+      Instruction *Inst = BI++;
+      if (isInstructionTriviallyDead(Inst))
+        Inst->eraseFromParent();
+      else if (Constant *C = ConstantFoldInstruction(Inst, Context)) {
+        Inst->replaceAllUsesWith(C);
+        Inst->eraseFromParent();
+      }
+    }
+  }
 
   ++NumEliminated;  // We just killed a branch!
 }