[llvm] [CodeGen] Reject undef tied use sharing a virtual register with another read (PR #222832)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 18 23:06:35 PDT 2026
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/222832
>From 7fe45af13b4eac7a74be160addc81003f3144c4d Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 9 Sep 2026 23:15:31 -0700
Subject: [PATCH 1/2] [CodeGen] Reject undef tied use sharing a virtual
register with another read
A virtual use operand, tied to a def of a different register, sharing
the register with another read, carries obligation that is hard to
follow by passes. For example, twoaddressinstruction would use different
physical registers, breaking the expectation of the `IsUndef` flag:
```
define void @f() {
%y = freeze i32 undef
%m = mul i32 %y, %y
call void asm sideeffect "# use $0", "{cx}"(i32 %m)
ret void
}
%2 = IMUL32rr undef %1(tied-def 0), undef %1 ; before twoaddressinstruction
%2 = IMUL32rr undef %2(tied-def 0), undef %1 ; after
imull %eax, %ecx ; different regs
```
RegAllocFast had the same issue until #222249. Let's just reject the
shape to retire the obligation. `%0 = XOR undef %0, undef %0` stays
legal, since rewriting that tie changes nothing.
ProcessImplicitDefs and DetectDeadLanes stop producing the rejected
form; X86 uses MOV32r0 as emitLongJmpShadowStackFix already did, and AVR
reads an IMPLICIT_DEF. Physical registers are unaffected, so post-RA
expansions keep the form.
Aided by Opus 5
---
llvm/include/llvm/CodeGen/MachineInstr.h | 5 ++++
llvm/include/llvm/CodeGen/MachineOperand.h | 10 +++++---
llvm/lib/CodeGen/DetectDeadLanes.cpp | 4 ++++
llvm/lib/CodeGen/MachineInstr.cpp | 17 ++++++++++++++
llvm/lib/CodeGen/MachineVerifier.cpp | 15 ++++++++++++
llvm/lib/CodeGen/ProcessImplicitDefs.cpp | 12 ++++++++--
llvm/lib/Target/AVR/AVRISelLowering.cpp | 6 +++--
llvm/lib/Target/X86/X86FrameLowering.cpp | 14 ++++++++---
llvm/lib/Target/X86/X86ISelLowering.cpp | 16 ++++++++-----
llvm/test/CodeGen/AMDGPU/dead-lane.mir | 8 +++----
.../AMDGPU/detect-dead-lanes-tied-undef.mir | 23 +++++++++++++++++++
llvm/test/CodeGen/AVR/shift32.ll | 5 ++++
.../RISCV/rvv/undef-earlyclobber-chain.mir | 4 +++-
llvm/test/CodeGen/X86/freeze.ll | 20 ++++++++++++++++
.../X86/regallocfast-undef-tied-use.mir | 4 ++--
llvm/test/CodeGen/X86/shadow-stack.ll | 2 +-
llvm/test/CodeGen/X86/win_coreclr_chkstk.ll | 1 +
llvm/test/MachineVerifier/tied-undef-use.mir | 22 ++++++++++++++++++
18 files changed, 164 insertions(+), 24 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/detect-dead-lanes-tied-undef.mir
create mode 100644 llvm/test/MachineVerifier/tied-undef-use.mir
diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h
index 87e40501b3f3c..d4ff59aceab50 100644
--- a/llvm/include/llvm/CodeGen/MachineInstr.h
+++ b/llvm/include/llvm/CodeGen/MachineInstr.h
@@ -1522,6 +1522,11 @@ class MachineInstr
return findRegisterUseOperandIdx(Reg, TRI, false) != -1;
}
+ /// Return true if two operands read (Reg, SubReg) and one is tied to a def of
+ /// another register. Such reads may not be marked undef: rewriting the tie
+ /// would separate them.
+ LLVM_ABI bool hasTiedAndOtherReadOf(Register Reg, unsigned SubReg) const;
+
/// Return true if the MachineInstr reads the specified virtual register.
/// Take into account that a partial define is a
/// read-modify-write operation.
diff --git a/llvm/include/llvm/CodeGen/MachineOperand.h b/llvm/include/llvm/CodeGen/MachineOperand.h
index a4734afeddc96..dfc8ecfd7af2c 100644
--- a/llvm/include/llvm/CodeGen/MachineOperand.h
+++ b/llvm/include/llvm/CodeGen/MachineOperand.h
@@ -126,10 +126,14 @@ class MachineOperand {
/// the same register. In that case, the instruction may depend on those
/// operands reading the same dont-care value. For example:
///
- /// %1 = XOR undef %2, undef %2
+ /// $eax = XOR32rr undef $eax, undef $eax
///
- /// Any register can be used for %2, and its value doesn't matter, but
- /// the two operands must be the same register.
+ /// Any register can be used, and its value doesn't matter, but the operands
+ /// must be the same register, so a pass that moves one of them to another
+ /// register has to move the rest with it. Rewriting a tie moves just the
+ /// tied operand: it follows the def while the other reads stay behind. So a
+ /// virtual register operand may not be tied to a def of another register;
+ /// read a register defined by IMPLICIT_DEF instead.
///
unsigned IsUndef : 1;
diff --git a/llvm/lib/CodeGen/DetectDeadLanes.cpp b/llvm/lib/CodeGen/DetectDeadLanes.cpp
index b5088050e1396..98c243945fb90 100644
--- a/llvm/lib/CodeGen/DetectDeadLanes.cpp
+++ b/llvm/lib/CodeGen/DetectDeadLanes.cpp
@@ -515,6 +515,10 @@ DetectDeadLanes::modifySubRegisterOperandStatus(const DeadLaneDetector &DLD,
}
if (MO.readsReg()) {
bool CrossCopy = false;
+ // A tied use is rewritten to the register of its def, which a
+ // sibling read marked undef could not follow.
+ if (OpMI.hasTiedAndOtherReadOf(Reg, MO.getSubReg()))
+ continue;
if (isUndefRegAtInput(MO, RegInfo)) {
LLVM_DEBUG(dbgs() << "Marking operand '" << MO << "' as undef in "
<< OpMI);
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index 1c0f4d694fa3d..ccde535f7a3ce 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -1131,6 +1131,23 @@ int MachineInstr::findRegisterUseOperandIdx(Register Reg,
return -1;
}
+bool MachineInstr::hasTiedAndOtherReadOf(Register Reg, unsigned SubReg) const {
+ bool Tied = false;
+ unsigned Reads = 0;
+ for (const MachineOperand &MO : operands()) {
+ if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg ||
+ MO.getSubReg() != SubReg)
+ continue;
+ // A tie the def already satisfies is rewritten to nothing, so it keeps
+ // the reads together rather than moving one of them.
+ if (MO.isTied() &&
+ getOperand(findTiedOperandIdx(getOperandNo(&MO))).getReg() != Reg)
+ Tied = true;
+ ++Reads;
+ }
+ return Tied && Reads > 1;
+}
+
/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
/// indicating if this instruction reads or writes Reg. This also considers
/// partial defines.
diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 072b5e61d04fd..349c82e1c6f73 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -2717,6 +2717,21 @@ MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
report("Missing tie flags on tied operand", MO, MONum);
if (MI->findTiedOperandIdx(OtherIdx) != MONum)
report("Inconsistent tie links", MO, MONum);
+
+ // Ban `%1 = OP undef %0(tied-def 0), undef %0`: rewriting the tie moves
+ // the tied operand to %1 and leaves the other read at %0, so the two
+ // undef operands stop reading one value.
+ if (MO->isUse() && MO->isUndef() && Reg.isVirtual() &&
+ OtherMO.getReg() != Reg) {
+ for (const MachineOperand &Other : MI->all_uses())
+ if (&Other != MO && Other.isUndef() && Other.getReg() == Reg &&
+ Other.getSubReg() == MO->getSubReg()) {
+ report("Tied undef use shares a virtual register with another read",
+ MO, MONum);
+ break;
+ }
+ }
+
if (MONum < MCID.getNumDefs()) {
if (OtherIdx < MCID.getNumOperands()) {
if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
diff --git a/llvm/lib/CodeGen/ProcessImplicitDefs.cpp b/llvm/lib/CodeGen/ProcessImplicitDefs.cpp
index 7b240f4586b6b..9ad9a07e80f02 100644
--- a/llvm/lib/CodeGen/ProcessImplicitDefs.cpp
+++ b/llvm/lib/CodeGen/ProcessImplicitDefs.cpp
@@ -87,16 +87,24 @@ void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
if (Reg.isVirtual()) {
// For virtual registers, mark all uses as <undef>, and convert users to
// implicit-def when possible.
+ bool AllUsesUndef = true;
for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
- MO.setIsUndef();
MachineInstr *UserMI = MO.getParent();
+ // A tied use is rewritten to the register of its def, which a sibling
+ // read marked undef could not follow. Leave those reads a definition.
+ if (UserMI->hasTiedAndOtherReadOf(Reg, MO.getSubReg())) {
+ AllUsesUndef = false;
+ continue;
+ }
+ MO.setIsUndef();
if (!canTurnIntoImplicitDef(UserMI))
continue;
LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
WorkList.insert(UserMI);
}
- MI->eraseFromParent();
+ if (AllUsesUndef)
+ MI->eraseFromParent();
return;
}
diff --git a/llvm/lib/Target/AVR/AVRISelLowering.cpp b/llvm/lib/Target/AVR/AVRISelLowering.cpp
index 8fd1c34af9d7b..c7b9530b5d35c 100644
--- a/llvm/lib/Target/AVR/AVRISelLowering.cpp
+++ b/llvm/lib/Target/AVR/AVRISelLowering.cpp
@@ -1951,9 +1951,11 @@ static void insertMultibyteShift(MachineInstr &MI, MachineBasicBlock *BB,
Register ExtByte = 0;
if (ArithmeticShift) {
// Sign-extend bit that was shifted out last.
+ Register UndefReg = MRI.createVirtualRegister(&AVR::GPR8RegClass);
+ BuildMI(*BB, MI, dl, TII.get(TargetOpcode::IMPLICIT_DEF), UndefReg);
BuildMI(*BB, MI, dl, TII.get(AVR::SBCRdRr), HighByte)
- .addReg(HighByte, RegState::Undef)
- .addReg(HighByte, RegState::Undef);
+ .addReg(UndefReg)
+ .addReg(UndefReg);
ExtByte = HighByte;
// The highest bit of the original value is the same as the zero-extend
// byte, so HighByte and ExtByte are the same.
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index 7b9f03c87ecdc..9d28389f0f8d8 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -1117,9 +1117,17 @@ void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
// Add code to MBB to check for overflow and set the new target stack pointer
// to zero if so.
- BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg)
- .addReg(ZeroReg, RegState::Undef)
- .addReg(ZeroReg, RegState::Undef);
+ if (InProlog) {
+ BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg)
+ .addReg(ZeroReg, RegState::Undef)
+ .addReg(ZeroReg, RegState::Undef);
+ } else {
+ Register Zero32 = MRI.createVirtualRegister(&X86::GR32RegClass);
+ BuildMI(&MBB, DL, TII.get(X86::MOV32r0), Zero32);
+ BuildMI(&MBB, DL, TII.get(X86::SUBREG_TO_REG), ZeroReg)
+ .addReg(Zero32)
+ .addImm(X86::sub_32bit);
+ }
BuildMI(&MBB, DL, TII.get(X86::MOV64rr), CopyReg).addReg(X86::RSP);
BuildMI(&MBB, DL, TII.get(X86::SUB64rr), TestReg)
.addReg(CopyReg)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a41cb2914660c..45fc986885a5d 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -38032,12 +38032,16 @@ void X86TargetLowering::emitSetJmpShadowStackFix(MachineInstr &MI,
// Initialize a register with zero.
MVT PVT = getPointerTy(MF->getDataLayout());
const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
- Register ZReg = MRI.createVirtualRegister(PtrRC);
- unsigned XorRROpc = (PVT == MVT::i64) ? X86::XOR64rr : X86::XOR32rr;
- BuildMI(*MBB, MI, MIMD, TII->get(XorRROpc))
- .addDef(ZReg)
- .addReg(ZReg, RegState::Undef)
- .addReg(ZReg, RegState::Undef);
+ Register ZReg = MRI.createVirtualRegister(&X86::GR32RegClass);
+ BuildMI(*MBB, MI, MIMD, TII->get(X86::MOV32r0), ZReg);
+
+ if (PVT == MVT::i64) {
+ Register TmpZReg = MRI.createVirtualRegister(PtrRC);
+ BuildMI(*MBB, MI, MIMD, TII->get(X86::SUBREG_TO_REG), TmpZReg)
+ .addReg(ZReg)
+ .addImm(X86::sub_32bit);
+ ZReg = TmpZReg;
+ }
// Read the current SSP Register value to the zeroed register.
Register SSPCopyReg = MRI.createVirtualRegister(PtrRC);
diff --git a/llvm/test/CodeGen/AMDGPU/dead-lane.mir b/llvm/test/CodeGen/AMDGPU/dead-lane.mir
index 85f5d3dd49f0c..287fb6df82744 100644
--- a/llvm/test/CodeGen/AMDGPU/dead-lane.mir
+++ b/llvm/test/CodeGen/AMDGPU/dead-lane.mir
@@ -3,15 +3,15 @@
# GCN-LABEL: name: dead_lane
# GCN: bb.0:
-# GCN-NEXT: %5:vgpr_32 = nofpexcept V_MAC_F32_e32 undef %1:vgpr_32, undef %1:vgpr_32, undef %5, implicit $mode, implicit $exec
-# GCN-NEXT: FLAT_STORE_DWORD undef %4:vreg_64, %5,
+# GCN-NEXT: %7:vgpr_32 = nofpexcept V_MAC_F32_e32 undef %1:vgpr_32, undef %1:vgpr_32, undef %7, implicit $mode, implicit $exec
+# GCN-NEXT: FLAT_STORE_DWORD undef %6:vreg_64, %7,
---
name: dead_lane
tracksRegLiveness: true
body: |
bb.0:
- %1:vgpr_32 = nofpexcept V_MAC_F32_e32 undef %0:vgpr_32, undef %0:vgpr_32, undef %0:vgpr_32, implicit $mode, implicit $exec
- %2:vgpr_32 = nofpexcept V_MAC_F32_e32 undef %0:vgpr_32, undef %0:vgpr_32, undef %0:vgpr_32, implicit $mode, implicit $exec
+ %1:vgpr_32 = nofpexcept V_MAC_F32_e32 undef %0:vgpr_32, undef %0:vgpr_32, undef %5:vgpr_32, implicit $mode, implicit $exec
+ %2:vgpr_32 = nofpexcept V_MAC_F32_e32 undef %0:vgpr_32, undef %0:vgpr_32, undef %6:vgpr_32, implicit $mode, implicit $exec
%3:vreg_64 = REG_SEQUENCE %1:vgpr_32, %subreg.sub0, %2:vgpr_32, %subreg.sub1
FLAT_STORE_DWORD undef %4:vreg_64, %3.sub0, 0, 0, implicit $exec, implicit $flat_scr
S_ENDPGM 0
diff --git a/llvm/test/CodeGen/AMDGPU/detect-dead-lanes-tied-undef.mir b/llvm/test/CodeGen/AMDGPU/detect-dead-lanes-tied-undef.mir
new file mode 100644
index 0000000000000..71e8668a9ac89
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/detect-dead-lanes-tied-undef.mir
@@ -0,0 +1,23 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -run-pass=detect-dead-lanes -o - %s | FileCheck %s
+
+## %2.sub1 is undefined, but one of the three reads is tied, so marking them
+## undef would leave the tie's rewrite with nothing to follow.
+
+---
+name: tied_undef_read
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: tied_undef_read
+ ; CHECK: [[DEF:%[0-9]+]]:vgpr_32 = IMPLICIT_DEF
+ ; CHECK-NEXT: dead [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 7, implicit $exec
+ ; CHECK-NEXT: [[REG_SEQUENCE:%[0-9]+]]:vreg_64 = REG_SEQUENCE undef [[V_MOV_B32_e32_]], %subreg.sub0, undef [[DEF]], %subreg.sub1
+ ; CHECK-NEXT: [[V_FMAC_F32_e32_:%[0-9]+]]:vgpr_32 = nofpexcept V_FMAC_F32_e32 [[REG_SEQUENCE]].sub1, [[REG_SEQUENCE]].sub1, [[REG_SEQUENCE]].sub1, implicit $mode, implicit $exec
+ ; CHECK-NEXT: S_ENDPGM 0, implicit [[V_FMAC_F32_e32_]]
+ %0:vgpr_32 = IMPLICIT_DEF
+ %1:vgpr_32 = V_MOV_B32_e32 7, implicit $exec
+ %2:vreg_64 = REG_SEQUENCE %1, %subreg.sub0, %0, %subreg.sub1
+ %3:vgpr_32 = nofpexcept V_FMAC_F32_e32 %2.sub1, %2.sub1, %2.sub1, implicit $mode, implicit $exec
+ S_ENDPGM 0, implicit %3
+...
diff --git a/llvm/test/CodeGen/AVR/shift32.ll b/llvm/test/CodeGen/AVR/shift32.ll
index 9e97fac2f8e39..e7f1f27c66f14 100644
--- a/llvm/test/CodeGen/AVR/shift32.ll
+++ b/llvm/test/CodeGen/AVR/shift32.ll
@@ -465,6 +465,7 @@ define i32 @ashr_i32_7(i32 %a) {
; CHECK-NEXT: rol r23
; CHECK-NEXT: rol r24
; CHECK-NEXT: rol r25
+; CHECK-NEXT: ; implicit-def: $r19
; CHECK-NEXT: sbc r19, r19
; CHECK-NEXT: mov r22, r23
; CHECK-NEXT: mov r23, r24
@@ -522,6 +523,7 @@ define i32 @ashr_i32_22(i32 %a) {
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r24
; CHECK-NEXT: rol r25
+; CHECK-NEXT: ; implicit-def: $r18
; CHECK-NEXT: sbc r18, r18
; CHECK-NEXT: lsl r24
; CHECK-NEXT: rol r25
@@ -540,6 +542,7 @@ define i32 @ashr_i32_23(i32 %a) {
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r24
; CHECK-NEXT: rol r25
+; CHECK-NEXT: ; implicit-def: $r23
; CHECK-NEXT: sbc r23, r23
; CHECK-NEXT: mov r22, r25
; CHECK-NEXT: mov r24, r23
@@ -553,6 +556,7 @@ define i32 @ashr_i32_30(i32 %a) {
; CHECK-LABEL: ashr_i32_30:
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r25
+; CHECK-NEXT: ; implicit-def: $r23
; CHECK-NEXT: sbc r23, r23
; CHECK-NEXT: lsl r25
; CHECK-NEXT: mov r22, r23
@@ -568,6 +572,7 @@ define i32 @ashr_i32_31(i32 %a) {
; CHECK-LABEL: ashr_i32_31:
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r25
+; CHECK-NEXT: ; implicit-def: $r22
; CHECK-NEXT: sbc r22, r22
; CHECK-NEXT: mov r23, r22
; CHECK-NEXT: movw r24, r22
diff --git a/llvm/test/CodeGen/RISCV/rvv/undef-earlyclobber-chain.mir b/llvm/test/CodeGen/RISCV/rvv/undef-earlyclobber-chain.mir
index 3e7f596b2dac4..2721516e26eae 100644
--- a/llvm/test/CodeGen/RISCV/rvv/undef-earlyclobber-chain.mir
+++ b/llvm/test/CodeGen/RISCV/rvv/undef-earlyclobber-chain.mir
@@ -75,14 +75,16 @@ body: |
bb.0.entry:
; CHECK-LABEL: name: undef_early_clobber_chain
; CHECK: [[DEF:%[0-9]+]]:vr = IMPLICIT_DEF
+ ; CHECK-NEXT: [[DEF1:%[0-9]+]]:vr = IMPLICIT_DEF
; CHECK-NEXT: dead $x0 = PseudoVSETIVLI 0, 208 /* e32, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
; CHECK-NEXT: [[INIT_UNDEF:%[0-9]+]]:vr = INIT_UNDEF
; CHECK-NEXT: early-clobber %1:vr = PseudoVRGATHER_VI_M1 undef [[DEF]], [[INIT_UNDEF]], 0, 0 /* vl */, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
; CHECK-NEXT: $v8 = COPY %1
; CHECK-NEXT: PseudoRET implicit $v8
%2:vr = IMPLICIT_DEF
+ %4:vr = IMPLICIT_DEF
dead $x0 = PseudoVSETIVLI 0, 208 /* e32, m1, ta, ma */, implicit-def $vl, implicit-def $vtype
- early-clobber %1:vr = PseudoVRGATHER_VI_M1 undef %2, undef %2, 0, 0, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
+ early-clobber %1:vr = PseudoVRGATHER_VI_M1 undef %2, undef %4, 0, 0, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
$v8 = COPY %1
PseudoRET implicit $v8
diff --git a/llvm/test/CodeGen/X86/freeze.ll b/llvm/test/CodeGen/X86/freeze.ll
index 38e3e23f7caac..de5ca17bdb262 100644
--- a/llvm/test/CodeGen/X86/freeze.ll
+++ b/llvm/test/CodeGen/X86/freeze.ll
@@ -6,6 +6,7 @@
define i32 @freeze_int() {
; X86ASM-LABEL: freeze_int:
; X86ASM: # %bb.0:
+; X86ASM-NEXT: # implicit-def: $eax
; X86ASM-NEXT: imull %eax, %eax
; X86ASM-NEXT: retq
%y1 = freeze i32 undef
@@ -13,6 +14,21 @@ define i32 @freeze_int() {
ret i32 %t1
}
+define void @freeze_int_asm() {
+; X86ASM-LABEL: freeze_int_asm:
+; X86ASM: # %bb.0:
+; X86ASM-NEXT: # implicit-def: $ecx
+; X86ASM-NEXT: imull %ecx, %ecx
+; X86ASM-NEXT: #APP
+; X86ASM-NEXT: # use %ecx
+; X86ASM-NEXT: #NO_APP
+; X86ASM-NEXT: retq
+ %y1 = freeze i32 undef
+ %t1 = mul i32 %y1, %y1
+ call void asm sideeffect "# use $0", "{cx}"(i32 %t1)
+ ret void
+}
+
define i5 @freeze_int2() {
; X86ASM-LABEL: freeze_int2:
; X86ASM: # %bb.0:
@@ -26,6 +42,7 @@ define i5 @freeze_int2() {
define float @freeze_float() {
; X86ASM-LABEL: freeze_float:
; X86ASM: # %bb.0:
+; X86ASM-NEXT: # implicit-def: $xmm0
; X86ASM-NEXT: addss %xmm0, %xmm0
; X86ASM-NEXT: retq
%y1 = freeze float undef
@@ -72,6 +89,7 @@ define ptr @freeze_ptr() {
define i32 @freeze_struct() {
; X86ASM-LABEL: freeze_struct:
; X86ASM: # %bb.0:
+; X86ASM-NEXT: # implicit-def: $eax
; X86ASM-NEXT: addl %eax, %eax
; X86ASM-NEXT: retq
%y1 = freeze %struct.T undef
@@ -84,6 +102,7 @@ define i32 @freeze_struct() {
define i32 @freeze_anonstruct() {
; X86ASM-LABEL: freeze_anonstruct:
; X86ASM: # %bb.0:
+; X86ASM-NEXT: # implicit-def: $eax
; X86ASM-NEXT: addl %eax, %eax
; X86ASM-NEXT: retq
%y1 = freeze {i32, i32} undef
@@ -110,6 +129,7 @@ define i32 @freeze_anonstruct2() {
define i64 @freeze_array() {
; X86ASM-LABEL: freeze_array:
; X86ASM: # %bb.0:
+; X86ASM-NEXT: # implicit-def: $rax
; X86ASM-NEXT: addq %rax, %rax
; X86ASM-NEXT: retq
%y1 = freeze [2 x i64] undef
diff --git a/llvm/test/CodeGen/X86/regallocfast-undef-tied-use.mir b/llvm/test/CodeGen/X86/regallocfast-undef-tied-use.mir
index d04ca0e88bc0a..0b6f4d8cc61cb 100644
--- a/llvm/test/CodeGen/X86/regallocfast-undef-tied-use.mir
+++ b/llvm/test/CodeGen/X86/regallocfast-undef-tied-use.mir
@@ -24,9 +24,9 @@ tracksRegLiveness: true
body: |
bb.0:
; CHECK-LABEL: name: undef_tied_use_distinct_def
- ; CHECK: renamable $rcx = XOR64rr undef renamable $rcx, undef renamable $rcx, implicit-def dead $eflags
+ ; CHECK: renamable $rcx = XOR64rr undef renamable $rcx, undef renamable $rax, implicit-def dead $eflags
; CHECK-NEXT: RET64 implicit killed $rcx
- %1:gr64 = XOR64rr undef %0:gr64, undef %0:gr64, implicit-def dead $eflags
+ %1:gr64 = XOR64rr undef %0:gr64, undef %2:gr64, implicit-def dead $eflags
$rcx = COPY %1
RET64 implicit $rcx
...
diff --git a/llvm/test/CodeGen/X86/shadow-stack.ll b/llvm/test/CodeGen/X86/shadow-stack.ll
index bf0a00732b88a..9d55db0ed76a5 100644
--- a/llvm/test/CodeGen/X86/shadow-stack.ll
+++ b/llvm/test/CodeGen/X86/shadow-stack.ll
@@ -135,7 +135,7 @@ define i32 @foo(i32 %i) local_unnamed_addr {
; X86_64-NEXT: movq %rsp, 16(%rax)
; X86_64-NEXT: leaq LBB1_4(%rip), %rcx
; X86_64-NEXT: movq %rcx, 8(%rax)
-; X86_64-NEXT: xorq %rcx, %rcx
+; X86_64-NEXT: xorl %ecx, %ecx
; X86_64-NEXT: rdsspq %rcx
; X86_64-NEXT: movq %rcx, 24(%rax)
; X86_64-NEXT: #EH_SjLj_Setup LBB1_4
diff --git a/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll b/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll
index 35fc13be0f86b..702d8117b4f44 100644
--- a/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll
+++ b/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll
@@ -111,6 +111,7 @@ declare i32 @bar(ptr) nounwind
define win64cc i32 @main4k_alloca(i64 %n) nounwind {
entry:
; WIN_X64: callq bar
+; WIN_X64: xorl [[Z:%e.*]], [[Z]]
; WIN_X64: movq %gs:16, [[R:%r.*]]
; WIN_X64: callq bar
; LINUX: callq bar
diff --git a/llvm/test/MachineVerifier/tied-undef-use.mir b/llvm/test/MachineVerifier/tied-undef-use.mir
new file mode 100644
index 0000000000000..e162630c6996e
--- /dev/null
+++ b/llvm/test/MachineVerifier/tied-undef-use.mir
@@ -0,0 +1,22 @@
+# RUN: not --crash llc -mtriple=x86_64 -run-pass=none -filetype=null %s 2>&1 | FileCheck %s
+# REQUIRES: x86-registered-target
+
+# Rewriting a tie moves the tied operand to the register of its def, leaving
+# behind any other read that had to agree with it.
+
+---
+name: tied_undef_use
+body: |
+ bb.0:
+ ; CHECK: Bad machine code: Tied undef use shares a virtual register with another read
+ %0:gr64 = XOR64rr undef %1:gr64, undef %1:gr64, implicit-def dead $eflags
+
+ ; A tie the def already satisfies is rewritten to nothing.
+ ; CHECK-NOT: Bad machine code
+ %2:gr64 = XOR64rr undef %2:gr64, undef %2:gr64, implicit-def dead $eflags
+
+ ; An untied pair is still the way to spell this once registers are fixed.
+ %3:gr64 = LEA64r undef %4:gr64_nosp, 1, undef %4:gr64_nosp, 0, $noreg
+
+ RET64 implicit %0, implicit %2, implicit %3
+...
>From 0787c21f625660cd7f5c4a5de69ba4444be3ec7e Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 18 Sep 2026 23:06:22 -0700
Subject: [PATCH 2/2] revert unneeded changes to X86/AVR; they build %0 = XOR
undef %0, undef %0, which this check does not reject
---
llvm/lib/Target/AVR/AVRISelLowering.cpp | 6 ++----
llvm/lib/Target/X86/X86FrameLowering.cpp | 14 +++-----------
llvm/lib/Target/X86/X86ISelLowering.cpp | 16 ++++++----------
llvm/test/CodeGen/AVR/shift32.ll | 5 -----
llvm/test/CodeGen/X86/shadow-stack.ll | 2 +-
llvm/test/CodeGen/X86/win_coreclr_chkstk.ll | 1 -
6 files changed, 12 insertions(+), 32 deletions(-)
diff --git a/llvm/lib/Target/AVR/AVRISelLowering.cpp b/llvm/lib/Target/AVR/AVRISelLowering.cpp
index 81335cde14b7e..79ed56679120b 100644
--- a/llvm/lib/Target/AVR/AVRISelLowering.cpp
+++ b/llvm/lib/Target/AVR/AVRISelLowering.cpp
@@ -2040,11 +2040,9 @@ static void insertMultibyteShift(MachineInstr &MI, MachineBasicBlock *BB,
Register ExtByte = 0;
if (ArithmeticShift) {
// Sign-extend bit that was shifted out last.
- Register UndefReg = MRI.createVirtualRegister(&AVR::GPR8RegClass);
- BuildMI(*BB, MI, dl, TII.get(TargetOpcode::IMPLICIT_DEF), UndefReg);
BuildMI(*BB, MI, dl, TII.get(AVR::SBCRdRr), HighByte)
- .addReg(UndefReg)
- .addReg(UndefReg);
+ .addReg(HighByte, RegState::Undef)
+ .addReg(HighByte, RegState::Undef);
ExtByte = HighByte;
// The highest bit of the original value is the same as the zero-extend
// byte, so HighByte and ExtByte are the same.
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index 9d28389f0f8d8..7b9f03c87ecdc 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -1117,17 +1117,9 @@ void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
// Add code to MBB to check for overflow and set the new target stack pointer
// to zero if so.
- if (InProlog) {
- BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg)
- .addReg(ZeroReg, RegState::Undef)
- .addReg(ZeroReg, RegState::Undef);
- } else {
- Register Zero32 = MRI.createVirtualRegister(&X86::GR32RegClass);
- BuildMI(&MBB, DL, TII.get(X86::MOV32r0), Zero32);
- BuildMI(&MBB, DL, TII.get(X86::SUBREG_TO_REG), ZeroReg)
- .addReg(Zero32)
- .addImm(X86::sub_32bit);
- }
+ BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg)
+ .addReg(ZeroReg, RegState::Undef)
+ .addReg(ZeroReg, RegState::Undef);
BuildMI(&MBB, DL, TII.get(X86::MOV64rr), CopyReg).addReg(X86::RSP);
BuildMI(&MBB, DL, TII.get(X86::SUB64rr), TestReg)
.addReg(CopyReg)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index ff90df8634c0b..33c645edbafe9 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -38173,16 +38173,12 @@ void X86TargetLowering::emitSetJmpShadowStackFix(MachineInstr &MI,
// Initialize a register with zero.
MVT PVT = getPointerTy(MF->getDataLayout());
const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
- Register ZReg = MRI.createVirtualRegister(&X86::GR32RegClass);
- BuildMI(*MBB, MI, MIMD, TII->get(X86::MOV32r0), ZReg);
-
- if (PVT == MVT::i64) {
- Register TmpZReg = MRI.createVirtualRegister(PtrRC);
- BuildMI(*MBB, MI, MIMD, TII->get(X86::SUBREG_TO_REG), TmpZReg)
- .addReg(ZReg)
- .addImm(X86::sub_32bit);
- ZReg = TmpZReg;
- }
+ Register ZReg = MRI.createVirtualRegister(PtrRC);
+ unsigned XorRROpc = (PVT == MVT::i64) ? X86::XOR64rr : X86::XOR32rr;
+ BuildMI(*MBB, MI, MIMD, TII->get(XorRROpc))
+ .addDef(ZReg)
+ .addReg(ZReg, RegState::Undef)
+ .addReg(ZReg, RegState::Undef);
// Read the current SSP Register value to the zeroed register.
Register SSPCopyReg = MRI.createVirtualRegister(PtrRC);
diff --git a/llvm/test/CodeGen/AVR/shift32.ll b/llvm/test/CodeGen/AVR/shift32.ll
index e7f1f27c66f14..9e97fac2f8e39 100644
--- a/llvm/test/CodeGen/AVR/shift32.ll
+++ b/llvm/test/CodeGen/AVR/shift32.ll
@@ -465,7 +465,6 @@ define i32 @ashr_i32_7(i32 %a) {
; CHECK-NEXT: rol r23
; CHECK-NEXT: rol r24
; CHECK-NEXT: rol r25
-; CHECK-NEXT: ; implicit-def: $r19
; CHECK-NEXT: sbc r19, r19
; CHECK-NEXT: mov r22, r23
; CHECK-NEXT: mov r23, r24
@@ -523,7 +522,6 @@ define i32 @ashr_i32_22(i32 %a) {
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r24
; CHECK-NEXT: rol r25
-; CHECK-NEXT: ; implicit-def: $r18
; CHECK-NEXT: sbc r18, r18
; CHECK-NEXT: lsl r24
; CHECK-NEXT: rol r25
@@ -542,7 +540,6 @@ define i32 @ashr_i32_23(i32 %a) {
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r24
; CHECK-NEXT: rol r25
-; CHECK-NEXT: ; implicit-def: $r23
; CHECK-NEXT: sbc r23, r23
; CHECK-NEXT: mov r22, r25
; CHECK-NEXT: mov r24, r23
@@ -556,7 +553,6 @@ define i32 @ashr_i32_30(i32 %a) {
; CHECK-LABEL: ashr_i32_30:
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r25
-; CHECK-NEXT: ; implicit-def: $r23
; CHECK-NEXT: sbc r23, r23
; CHECK-NEXT: lsl r25
; CHECK-NEXT: mov r22, r23
@@ -572,7 +568,6 @@ define i32 @ashr_i32_31(i32 %a) {
; CHECK-LABEL: ashr_i32_31:
; CHECK: ; %bb.0:
; CHECK-NEXT: lsl r25
-; CHECK-NEXT: ; implicit-def: $r22
; CHECK-NEXT: sbc r22, r22
; CHECK-NEXT: mov r23, r22
; CHECK-NEXT: movw r24, r22
diff --git a/llvm/test/CodeGen/X86/shadow-stack.ll b/llvm/test/CodeGen/X86/shadow-stack.ll
index 9d55db0ed76a5..bf0a00732b88a 100644
--- a/llvm/test/CodeGen/X86/shadow-stack.ll
+++ b/llvm/test/CodeGen/X86/shadow-stack.ll
@@ -135,7 +135,7 @@ define i32 @foo(i32 %i) local_unnamed_addr {
; X86_64-NEXT: movq %rsp, 16(%rax)
; X86_64-NEXT: leaq LBB1_4(%rip), %rcx
; X86_64-NEXT: movq %rcx, 8(%rax)
-; X86_64-NEXT: xorl %ecx, %ecx
+; X86_64-NEXT: xorq %rcx, %rcx
; X86_64-NEXT: rdsspq %rcx
; X86_64-NEXT: movq %rcx, 24(%rax)
; X86_64-NEXT: #EH_SjLj_Setup LBB1_4
diff --git a/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll b/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll
index 702d8117b4f44..35fc13be0f86b 100644
--- a/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll
+++ b/llvm/test/CodeGen/X86/win_coreclr_chkstk.ll
@@ -111,7 +111,6 @@ declare i32 @bar(ptr) nounwind
define win64cc i32 @main4k_alloca(i64 %n) nounwind {
entry:
; WIN_X64: callq bar
-; WIN_X64: xorl [[Z:%e.*]], [[Z]]
; WIN_X64: movq %gs:16, [[R:%r.*]]
; WIN_X64: callq bar
; LINUX: callq bar
More information about the llvm-commits
mailing list