Fixes the issue of removing manually added fake conditional branches
[oota-llvm.git] / lib / Target / AArch64 / AArch64InstrInfo.cpp
index f398117de953b2f2d9a55673cfbaa0ca7c11ecab..c9c982ed7b5949bd8d1d897450d708146b2bbfc5 100644 (file)
@@ -217,6 +217,25 @@ bool AArch64InstrInfo::ReverseBranchCondition(
   return false;
 }
 
+// XXX-update: Returns whether we can remove a conditional branch instruction.
+// If it's one that is mannually added by us, then don't remove it (return
+// false). All their successors are the same.
+static bool shouldRemoveConditionalBranch(MachineInstr* I) {
+  auto* MBB = I->getParent();
+  assert(isCondBranchOpcode(I->getOpcode()));
+  bool SameSuccessor = true;
+  MachineBasicBlock* BB = nullptr;
+  for (auto* Succ : MBB->successors()) {
+    if (!BB) {
+      BB = Succ;
+    }
+    if (BB != Succ) {
+      SameSuccessor = false;
+    }
+  }
+  return !SameSuccessor;
+}
+
 unsigned AArch64InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
   if (I == MBB.end())
@@ -226,6 +245,11 @@ unsigned AArch64InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
       !isCondBranchOpcode(I->getOpcode()))
     return 0;
 
+  // XXX-update: Don't remove fake conditional branches.
+  if (isCondBranchOpcode(I->getOpcode()) && !shouldRemoveConditionalBranch(I)) {
+    return 0;
+  }
+
   // Remove the branch.
   I->eraseFromParent();
 
@@ -237,6 +261,11 @@ unsigned AArch64InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
   if (!isCondBranchOpcode(I->getOpcode()))
     return 1;
 
+  // XXX-update: Don't remove fake conditional branches.
+  if (!shouldRemoveConditionalBranch(I)) {
+    return 1;
+  }
+
   // Remove the branch.
   I->eraseFromParent();
   return 2;