Skip to content

Commit e775efc

Browse files
committed
[LV] Apply loop guards when checking recur during hoisting RT checks.
Apply loop guards when checking if the recurrence is non-negative in cases where runtime checks are hoisted out of an inner loop.
1 parent fbaec0f commit e775efc

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,8 @@ static PointerBounds expandBounds(const RuntimeCheckingPtrGroup *CG,
17601760
Low = cast<SCEVAddRecExpr>(Low)->getStart();
17611761
// If there is a possibility that the stride is negative then we have
17621762
// to generate extra checks to ensure the stride is positive.
1763-
if (!SE.isKnownNonNegative(Recur)) {
1763+
if (!SE.isKnownNonNegative(
1764+
SE.applyLoopGuards(Recur, HighAR->getLoop()))) {
17641765
Stride = Recur;
17651766
LLVM_DEBUG(dbgs() << "LAA: ... but need to check stride is "
17661767
"positive: "

llvm/test/Transforms/LoopVectorize/runtime-checks-hoist.ll

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,8 @@ outer.exit:
15091509
ret void
15101510
}
15111511

1512-
; TODO: STRIDE_CHECK can be eliminated via loop guards.
1512+
; The stride for the access in the inner loop is known to be non-negative via
1513+
; loop guards.
15131514
define void @stride_check_known_via_loop_guard(ptr %C, ptr %A, i32 %Acols) {
15141515
; CHECK-LABEL: define void @stride_check_known_via_loop_guard
15151516
; CHECK-SAME: (ptr [[C:%.*]], ptr [[A:%.*]], i32 [[ACOLS:%.*]]) {
@@ -1518,8 +1519,6 @@ define void @stride_check_known_via_loop_guard(ptr %C, ptr %A, i32 %Acols) {
15181519
; CHECK-NEXT: br i1 [[PRE_C]], label [[EXIT:%.*]], label [[OUTER_HEADER_PREHEADER:%.*]]
15191520
; CHECK: outer.header.preheader:
15201521
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[A]], i64 8
1521-
; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[ACOLS]] to i64
1522-
; CHECK-NEXT: [[TMP1:%.*]] = shl nsw i64 [[TMP0]], 3
15231522
; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[C]], i64 34359738368
15241523
; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
15251524
; CHECK: outer.header:
@@ -1533,23 +1532,21 @@ define void @stride_check_known_via_loop_guard(ptr %C, ptr %A, i32 %Acols) {
15331532
; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[A]], [[SCEVGEP1]]
15341533
; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[C]], [[SCEVGEP]]
15351534
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
1536-
; CHECK-NEXT: [[STRIDE_CHECK:%.*]] = icmp slt i64 [[TMP1]], 0
1537-
; CHECK-NEXT: [[TMP2:%.*]] = or i1 [[FOUND_CONFLICT]], [[STRIDE_CHECK]]
1538-
; CHECK-NEXT: br i1 [[TMP2]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
1535+
; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
15391536
; CHECK: vector.ph:
15401537
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
15411538
; CHECK: vector.body:
15421539
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
1543-
; CHECK-NEXT: [[TMP3:%.*]] = add i32 [[INDEX]], 0
1544-
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds double, ptr [[C]], i32 [[TMP3]]
1545-
; CHECK-NEXT: [[TMP5:%.*]] = load double, ptr [[ARRAYIDX_US]], align 8, !alias.scope [[META69:![0-9]+]], !noalias [[META72:![0-9]+]]
1546-
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x double> poison, double [[TMP5]], i64 0
1540+
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[INDEX]], 0
1541+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds double, ptr [[C]], i32 [[TMP0]]
1542+
; CHECK-NEXT: [[TMP2:%.*]] = load double, ptr [[ARRAYIDX_US]], align 8, !alias.scope [[META69:![0-9]+]], !noalias [[META72:![0-9]+]]
1543+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x double> poison, double [[TMP2]], i64 0
15471544
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT]], <4 x double> poison, <4 x i32> zeroinitializer
1548-
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds double, ptr [[TMP4]], i32 0
1549-
; CHECK-NEXT: store <4 x double> [[BROADCAST_SPLAT]], ptr [[TMP6]], align 8, !alias.scope [[META72]]
1545+
; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds double, ptr [[TMP1]], i32 0
1546+
; CHECK-NEXT: store <4 x double> [[BROADCAST_SPLAT]], ptr [[TMP3]], align 8, !alias.scope [[META72]]
15501547
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
1551-
; CHECK-NEXT: [[TMP7:%.*]] = icmp eq i32 [[INDEX_NEXT]], 0
1552-
; CHECK-NEXT: br i1 [[TMP7]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP74:![0-9]+]]
1548+
; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[INDEX_NEXT]], 0
1549+
; CHECK-NEXT: br i1 [[TMP4]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP74:![0-9]+]]
15531550
; CHECK: middle.block:
15541551
; CHECK-NEXT: br i1 true, label [[OUTER_LATCH]], label [[SCALAR_PH]]
15551552
; CHECK: scalar.ph:

0 commit comments

Comments
 (0)