Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8871,9 +8871,17 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
const APInt *C;
if (match(V, m_APInt(C)))
return ConstantRange(*C);
unsigned BitWidth = V->getType()->getScalarSizeInBits();

if (auto *VC = dyn_cast<ConstantDataVector>(V)) {
ConstantRange CR = ConstantRange::getEmpty(BitWidth);
for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
++ElemIdx)
CR = CR.unionWith(VC->getElementAsAPInt(ElemIdx));
return CR;
}

InstrInfoQuery IIQ(UseInstrInfo);
unsigned BitWidth = V->getType()->getScalarSizeInBits();
ConstantRange CR = ConstantRange::getFull(BitWidth);
if (auto *BO = dyn_cast<BinaryOperator>(V)) {
APInt Lower = APInt(BitWidth, 0);
Expand Down
4 changes: 1 addition & 3 deletions llvm/test/Transforms/InstCombine/add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,9 @@ define <2 x i8> @test18vec_nsw(<2 x i8> %A) {
ret <2 x i8> %C
}

; TODO: fix ValueTracking overflow check for non-splat vector, could be attached nsw
; this shouldn't overflow.
define <2 x i8> @test18vec_nsw_false(<2 x i8> %A) {
; CHECK-LABEL: @test18vec_nsw_false(
; CHECK-NEXT: [[C:%.*]] = sub <2 x i8> <i8 -125, i8 -126>, [[A:%.*]]
; CHECK-NEXT: [[C:%.*]] = sub nsw <2 x i8> <i8 -125, i8 -126>, [[A:%.*]]
; CHECK-NEXT: ret <2 x i8> [[C]]
;
%B = xor <2 x i8> %A, <i8 -1, i8 -1>
Expand Down
7 changes: 2 additions & 5 deletions llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,19 @@ define <2 x i8> @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov1(<2 x i8> %arg
ret <2 x i8> %t1
}


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

; TODO: We can add nsw on sub, curret Value Tracking can't decide this is not overflowed?
define <2 x i8> @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov3(<2 x i8> %arg) {
; CHECK-LABEL: @non_splat_vec_add_nsw_const_const_sub_nsw_not_ov3(
; CHECK-NEXT: [[T1:%.*]] = sub <2 x i8> <i8 -120, i8 -127>, [[ARG:%.*]]
; CHECK-NEXT: [[T1:%.*]] = sub nsw <2 x i8> <i8 -120, i8 -127>, [[ARG:%.*]]
; CHECK-NEXT: ret <2 x i8> [[T1]]
;
%t0 = add nsw <2 x i8> %arg, <i8 0, i8 1>
Expand Down
4 changes: 1 addition & 3 deletions llvm/test/Transforms/InstCombine/saturating-add-sub.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1052,11 +1052,9 @@ define <2 x i8> @test_vector_usub_add_nuw_no_ov(<2 x i8> %a) {
ret <2 x i8> %r
}

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