Skip to content

Commit 66f158d

Browse files
authored
[TailDuplicator] Determine if computed gotos using blockaddress (#132536)
Using `blockaddress` should be more reliable than determining if an operand comes from a jump table index. Alternative: Add the `MachineInstr::MIFlag::ComputedGoto` flag when lowering `indirectbr`. But I don't think this approach is suitable to backport.
1 parent 4775e6d commit 66f158d

File tree

4 files changed

+203
-89
lines changed

4 files changed

+203
-89
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ class MachineBasicBlock
313313
const MachineFunction *getParent() const { return xParent; }
314314
MachineFunction *getParent() { return xParent; }
315315

316+
/// Returns true if the original IR terminator is an `indirectbr`. This
317+
/// typically corresponds to a `goto` in C, rather than jump tables.
318+
bool terminatorIsComputedGoto() const {
319+
return back().isIndirectBranch() &&
320+
llvm::all_of(successors(), [](const MachineBasicBlock *Succ) {
321+
return Succ->isIRBlockAddressTaken();
322+
});
323+
}
324+
316325
using instr_iterator = Instructions::iterator;
317326
using const_instr_iterator = Instructions::const_iterator;
318327
using reverse_instr_iterator = Instructions::reverse_iterator;

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -994,17 +994,8 @@ class MachineInstr
994994

995995
/// Return true if this is an indirect branch, such as a
996996
/// branch through a register.
997-
bool isIndirectBranch(QueryType Type = AnyInBundle,
998-
bool IncludeJumpTable = true) const {
999-
return hasProperty(MCID::IndirectBranch, Type) &&
1000-
(IncludeJumpTable || !llvm::any_of(operands(), [](const auto &Op) {
1001-
return Op.isJTI();
1002-
}));
1003-
}
1004-
1005-
bool isComputedGoto(QueryType Type = AnyInBundle) const {
1006-
// Jump tables are not considered computed gotos.
1007-
return isIndirectBranch(Type, /*IncludeJumpTable=*/false);
997+
bool isIndirectBranch(QueryType Type = AnyInBundle) const {
998+
return hasProperty(MCID::IndirectBranch, Type);
1008999
}
10091000

10101001
/// Return true if this is a branch which may fall
@@ -2088,6 +2079,9 @@ class MachineInstr
20882079
MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol,
20892080
MDNode *HeapAllocMarker, MDNode *PCSections,
20902081
uint32_t CFIType, MDNode *MMRAs);
2082+
2083+
/// Returns true if all successors are IRBlockAddressTaken.
2084+
bool jumpToIRBlockAddressTaken() const;
20912085
};
20922086

20932087
/// Special DenseMapInfo traits to compare MachineInstr* by *value* of the

llvm/lib/CodeGen/TailDuplicator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
604604
bool HasComputedGoto = false;
605605
if (!TailBB.empty()) {
606606
HasIndirectbr = TailBB.back().isIndirectBranch();
607-
HasComputedGoto = TailBB.back().isComputedGoto();
607+
HasComputedGoto = TailBB.terminatorIsComputedGoto();
608608
}
609609

610610
if (HasIndirectbr && PreRegAlloc)

0 commit comments

Comments
 (0)