[llvm] [X86] Fix X86CmovConversion sinking volatile loads into conditional branches (PR #219827)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 11:00:19 PDT 2026


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/219827

>From 53d57f5a25d92b4b79392aba962d8f9b91a1dfea Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Sun, 30 Aug 2026 13:11:19 -0400
Subject: [PATCH] [X86] Fix X86CmovConversion sinking volatile loads into
 conditional branches

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
---
 llvm/lib/Target/X86/X86CmovConversion.cpp         |  2 +-
 llvm/test/CodeGen/X86/cmov-conversion-volatile.ll | 15 +++++++++++++++
 llvm/test/CodeGen/X86/pr15981.ll                  |  5 +----
 llvm/test/CodeGen/X86/select-ext.ll               |  5 +----
 4 files changed, 18 insertions(+), 9 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/cmov-conversion-volatile.ll

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