Skip to content

Commit 885fe7f

Browse files
committed
[SCEV] Handle more add/addrec mixed in computeConstantDifference()
computeConstantDifference() can currently look through addrecs with identical steps, and then through adds with identical operands (apart from constants). However, it fails to handle minor variations, such as two nested add recs, or an outer add with an inner addrec (rather than the other way around). This patch supports these cases by adding a loop over the simplifications, limited to a small number of iterations. The motivation is the same as in #101339, to make computeConstantDifference() powerful enough to replace existing uses of `dyn_cast<SCEVConstant>(getMinusSCEV())` with it. Though as the IR test diff shows, other callers may also benefit.
1 parent 337c8b1 commit 885fe7f

File tree

3 files changed

+96
-83
lines changed

3 files changed

+96
-83
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11933,56 +11933,83 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
1193311933
// We avoid subtracting expressions here because this function is usually
1193411934
// fairly deep in the call stack (i.e. is called many times).
1193511935

11936-
// X - X = 0.
1193711936
unsigned BW = getTypeSizeInBits(More->getType());
11938-
if (More == Less)
11939-
return APInt(BW, 0);
11940-
11941-
if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
11942-
const auto *LAR = cast<SCEVAddRecExpr>(Less);
11943-
const auto *MAR = cast<SCEVAddRecExpr>(More);
11944-
11945-
if (LAR->getLoop() != MAR->getLoop())
11946-
return std::nullopt;
11947-
11948-
// We look at affine expressions only; not for correctness but to keep
11949-
// getStepRecurrence cheap.
11950-
if (!LAR->isAffine() || !MAR->isAffine())
11951-
return std::nullopt;
11937+
APInt Diff(BW, 0);
11938+
// Try various simplifications to reduce the difference to a constant. Limit
11939+
// the number of allowed simplifications to keep compile-time low.
11940+
for (unsigned I = 0; I < 4; ++I) {
11941+
if (More == Less)
11942+
return Diff;
11943+
11944+
// Reduce addrecs with identical steps to their start value.
11945+
if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
11946+
const auto *LAR = cast<SCEVAddRecExpr>(Less);
11947+
const auto *MAR = cast<SCEVAddRecExpr>(More);
11948+
11949+
if (LAR->getLoop() != MAR->getLoop())
11950+
return std::nullopt;
11951+
11952+
// We look at affine expressions only; not for correctness but to keep
11953+
// getStepRecurrence cheap.
11954+
if (!LAR->isAffine() || !MAR->isAffine())
11955+
return std::nullopt;
11956+
11957+
if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
11958+
return std::nullopt;
11959+
11960+
Less = LAR->getStart();
11961+
More = MAR->getStart();
11962+
continue;
11963+
}
1195211964

11953-
if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
11965+
// Try to cancel out common factors in two add expressions.
11966+
SmallDenseMap<const SCEV *, int, 8> Multiplicity;
11967+
auto Add = [&](const SCEV *S, int Mul) {
11968+
if (auto *C = dyn_cast<SCEVConstant>(S))
11969+
Diff += C->getAPInt() * Mul;
11970+
else
11971+
Multiplicity[S] += Mul;
11972+
};
11973+
auto Decompose = [&](const SCEV *S, int Mul) {
11974+
if (isa<SCEVAddExpr>(S)) {
11975+
for (const SCEV *Op : S->operands())
11976+
Add(Op, Mul);
11977+
} else
11978+
Add(S, Mul);
11979+
};
11980+
Decompose(More, 1);
11981+
Decompose(Less, -1);
11982+
11983+
// Check whether all the non-constants cancel out, or reduce to new
11984+
// More/Less values.
11985+
const SCEV *NewMore = nullptr, *NewLess = nullptr;
11986+
for (const auto [S, Mul] : Multiplicity) {
11987+
if (Mul == 0)
11988+
continue;
11989+
if (Mul == 1) {
11990+
if (NewMore)
11991+
return std::nullopt;
11992+
NewMore = S;
11993+
} else if (Mul == -1) {
11994+
if (NewLess)
11995+
return std::nullopt;
11996+
NewLess = S;
11997+
} else
11998+
return std::nullopt;
11999+
}
12000+
12001+
// Values stayed the same, no point in trying further.
12002+
if (NewMore == More || NewLess == Less)
1195412003
return std::nullopt;
1195512004

