aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/MachineBlockPlacement.cpp
diff options
context:
space:
mode:
authorEllis Hoag <ellis.sparky.hoag@gmail.com>2024-11-12 09:50:29 -0800
committerGitHub <noreply@github.com>2024-11-12 09:50:29 -0800
commitb8d6659bff25458693c99a7c53372afcf6d66d7d (patch)
tree2b0e547da482e6b36d6db22f33e80efb3d1aed2a /llvm/lib/CodeGen/MachineBlockPlacement.cpp
parent789de766b5fc9c8ffa6e808a8baf0e585ac2e818 (diff)
downloadllvm-b8d6659bff25458693c99a7c53372afcf6d66d7d.zip
llvm-b8d6659bff25458693c99a7c53372afcf6d66d7d.tar.gz
llvm-b8d6659bff25458693c99a7c53372afcf6d66d7d.tar.bz2
[CodeLayout] Do not flip branch condition when using optsize (#114607)
* Do not use profile data when flipping a branch condition when optimizing for size. This should improving outlining and ICF due to more uniform instruction sequences. * Refactor `optimizeBranches()` to use early `continue`s * Use the correct debug location for `insertBranch()`
Diffstat (limited to 'llvm/lib/CodeGen/MachineBlockPlacement.cpp')
-rw-r--r--llvm/lib/CodeGen/MachineBlockPlacement.cpp44
1 files changed, 25 insertions, 19 deletions
diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
index d1dced9..bdad63f 100644
--- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp
@@ -2906,7 +2906,7 @@ void MachineBlockPlacement::buildCFGChains() {
void MachineBlockPlacement::optimizeBranches() {
BlockChain &FunctionChain = *BlockToChain[&F->front()];
- SmallVector<MachineOperand, 4> Cond; // For analyzeBranch.
+ SmallVector<MachineOperand, 4> Cond;
// Now that all the basic blocks in the chain have the proper layout,
// make a final call to analyzeBranch with AllowModify set.
@@ -2916,24 +2916,30 @@ void MachineBlockPlacement::optimizeBranches() {
// a fallthrough when it occurs after predicated terminators.
for (MachineBasicBlock *ChainBB : FunctionChain) {
Cond.clear();
- MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For analyzeBranch.
- if (!TII->analyzeBranch(*ChainBB, TBB, FBB, Cond, /*AllowModify*/ true)) {
- // If PrevBB has a two-way branch, try to re-order the branches
- // such that we branch to the successor with higher probability first.
- if (TBB && !Cond.empty() && FBB &&
- MBPI->getEdgeProbability(ChainBB, FBB) >
- MBPI->getEdgeProbability(ChainBB, TBB) &&
- !TII->reverseBranchCondition(Cond)) {
- LLVM_DEBUG(dbgs() << "Reverse order of the two branches: "
- << getBlockName(ChainBB) << "\n");
- LLVM_DEBUG(dbgs() << " Edge probability: "
- << MBPI->getEdgeProbability(ChainBB, FBB) << " vs "
- << MBPI->getEdgeProbability(ChainBB, TBB) << "\n");
- DebugLoc dl; // FIXME: this is nowhere
- TII->removeBranch(*ChainBB);
- TII->insertBranch(*ChainBB, FBB, TBB, Cond, dl);
- }
- }
+ MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
+ if (TII->analyzeBranch(*ChainBB, TBB, FBB, Cond, /*AllowModify*/ true))
+ continue;
+ if (!TBB || !FBB || Cond.empty())
+ continue;
+ // If we are optimizing for size we do not consider the runtime performance.
+ // Instead, we retain the original branch condition so we have more uniform
+ // instructions which will benefit ICF.
+ if (llvm::shouldOptimizeForSize(ChainBB, PSI, MBFI.get()))
+ continue;
+ // If ChainBB has a two-way branch, try to re-order the branches
+ // such that we branch to the successor with higher probability first.
+ if (MBPI->getEdgeProbability(ChainBB, TBB) >=
+ MBPI->getEdgeProbability(ChainBB, FBB))
+ continue;
+ if (TII->reverseBranchCondition(Cond))
+ continue;
+ LLVM_DEBUG(dbgs() << "Reverse order of the two branches: "
+ << getBlockName(ChainBB) << "\n");
+ LLVM_DEBUG(dbgs() << " " << getBlockName(TBB) << " < " << getBlockName(FBB)
+ << "\n");
+ auto Dl = ChainBB->findBranchDebugLoc();
+ TII->removeBranch(*ChainBB);
+ TII->insertBranch(*ChainBB, FBB, TBB, Cond, Dl);
}
}