Avoid inserting an entry block unless we need it
[oota-llvm.git] / lib / Transforms / Scalar / Reassociate.cpp
index f669262ee66d0c7a249080974dc0b5e021a9f994..24d7dcebe8e4fd022c2759d117c6f0d1d3cdef30 100644 (file)
 #include "Support/PostOrderIterator.h"
 #include "Support/StatisticReporter.h"
 
-//#define DEBUG_REASSOC(x) std::cerr << x
-#define DEBUG_REASSOC(x)
-
 static Statistic<> NumLinear ("reassociate\t- Number of insts linearized");
 static Statistic<> NumChanged("reassociate\t- Number of insts reassociated");
 static Statistic<> NumSwapped("reassociate\t- Number of insts with operands swapped");
 
 namespace {
   class Reassociate : public FunctionPass {
-    map<BasicBlock*, unsigned> RankMap;
+    std::map<BasicBlock*, unsigned> RankMap;
   public:
-    const char *getPassName() const {
-      return "Expression Reassociation";
-    }
-
-    bool runOnFunction(Function *F);
+    bool runOnFunction(Function &F);
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.preservesCFG();
     }
   private:
-    void BuildRankMap(Function *F);
+    void BuildRankMap(Function &F);
     unsigned getRank(Value *V);
     bool ReassociateExpr(BinaryOperator *I);
     bool ReassociateBB(BasicBlock *BB);
   };
+
+  RegisterOpt<Reassociate> X("reassociate", "Reassociate expressions");
 }
 
 Pass *createReassociatePass() { return new Reassociate(); }
 
-void Reassociate::BuildRankMap(Function *F) {
+void Reassociate::BuildRankMap(Function &F) {
   unsigned i = 1;
-  ReversePostOrderTraversal<Function*> RPOT(F);
+  ReversePostOrderTraversal<Function*> RPOT(&F);
   for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
          E = RPOT.end(); I != E; ++I)
     RankMap[*I] = ++i;
@@ -125,7 +120,7 @@ bool Reassociate::ReassociateExpr(BinaryOperator *I) {
     std::swap(LHSRank, RHSRank);
     Changed = true;
     ++NumSwapped;
-    DEBUG_REASSOC("Transposed: " << I << " Result BB: " << I->getParent());
+    DEBUG(std::cerr << "Transposed: " << I << " Result BB: " << I->getParent());
   }
   
   // If the LHS is the same operator as the current one is, and if we are the
@@ -147,7 +142,8 @@ bool Reassociate::ReassociateExpr(BinaryOperator *I) {
         I->setOperand(1, LHSI);
 
         ++NumChanged;
-        DEBUG_REASSOC("Reassociated: " << I << " Result BB: " <<I->getParent());
+        DEBUG(std::cerr << "Reassociated: " << I << " Result BB: "
+                        << I->getParent());
 
         // Since we modified the RHS instruction, make sure that we recheck it.
         ReassociateExpr(LHSI);
@@ -184,15 +180,11 @@ static Value *NegateValue(Value *V, BasicBlock *BB, BasicBlock::iterator &BI) {
       // adding it now, we are assured that the neg instructions we just
       // inserted dominate the instruction we are about to insert after them.
       //
-      BasicBlock::iterator NBI = BI;
-
-      // Scan through the inserted instructions, looking for RHS, which must be
-      // after LHS in the instruction list.
-      while (*NBI != RHS) ++NBI;
+      BasicBlock::iterator NBI = cast<Instruction>(RHS);
 
       Instruction *Add =
         BinaryOperator::create(Instruction::Add, LHS, RHS, I->getName()+".neg");
-      BB->getInstList().insert(NBI+1, Add);  // Add to the basic block...
+      BB->getInstList().insert(++NBI, Add);  // Add to the basic block...
       return Add;
     }
 
@@ -211,12 +203,11 @@ static Value *NegateValue(Value *V, BasicBlock *BB, BasicBlock::iterator &BI) {
 bool Reassociate::ReassociateBB(BasicBlock *BB) {
   bool Changed = false;
   for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) {
-    Instruction *Inst = *BI;
 
     // If this instruction is a commutative binary operator, and the ranks of
     // the two operands are sorted incorrectly, fix it now.
     //
-    if (BinaryOperator *I = isCommutativeOperator(Inst)) {
+    if (BinaryOperator *I = isCommutativeOperator(BI)) {
       if (!I->use_empty()) {
         // Make sure that we don't have a tree-shaped computation.  If we do,
         // linearize it.  Convert (A+B)+(C+D) into ((A+B)+C)+D
@@ -238,7 +229,7 @@ bool Reassociate::ReassociateBB(BasicBlock *BB) {
           I = Tmp;
           ++NumLinear;
           Changed = true;
-          DEBUG_REASSOC("Linearized: " << I << " Result BB: " << BB);
+          DEBUG(std::cerr << "Linearized: " << I << " Result BB: " << BB);
         }
 
         // Make sure that this expression is correctly reassociated with respect
@@ -247,29 +238,30 @@ bool Reassociate::ReassociateBB(BasicBlock *BB) {
         Changed |= ReassociateExpr(I);
       }
 
-    } else if (Inst->getOpcode() == Instruction::Sub &&
-               Inst->getOperand(0) != Constant::getNullValue(Inst->getType())) {
+    } else if (BI->getOpcode() == Instruction::Sub &&
+               BI->getOperand(0) != Constant::getNullValue(BI->getType())) {
       // Convert a subtract into an add and a neg instruction... so that sub
       // instructions can be commuted with other add instructions...
       //
       Instruction *New = BinaryOperator::create(Instruction::Add,
-                                                Inst->getOperand(0),
-                                                Inst->getOperand(1),
-                                                Inst->getName());
-      Value *NegatedValue = Inst->getOperand(1);
+                                                BI->getOperand(0),
+                                                BI->getOperand(1),
+                                                BI->getName());
+      Value *NegatedValue = BI->getOperand(1);
 
       // Everyone now refers to the add instruction...
-      Inst->replaceAllUsesWith(New);
+      BI->replaceAllUsesWith(New);
 
       // Put the new add in the place of the subtract... deleting the subtract
-      delete BB->getInstList().replaceWith(BI, New);
+      BI = BB->getInstList().erase(BI);
+      BI = ++BB->getInstList().insert(BI, New);
 
       // Calculate the negative value of Operand 1 of the sub instruction...
       // and set it as the RHS of the add instruction we just made...
       New->setOperand(1, NegateValue(NegatedValue, BB, BI));
       --BI;
       Changed = true;
-      DEBUG_REASSOC("Negated: " << New << " Result BB: " << BB);
+      DEBUG(std::cerr << "Negated: " << New << " Result BB: " << BB);
     }
   }
 
@@ -277,13 +269,13 @@ bool Reassociate::ReassociateBB(BasicBlock *BB) {
 }
 
 
-bool Reassociate::runOnFunction(Function *F) {
+bool Reassociate::runOnFunction(Function &F) {
   // Recalculate the rank map for F
   BuildRankMap(F);
 
   bool Changed = false;
-  for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
-    Changed |= ReassociateBB(*FI);
+  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
+    Changed |= ReassociateBB(FI);
 
   // We are done with the rank map...
   RankMap.clear();