Fix bug: LICM/sink_multiple_exits.ll
[oota-llvm.git] / lib / Transforms / Scalar / LICM.cpp
index 2293c17c00154011e913658ec4987a090e999536..0d8a848cc0d76444992bf7af8904c86f7577ba50 100644 (file)
@@ -366,7 +366,12 @@ void LICM::sink(Instruction &I) {
   DEBUG(std::cerr << "LICM sinking instruction: " << I);
 
   const std::vector<BasicBlock*> &ExitBlocks = CurLoop->getExitBlocks();
-  
+  std::vector<Value*> Operands(I.op_begin(), I.op_end());
+
+  if (isa<LoadInst>(I)) ++NumMovedLoads;
+  ++NumSunk;
+  Changed = true;
+
   // The case where there is only a single exit node of this loop is common
   // enough that we handle it as a special (more efficient) case.  It is more
   // efficient to handle because there are no PHI nodes that need to be placed.
@@ -386,7 +391,6 @@ void LICM::sink(Instruction &I) {
   } else if (ExitBlocks.size() == 0) {
     // The instruction is actually dead if there ARE NO exit blocks.
     I.getParent()->getInstList().erase(&I);
-    return;   // Don't count this as a sunk instruction, don't check operands.
   } else {
     // Otherwise, if we have multiple exits, use the PromoteMem2Reg function to
     // do all of the hard work of inserting PHI nodes as necessary.  We convert
@@ -437,10 +441,8 @@ void LICM::sink(Instruction &I) {
       BasicBlock *ExitBlock = ExitBlocks[i];
 
       if (isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB)) {
-        std::set<BasicBlock*>::iterator SI =
-          InsertedBlocks.lower_bound(ExitBlock);
         // If we haven't already processed this exit block, do so now.
-        if (SI == InsertedBlocks.end() || *SI != ExitBlock) {
+        if (InsertedBlocks.insert(ExitBlock).second) {
           // Insert the code after the last PHI node...
           BasicBlock::iterator InsertPt = ExitBlock->begin();
           while (isa<PHINode>(InsertPt)) ++InsertPt;
@@ -449,7 +451,7 @@ void LICM::sink(Instruction &I) {
           // instruction, otherwise clone the original instruction and insert
           // the copy.
           Instruction *New;
-          if (InsertedBlocks.empty()) {
+          if (InsertedBlocks.size() == 1) {
             I.getParent()->getInstList().remove(&I);
             ExitBlock->getInstList().insert(InsertPt, &I);
             New = &I;
@@ -461,9 +463,6 @@ void LICM::sink(Instruction &I) {
           
           // Now that we have inserted the instruction, store it into the alloca
           new StoreInst(New, AI, InsertPt);
-          
-          // Remember we processed this block
-          InsertedBlocks.insert(SI, ExitBlock);
         }
       }
     }
@@ -474,17 +473,12 @@ void LICM::sink(Instruction &I) {
     PromoteMemToReg(Allocas, *DT, *DF, AA->getTargetData());
   }
   
-  if (isa<LoadInst>(I)) ++NumMovedLoads;
-  ++NumSunk;
-  Changed = true;
-
   // Since we just sunk an instruction, check to see if any other instructions
   // used by this instruction are now sinkable.  If so, sink them too.
-  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
-    if (Instruction *OpI = dyn_cast<Instruction>(I.getOperand(i)))
+  for (unsigned i = 0, e = Operands.size(); i != e; ++i)
+    if (Instruction *OpI = dyn_cast<Instruction>(Operands[i]))
       if (CurLoop->contains(OpI->getParent()) && canSinkOrHoistInst(*OpI) &&
-          isNotUsedInLoop(*OpI) &&
-          isSafeToExecuteUnconditionally(*OpI))
+          isNotUsedInLoop(*OpI) && isSafeToExecuteUnconditionally(*OpI))
         sink(*OpI);
 }
 
@@ -603,9 +597,7 @@ void LICM::PromoteValuesInLoop() {
 
   const std::vector<BasicBlock*> &ExitBlocks = CurLoop->getExitBlocks();
   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
-    if (!ProcessedBlocks.count(ExitBlocks[i])) {
-      ProcessedBlocks.insert(ExitBlocks[i]);
-    
+    if (ProcessedBlocks.insert(ExitBlocks[i]).second) {
       // Copy all of the allocas into their memory locations...
       BasicBlock::iterator BI = ExitBlocks[i]->begin();
       while (isa<PHINode>(*BI))