[llvm-commits] [llvm] r144499 - /llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp

Chandler Carruth chandlerc at gmail.com
Sun Nov 13 04:17:28 PST 2011


Author: chandlerc
Date: Sun Nov 13 06:17:28 2011
New Revision: 144499

URL: http://llvm.org/viewvc/llvm-project?rev=144499&view=rev
Log:
Teach MBP to force-merge layout successors for blocks with unanalyzable
branches that also may involve fallthrough. In the case of blocks with
no fallthrough, we can still re-order the blocks profitably. For example
instruction decoding will in some cases continue past an indirect jump,
making laying out its most likely successor there profitable.

Note, no test case. I don't know how to write a test case that exercises
this logic, but it matches the described desired semantics in
discussions with Jakob and others. If anyone has a nice example of IR
that will trigger this, that would be lovely.

Also note, there are still assertion failures in real world code with
this. I'm digging into those next, now that I know this isn't the cause.

Modified:
    llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp

Modified: llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp?rev=144499&r1=144498&r2=144499&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp Sun Nov 13 06:17:28 2011
@@ -413,15 +413,32 @@
   assert(*Chain.begin() == BB);
   MachineBasicBlock *LoopHeaderBB = BB;
   markChainSuccessors(Chain, LoopHeaderBB, BlockWorkList, BlockFilter);
+  SmallVector<MachineOperand, 4> Cond; // For AnalyzeBranch.
   BB = *llvm::prior(Chain.end());
   for (;;) {
     assert(BB);
     assert(BlockToChain[BB] == &Chain);
     assert(*llvm::prior(Chain.end()) == BB);
+    MachineBasicBlock *BestSucc = 0;
+
+    // Check for unreasonable branches, and forcibly merge the existing layout
+    // successor for them. We can handle cases that AnalyzeBranch can't: jump
+    // tables etc are fine. The case we want to handle specially is when there
+    // is potential fallthrough, but the branch cannot be analyzed. This
+    // includes blocks without terminators as well as other cases.
+    Cond.clear();
+    MachineBasicBlock *TBB = 0, *FBB = 0; // For AnalyzeBranch.
+    if (TII->AnalyzeBranch(*BB, TBB, FBB, Cond) && BB->canFallThrough()) {
+      MachineFunction::iterator I(BB);
+      assert(llvm::next(I) != BB->getParent()->end() &&
+             "The final block in the function can fallthrough!");
+      BestSucc = llvm::next(I);
+    }
 
-    // Look for the best viable successor if there is one to place immediately
-    // after this block.
-    MachineBasicBlock *BestSucc = selectBestSuccessor(BB, Chain, BlockFilter);
+    // Otherwise, look for the best viable successor if there is one to place
+    // immediately after this block.
+    if (!BestSucc)
+      BestSucc = selectBestSuccessor(BB, Chain, BlockFilter);
 
     // If an immediate successor isn't available, look for the best viable
     // block among those we've identified as not violating the loop's CFG at





More information about the llvm-commits mailing list