aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/BranchFolding.cpp
diff options
context:
space:
mode:
authorMikhail Goncharov <goncharov.mikhail@gmail.com>2023-02-01 16:09:35 +0100
committerMikhail Goncharov <goncharov.mikhail@gmail.com>2023-02-01 16:09:35 +0100
commit3dcbbddf169354078497724eabd9e6a0b82596c2 (patch)
tree22417e14d3647ba938bd2a2546ae169f0b4aaad5 /llvm/lib/CodeGen/BranchFolding.cpp
parent157c12310cbc08d7775714ad12b819c8511065a2 (diff)
downloadllvm-3dcbbddf169354078497724eabd9e6a0b82596c2.zip
llvm-3dcbbddf169354078497724eabd9e6a0b82596c2.tar.gz
llvm-3dcbbddf169354078497724eabd9e6a0b82596c2.tar.bz2
Revert "Improve and enable folding of conditional branches with tail calls."
This reverts commit c05ddc9cbc12b1f2038380f57a16c4ca98c614b7. Fails under asan: https://lab.llvm.org/buildbot/#/builders/168/builds/11637 Failed Tests (3): LLVM :: CodeGen/X86/jump_sign.ll LLVM :: CodeGen/X86/or-branch.ll LLVM :: CodeGen/X86/tailcall-extract.ll
Diffstat (limited to 'llvm/lib/CodeGen/BranchFolding.cpp')
-rw-r--r--llvm/lib/CodeGen/BranchFolding.cpp61
1 files changed, 32 insertions, 29 deletions
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index b87338d..d491691 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -1507,39 +1507,42 @@ ReoptimizeBlock:
}
}
- if (!IsEmptyBlock(MBB)) {
+ bool OptForSize =
+ MF.getFunction().hasOptSize() ||
+ llvm::shouldOptimizeForSize(MBB, PSI, &MBBFreqInfo);
+ if (!IsEmptyBlock(MBB) && MBB->pred_size() == 1 && OptForSize) {
+ // Changing "Jcc foo; foo: jmp bar;" into "Jcc bar;" might change the branch
+ // direction, thereby defeating careful block placement and regressing
+ // performance. Therefore, only consider this for optsize functions.
MachineInstr &TailCall = *MBB->getFirstNonDebugInstr();
if (TII->isUnconditionalTailCall(TailCall)) {
- for (auto &Pred : MBB->predecessors()) {
- MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
- SmallVector<MachineOperand, 4> PredCond;
- bool PredAnalyzable =
- !TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true);
-
- // Only eliminate if MBB == TBB (Taken Basic Block)
- if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB &&
- PredTBB != PredFBB) {
- // The predecessor has a conditional branch to this block which
- // consists of only a tail call. Try to fold the tail call into the
- // conditional branch.
- if (TII->canMakeTailCallConditional(PredCond, TailCall)) {
- // TODO: It would be nice if analyzeBranch() could provide a pointer
- // to the branch instruction so replaceBranchWithTailCall() doesn't
- // have to search for it.
- TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall);
- ++NumTailCalls;
- MadeChange = true;
- Pred->removeSuccessor(MBB);
- }
+ MachineBasicBlock *Pred = *MBB->pred_begin();
+ MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
+ SmallVector<MachineOperand, 4> PredCond;
+ bool PredAnalyzable =
+ !TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true);
+
+ if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB &&
+ PredTBB != PredFBB) {
+ // The predecessor has a conditional branch to this block which consists
+ // of only a tail call. Try to fold the tail call into the conditional
+ // branch.
+ if (TII->canMakeTailCallConditional(PredCond, TailCall)) {
+ // TODO: It would be nice if analyzeBranch() could provide a pointer
+ // to the branch instruction so replaceBranchWithTailCall() doesn't
+ // have to search for it.
+ TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall);
+ ++NumTailCalls;
+ Pred->removeSuccessor(MBB);
+ MadeChange = true;
+ return MadeChange;
}
- // If the predecessor is falling through to this block, we could reverse
- // the branch condition and fold the tail call into that. However, after
- // that we might have to re-arrange the CFG to fall through to the other
- // block and there is a high risk of regressing code size rather than
- // improving it.
}
- if (MadeChange)
- return MadeChange;
+ // If the predecessor is falling through to this block, we could reverse
+ // the branch condition and fold the tail call into that. However, after
+ // that we might have to re-arrange the CFG to fall through to the other
+ // block and there is a high risk of regressing code size rather than
+ // improving it.
}
}