Skip to content

Commit a267468

Browse files
committed
Ensure tailcall forming before reversing branch
1 parent 8a4c2bf commit a267468

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,17 +1574,24 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
15741574
(MBB == PredTBB && (PredHasHotSuccessor ? IsEdgeCold : true));
15751575

15761576
// When we have PGO (or equivalent) information, we want to fold the
1577-
// fallthrough if it's cold. Folding a fallthrough puts it behind a
1578-
// conditional branch which isn't desirable if it's hot. When there
1579-
// isn't any PGO information available we want to fold the taken block
1580-
// if it's possible and we never want to fold the fallthrough as we
1581-
// don't know if that is desirable.
1577+
// fallthrough if it's cold.
1578+
// Alternatively if the taken block is cold we want to fold that.
1579+
// Folding a block puts it behind a conditional branch which isn't
1580+
// desirable if it's hot.
1581+
//
1582+
// When there isn't any PGO information available we want to fold the
1583+
// taken block if it's possible and we never want to fold the
1584+
// fallthrough as we don't know if that is desirable.
15821585
if (PredAnalyzable && !PredCond.empty() && PredTBB != PredFBB &&
15831586
(CanFoldTakenBlock || CanFoldFallThrough)) {
1584-
SmallVector<MachineOperand, 4> ReversedCond(PredCond);
15851587
if (CanFoldFallThrough) {
1588+
SmallVector<MachineOperand, 4> ReversedCond(PredCond);
15861589
DebugLoc Dl = MBB->findBranchDebugLoc();
15871590
TII->reverseBranchCondition(ReversedCond);
1591+
1592+
if (!TII->canMakeTailCallConditional(ReversedCond, TailCall))
1593+
continue;
1594+
15881595
TII->removeBranch(*Pred);
15891596
TII->insertBranch(*Pred, MBB, PredTBB, ReversedCond, Dl);
15901597
}

0 commit comments

Comments
 (0)