Skip to content

Commit ba669c5

Browse files
artagnonfhahn
andcommitted
[LV] Fix sentinel, tweak test for coverage
Co-authored-by: Florian Hahn <[email protected]>
1 parent 2456f27 commit ba669c5

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,10 @@ RecurrenceDescriptor::isFindIVPattern(RecurKind Kind, Loop *TheLoop,
722722

723723
// Keep the minimum (FindLast) or maximum (FindFirst) value of the
724724
// recurrence type as the sentinel value. The maximum acceptable range for
725-
// the induction variable, called the valid range, will be defined as
726-
// [<sentinel value> + 1, <sentinel value>)
727-
// where <sentinel value> is [Signed|Unsigned]Min(<recurrence type>) for
728-
// FindLastIV or [Signed|Unsigned]Max(<recurrence type>) for FindFirstIV,
729-
// noting that this is a wrapped range since Start > End.
725+
// the induction variable, called the valid range will exclude <sentinel
726+
// value>, where <sentinel value> is [Signed|Unsigned]Min(<recurrence type>)
727+
// for FindLastIV or [Signed|Unsigned]Max(<recurrence type>) for
728+
// FindFirstIV.
730729
// TODO: This range restriction can be lifted by adding an additional
731730
// virtual OR reduction.
732731
auto CheckRange = [&](bool IsSigned) {
@@ -739,9 +738,13 @@ RecurrenceDescriptor::isFindIVPattern(RecurKind Kind, Loop *TheLoop,
739738
: APInt::getMinValue(NumBits);
740739
ValidRange = ConstantRange::getNonEmpty(Sentinel + 1, Sentinel);
741740
} else {
742-
APInt Sentinel = IsSigned ? APInt::getSignedMaxValue(NumBits)
743-
: APInt::getMaxValue(NumBits);
744-
ValidRange = ConstantRange::getNonEmpty(Sentinel + 1, Sentinel);
741+
if (IsSigned)
742+
ValidRange =
743+
ConstantRange::getNonEmpty(APInt::getSignedMinValue(NumBits),
744+
APInt::getSignedMaxValue(NumBits) - 1);
745+
else
746+
ValidRange = ConstantRange::getNonEmpty(
747+
APInt::getMinValue(NumBits), APInt::getMaxValue(NumBits) - 1);
745748
}
746749

747750
LLVM_DEBUG(dbgs() << "LV: "

llvm/test/Transforms/LoopVectorize/iv-select-cmp-decreasing.ll

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,41 +1142,46 @@ exit: ; preds = %loop
11421142

11431143
; The unsigned sentinel value for decreasing-IV vectorization is ULONG_MAX,
11441144
; and since the IV hits this value, it is impossible to vectorize this case.
1145-
define i64 @not_vectorized_select_decreasing_induction_icmp_iv_out_of_bound(ptr %a) {
1145+
; This test includes both signed and unsigned sentinel values.
1146+
define i64 @not_vectorized_select_decreasing_induction_icmp_iv_out_of_bound(ptr %a, ptr %b, i64 %rdx.start) {
11461147
; CHECK-LABEL: define i64 @not_vectorized_select_decreasing_induction_icmp_iv_out_of_bound(
1147-
; CHECK-SAME: ptr [[A:%.*]]) {
1148+
; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]], i64 [[RDX_START:%.*]]) {
11481149
; CHECK-NEXT: [[ENTRY:.*]]:
11491150
; CHECK-NEXT: br label %[[LOOP:.*]]
11501151
; CHECK: [[LOOP]]:
11511152
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ -1, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
1152-
; CHECK-NEXT: [[RDX:%.*]] = phi i64 [ 331, %[[ENTRY]] ], [ [[SPEC_SELECT:%.*]], %[[LOOP]] ]
1153-
; CHECK-NEXT: [[GEP_A_IV:%.*]] = getelementptr inbounds i64, ptr [[A]], i64 [[IV]]
1154-
; CHECK-NEXT: [[LD_A:%.*]] = load i64, ptr [[GEP_A_IV]], align 8
1155-
; CHECK-NEXT: [[CMP_A_3:%.*]] = icmp sgt i64 [[LD_A]], 3
1156-
; CHECK-NEXT: [[SPEC_SELECT]] = select i1 [[CMP_A_3]], i64 [[IV]], i64 [[RDX]]
1157-
; CHECK-NEXT: [[IV_NEXT]] = add nsw i64 [[IV]], -1
1158-
; CHECK-NEXT: [[EXIT_COND:%.*]] = icmp eq i64 [[IV]], 0
1153+
; CHECK-NEXT: [[RDX:%.*]] = phi i64 [ [[RDX_START]], %[[ENTRY]] ], [ [[COND:%.*]], %[[LOOP]] ]
1154+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], -1
1155+
; CHECK-NEXT: [[GEP_A_IV:%.*]] = getelementptr inbounds i8, ptr [[A]], i64 [[IV_NEXT]]
1156+
; CHECK-NEXT: [[LD_A:%.*]] = load i8, ptr [[GEP_A_IV]], align 1
1157+
; CHECK-NEXT: [[GEP_B_IV:%.*]] = getelementptr inbounds i8, ptr [[B]], i64 [[IV_NEXT]]
1158+
; CHECK-NEXT: [[LD_B:%.*]] = load i8, ptr [[GEP_B_IV]], align 1
1159+
; CHECK-NEXT: [[CMP_A_B:%.*]] = icmp sgt i8 [[LD_A]], [[LD_B]]
1160+
; CHECK-NEXT: [[COND]] = select i1 [[CMP_A_B]], i64 [[IV_NEXT]], i64 [[RDX]]
1161+
; CHECK-NEXT: [[EXIT_COND:%.*]] = icmp eq i64 [[IV_NEXT]], 0
11591162
; CHECK-NEXT: br i1 [[EXIT_COND]], label %[[EXIT:.*]], label %[[LOOP]]
11601163
; CHECK: [[EXIT]]:
1161-
; CHECK-NEXT: [[SPEC_SELECT_LCSSA:%.*]] = phi i64 [ [[SPEC_SELECT]], %[[LOOP]] ]
1162-
; CHECK-NEXT: ret i64 [[SPEC_SELECT_LCSSA]]
1164+
; CHECK-NEXT: [[COND_LCSSA:%.*]] = phi i64 [ [[COND]], %[[LOOP]] ]
1165+
; CHECK-NEXT: ret i64 [[COND_LCSSA]]
11631166
;
11641167
entry:
11651168
br label %loop
11661169

1167-
loop: ; preds = %entry, %loop
1170+
loop:
11681171
%iv = phi i64 [ -1, %entry ], [ %iv.next, %loop ]
1169-
%rdx = phi i64 [ 331, %entry ], [ %spec.select, %loop ]
1170-
%gep.a.iv = getelementptr inbounds i64, ptr %a, i64 %iv
1171-
%ld.a = load i64, ptr %gep.a.iv, align 8
1172-
%cmp.a.3 = icmp sgt i64 %ld.a, 3
1173-
%spec.select = select i1 %cmp.a.3, i64 %iv, i64 %rdx
1174-
%iv.next = add nsw i64 %iv, -1
1175-
%exit.cond = icmp eq i64 %iv, 0
1172+
%rdx = phi i64 [ %rdx.start, %entry ], [ %cond, %loop ]
1173+
%iv.next = add i64 %iv, -1
1174+
%gep.a.iv = getelementptr inbounds i8, ptr %a, i64 %iv.next
1175+
%ld.a = load i8, ptr %gep.a.iv, align 1
1176+
%gep.b.iv = getelementptr inbounds i8, ptr %b, i64 %iv.next
1177+
%ld.b = load i8, ptr %gep.b.iv, align 1
1178+
%cmp.a.b = icmp sgt i8 %ld.a, %ld.b
1179+
%cond = select i1 %cmp.a.b, i64 %iv.next, i64 %rdx
1180+
%exit.cond = icmp eq i64 %iv.next, 0
11761181
br i1 %exit.cond, label %exit, label %loop
11771182

1178-
exit: ; preds = %loop
1179-
ret i64 %spec.select
1183+
exit:
1184+
ret i64 %cond
11801185
}
11811186

11821187
define i64 @not_vectorized_select_decreasing_induction_icmp_non_const_start(ptr %a, ptr %b, i64 %rdx.start, i64 %n) {

0 commit comments

Comments
 (0)