- Somehow I forgot about one / une.
[oota-llvm.git] / lib / Transforms / Utils / PromoteMemoryToRegister.cpp
index 523d548540d651d46980b12cd4ab604ee146d365..ebd68ddcbebf7a74354ec945eec502398566d01a 100644 (file)
@@ -834,9 +834,9 @@ bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
 
   // Create a PhiNode using the dereferenced type... and add the phi-node to the
   // BasicBlock.
-  PN = new PHINode(Allocas[AllocaNo]->getAllocatedType(),
-                   Allocas[AllocaNo]->getName() + "." +
-                   utostr(Version++), BB->begin());
+  PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(),
+                       Allocas[AllocaNo]->getName() + "." +
+                       utostr(Version++), BB->begin());
   ++NumPHIInsert;
   PhiToAllocaMap[PN] = AllocaNo;
   PN->reserveOperandSpace(getNumPreds(BB));
@@ -849,7 +849,6 @@ bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo,
   return true;
 }
 
-
 // RenamePass - Recursively traverse the CFG of the function, renaming loads and
 // stores to the allocas which we are promoting.  IncomingVals indicates what
 // value each Alloca contains on exit from the predecessor block Pred.
@@ -877,12 +876,18 @@ NextIteration:
     // If we have PHI nodes to update, compute the number of edges from Pred to
     // BB.
     if (!HasPredEntries) {
-      TerminatorInst *PredTerm = Pred->getTerminator();
+      // We want to be able to distinguish between PHI nodes being inserted by
+      // this invocation of mem2reg from those phi nodes that already existed in
+      // the IR before mem2reg was run.  We determine that APN is being inserted
+      // because it is missing incoming edges.  All other PHI nodes being
+      // inserted by this pass of mem2reg will have the same number of incoming
+      // operands so far.  Remember this count.
+      unsigned NewPHINumOperands = APN->getNumOperands();
+      
       unsigned NumEdges = 0;
-      for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
-        if (PredTerm->getSuccessor(i) == BB)
+      for (succ_iterator I = succ_begin(Pred), E = succ_end(Pred); I != E; ++I)
+        if (*I == BB)
           ++NumEdges;
-      }
       assert(NumEdges && "Must be at least one edge from Pred to BB!");
       
       // Add entries for all the phis.
@@ -902,16 +907,9 @@ NextIteration:
         APN = dyn_cast<PHINode>(PNI);
         if (APN == 0) break;
         
-        // Verify it doesn't already have entries for Pred.  If it does, it is
-        // not being inserted by this mem2reg invocation.
-        HasPredEntries = false;
-        for (unsigned i = 0, e = APN->getNumIncomingValues(); i != e; ++i) {
-          if (APN->getIncomingBlock(i) == Pred) {
-            HasPredEntries = true;
-            break;
-          }
-        }
-      } while (!HasPredEntries);
+        // Verify that it is missing entries.  If not, it is not being inserted
+        // by this mem2reg invocation so we want to ignore it.
+      } while (APN->getNumOperands() == NewPHINumOperands);
     }
   }
   
@@ -952,18 +950,17 @@ NextIteration:
   }
 
   // 'Recurse' to our successors.
-  TerminatorInst *TI = BB->getTerminator();
-  unsigned NumSuccs = TI->getNumSuccessors();
-  if (NumSuccs == 0) return;
-  
-  // Add all-but-one successor to the worklist.
-  for (unsigned i = 0; i != NumSuccs-1; i++)
-    Worklist.push_back(RenamePassData(TI->getSuccessor(i), BB, IncomingVals));
-  
+  succ_iterator I = succ_begin(BB), E = succ_end(BB);
+  if (I == E) return;
+
   // Handle the last successor without using the worklist.  This allows us to
   // handle unconditional branches directly, for example.
+  --E;
+  for (; I != E; ++I)
+    Worklist.push_back(RenamePassData(*I, BB, IncomingVals));
+
   Pred = BB;
-  BB = TI->getSuccessor(NumSuccs-1);
+  BB = *I;
   goto NextIteration;
 }