Skip to content

Commit cf7bb13

Browse files
dianqktstellar
authored andcommitted
[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. (cherry picked from commit 66f158d)
1 parent 656289f commit cf7bb13

File tree

3 files changed

+198
-78
lines changed

3 files changed

+198
-78
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

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

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

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)