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.

llvm-svn: 144499
This commit is contained in:
Chandler Carruth 2011-11-13 12:17:28 +00:00
parent f9213fe721
commit 0bb42c0f86
1 changed files with 20 additions and 3 deletions

View File

@ -413,15 +413,32 @@ void MachineBlockPlacement::buildChain(
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;
// Look for the best viable successor if there is one to place immediately
// after this block.
MachineBasicBlock *BestSucc = selectBestSuccessor(BB, Chain, BlockFilter);
// 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);
}
// 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