[llvm] [X86] Avoid shl->mul lowbit-isolate fold when BMI1/BMI2 are available (PR #217776)

PJ Dailey via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 09:02:26 PDT 2026


================
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=x86-64-v2 | FileCheck %s --check-prefix=NOBMI
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=x86-64-v3 | FileCheck %s --check-prefix=BMI
+
+define i32 @mul_unconstrained(i32 %x, i32 %y){
+; NOBMI-LABEL: mul_unconstrained:
+; NOBMI:       # %bb.0:
+; NOBMI-NEXT:    movl %esi, %eax
+; NOBMI-NEXT:    negl %eax
+; NOBMI-NEXT:    andl %esi, %eax
+; NOBMI-NEXT:    imull %edi, %eax
+; NOBMI-NEXT:    retq
+;
+; BMI-LABEL: mul_unconstrained:
+; BMI:       # %bb.0:
+; BMI-NEXT:    blsil %esi, %eax
----------------
PjDailey11 wrote:

I don't mean to narrow the scope of the PR. `shl` to `mul` only reverses
safely when `y` can't be zero, since the shift version breaks at `y == 0`
and `mul` doesn't.

Covering the general case means adding a zero check before the fast
path, which costs something every time even when `y` isn't zero. Nobody
has benchmarked it to verify yet.

Is the partial fix sufficient or would you want to see the zero check as
well?

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


More information about the llvm-commits mailing list