Allow min/max detection to see through casts.
[oota-llvm.git] / lib / CodeGen / CodeGenPrepare.cpp
index 641316105c017ae3439feded3dce0f1d20a3048e..f37a2874b2565ac79ced410323b4d9ca9d0e2582 100644 (file)
@@ -598,7 +598,10 @@ simplifyRelocatesOffABase(IntrinsicInst *RelocatedBase,
       continue;
 
     // Create a Builder and replace the target callsite with a gep
-    IRBuilder<> Builder(ToReplace);
+    assert(RelocatedBase->getNextNode() && "Should always have one since it's not a terminator");
+
+    // Insert after RelocatedBase
+    IRBuilder<> Builder(RelocatedBase->getNextNode());
     Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc());
 
     // If gc_relocate does not match the actual type, cast it to the right type.
@@ -626,14 +629,10 @@ simplifyRelocatesOffABase(IntrinsicInst *RelocatedBase,
     if (RelocatedBase->getType() != Base->getType()) {
       ActualRelocatedBase =
           cast<Instruction>(Builder.CreateBitCast(RelocatedBase, Base->getType()));
-      ActualRelocatedBase->removeFromParent();
-      ActualRelocatedBase->insertAfter(cast<Instruction>(RelocatedBase));
     }
     Value *Replacement = Builder.CreateGEP(
         Derived->getSourceElementType(), ActualRelocatedBase, makeArrayRef(OffsetV));
     Instruction *ReplacementInst = cast<Instruction>(Replacement);
-    ReplacementInst->removeFromParent();
-    ReplacementInst->insertAfter(ActualRelocatedBase);
     Replacement->takeName(ToReplace);
     // If the newly generated derived pointer's type does not match the original derived
     // pointer's type, cast the new derived pointer to match it. Same reasoning as above.
@@ -641,8 +640,6 @@ simplifyRelocatesOffABase(IntrinsicInst *RelocatedBase,
     if (ReplacementInst->getType() != ToReplace->getType()) {
       ActualReplacement =
           cast<Instruction>(Builder.CreateBitCast(ReplacementInst, ToReplace->getType()));
-      ActualReplacement->removeFromParent();
-      ActualReplacement->insertAfter(ReplacementInst);
     }
     ToReplace->replaceAllUsesWith(ActualReplacement);
     ToReplace->eraseFromParent();
@@ -3207,8 +3204,8 @@ bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
 
     // For a PHI node, push all of its incoming values.
     if (PHINode *P = dyn_cast<PHINode>(V)) {
-      for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i)
-        worklist.push_back(P->getIncomingValue(i));
+      for (Value *IncValue : P->incoming_values())
+        worklist.push_back(IncValue);
       continue;
     }