[llvm] [SLP]Fix wrap flags for lanes emitted in negated form (PR #230453)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 9 04:05:33 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-vectorizers
Author: Alexey Bataev (alexey-bataev)
<details>
<summary>Changes</summary>
sub C, x emitted as add x, -C (or a swapped add/sub lane) negates the
value; nsw/nuw of the original do not cover the negated overflow.
Drop poison-generating flags on the emitted vector add/sub then.
Fixes #<!-- -->230391
---
Full diff: https://github.com/llvm/llvm-project/pull/230453.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+35)
- (modified) llvm/test/Transforms/SLPVectorizer/X86/negated-lane-wrap-flags.ll (+2-2)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 6b0482822c4cc..76b1fc8f66ce9 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -23993,6 +23993,41 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
auto *I = dyn_cast<Instruction>(V);
if (!I)
return V;
+ // A lane may be emitted in negated form (sub C, x as add x, -C, or an
+ // add/sub lane with swapped operands) when all its uses are
+ // sign-insensitive (icmp eq/ne 0 or abs). The value stays correct for
+ // such uses, but nsw/nuw of the original instruction do not cover the
+ // negated overflow conditions.
+ if (E->State == TreeEntry::Vectorize && E->getNumOperands() == 2 &&
+ I->getType()->isIntOrIntVectorTy() &&
+ (Opcode == Instruction::Add || Opcode == Instruction::Sub)) {
+ auto IsNegatedLane = [&](Instruction *LaneI, unsigned Lane) {
+ unsigned LaneOpcode = LaneI->getOpcode();
+ if (LaneOpcode != Instruction::Add && LaneOpcode != Instruction::Sub)
+ return false;
+ Value *L = E->getOperand(0)[Lane];
+ Value *R = E->getOperand(1)[Lane];
+ if (LaneOpcode == Opcode)
+ // Only a sub lane emitted with swapped operands is negated.
+ return Opcode == Instruction::Sub && L == LaneI->getOperand(1) &&
+ R == LaneI->getOperand(0) && L != R;
+ if (Opcode == Instruction::Add)
+ // sub C, x emitted as x + -C.
+ return isa<Constant>(LaneI->getOperand(0)) &&
+ !isa<Constant>(LaneI->getOperand(1));
+ // add x, C emitted as -C - x.
+ return isa<Constant>(L) && !isa<Constant>(R);
+ };
+ if (any_of(enumerate(E->Scalars), [&](const auto &P) {
+ auto *LaneI = dyn_cast<Instruction>(P.value());
+ return LaneI &&
+ (!E->hasCopyableElements() ||
+ !E->isCopyableElement(LaneI)) &&
+ (VL.empty() || is_contained(VL, LaneI)) &&
+ IsNegatedLane(LaneI, P.index());
+ }))
+ I->dropPoisonGeneratingFlags();
+ }
I = ::propagateMetadata(I, UniqueInsts.getArrayRef());
// For copyable elements the lane is synthesized using a binop identity
// value, so the operand at that lane is the copyable scalar's value.
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/negated-lane-wrap-flags.ll b/llvm/test/Transforms/SLPVectorizer/X86/negated-lane-wrap-flags.ll
index 54dd30dff42fe..a1a84e2c96880 100644
--- a/llvm/test/Transforms/SLPVectorizer/X86/negated-lane-wrap-flags.ll
+++ b/llvm/test/Transforms/SLPVectorizer/X86/negated-lane-wrap-flags.ll
@@ -8,7 +8,7 @@ define void @sub_const_lhs_cmp_zero(ptr %p, ptr %in) {
; CHECK-LABEL: define void @sub_const_lhs_cmp_zero(
; CHECK-SAME: ptr [[P:%.*]], ptr [[IN:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr [[IN]], align 4
-; CHECK-NEXT: [[TMP2:%.*]] = add nsw <4 x i32> [[TMP1]], <i32 1, i32 3, i32 5, i32 7>
+; CHECK-NEXT: [[TMP2:%.*]] = add <4 x i32> [[TMP1]], <i32 1, i32 3, i32 5, i32 7>
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <4 x i32> [[TMP2]], zeroinitializer
; CHECK-NEXT: [[TMP4:%.*]] = zext <4 x i1> [[TMP3]] to <4 x i32>
; CHECK-NEXT: store <4 x i32> [[TMP4]], ptr [[P]], align 4
@@ -56,7 +56,7 @@ define i1 @add_lane_swapped_abs(ptr %p, ptr %in, ptr %q) {
; CHECK-NEXT: [[TMP3:%.*]] = insertelement <4 x i32> <i32 poison, i32 poison, i32 5, i32 poison>, i32 [[Q3]], i64 3
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <2 x i32> [[TMP2]], <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>
; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <4 x i32> [[TMP3]], <4 x i32> [[TMP4]], <4 x i32> <i32 4, i32 5, i32 2, i32 3>
-; CHECK-NEXT: [[TMP6:%.*]] = sub nsw <4 x i32> [[TMP5]], [[TMP1]]
+; CHECK-NEXT: [[TMP6:%.*]] = sub <4 x i32> [[TMP5]], [[TMP1]]
; CHECK-NEXT: [[TMP7:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[TMP6]], i1 true)
; CHECK-NEXT: store <4 x i32> [[TMP7]], ptr [[P]], align 4
; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <4 x i32> [[TMP6]], <4 x i32> poison, <2 x i32> <i32 1, i32 3>
``````````
</details>
https://github.com/llvm/llvm-project/pull/230453
More information about the llvm-commits
mailing list