11956-
Less = LAR->getStart();
11957-
More = MAR->getStart();
11958-
11959-
// fall through
12005+
More = NewMore;
12006+
Less = NewLess;
12007+
if (!More || !Less)
12008+
break;
1196012009
}
1196112010

11962-
// Try to cancel out common factors in two add expressions.
11963-
SmallDenseMap<const SCEV *, int, 8> Multiplicity;
11964-
APInt Diff(BW, 0);
11965-
auto Add = [&](const SCEV *S, int Mul) {
11966-
if (auto *C = dyn_cast<SCEVConstant>(S))
11967-
Diff += C->getAPInt() * Mul;
11968-
else
11969-
Multiplicity[S] += Mul;
11970-
};
11971-
auto Decompose = [&](const SCEV *S, int Mul) {
11972-
if (isa<SCEVAddExpr>(S)) {
11973-
for (const SCEV *Op : S->operands())
11974-
Add(Op, Mul);
11975-
} else
11976-
Add(S, Mul);
11977-
};
11978-
Decompose(More, 1);
11979-
Decompose(Less, -1);
11980-
11981-
// Check whether all the non-constants cancel out.
11982-
for (const auto &[_, Mul] : Multiplicity)
11983-
if (Mul != 0)
11984-
return std::nullopt;
11985-
12011+
if (More || Less)
12012+
return std::nullopt; // Did not reduce to constant.
1198612013
return Diff;
1198712014
}
1198812015

