Skip to content

Commit 2060bfc

Browse files
authored
[ValueTracking] Add support for non-splat vecs in computeConstantRange (#72365)
Related patch: #68331 This missed optimization is discovered with the help of AliveToolkit/alive2#962.
1 parent 75cf672 commit 2060bfc

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8871,9 +8871,17 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
88718871
const APInt *C;
88728872
if (match(V, m_APInt(C)))
88738873
return ConstantRange(*C);
8874+
unsigned BitWidth = V->getType()->getScalarSizeInBits();
8875+
8876+
if (auto *VC = dyn_cast<ConstantDataVector>(V)) {
8877+
ConstantRange CR = ConstantRange::getEmpty(BitWidth);
8878+
for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
8879+
++ElemIdx)
8880+
CR = CR.unionWith(VC->getElementAsAPInt(ElemIdx));
8881+
return CR;
8882+
}
88748883

88758884
InstrInfoQuery IIQ(UseInstrInfo);
8876-
unsigned BitWidth = V->getType()->getScalarSizeInBits();
88778885
ConstantRange CR = ConstantRange::getFull(BitWidth);
88788886
if (auto *BO = dyn_cast<BinaryOperator>(V)) {
88798887
APInt Lower = APInt(BitWidth, 0);

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,9 @@ define <2 x i8> @test18vec_nsw(<2 x i8> %A) {
440440
ret <2 x i8> %C
441441
}
442442

443-
; TODO: fix ValueTracking overflow check for non-splat vector, could be attached nsw
444-
; this shouldn't overflow.
445443
define <2 x i8> @test18vec_nsw_false(<2 x i8> %A) {
446444
; CHECK-LABEL: @test18vec_nsw_false(
447-
; CHECK-NEXT: [[C:%.*]] = sub <2 x i8> <i8 -125, i8 -126>, [[A:%.*]]
445+
; CHECK-NEXT: [[C:%.*]] = sub nsw <2 x i8> <i8 -125, i8 -126>, [[A:%.*]]
448446
; CHECK-NEXT: ret <2 x i8> [[C]]
449447
;
450448
%B = xor <2 x i8> %A, <i8 -1, i8 -1>

llvm/test/Transforms/InstCombine/addsub-constant-folding.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,19 @@ define <2 x i8> @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov1(<2 x i8> %arg
203203
ret <2 x i8> %t1
204204
}
205205

206-
207-
; TODO: We can add nsw on sub, current Value Tracking use [max element,min element] constant range, to check overflow for vector?
208206
define <2 x i8> @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov2(<2 x i8> %arg) {
209207
; CHECK-LABEL: @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov2(
210-
; CHECK-NEXT: [[T1:%.*]] = sub <2 x i8> <i8 -126, i8 -128>, [[ARG:%.*]]
208+
; CHECK-NEXT: [[T1:%.*]] = sub nsw <2 x i8> <i8 -126, i8 -128>, [[ARG:%.*]]
211209
; CHECK-NEXT: ret <2 x i8> [[T1]]
212210
;
213211
%t0 = add nsw <2 x i8> %arg, <i8 1, i8 2>
214212
%t1 = sub nsw <2 x i8> <i8 -125, i8 -126>, %t0
215213
ret <2 x i8> %t1
216214
}
217215

218-
; TODO: We can add nsw on sub, curret Value Tracking can't decide this is not overflowed?
219216
define <2 x i8> @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov3(<2 x i8> %arg) {
220217
; CHECK-LABEL: @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov3(
221-
; CHECK-NEXT: [[T1:%.*]] = sub <2 x i8> <i8 -120, i8 -127>, [[ARG:%.*]]
218+
; CHECK-NEXT: [[T1:%.*]] = sub nsw <2 x i8> <i8 -120, i8 -127>, [[ARG:%.*]]
222219
; CHECK-NEXT: ret <2 x i8> [[T1]]
223220
;
224221
%t0 = add nsw <2 x i8> %arg, <i8 0, i8 1>

llvm/test/Transforms/InstCombine/saturating-add-sub.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,11 +1052,9 @@ define <2 x i8> @test_vector_usub_add_nuw_no_ov(<2 x i8> %a) {
10521052
ret <2 x i8> %r
10531053
}
10541054

1055-
; Can be optimized if the usub.sat RHS constant range handles non-splat vectors.
10561055
define <2 x i8> @test_vector_usub_add_nuw_no_ov_nonsplat1(<2 x i8> %a) {
10571056
; CHECK-LABEL: @test_vector_usub_add_nuw_no_ov_nonsplat1(
1058-
; CHECK-NEXT: [[B:%.*]] = add nuw <2 x i8> [[A:%.*]], <i8 10, i8 10>
1059-
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> [[B]], <2 x i8> <i8 10, i8 9>)
1057+
; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[A:%.*]], <i8 0, i8 1>
10601058
; CHECK-NEXT: ret <2 x i8> [[R]]
10611059
;
10621060
%b = add nuw <2 x i8> %a, <i8 10, i8 10>

0 commit comments

Comments
 (0)