[llvm-branch-commits] [llvm] [MachineCSE] Don't reuse an instruction after an INLINEASM_BR elsewhere (PR #235939)
Bill Wendling via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 1 03:49:44 PST 2026
https://github.com/isanbard updated https://github.com/llvm/llvm-project/pull/235939
>From aaa5ddcec6485b49aa6e7fdcd7671b87f27c59ea Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Mon, 26 Oct 2026 14:22:59 -0700
Subject: [PATCH] [MachineCSE] Don't reuse an instruction after an INLINEASM_BR
elsewhere
An INLINEASM_BR can branch to its indirect targets before the instructions
after it in its block run, but MachineCSE let those instructions replace
ones in any block that their block dominates, including the indirect
targets. E.g., when both paths out of a callbr sign-extend its output, the
indirect target reused the extension on the path to the default target and
read a register that its own path never wrote.
Reuse an instruction after an INLINEASM_BR only in its own block. MachineSink
already refuses to sink into an indirect target for the same reason.
Assisted-by: Claude Opus 5.5
---
llvm/lib/CodeGen/MachineCSE.cpp | 22 +++++
.../CodeGen/X86/machine-cse-inlineasm-br.mir | 83 +++++++++++++++++++
2 files changed, 105 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/machine-cse-inlineasm-br.mir
diff --git a/llvm/lib/CodeGen/MachineCSE.cpp b/llvm/lib/CodeGen/MachineCSE.cpp
index 23ddccbdacd7a1..3754891ce33f5c 100644
--- a/llvm/lib/CodeGen/MachineCSE.cpp
+++ b/llvm/lib/CodeGen/MachineCSE.cpp
@@ -255,6 +255,18 @@ bool MachineCSEImpl::isPhysDefTriviallyDead(
return false;
}
+/// Return true if \p MI is after an INLINEASM_BR in its block, which can branch
+/// to an indirect target before \p MI runs.
+static bool isAfterInlineAsmBr(const MachineInstr &MI) {
+ const MachineBasicBlock &MBB = *MI.getParent();
+ if (!MBB.mayHaveInlineAsmBr())
+ return false;
+ return any_of(make_range(MBB.begin(), MachineBasicBlock::const_iterator(MI)),
+ [](const MachineInstr &I) {
+ return I.getOpcode() == TargetOpcode::INLINEASM_BR;
+ });
+}
+
static bool isCallerPreservedOrConstPhysReg(MCRegister Reg,
const MachineInstr &MI,
unsigned OpIdx,
@@ -617,6 +629,16 @@ bool MachineCSEImpl::ProcessBlockCSE(MachineBasicBlock *MBB) {
continue;
}
+ // A subexpression after an INLINEASM_BR doesn't reach the INLINEASM_BR's
+ // indirect targets, so it can replace MI only in its own block.
+ if (MI.getParent() != CSMI->getParent() && isAfterInlineAsmBr(*CSMI)) {
+ LLVM_DEBUG(dbgs() << "*** Subexpression is after an INLINEASM_BR in "
+ "another BB, avoid CSE!\n");
+ VNT.insert(&MI, CurrVN++);
+ Exps.push_back(&MI);
+ continue;
+ }
+
// Check if it's profitable to perform this CSE.
bool DoCSE = true;
unsigned NumDefs = MI.getNumDefs();
diff --git a/llvm/test/CodeGen/X86/machine-cse-inlineasm-br.mir b/llvm/test/CodeGen/X86/machine-cse-inlineasm-br.mir
new file mode 100644
index 00000000000000..b7adcd2849451b
--- /dev/null
+++ b/llvm/test/CodeGen/X86/machine-cse-inlineasm-br.mir
@@ -0,0 +1,83 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=x86_64-- -run-pass=machine-cse -verify-machineinstrs -o - %s | FileCheck %s
+# RUN: llc -mtriple=x86_64-- -passes=machine-cse -verify-machineinstrs -o - %s | FileCheck %s
+
+# An INLINEASM_BR can branch to its indirect target before the instructions
+# after it run, so the indirect target can't reuse one of them.
+---
+name: after_inlineasm_br
+tracksRegLiveness: true
+body: |
+ ; CHECK-LABEL: name: after_inlineasm_br
+ ; CHECK: bb.0:
+ ; CHECK-NEXT: successors: %bb.1(0x40000000), %bb.2(0x40000000)
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: INLINEASM_BR &"", attdialect, regdef:GR32, def %0, imm, %bb.2
+ ; CHECK-NEXT: [[MOVSX64rr32_:%[0-9]+]]:gr64 = MOVSX64rr32 %0
+ ; CHECK-NEXT: JMP_1 %bb.1
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.1:
+ ; CHECK-NEXT: $rax = COPY [[MOVSX64rr32_]]
+ ; CHECK-NEXT: RET 0, $rax
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.2 (inlineasm-br-indirect-target):
+ ; CHECK-NEXT: [[MOVSX64rr32_1:%[0-9]+]]:gr64 = MOVSX64rr32 %0
+ ; CHECK-NEXT: $rax = COPY [[MOVSX64rr32_1]]
+ ; CHECK-NEXT: RET 0, $rax
+ bb.0:
+ successors: %bb.1, %bb.2
+
+ INLINEASM_BR &"", attdialect, regdef:GR32, def %0:gr32, imm, %bb.2
+ %1:gr64 = MOVSX64rr32 %0
+ JMP_1 %bb.1
+
+ bb.1:
+ $rax = COPY %1
+ RET 0, $rax
+
+ bb.2 (inlineasm-br-indirect-target):
+ %2:gr64 = MOVSX64rr32 %0
+ $rax = COPY %2
+ RET 0, $rax
+...
+
+# An instruction before the INLINEASM_BR runs on every path.
+---
+name: before_inlineasm_br
+tracksRegLiveness: true
+body: |
+ ; CHECK-LABEL: name: before_inlineasm_br
+ ; CHECK: bb.0:
+ ; CHECK-NEXT: successors: %bb.1(0x40000000), %bb.2(0x40000000)
+ ; CHECK-NEXT: liveins: $edi
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:gr32 = COPY $edi
+ ; CHECK-NEXT: [[MOVSX64rr32_:%[0-9]+]]:gr64 = MOVSX64rr32 [[COPY]]
+ ; CHECK-NEXT: INLINEASM_BR &"", attdialect, imm, %bb.2
+ ; CHECK-NEXT: JMP_1 %bb.1
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.1:
+ ; CHECK-NEXT: $rax = COPY [[MOVSX64rr32_]]
+ ; CHECK-NEXT: RET 0, $rax
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.2 (inlineasm-br-indirect-target):
+ ; CHECK-NEXT: $rax = COPY [[MOVSX64rr32_]]
+ ; CHECK-NEXT: RET 0, $rax
+ bb.0:
+ successors: %bb.1, %bb.2
+ liveins: $edi
+
+ %0:gr32 = COPY $edi
+ %1:gr64 = MOVSX64rr32 %0
+ INLINEASM_BR &"", attdialect, imm, %bb.2
+ JMP_1 %bb.1
+
+ bb.1:
+ $rax = COPY %1
+ RET 0, $rax
+
+ bb.2 (inlineasm-br-indirect-target):
+ %2:gr64 = MOVSX64rr32 %0
+ $rax = COPY %2
+ RET 0, $rax
+...
More information about the llvm-branch-commits
mailing list