Fix indvars to not assume that a loop with a single unique exit
[oota-llvm.git] / lib / Transforms / Scalar / SimplifyCFGPass.cpp
index b8bce801a1fb2c8aa8888091133e6eb04b90683d..155d5d81e30a4757be20641c244c22e40aa48d8d 100644 (file)
@@ -58,7 +58,7 @@ FunctionPass *llvm::createCFGSimplificationPass() {
 
 /// ChangeToUnreachable - Insert an unreachable instruction before the specified
 /// instruction, making it and the rest of the code in the block dead.
-static void ChangeToUnreachable(Instruction *I, LLVMContextContext) {
+static void ChangeToUnreachable(Instruction *I, LLVMContext *Context) {
   BasicBlock *BB = I->getParent();
   // Loop over all of the successors, removing BB's entry from any PHI
   // nodes.
@@ -97,7 +97,7 @@ static void ChangeToCall(InvokeInst *II) {
 
 static bool MarkAliveBlocks(BasicBlock *BB,
                             SmallPtrSet<BasicBlock*, 128> &Reachable,
-                            LLVMContextContext) {
+                            LLVMContext *Context) {
   
   SmallVector<BasicBlock*, 128> Worklist;
   Worklist.push_back(BB);
@@ -109,6 +109,8 @@ static bool MarkAliveBlocks(BasicBlock *BB,
     if (!Reachable.insert(BB))
       continue;
 
+    bool AsmFound = false;
+    StoreInst *UndefStore = 0;
     // Do a quick scan of the basic block, turning any obviously unreachable
     // instructions into LLVM unreachable insts.  The instruction combining pass
     // canonicalizes unreachable insts into stores to null or undef.
@@ -125,20 +127,27 @@ static bool MarkAliveBlocks(BasicBlock *BB,
           }
           break;
         }
+        if (isa<InlineAsm>(CI->getOperand(0)))
+          AsmFound = true;
       }
       
       if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
         Value *Ptr = SI->getOperand(1);
-        
-        if (isa<UndefValue>(Ptr) ||
-            (isa<ConstantPointerNull>(Ptr) &&
-             cast<PointerType>(Ptr->getType())->getAddressSpace() == 0)) {
-          ChangeToUnreachable(SI, Context);
-          Changed = true;
-          break;
-        }
+
+        if ((isa<UndefValue>(Ptr) ||
+             (isa<ConstantPointerNull>(Ptr) &&
+              cast<PointerType>(Ptr->getType())->getAddressSpace() == 0)) &&
+            !UndefStore)
+          UndefStore = SI;
       }
     }
+    // We can't delete asm's just because their inputs are undefined;
+    // xor R, R is a common idiom for zeroing a register, for example.
+    // Assume user knows what he is doing.
+    if (UndefStore && !AsmFound) {
+      ChangeToUnreachable(UndefStore, Context);
+      Changed = true;
+    }
 
     // Turn invokes that call 'nounwind' functions into ordinary calls.
     if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))