[llvm] 841e01d - [ISEL] Fix x86-64 instruction selection bug leaking upper 32 bits (#205600)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 9 04:06:04 PDT 2026


Author: Ammar Askar
Date: 2026-07-09T11:05:59Z
New Revision: 841e01daa0126941cd3b1b322b3e3b5605fcd03c

URL: https://github.com/llvm/llvm-project/commit/841e01daa0126941cd3b1b322b3e3b5605fcd03c
DIFF: https://github.com/llvm/llvm-project/commit/841e01daa0126941cd3b1b322b3e3b5605fcd03c.diff

LOG: [ISEL] Fix x86-64 instruction selection bug leaking upper 32 bits (#205600)

The x86 backend had optimization patterns that matched:
  `(or (and GR32:$dst, -256), (i32 (zextloadi8 addr:$src)))`
and lowered it to:
  `(INSERT_SUBREG (i32 (COPY $dst)), (MOV8rm  i8mem:$src), sub_8bit)`

INSERT_SUBREG for sub_8bit emits a movb instruction which preserves the
upper 56 bits. Now, if the GR32 dst came from a node that does not zero
the upper 32 bits (like IMPLICIT_DEF or EXTRACT_SUBREG), those upper 32
bits would be leaked into the resulting register without being zeroed.

This fixes it by ensuring the input operand satisfies def32 which
requires the upper 32 bits of the register to be set.

Added: 
    

Modified: 
    llvm/lib/Target/X86/X86InstrCompiler.td
    llvm/test/CodeGen/X86/insert.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index 35dfdf2d0067c..5a6597affd51f 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -1680,17 +1680,38 @@ def : Pat<(or (and GR64:$dst, -256),
               (i64 (zextloadi8 addr:$src))),
           (INSERT_SUBREG (i64 (COPY $dst)), (MOV8rm  i8mem:$src), sub_8bit)>;
 
-def : Pat<(or (and GR32:$dst, -256),
-              (i32 (zextloadi8 addr:$src))),
-          (INSERT_SUBREG (i32 (COPY $dst)), (MOV8rm  i8mem:$src), sub_8bit)>;
-
 def : Pat<(or (and GR64:$dst, -65536),
               (i64 (zextloadi16 addr:$src))),
           (INSERT_SUBREG (i64 (COPY $dst)), (MOV16rm  i16mem:$src), sub_16bit)>;
 
-def : Pat<(or (and GR32:$dst, -65536),
-              (i32 (zextloadi16 addr:$src))),
-          (INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm  i16mem:$src), sub_16bit)>;
+let Predicates = [Not64BitMode] in {
+  def : Pat<(or (and GR32:$dst, -256),
+                (i32 (zextloadi8 addr:$src))),
+            (INSERT_SUBREG (i32 (COPY $dst)), (MOV8rm  i8mem:$src), sub_8bit)>;
+
+  def : Pat<(or (and GR32:$dst, -65536),
+                (i32 (zextloadi16 addr:$src))),
+            (INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm  i16mem:$src), sub_16bit)>;
+}
+
+let Predicates = [In64BitMode] in {
+  def : Pat<(or (and def32:$dst, -256),
+                (i32 (zextloadi8 addr:$src))),
+            (INSERT_SUBREG (i32 (COPY $dst)), (MOV8rm  i8mem:$src), sub_8bit)>;
+
+  def : Pat<(or (and def32:$dst, -65536),
+                (i32 (zextloadi16 addr:$src))),
+            (INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm  i16mem:$src), sub_16bit)>;
+
+  // These patterns use MOV32rr since GR32 could have junk in the upper 32-bits.
+  def : Pat<(or (and GR32:$dst, -256),
+                (i32 (zextloadi8 addr:$src))),
+            (MOV32rr (INSERT_SUBREG (i32 (COPY $dst)), (MOV8rm  i8mem:$src), sub_8bit))>;
+
+  def : Pat<(or (and GR32:$dst, -65536),
+                (i32 (zextloadi16 addr:$src))),
+            (MOV32rr (INSERT_SUBREG (i32 (COPY $dst)), (MOV16rm  i16mem:$src), sub_16bit))>;
+}
 
 // To avoid needing to materialize an immediate in a register, use a 32-bit and
 // with implicit zero-extension instead of a 64-bit and if the immediate has at

