Skip to content

Commit 32d1197

Browse files
committed
[LV] Use SCEV for subtraction of src/sink for diff runtime checks.
Instead of expanding the src/sink SCEV expressions and emitting an IR sub to compute the difference, the subtraction can be directly be performed by ScalarEvolution. This allows the subtraction to be simplified by SCEV, which in turn can reduced the number of redundant runtime check instructions generated. It also allows to generate checks that are invariant w.r.t. an outer loop, if he inner loop AddRecs have the same outer loop AddRec as start.
1 parent eed17dc commit 32d1197

File tree

6 files changed

+184
-313
lines changed

6 files changed

+184
-313
lines changed

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,9 +1793,9 @@ Value *llvm::addDiffRuntimeChecks(
17931793
auto *VFTimesUFTimesSize =
17941794
ChkBuilder.CreateMul(GetVF(ChkBuilder, Ty->getScalarSizeInBits()),
17951795
ConstantInt::get(Ty, IC * C.AccessSize));
1796-
Value *Sink = Expander.expandCodeFor(C.SinkStart, Ty, Loc);
1797-
Value *Src = Expander.expandCodeFor(C.SrcStart, Ty, Loc);
1798-
Value *Diff = ChkBuilder.CreateSub(Sink, Src);
1796+
auto &SE = *Expander.getSE();
1797+
Value *Diff = Expander.expandCodeFor(
1798+
SE.getMinusSCEV(C.SinkStart, C.SrcStart), Ty, Loc);
17991799
Value *IsConflict =
18001800
ChkBuilder.CreateICmpULT(Diff, VFTimesUFTimesSize, "diff.check");
18011801
if (C.NeedsFreeze)

llvm/test/Transforms/LoopVectorize/AArch64/sve-vector-reverse.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ define void @vector_reverse_i64(i64 %N, ptr %a, ptr %b) #0 {
117117
; CHECK: vector.memcheck:
118118
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
119119
; CHECK-NEXT: [[TMP3:%.*]] = shl i64 [[TMP2]], 7
120-
; CHECK-NEXT: [[TMP4:%.*]] = shl i64 [[N]], 3
121-
; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[TMP4]], [[B1]]
122-
; CHECK-NEXT: [[TMP6:%.*]] = add i64 [[TMP4]], [[A2]]
123-
; CHECK-NEXT: [[TMP7:%.*]] = sub i64 [[TMP5]], [[TMP6]]
120+
; CHECK-NEXT: [[TMP7:%.*]] = sub i64 [[B1]], [[A2]]
124121
; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP7]], [[TMP3]]
125122
; CHECK-NEXT: br i1 [[DIFF_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
126123
; CHECK: vector.ph:

llvm/test/Transforms/LoopVectorize/runtime-check-small-clamped-bounds.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ define void @load_clamped_index_offset_1(ptr %A, ptr %B, i32 %N) {
174174
; CHECK-NEXT: [[TMP6:%.*]] = or i1 [[TMP4]], [[TMP5]]
175175
; CHECK-NEXT: br i1 [[TMP6]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK:%.*]]
176176
; CHECK: vector.memcheck:
177-
; CHECK-NEXT: [[TMP7:%.*]] = add nuw i64 [[B1]], 4
178-
; CHECK-NEXT: [[TMP8:%.*]] = add i64 [[A2]], 4
179-
; CHECK-NEXT: [[TMP9:%.*]] = sub i64 [[TMP7]], [[TMP8]]
180-
; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP9]], 16
177+
; CHECK-NEXT: [[TMP7:%.*]] = sub i64 [[B1]], [[A2]]
178+
; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP7]], 16
181179
; CHECK-NEXT: br i1 [[DIFF_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
182180
; CHECK: vector.ph:
183181
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[TMP0]], 4

0 commit comments

Comments
 (0)