[llvm] 7be1887 - [VPlan] Don't preserve NSW in mul -> shl conversion for bw-1 op. (#207280)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 14:49:50 PDT 2026
Author: Florian Hahn
Date: 2026-07-02T21:49:44Z
New Revision: 7be188763ab75868ad5c9ac0a2c16ce7d373b053
URL: https://github.com/llvm/llvm-project/commit/7be188763ab75868ad5c9ac0a2c16ce7d373b053
DIFF: https://github.com/llvm/llvm-project/commit/7be188763ab75868ad5c9ac0a2c16ce7d373b053.diff
LOG: [VPlan] Don't preserve NSW in mul -> shl conversion for bw-1 op. (#207280)
When the shift amount is bitwidth - 1, shl nsw would be poison, while
mul nsw is defined. Don't preserve the flag.
https://alive2.llvm.org/ce/z/NgHU_m
Fixes https://github.com/llvm/llvm-project/issues/205252.
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
llvm/test/Transforms/LoopVectorize/VPlan/expand-scev.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 6964abedecbd3..90d6ffcd1f5aa 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1578,11 +1578,17 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
const APInt *APC;
if (CanCreateNewRecipe && match(Def, m_c_Mul(m_VPValue(A), m_APInt(APC))) &&
- APC->isPowerOf2())
+ APC->isPowerOf2()) {
+ auto *MulR = cast<VPRecipeWithIRFlags>(Def);
+ unsigned ShiftAmt = APC->exactLogBase2();
+ VPIRFlags::WrapFlagsTy NW(MulR->hasNoUnsignedWrap(),
+ MulR->hasNoSignedWrap() &&
+ ShiftAmt != APC->getBitWidth() - 1);
return Def->replaceAllUsesWith(Builder.createNaryOp(
Instruction::Shl,
- {A, Plan->getConstantInt(APC->getBitWidth(), APC->exactLogBase2())},
- *cast<VPRecipeWithIRFlags>(Def), Def->getDebugLoc()));
+ {A, Plan->getConstantInt(APC->getBitWidth(), ShiftAmt)}, NW,
+ Def->getDebugLoc()));
+ }
if (CanCreateNewRecipe && match(Def, m_UDiv(m_VPValue(A), m_APInt(APC))) &&
APC->isPowerOf2())
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/expand-scev.ll b/llvm/test/Transforms/LoopVectorize/VPlan/expand-scev.ll
index 98c01022e8a70..3c56f32e36721 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/expand-scev.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/expand-scev.ll
@@ -296,7 +296,7 @@ define i32 @mul_by_signed_min_expanded_to_shl(i1 %a) {
; CHECK-NEXT: IR %rem.neg = zext i1 %a to i32
; CHECK-NEXT: IR %sub = sub i32 %rem.neg, 1
; CHECK-NEXT: IR %shl = shl i32 %sub, 31
-; CHECK-NEXT: EMIT vp<[[VP2:%[0-9]+]]> = shl nuw nsw ir<%rem.neg>, ir<31>
+; CHECK-NEXT: EMIT vp<[[VP2:%[0-9]+]]> = shl nuw ir<%rem.neg>, ir<31>
; CHECK-NEXT: EMIT vp<[[VP3:%[0-9]+]]> = add vp<[[VP2]]>, ir<-2147483547>
; CHECK-NEXT: EMIT vp<%min.iters.check> = icmp ult vp<[[VP3]]>, ir<4>
; CHECK-NEXT: EMIT branch-on-cond vp<%min.iters.check>
More information about the llvm-commits
mailing list