diff options
author | Mikhail Goncharov <goncharov.mikhail@gmail.com> | 2023-02-01 16:09:35 +0100 |
---|---|---|
committer | Mikhail Goncharov <goncharov.mikhail@gmail.com> | 2023-02-01 16:09:35 +0100 |
commit | 3dcbbddf169354078497724eabd9e6a0b82596c2 (patch) | |
tree | 22417e14d3647ba938bd2a2546ae169f0b4aaad5 /llvm/lib/CodeGen/BranchFolding.cpp | |
parent | 157c12310cbc08d7775714ad12b819c8511065a2 (diff) | |
download | llvm-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.cpp | 61 |
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. } } |