Skip to content

Commit a034b82

Browse files
committed
[BranchFolding] Add an optional target hook to skip branch folding when it's unsafe
1 parent 81e6552 commit a034b82

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,12 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
23352335
llvm_unreachable("unknown number of operands necessary");
23362336
}
23372337

2338+
/// Return false if \p MBB whose any predecessor is not analyzable is not safe
2339+
/// to do \p BranchFolding.
2340+
virtual bool isMBBSafeToBranchFolding(const MachineBasicBlock &MBB) const {
2341+
return true;
2342+
}
2343+
23382344
private:
23392345
mutable std::unique_ptr<MIRFormatter> Formatter;
23402346
unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,19 @@ static void salvageDebugInfoFromEmptyBlock(const TargetInstrInfo *TII,
13461346
copyDebugInfoToPredecessor(TII, MBB, *PredBB);
13471347
}
13481348

1349+
static bool areAllPredsAnalyzable(const llvm::TargetInstrInfo *TII,
1350+
MachineBasicBlock *MBB) {
1351+
for (auto itr = MBB->pred_begin(); itr != MBB->pred_end(); ++itr) {
1352+
MachineBasicBlock *CurTBB = nullptr, *CurFBB = nullptr;
1353+
SmallVector<MachineOperand, 4> CurCond;
1354+
bool CantAnalyzable = TII->analyzeBranch(**itr, CurTBB, CurFBB, CurCond,
1355+
/*AllowModify*/ false);
1356+
if (CantAnalyzable)
1357+
return false;
1358+
}
1359+
return true;
1360+
}
1361+
13491362
bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
13501363
bool MadeChange = false;
13511364
MachineFunction &MF = *MBB->getParent();
@@ -1389,6 +1402,9 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
13891402
// TODO: Is it ever worth rewriting predecessors which don't already
13901403
// jump to a landing pad, and so can safely jump to the fallthrough?
13911404
} else if (MBB->isSuccessor(&*FallThrough)) {
1405+
if (!TII->isMBBSafeToBranchFolding(*MBB) &&
1406+
!areAllPredsAnalyzable(TII, MBB))
1407+
return MadeChange;
13921408
// Rewrite all predecessors of the old block to go to the fallthrough
13931409
// instead.
13941410
while (!MBB->pred_empty()) {

0 commit comments

Comments
 (0)