Fixes the issue of removing manually added fake conditional branches
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index e0b7d73339d700a27ed31d8d1bce75e4242b6f67..bd89ee366962f30dbd72c1c4050bcc9e3eeaa21f 100644 (file)
@@ -493,6 +493,15 @@ static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB,
     MachineBasicBlock *NextBB = &*I;
     if (TBB == NextBB && !Cond.empty() && !FBB) {
       if (!TII->ReverseBranchCondition(Cond)) {
+        // XXX-disabled: Don't fold conditional branches that we added
+        // intentionally.
+        MachineBasicBlock::iterator I = CurMBB->getLastNonDebugInstr();
+        if (I != CurMBB->end()) {
+          if (I->isConditionalBranch()) {
+            return;
+          }
+        }
+
         TII->RemoveBranch(*CurMBB);
         TII->InsertBranch(*CurMBB, SuccBB, nullptr, Cond, dl);
         return;
@@ -1254,7 +1263,6 @@ ReoptimizeBlock:
       goto ReoptimizeBlock;
     }
 
-    /*
     // If the previous block unconditionally falls through to this block and
     // this block has no other predecessors, move the contents of this block
     // into the prior block. This doesn't usually happen when SimplifyCFG
@@ -1290,7 +1298,6 @@ ReoptimizeBlock:
       MadeChange = true;
       return MadeChange;
     }
-    */
 
     // If the previous branch *only* branches to *this* block (conditional or
     // not) remove the branch.
@@ -1313,6 +1320,15 @@ ReoptimizeBlock:
     // If the prior block branches somewhere else on the condition and here if
     // the condition is false, remove the uncond second branch.
     if (PriorFBB == MBB) {
+      // XXX-disabled: Don't fold conditional branches that we added
+      // intentionally.
+      MachineBasicBlock::iterator I = PrevBB.getLastNonDebugInstr();
+      if (I != PrevBB.end()) {
+        if (I->isConditionalBranch()) {
+          return MadeChange ;
+        }
+      }
+
       DebugLoc dl = getBranchDebugLoc(PrevBB);
       TII->RemoveBranch(PrevBB);
       TII->InsertBranch(PrevBB, PriorTBB, nullptr, PriorCond, dl);
@@ -1325,6 +1341,15 @@ ReoptimizeBlock:
     // if the branch condition is reversible, reverse the branch to create a
     // fall-through.
     if (PriorTBB == MBB) {
+      // XXX-disabled: Don't fold conditional branches that we added
+      // intentionally.
+      MachineBasicBlock::iterator I = PrevBB.getLastNonDebugInstr();
+      if (I != PrevBB.end()) {
+        if (I->isConditionalBranch()) {
+          return MadeChange ;
+        }
+      }
+
       SmallVector<MachineOperand, 4> NewPriorCond(PriorCond);
       if (!TII->ReverseBranchCondition(NewPriorCond)) {
         DebugLoc dl = getBranchDebugLoc(PrevBB);