Fixes a bug in finding the upcoming store/conditional branch instruction
[oota-llvm.git] / lib / CodeGen / CodeGenPrepare.cpp
index 5c9858dcec2f3cc2cb0975322800992b670f96a1..d7037a9320013c7b5d427ff189558832a2ac0136 100644 (file)
@@ -667,32 +667,23 @@ Instruction* findFirstStoreCondBranchInst(LoadInst* LI) {
   auto BE = BB->end();
   auto BBI = BasicBlock::iterator(LI);
   BBI++;
   auto BE = BB->end();
   auto BBI = BasicBlock::iterator(LI);
   BBI++;
-  while (true) {
-    for (; BBI != BE; BBI++) {
-      auto* Inst = dyn_cast<Instruction>(&*BBI);
-      if (Inst == nullptr) {
-        continue;
-      }
-      if (Inst->getOpcode() == Instruction::Store) {
+  for (; BBI != BE; BBI++) {
+    auto* Inst = dyn_cast<Instruction>(&*BBI);
+    if (Inst == nullptr) {
+      continue;
+    }
+    if (Inst->getOpcode() == Instruction::Store) {
+      return Inst;
+    } else if (Inst->getOpcode() == Instruction::Br) {
+      auto* BrInst = dyn_cast<BranchInst>(Inst);
+      if (BrInst->isConditional()) {
         return Inst;
         return Inst;
-      } else if (Inst->getOpcode() == Instruction::Br) {
-        auto* BrInst = dyn_cast<BranchInst>(Inst);
-        if (BrInst->isConditional()) {
-          return Inst;
-        } else {
-          // Reinitialize iterators with the destination of the unconditional
-          // branch.
-          BB = BrInst->getSuccessor(0);
-          BBI = BB->begin();
-          BE = BB->end();
-          break;
-        }
+      } else {
+        return nullptr;
       }
     }
       }
     }
-    if (BBI == BE) {
-      return nullptr;
-    }
   }
   }
+  return nullptr;
 }
 
 // XXX-comment: Returns whether the code has been changed.
 }
 
 // XXX-comment: Returns whether the code has been changed.
@@ -780,7 +771,8 @@ bool AddFakeConditionalBranchAfterMonotonicLoads(
         if (StoreAddressDependOnValue(dyn_cast<StoreInst>(FirstInst), LI)) {
           continue;
         }
         if (StoreAddressDependOnValue(dyn_cast<StoreInst>(FirstInst), LI)) {
           continue;
         }
-      } else if (FirstInst->getOpcode() == Instruction::Br) {
+      } else if (FirstInst->getOpcode() == Instruction::Br &&
+                 isa<BranchInst>(FirstInst)) {
         if (ConditionalBranchDependsOnValue(dyn_cast<BranchInst>(FirstInst),
                                             LI)) {
           continue;
         if (ConditionalBranchDependsOnValue(dyn_cast<BranchInst>(FirstInst),
                                             LI)) {
           continue;