[llvm] [X86] Select BLSI for i8 operands (#202344) (PR #204746)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 00:45:56 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Harishankar.1 (Harishankar14)

<details>
<summary>Changes</summary>

Adds a tablegen pattern to select BLSI for `and (neg x), x` at i8.

Fixes #<!-- -->202344

---
Full diff: https://github.com/llvm/llvm-project/pull/204746.diff


2 Files Affected:

- (modified) llvm/lib/Target/X86/X86InstrCompiler.td (+8) 
- (modified) llvm/test/CodeGen/X86/bmi.ll (+28-21) 


``````````diff
diff --git a/llvm/lib/Target/X86/X86InstrCompiler.td b/llvm/lib/Target/X86/X86InstrCompiler.td
index c7ea2985d1353..f87326ecdfc4d 100644
--- a/llvm/lib/Target/X86/X86InstrCompiler.td
+++ b/llvm/lib/Target/X86/X86InstrCompiler.td
@@ -2328,3 +2328,11 @@ def : Pat<(cttz_zero_poison (loadi64 addr:$src)), (BSF64rm (i64 (IMPLICIT_DEF)),
 let Predicates = [HasMOVBE] in {
  def : Pat<(bswap GR16:$src), (ROL16ri GR16:$src, (i8 8))>;
 }
+// BLSI {32/64-bit operands}. For an i8/i16 value, the lowest
+// set bit is unchanged by any garbage in the upper bits.
+// So Extending it using IMPLICIT_DEF,INSERT_SUBREG and  extract.
+let Predicates = [HasBMI] in {
+  def : Pat<(and GR8:$src, (ineg_su GR8:$src)),
+            (EXTRACT_SUBREG (BLSI32rr (INSERT_SUBREG (i32 (IMPLICIT_DEF)), GR8:$src, sub_8bit)), sub_8bit)>;
+            
+}
\ No newline at end of file
diff --git a/llvm/test/CodeGen/X86/bmi.ll b/llvm/test/CodeGen/X86/bmi.ll
index f37176d7c0a37..2c1b3dfbbbb4e 100644
--- a/llvm/test/CodeGen/X86/bmi.ll
+++ b/llvm/test/CodeGen/X86/bmi.ll
@@ -632,46 +632,53 @@ entry:
   ret i32 %and
 }
 
-define i32 @blsi32(i32 %x)   {
-; X86-LABEL: blsi32:
+define i8 @blsi8_trunc(i32 %x) {
+; X86-LABEL: blsi8_trunc:
 ; X86:       # %bb.0:
-; X86-NEXT:    blsil {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    movzbl {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    blsil %eax, %eax
+; X86-NEXT:    # kill: def $al killed $al killed $eax
 ; X86-NEXT:    retl
 ;
-; X64-LABEL: blsi32:
+; X64-LABEL: blsi8_trunc:
 ; X64:       # %bb.0:
 ; X64-NEXT:    blsil %edi, %eax
+; X64-NEXT:    # kill: def $al killed $al killed $eax
 ; X64-NEXT:    retq
 ;
-; EGPR-LABEL: blsi32:
+; EGPR-LABEL: blsi8_trunc:
 ; EGPR:       # %bb.0:
-; EGPR-NEXT:    blsil %edi, %eax # EVEX TO VEX Compression encoding: [0xc4,0xe2,0x78,0xf3,0xdf]
+; EGPR-NEXT:    blsil %edi, %eax # encoding: [0xc4,0xe2,0x78,0xf3,0xdf]
+; EGPR-NEXT:    # kill: def $al killed $al killed $eax
 ; EGPR-NEXT:    retq # encoding: [0xc3]
-  %tmp = sub i32 0, %x
-  %tmp2 = and i32 %x, %tmp
-  ret i32 %tmp2
+  %t = trunc i32 %x to i8
+  %neg = sub i8 0, %t
+  %and = and i8 %t, %neg
+  ret i8 %and
 }
 
-define i32 @blsi32_load(ptr %x)   {
-; X86-LABEL: blsi32_load:
+define i16 @blsi16_trunc(i32 %x) {
+; X86-LABEL: blsi16_trunc:
 ; X86:       # %bb.0:
-; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
-; X86-NEXT:    blsil (%eax), %eax
+; X86-NEXT:    blsil {{[0-9]+}}(%esp), %eax
+; X86-NEXT:    # kill: def $ax killed $ax killed $eax
 ; X86-NEXT:    retl
 ;
-; X64-LABEL: blsi32_load:
+; X64-LABEL: blsi16_trunc:
 ; X64:       # %bb.0:
-; X64-NEXT:    blsil (%rdi), %eax
+; X64-NEXT:    blsil %edi, %eax
+; X64-NEXT:    # kill: def $ax killed $ax killed $eax
 ; X64-NEXT:    retq
 ;
-; EGPR-LABEL: blsi32_load:
+; EGPR-LABEL: blsi16_trunc:
 ; EGPR:       # %bb.0:
-; EGPR-NEXT:    blsil (%rdi), %eax # EVEX TO VEX Compression encoding: [0xc4,0xe2,0x78,0xf3,0x1f]
+; EGPR-NEXT:    blsil %edi, %eax # EVEX TO VEX Compression encoding: [0xc4,0xe2,0x78,0xf3,0xdf]
+; EGPR-NEXT:    # kill: def $ax killed $ax killed $eax
 ; EGPR-NEXT:    retq # encoding: [0xc3]
-  %x1 = load i32, ptr %x
-  %tmp = sub i32 0, %x1
-  %tmp2 = and i32 %x1, %tmp
-  ret i32 %tmp2
+  %t = trunc i32 %x to i16
+  %neg = sub i16 0, %t
+  %and = and i16 %t, %neg
+  ret i16 %and
 }
 
 define i32 @blsi32_z(i32 %a, i32 %b) nounwind {

``````````

</details>


https://github.com/llvm/llvm-project/pull/204746


More information about the llvm-commits mailing list