Reverts wrong modification to MachineBlockPlacement & BranchFolding; uses a new strat...
[oota-llvm.git] / lib / CodeGen / CodeGenPrepare.cpp
index 9ada111eaa92e683b1b40db7883fc116115880b2..1a07bfc0c3577f71585153b63635d87173c47479 100644 (file)
@@ -729,10 +729,16 @@ void AddFakeConditionalBranch(Instruction* SplitInst, Value* Condition) {
   TerminatorInst* ThenTerm = nullptr;
   TerminatorInst* ElseTerm = nullptr;
   SplitBlockAndInsertIfThenElse(Condition, SplitInst, &ThenTerm, &ElseTerm);
   TerminatorInst* ThenTerm = nullptr;
   TerminatorInst* ElseTerm = nullptr;
   SplitBlockAndInsertIfThenElse(Condition, SplitInst, &ThenTerm, &ElseTerm);
+  assert(ThenTerm && ElseTerm &&
+         "Then/Else terminators cannot be empty after basic block spliting");
   auto* ThenBB = ThenTerm->getParent();
   auto* ElseBB = ElseTerm->getParent();
   auto* ThenBB = ThenTerm->getParent();
   auto* ElseBB = ElseTerm->getParent();
+  auto* TailBB = ThenBB->getSingleSuccessor();
+  assert(TailBB && "Tail block cannot be empty after basic block spliting");
+
   ThenBB->disableCanEliminateBlock();
   ThenBB->disableCanEliminateBlock();
   ThenBB->disableCanEliminateBlock();
   ThenBB->disableCanEliminateBlock();
+  TailBB->disableCanEliminateBlock();
   ThenBB->setName(BB->getName() + "Then.Fake");
   ElseBB->setName(BB->getName() + "Else.Fake");
   DEBUG(dbgs() << "Add fake conditional branch:\n"
   ThenBB->setName(BB->getName() + "Then.Fake");
   ElseBB->setName(BB->getName() + "Else.Fake");
   DEBUG(dbgs() << "Add fake conditional branch:\n"
@@ -750,7 +756,7 @@ void TaintRelaxedLoads(LoadInst* LI) {
 }
 
 // XXX-comment: Returns whether the code has been changed.
 }
 
 // XXX-comment: Returns whether the code has been changed.
-bool AddsFakeConditionalBranchAfterMonotonicLoads(
+bool AddFakeConditionalBranchAfterMonotonicLoads(
     const SmallVector<LoadInst*, 1>& MonotonicLoadInsts) {
   bool Changed = false;
   for (auto* LI : MonotonicLoadInsts) {
     const SmallVector<LoadInst*, 1>& MonotonicLoadInsts) {
   bool Changed = false;
   for (auto* LI : MonotonicLoadInsts) {
@@ -1032,29 +1038,7 @@ bool StoreDependOnValue(StoreInst* SI, Value* Dep) {
 
 
 bool CodeGenPrepare::runOnFunction(Function &F) {
 
 
 bool CodeGenPrepare::runOnFunction(Function &F) {
-  // XXX-comment: Delay dealing with relaxed loads in this function to avoid
-  // further changes done by other passes (e.g., SimplifyCFG).
-
-  // Collect all the relaxed loads.
-  SmallVector<LoadInst*, 1> MonotonicLoadInsts;
-  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
-    if (I->isAtomic()) {
-      switch (I->getOpcode()) {
-        case Instruction::Load: {
-          auto* LI = dyn_cast<LoadInst>(&*I);
-          if (LI->getOrdering() == Monotonic) {
-            MonotonicLoadInsts.push_back(LI);
-          }
-          break;
-        }
-        default: {
-          break;
-        }
-      }
-    }
-  }
-  bool EverMadeChange =
-      AddsFakeConditionalBranchAfterMonotonicLoads(MonotonicLoadInsts);
+  bool EverMadeChange = false;
 
   if (skipOptnoneFunction(F))
     return false;
 
   if (skipOptnoneFunction(F))
     return false;
@@ -1169,6 +1153,29 @@ bool CodeGenPrepare::runOnFunction(Function &F) {
       EverMadeChange |= simplifyOffsetableRelocate(*I);
   }
 
       EverMadeChange |= simplifyOffsetableRelocate(*I);
   }
 
+  // XXX-comment: Delay dealing with relaxed loads in this function to avoid
+  // further changes done by other passes (e.g., SimplifyCFG).
+  // Collect all the relaxed loads.
+  SmallVector<LoadInst*, 1> MonotonicLoadInsts;
+  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
+    if (I->isAtomic()) {
+      switch (I->getOpcode()) {
+        case Instruction::Load: {
+          auto* LI = dyn_cast<LoadInst>(&*I);
+          if (LI->getOrdering() == Monotonic) {
+            MonotonicLoadInsts.push_back(LI);
+          }
+          break;
+        }
+        default: {
+          break;
+        }
+      }
+    }
+  }
+  EverMadeChange |=
+      AddFakeConditionalBranchAfterMonotonicLoads(MonotonicLoadInsts);
+
   return EverMadeChange;
 }
 
   return EverMadeChange;
 }
 
@@ -1215,11 +1222,6 @@ bool CodeGenPrepare::eliminateMostlyEmptyBlocks(Function &F) {
   // Note that this intentionally skips the entry block.
   for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
     BasicBlock *BB = &*I++;
   // Note that this intentionally skips the entry block.
   for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
     BasicBlock *BB = &*I++;
-    // XXX-disabled: Do not eliminate the added fake basic block.
-    if (!BB->getCanEliminateBlock()) {
-      continue;
-    }
-
     // If this block doesn't end with an uncond branch, ignore it.
     BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
     if (!BI || !BI->isUnconditional())
     // If this block doesn't end with an uncond branch, ignore it.
     BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
     if (!BI || !BI->isUnconditional())