diff  --git a/llvm/test/CodeGen/X86/insert.ll b/llvm/test/CodeGen/X86/insert.ll
index 381de2ecaa164..53d0040fa2464 100644
--- a/llvm/test/CodeGen/X86/insert.ll
+++ b/llvm/test/CodeGen/X86/insert.ll
@@ -58,8 +58,8 @@ define i32 @sub8_32(i32 noundef %res, ptr %byte) {
 ;
 ; X64-LABEL: sub8_32:
 ; X64:       # %bb.0: # %entry
+; X64-NEXT:    movb (%rsi), %dil
 ; X64-NEXT:    movl %edi, %eax
-; X64-NEXT:    movb (%rsi), %al
 ; X64-NEXT:    retq
 entry:
   %and = and i32 %res, -256
@@ -81,8 +81,8 @@ define i32 @sub16_32(i32 noundef %res, ptr %byte) {
 ;
 ; X64-LABEL: sub16_32:
 ; X64:       # %bb.0: # %entry
+; X64-NEXT:    movw (%rsi), %di
 ; X64-NEXT:    movl %edi, %eax
-; X64-NEXT:    movw (%rsi), %ax
 ; X64-NEXT:    retq
 entry:
   %and = and i32 %res, -65536
@@ -91,3 +91,59 @@ entry:
   %or = or i32 %and, %conv2
   ret i32 %or
 }
+
+define void @sub8_32_store_i64ptr(i32 noundef %res, ptr %byte, ptr %out) {
+; X86-LABEL: sub8_32_store_i64ptr:
+; X86:       # %bb.0: # %entry
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
+; X86-NEXT:    movb (%edx), %cl
+; X86-NEXT:    movl %ecx, (%eax)
+; X86-NEXT:    movl $0, 4(%eax)
+; X86-NEXT:    retl
+;
+; X64-LABEL: sub8_32_store_i64ptr:
+; X64:       # %bb.0: # %entry
+; X64-NEXT:    movb (%rsi), %dil
+; X64-NEXT:    movl %edi, %eax
+; X64-NEXT:    movq %rax, (%rdx)
+; X64-NEXT:    retq
+entry:
+  %and = and i32 %res, -256
+  %d = load i8, ptr %byte, align 1
+  %conv2 = zext i8 %d to i32
+  %or = or i32 %and, %conv2
+  %z = zext i32 %or to i64
+  store i64 %z, ptr %out
+  ret void
+}
+
+define void @sub16_32_store_i64ptr(i32 noundef %res, ptr %byte, ptr %out) {
+; X86-LABEL: sub16_32_store_i64ptr:
+; X86:       # %bb.0: # %entry
+; X86-NEXT:    movzwl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    shll $16, %eax
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
+; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
+; X86-NEXT:    movzwl (%edx), %edx
+; X86-NEXT:    orl %eax, %edx
+; X86-NEXT:    movl %edx, (%ecx)
+; X86-NEXT:    movl $0, 4(%ecx)
+; X86-NEXT:    retl
+;
+; X64-LABEL: sub16_32_store_i64ptr:
+; X64:       # %bb.0: # %entry
+; X64-NEXT:    movw (%rsi), %di
+; X64-NEXT:    movl %edi, %eax
+; X64-NEXT:    movq %rax, (%rdx)
+; X64-NEXT:    retq
+entry:
+  %and = and i32 %res, -65536
+  %d = load i16, ptr %byte, align 1
+  %conv2 = zext i16 %d to i32
+  %or = or i32 %and, %conv2
+  %z = zext i32 %or to i64
+  store i64 %z, ptr %out
+  ret void
+}


        


More information about the llvm-commits mailing list