[llvm] d3233e8 - [SLP]Do not allow undefs being combined with divs
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 10 16:59:30 PDT 2025
Author: Alexey Bataev
Date: 2025-10-10T16:59:05-07:00
New Revision: d3233e806e1a80020b09c161def31ce2c75804d5
URL: https://github.com/llvm/llvm-project/commit/d3233e806e1a80020b09c161def31ce2c75804d5
DIFF: https://github.com/llvm/llvm-project/commit/d3233e806e1a80020b09c161def31ce2c75804d5.diff
LOG: [SLP]Do not allow undefs being combined with divs
Undefs/poisons with divs in vector operations lead to undefined
behavior, disabling this combination
Fixes #162663
Added:
llvm/test/Transforms/SLPVectorizer/X86/poison-within-divisions.ll
Modified:
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 21a42c20b04f2..2388375b8c2bb 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -10664,7 +10664,9 @@ class InstructionsCompatibilityAnalysis {
void findAndSetMainInstruction(ArrayRef<Value *> VL, const BoUpSLP &R) {
BasicBlock *Parent = nullptr;
// Checks if the instruction has supported opcode.
- auto IsSupportedInstruction = [&](Instruction *I) {
+ auto IsSupportedInstruction = [&](Instruction *I, bool AnyUndef) {
+ if (AnyUndef && (I->isIntDivRem() || I->isFPDivRem() || isa<CallInst>(I)))
+ return false;
return I && isSupportedOpcode(I->getOpcode()) &&
(!doesNotNeedToBeScheduled(I) || !R.isVectorized(I));
};
@@ -10672,10 +10674,13 @@ class InstructionsCompatibilityAnalysis {
// will be unable to schedule anyway.
SmallDenseSet<Value *, 8> Operands;
SmallMapVector<unsigned, SmallVector<Instruction *>, 4> Candidates;
+ bool AnyUndef = false;
for (Value *V : VL) {
auto *I = dyn_cast<Instruction>(V);
- if (!I)
+ if (!I) {
+ AnyUndef |= isa<UndefValue>(V);
continue;
+ }
if (!DT.isReachableFromEntry(I->getParent()))
continue;
if (Candidates.empty()) {
@@ -10710,7 +10715,7 @@ class InstructionsCompatibilityAnalysis {
if (P.second.size() < BestOpcodeNum)
continue;
for (Instruction *I : P.second) {
- if (IsSupportedInstruction(I) && !Operands.contains(I)) {
+ if (IsSupportedInstruction(I, AnyUndef) && !Operands.contains(I)) {
MainOp = I;
BestOpcodeNum = P.second.size();
break;
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/poison-within-divisions.ll b/llvm/test/Transforms/SLPVectorizer/X86/poison-within-divisions.ll
new file mode 100644
index 0000000000000..76ef396e3bc62
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/poison-within-divisions.ll
@@ -0,0 +1,98 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt --passes=slp-vectorizer -S -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+define i32 @test(i1 %tobool2.not, i64 %conv21) {
+; CHECK-LABEL: define i32 @test(
+; CHECK-SAME: i1 [[TOBOOL2_NOT:%.*]], i64 [[CONV21:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[WHILE_BODY:.*]]
+; CHECK: [[WHILE_BODY]]:
+; CHECK-NEXT: [[Q24_659:%.*]] = phi i32 [ [[Q24_655:%.*]], %[[IF_END35:.*]] ], [ 0, %[[ENTRY]] ]
+; CHECK-NEXT: [[L15_1:%.*]] = phi i32 [ [[L15_4:%.*]], %[[IF_END35]] ], [ 0, %[[ENTRY]] ]
+; CHECK-NEXT: br i1 [[TOBOOL2_NOT]], label %[[IF_END4:.*]], label %[[Q:.*]]
+; CHECK: [[IF_END4]]:
+; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[Q24_659]], 0
+; CHECK-NEXT: br label %[[AB:.*]]
+; CHECK: [[AB]]:
+; CHECK-NEXT: [[Q24_658:%.*]] = phi i32 [ [[Q24_660:%.*]], %[[IF_END35]] ], [ 0, %[[IF_END4]] ]
+; CHECK-NEXT: [[M_1:%.*]] = phi i1 [ false, %[[IF_END35]] ], [ [[TMP0]], %[[IF_END4]] ]
+; CHECK-NEXT: [[O_2:%.*]] = phi i32 [ [[O_7:%.*]], %[[IF_END35]] ], [ 0, %[[IF_END4]] ]
+; CHECK-NEXT: [[Q24_2:%.*]] = phi i32 [ [[Q24_7:%.*]], %[[IF_END35]] ], [ 0, %[[IF_END4]] ]
+; CHECK-NEXT: br i1 [[M_1]], label %[[AE:.*]], label %[[AC:.*]]
+; CHECK: [[Q]]:
+; CHECK-NEXT: [[TOBOOL16_NOT:%.*]] = icmp ne i32 [[L15_1]], 0
+; CHECK-NEXT: [[SPEC_SELECT2:%.*]] = zext i1 [[TOBOOL16_NOT]] to i32
+; CHECK-NEXT: br label %[[AE]]
+; CHECK: [[AE]]:
+; CHECK-NEXT: [[Q24_655]] = phi i32 [ [[Q24_658]], %[[AB]] ], [ 0, %[[Q]] ]
+; CHECK-NEXT: [[M_3:%.*]] = phi i64 [ 0, %[[AB]] ], [ 1, %[[Q]] ]
+; CHECK-NEXT: [[L15_4]] = phi i32 [ poison, %[[AB]] ], [ [[SPEC_SELECT2]], %[[Q]] ]
+; CHECK-NEXT: [[O_4:%.*]] = phi i32 [ [[O_2]], %[[AB]] ], [ 0, %[[Q]] ]
+; CHECK-NEXT: [[Q24_4:%.*]] = phi i32 [ [[Q24_2]], %[[AB]] ], [ 0, %[[Q]] ]
+; CHECK-NEXT: br i1 [[TOBOOL2_NOT]], label %[[IF_END35]], label %[[IF_THEN20:.*]]
+; CHECK: [[IF_THEN20]]:
+; CHECK-NEXT: [[DIV22:%.*]] = udiv i64 [[M_3]], [[CONV21]]
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[DIV22]] to i32
+; CHECK-NEXT: [[CONV23:%.*]] = sub i32 0, [[TMP1]]
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[M_3]] to i32
+; CHECK-NEXT: [[CONV25:%.*]] = xor i32 [[TMP2]], 1
+; CHECK-NEXT: br label %[[IF_END35]]
+; CHECK: [[AC]]:
+; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL2_NOT]], i32 [[Q24_2]], i32 [[O_2]]
+; CHECK-NEXT: ret i32 [[SPEC_SELECT]]
+; CHECK: [[IF_END35]]:
+; CHECK-NEXT: [[Q24_660]] = phi i32 [ 0, %[[AE]] ], [ [[CONV25]], %[[IF_THEN20]] ]
+; CHECK-NEXT: [[O_7]] = phi i32 [ [[O_4]], %[[AE]] ], [ [[CONV23]], %[[IF_THEN20]] ]
+; CHECK-NEXT: [[Q24_7]] = phi i32 [ [[Q24_4]], %[[AE]] ], [ [[CONV25]], %[[IF_THEN20]] ]
+; CHECK-NEXT: br i1 [[TOBOOL2_NOT]], label %[[WHILE_BODY]], label %[[AB]]
+;
+entry:
+ br label %while.body
+
+while.body:
+ %q24.659 = phi i32 [ %q24.655, %if.end35 ], [ 0, %entry ]
+ %l15.1 = phi i32 [ %l15.4, %if.end35 ], [ 0, %entry ]
+ br i1 %tobool2.not, label %if.end4, label %q
+
+if.end4:
+ %0 = icmp eq i32 %q24.659, 0
+ br label %ab
+
+ab:
+ %q24.658 = phi i32 [ %q24.660, %if.end35 ], [ 0, %if.end4 ]
+ %m.1 = phi i1 [ false, %if.end35 ], [ %0, %if.end4 ]
+ %o.2 = phi i32 [ %o.7, %if.end35 ], [ 0, %if.end4 ]
+ %q24.2 = phi i32 [ %q24.7, %if.end35 ], [ 0, %if.end4 ]
+ br i1 %m.1, label %ae, label %ac
+
+q:
+ %tobool16.not = icmp ne i32 %l15.1, 0
+ %spec.select2 = zext i1 %tobool16.not to i32
+ br label %ae
+
+ae:
+ %q24.655 = phi i32 [ %q24.658, %ab ], [ 0, %q ]
+ %m.3 = phi i64 [ 0, %ab ], [ 1, %q ]
+ %l15.4 = phi i32 [ poison, %ab ], [ %spec.select2, %q ]
+ %o.4 = phi i32 [ %o.2, %ab ], [ 0, %q ]
+ %q24.4 = phi i32 [ %q24.2, %ab ], [ 0, %q ]
+ br i1 %tobool2.not, label %if.end35, label %if.then20
+
+if.then20:
+ %div22 = udiv i64 %m.3, %conv21
+ %1 = trunc i64 %div22 to i32
+ %conv23 = sub i32 0, %1
+ %2 = trunc i64 %m.3 to i32
+ %conv25 = xor i32 %2, 1
+ br label %if.end35
+
+ac:
+ %spec.select = select i1 %tobool2.not, i32 %q24.2, i32 %o.2
+ ret i32 %spec.select
+
+if.end35:
+ %q24.660 = phi i32 [ 0, %ae ], [ %conv25, %if.then20 ]
+ %o.7 = phi i32 [ %o.4, %ae ], [ %conv23, %if.then20 ]
+ %q24.7 = phi i32 [ %q24.4, %ae ], [ %conv25, %if.then20 ]
+ br i1 %tobool2.not, label %while.body, label %ab
+}
More information about the llvm-commits
mailing list