[llvm] cabad35 - [X86] Fix X86CmovConversion sinking volatile loads into conditional branches (#219827)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 31 05:12:15 PDT 2026
Author: AZero13
Date: 2026-08-31T20:12:09+08:00
New Revision: cabad35749bae3f01ca22c34503783040eaf6540
URL: https://github.com/llvm/llvm-project/commit/cabad35749bae3f01ca22c34503783040eaf6540
DIFF: https://github.com/llvm/llvm-project/commit/cabad35749bae3f01ca22c34503783040eaf6540.diff
LOG: [X86] Fix X86CmovConversion sinking volatile loads into conditional branches (#219827)
The `X86CmovConversion` pass unfolds memory operands of `CMOVrm`
instructions and sinks the resulting load into a conditionally executed
basic block. While safe for ordinary loads, doing this for a volatile
load violates the LLVM LangRef since it causes a volatile memory access
to conditionally not execute.
Fix this by not converting with hasOrderedMemoryRef is true.
Fixes #219814
Added:
llvm/test/CodeGen/X86/cmov-conversion-volatile.ll
Modified:
llvm/lib/Target/X86/X86CmovConversion.cpp
llvm/test/CodeGen/X86/pr15981.ll
llvm/test/CodeGen/X86/select-ext.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/X86/X86CmovConversion.cpp b/llvm/lib/Target/X86/X86CmovConversion.cpp
index 999ff334700b7..002b3b2beb551 100644
--- a/llvm/lib/Target/X86/X86CmovConversion.cpp
+++ b/llvm/lib/Target/X86/X86CmovConversion.cpp
@@ -315,7 +315,7 @@ bool X86CmovConversionImpl::collectCmovCandidates(
// unpredictable, skip it and do not convert it to branch.
if (CC != X86::COND_INVALID &&
!I.getFlag(MachineInstr::MIFlag::Unpredictable) &&
- (IncludeLoads || !I.mayLoad())) {
+ (IncludeLoads || !I.mayLoad()) && !I.hasOrderedMemoryRef()) {
if (Group.empty()) {
// We found first CMOV in the range, reset flags.
FirstCC = CC;
diff --git a/llvm/test/CodeGen/X86/cmov-conversion-volatile.ll b/llvm/test/CodeGen/X86/cmov-conversion-volatile.ll
new file mode 100644
index 0000000000000..4cc7237556fcf
--- /dev/null
+++ b/llvm/test/CodeGen/X86/cmov-conversion-volatile.ll
@@ -0,0 +1,15 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu -O1 < %s | FileCheck %s
+
+define i32 @fq(ptr %0, i32 %1) {
+; CHECK-LABEL: fq:
+; CHECK: # %bb.0:
+; CHECK-NEXT: testl %esi, %esi
+; CHECK-NEXT: movl $7, %eax
+; CHECK-NEXT: cmovel (%rdi), %eax
+; CHECK-NEXT: retq
+ %3 = load volatile i32, ptr %0, align 4
+ %4 = icmp eq i32 %1, 0
+ %5 = select i1 %4, i32 %3, i32 7
+ ret i32 %5
+}
diff --git a/llvm/test/CodeGen/X86/pr15981.ll b/llvm/test/CodeGen/X86/pr15981.ll
index 3debce8fd4b1a..cf033f2d8fd2b 100644
--- a/llvm/test/CodeGen/X86/pr15981.ll
+++ b/llvm/test/CodeGen/X86/pr15981.ll
@@ -44,10 +44,7 @@ define void @fn2() {
; X64: # %bb.0:
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: decl a(%rip)
-; X64-NEXT: je .LBB1_2
-; X64-NEXT: # %bb.1:
-; X64-NEXT: movl b(%rip), %eax
-; X64-NEXT: .LBB1_2:
+; X64-NEXT: cmovnel b(%rip), %eax
; X64-NEXT: movl %eax, c(%rip)
; X64-NEXT: retq
%1 = load volatile i32, ptr @b, align 4
diff --git a/llvm/test/CodeGen/X86/select-ext.ll b/llvm/test/CodeGen/X86/select-ext.ll
index a259693fcec29..ff8c38ce083dd 100644
--- a/llvm/test/CodeGen/X86/select-ext.ll
+++ b/llvm/test/CodeGen/X86/select-ext.ll
@@ -40,10 +40,7 @@ define i64 @zext_scalar_neg(ptr %p, ptr %q, i1 zeroext %c) {
; CHECK: # %bb.0:
; CHECK-NEXT: movsbl (%rdi), %eax
; CHECK-NEXT: testl %edx, %edx
-; CHECK-NEXT: je .LBB2_2
-; CHECK-NEXT: # %bb.1:
-; CHECK-NEXT: movzwl (%rsi), %eax
-; CHECK-NEXT: .LBB2_2:
+; CHECK-NEXT: cmovnew (%rsi), %ax
; CHECK-NEXT: movzwl %ax, %eax
; CHECK-NEXT: retq
%ld1 = load volatile i8, ptr %p
More information about the llvm-commits
mailing list