Skip to content

release/20.x: [X86] When expanding LCMPXCHG16B_SAVE_RBX, substitute RBX in base (#134109) #134331

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

Merged
merged 1 commit into from
Apr 11, 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
14 changes: 12 additions & 2 deletions llvm/lib/Target/X86/X86ExpandPseudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,18 @@ bool X86ExpandPseudo::expandMI(MachineBasicBlock &MBB,
TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, InArg.getReg(), false);
// Create the actual instruction.
MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(X86::LCMPXCHG16B));
// Copy the operands related to the address.
for (unsigned Idx = 1; Idx < 6; ++Idx)
// Copy the operands related to the address. If we access a frame variable,
// we need to replace the RBX base with SaveRbx, as RBX has another value.
const MachineOperand &Base = MBBI->getOperand(1);
if (Base.getReg() == X86::RBX || Base.getReg() == X86::EBX)
NewInstr->addOperand(MachineOperand::CreateReg(
Base.getReg() == X86::RBX
? SaveRbx
: Register(TRI->getSubReg(SaveRbx, X86::sub_32bit)),
/*IsDef=*/false));
else
NewInstr->addOperand(Base);
for (unsigned Idx = 1 + 1; Idx < 1 + X86::AddrNumOperands; ++Idx)
NewInstr->addOperand(MBBI->getOperand(Idx));
// Finally, restore the value of RBX.
TII->copyPhysReg(MBB, MBBI, DL, X86::RBX, SaveRbx,
Expand Down
34 changes: 34 additions & 0 deletions llvm/test/CodeGen/X86/base-pointer-and-cmpxchg.ll
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,39 @@ tail call void asm sideeffect "nop", "~{rax},~{rcx},~{rdx},~{rsi},~{rdi},~{rbp},
store i32 %n, ptr %idx
ret i1 %res
}

; If we compare-and-exchange a frame variable, we additionally need to rewrite
; the memory operand to use the SAVE_rbx instead of rbx, which already contains
; the input operand.
;
; CHECK-LABEL: cmp_and_swap16_frame:
; Check that we actually use rbx.
; gnux32 use the 32bit variant of the registers.
; USE_BASE_64: movq %rsp, %rbx
; USE_BASE_32: movl %esp, %ebx
; Here we drop the inline assembly because the frame pointer is used anyway. So
; rbx is not spilled to the stack but goes into a (hopefully numbered) register.
; USE_BASE: movq %rbx, [[SAVE_rbx:%r[0-9]+]]
;
; USE_BASE: movq {{[^ ]+}}, %rbx
; The use of the frame variable expands to N(%rbx) or N(%ebx). But we've just
; overwritten that with the input operand. We need to use SAVE_rbx instead.
; USE_BASE_64-NEXT: cmpxchg16b {{[0-9]*}}([[SAVE_rbx]])
; USE_BASE_32-NEXT: cmpxchg16b {{[0-9]*}}([[SAVE_rbx]]d)
; USE_BASE-NEXT: movq [[SAVE_rbx]], %rbx
;
; DONT_USE_BASE-NOT: movq %rsp, %rbx
; DONT_USE_BASE-NOT: movl %esp, %ebx
; DONT_USE_BASE: cmpxchg
define i1 @cmp_and_swap16_frame(i128 %a, i128 %b, i32 %n) {
%local = alloca i128, align 16
%dummy = alloca i32, i32 %n
%cmp = cmpxchg ptr %local, i128 %a, i128 %b seq_cst seq_cst
%res = extractvalue { i128, i1 } %cmp, 1
%idx = getelementptr i32, ptr %dummy, i32 5
store i32 %n, ptr %idx
ret i1 %res
}

!llvm.module.flags = !{!0}
!0 = !{i32 2, !"override-stack-alignment", i32 32}
Loading