Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TailDuplicator] Determine if computed gotos using blockaddress #132536

Merged
merged 6 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/include/llvm/CodeGen/MachineBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ class MachineBasicBlock
const MachineFunction *getParent() const { return xParent; }
MachineFunction *getParent() { return xParent; }

/// Returns true if the original IR terminator is an `indirectbr`. This
/// typically corresponds to a `goto` in C, rather than jump tables.
bool terminatorIsComputedGoto() const {
return back().isIndirectBranch() &&
llvm::all_of(successors(), [](const MachineBasicBlock *Succ) {
return Succ->isIRBlockAddressTaken();
});
}

using instr_iterator = Instructions::iterator;
using const_instr_iterator = Instructions::const_iterator;
using reverse_instr_iterator = Instructions::reverse_iterator;
Expand Down
16 changes: 5 additions & 11 deletions llvm/include/llvm/CodeGen/MachineInstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -994,17 +994,8 @@ class MachineInstr

/// Return true if this is an indirect branch, such as a
/// branch through a register.
bool isIndirectBranch(QueryType Type = AnyInBundle,
bool IncludeJumpTable = true) const {
return hasProperty(MCID::IndirectBranch, Type) &&
(IncludeJumpTable || !llvm::any_of(operands(), [](const auto &Op) {
return Op.isJTI();
}));
}

bool isComputedGoto(QueryType Type = AnyInBundle) const {
// Jump tables are not considered computed gotos.
return isIndirectBranch(Type, /*IncludeJumpTable=*/false);
bool isIndirectBranch(QueryType Type = AnyInBundle) const {
return hasProperty(MCID::IndirectBranch, Type);
}

/// Return true if this is a branch which may fall
Expand Down Expand Up @@ -2088,6 +2079,9 @@ class MachineInstr
MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol,
MDNode *HeapAllocMarker, MDNode *PCSections,
uint32_t CFIType, MDNode *MMRAs);

/// Returns true if all successors are IRBlockAddressTaken.
bool jumpToIRBlockAddressTaken() const;
};

/// Special DenseMapInfo traits to compare MachineInstr* by *value* of the
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/TailDuplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
bool HasComputedGoto = false;
if (!TailBB.empty()) {
HasIndirectbr = TailBB.back().isIndirectBranch();
HasComputedGoto = TailBB.back().isComputedGoto();
HasComputedGoto = TailBB.terminatorIsComputedGoto();
}

if (HasIndirectbr && PreRegAlloc)
Expand Down
Loading