llvm/test/Transforms/LoopVectorize/skeleton-lcssa-crash.ll

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ define i16 @test(ptr %arg, i64 %N) {
2929
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 2
3030
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
3131
; CHECK: vector.memcheck:
32-
; CHECK-NEXT: [[UGLYGEP:%.*]] = getelementptr i8, ptr [[L_2_LCSSA]], i64 2
33-
; CHECK-NEXT: [[UGLYGEP5:%.*]] = getelementptr i8, ptr [[L_1_LCSSA]], i64 2
32+
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[L_2_LCSSA]], i64 2
33+
; CHECK-NEXT: [[SCEVGEP5:%.*]] = getelementptr i8, ptr [[L_1_LCSSA]], i64 2
3434
; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[N]], 1
3535
; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[TMP1]], 4
36-
; CHECK-NEXT: [[UGLYGEP6:%.*]] = getelementptr i8, ptr [[L_1_LCSSA]], i64 [[TMP2]]
37-
; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[L_2_LCSSA]], [[UGLYGEP6]]
38-
; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[UGLYGEP5]], [[UGLYGEP]]
36+
; CHECK-NEXT: [[SCEVGEP6:%.*]] = getelementptr i8, ptr [[L_1_LCSSA]], i64 [[TMP2]]
37+
; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[L_2_LCSSA]], [[SCEVGEP6]]
38+
; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[SCEVGEP5]], [[SCEVGEP]]
3939
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
4040
; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
4141
; CHECK: vector.ph:
@@ -48,10 +48,10 @@ define i16 @test(ptr %arg, i64 %N) {
4848
; CHECK-NEXT: [[TMP4:%.*]] = add nuw nsw i64 [[TMP3]], 1
4949
; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i16, ptr [[L_1]], i64 [[TMP4]]
5050
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds i16, ptr [[TMP5]], i32 0
51-
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i16>, ptr [[TMP6]], align 2, !alias.scope !0
51+
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i16>, ptr [[TMP6]], align 2, !alias.scope [[META0:![0-9]+]]
5252
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i16, ptr [[L_2]], i64 0
5353
; CHECK-NEXT: [[TMP8:%.*]] = extractelement <2 x i16> [[WIDE_LOAD]], i32 1
54-
; CHECK-NEXT: store i16 [[TMP8]], ptr [[TMP7]], align 2, !alias.scope !3, !noalias !0
54+
; CHECK-NEXT: store i16 [[TMP8]], ptr [[TMP7]], align 2, !alias.scope [[META3:![0-9]+]], !noalias [[META0]]
5555
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
5656
; CHECK-NEXT: [[TMP9:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
5757
; CHECK-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
@@ -74,7 +74,7 @@ define i16 @test(ptr %arg, i64 %N) {
7474
; CHECK-NEXT: [[LOOP_L_1:%.*]] = load i16, ptr [[GEP_1]], align 2
7575
; CHECK-NEXT: [[GEP_2:%.*]] = getelementptr inbounds i16, ptr [[L_2_LCSSA]], i64 0
7676
; CHECK-NEXT: store i16 [[LOOP_L_1]], ptr [[GEP_2]], align 2
77-
; CHECK-NEXT: br i1 [[C_5]], label [[LOOP_3]], label [[EXIT_LOOPEXIT]], !llvm.loop [[LOOP7:![0-9]+]]
77+
; CHECK-NEXT: br i1 [[C_5]], label [[LOOP_3]], label [[EXIT_LOOPEXIT]], !llvm.loop [[LOOP8:![0-9]+]]
7878
; CHECK: exit.loopexit:
7979
; CHECK-NEXT: br label [[EXIT:%.*]]
8080
; CHECK: exit.loopexit1:
@@ -138,31 +138,17 @@ define void @test2(ptr %dst) {
138138
; CHECK-NEXT: [[INDVAR_NEXT]] = add i32 [[INDVAR]], 1
139139
; CHECK-NEXT: br i1 [[C_1]], label [[LOOP_2]], label [[LOOP_3_PH:%.*]]
140140
; CHECK: loop.3.ph:
141-
; CHECK-NEXT: [[INDVAR_LCSSA1:%.*]] = phi i32 [ [[INDVAR]], [[LOOP_2]] ]
142141
; CHECK-NEXT: [[INDVAR_LCSSA:%.*]] = phi i32 [ [[INDVAR]], [[LOOP_2]] ]
143142
; CHECK-NEXT: [[IV_1_LCSSA:%.*]] = phi i64 [ [[IV_1]], [[LOOP_2]] ]
144143
; CHECK-NEXT: [[TMP0:%.*]] = and i64 [[IV_1_LCSSA]], 4294967295
145-
; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[INDVAR_LCSSA1]], -1
144+
; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[INDVAR_LCSSA]], -1
146145
; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[TMP1]], 1000
147-
; CHECK-NEXT: [[SMIN2:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP2]], i32 1)
148-
; CHECK-NEXT: [[TMP3:%.*]] = sub i32 [[TMP2]], [[SMIN2]]
146+
; CHECK-NEXT: [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP2]], i32 1)
147+
; CHECK-NEXT: [[TMP3:%.*]] = sub i32 [[TMP2]], [[SMIN]]
149148
; CHECK-NEXT: [[TMP4:%.*]] = zext i32 [[TMP3]] to i64
150149
; CHECK-NEXT: [[TMP5:%.*]] = add nuw nsw i64 [[TMP4]], 1
151150
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP5]], 2
152-
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_SCEVCHECK:%.*]]
153-
; CHECK: vector.scevcheck:
154-
; CHECK-NEXT: [[TMP6:%.*]] = mul i32 [[INDVAR_LCSSA]], -1
155-
; CHECK-NEXT: [[TMP7:%.*]] = add i32 [[TMP6]], 1000
156-
; CHECK-NEXT: [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP7]], i32 1)
157-
; CHECK-NEXT: [[TMP8:%.*]] = sub i32 [[TMP7]], [[SMIN]]
158-
; CHECK-NEXT: [[TMP9:%.*]] = add i32 [[TMP6]], 999
159-
; CHECK-NEXT: [[MUL:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 1, i32 [[TMP8]])
160-
; CHECK-NEXT: [[MUL_RESULT:%.*]] = extractvalue { i32, i1 } [[MUL]], 0
161-
; CHECK-NEXT: [[MUL_OVERFLOW:%.*]] = extractvalue { i32, i1 } [[MUL]], 1
162-
; CHECK-NEXT: [[TMP10:%.*]] = sub i32 [[TMP9]], [[MUL_RESULT]]
163-
; CHECK-NEXT: [[TMP11:%.*]] = icmp ugt i32 [[TMP10]], [[TMP9]]
164-
; CHECK-NEXT: [[TMP12:%.*]] = or i1 [[TMP11]], [[MUL_OVERFLOW]]
165-
; CHECK-NEXT: br i1 [[TMP12]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
151+
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
166152
; CHECK: vector.ph:
167153
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[TMP5]], 2
168154
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[TMP5]], [[N_MOD_VF]]
@@ -171,21 +157,21 @@ define void @test2(ptr %dst) {
171157
; CHECK: vector.body:
172158
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
173159
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = sub i64 [[TMP0]], [[INDEX]]
174-
; CHECK-NEXT: [[TMP13:%.*]] = add i64 [[OFFSET_IDX]], 0
175-
; CHECK-NEXT: [[TMP14:%.*]] = add nsw i64 [[TMP13]], -1
176-
; CHECK-NEXT: [[TMP15:%.*]] = and i64 [[TMP14]], 4294967295
177-
; CHECK-NEXT: [[TMP16:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[TMP15]]
178-
; CHECK-NEXT: [[TMP17:%.*]] = getelementptr inbounds i32, ptr [[TMP16]], i32 0
179-
; CHECK-NEXT: [[TMP18:%.*]] = getelementptr inbounds i32, ptr [[TMP17]], i32 -1
180-
; CHECK-NEXT: store <2 x i32> zeroinitializer, ptr [[TMP18]], align 4
160+
; CHECK-NEXT: [[TMP6:%.*]] = add i64 [[OFFSET_IDX]], 0
161+
; CHECK-NEXT: [[TMP7:%.*]] = add nsw i64 [[TMP6]], -1
162+
; CHECK-NEXT: [[TMP8:%.*]] = and i64 [[TMP7]], 4294967295
163+
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[TMP8]]
164+
; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[TMP9]], i32 0
165+
; CHECK-NEXT: [[TMP11:%.*]] = getelementptr inbounds i32, ptr [[TMP10]], i32 -1
166+
; CHECK-NEXT: store <2 x i32> zeroinitializer, ptr [[TMP11]], align 4
181167
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
182-
; CHECK-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
183-
; CHECK-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
168+
; CHECK-NEXT: [[TMP12:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
169+
; CHECK-NEXT: br i1 [[TMP12]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP9:![0-9]+]]
184170
; CHECK: middle.block:
185171
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP5]], [[N_VEC]]
186172
; CHECK-NEXT: br i1 [[CMP_N]], label [[LOOP_1_LATCH:%.*]], label [[SCALAR_PH]]
187173
; CHECK: scalar.ph:
188-
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[TMP0]], [[LOOP_3_PH]] ], [ [[TMP0]], [[VECTOR_SCEVCHECK]] ]
174+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[TMP0]], [[LOOP_3_PH]] ]
189175
; CHECK-NEXT: br label [[LOOP_3:%.*]]
190176
; CHECK: loop.3:
191177
; CHECK-NEXT: [[IV_2:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_2_NEXT:%.*]], [[LOOP_3]] ]
@@ -195,7 +181,7 @@ define void @test2(ptr %dst) {
195181
; CHECK-NEXT: store i32 0, ptr [[GEP_DST]], align 4
196182
; CHECK-NEXT: [[IV_2_TRUNC:%.*]] = trunc i64 [[IV_2]] to i32
197183
; CHECK-NEXT: [[EC:%.*]] = icmp sgt i32 [[IV_2_TRUNC]], 1
198-
; CHECK-NEXT: br i1 [[EC]], label [[LOOP_3]], label [[LOOP_1_LATCH]], !llvm.loop [[LOOP9:![0-9]+]]
184+
; CHECK-NEXT: br i1 [[EC]], label [[LOOP_3]], label [[LOOP_1_LATCH]], !llvm.loop [[LOOP10:![0-9]+]]
199185
; CHECK: loop.1.latch:
200186
; CHECK-NEXT: [[C_2:%.*]] = call i1 @cond()
201187
; CHECK-NEXT: br i1 [[C_2]], label [[EXIT:%.*]], label [[LOOP_1_HEADER]]

llvm/unittests/Analysis/ScalarEvolutionTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,8 @@ TEST_F(ScalarEvolutionsTest, SCEVComputeConstantDifference) {
11991199
EXPECT_EQ(diff(ScevIV, ScevIVNext), -1);
12001200
EXPECT_EQ(diff(ScevIVNext, ScevIV), 1);
12011201
EXPECT_EQ(diff(ScevIVNext, ScevIVNext), 0);
1202-
EXPECT_EQ(diff(ScevIV2P3, ScevIV2), std::nullopt); // TODO
1203-
EXPECT_EQ(diff(ScevIV2PVar, ScevIV2PVarP3), std::nullopt); // TODO
1202+
EXPECT_EQ(diff(ScevIV2P3, ScevIV2), 3);
1203+
EXPECT_EQ(diff(ScevIV2PVar, ScevIV2PVarP3), -3);
12041204
EXPECT_EQ(diff(ScevV0, ScevIV), std::nullopt);
12051205
EXPECT_EQ(diff(ScevIVNext, ScevV3), std::nullopt);
12061206
EXPECT_EQ(diff(ScevYY, ScevV3), std::nullopt);

0 commit comments

Comments
 (0)