Skip to content

Commit 3ef8137

Browse files
committed
[ValueTracking] Handle range assume bundles in computeKnownBits
1 parent 989ac2d commit 3ef8137

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -831,17 +831,22 @@ void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
831831
"Got assumption for the wrong function!");
832832

833833
if (Elem.Index != AssumptionCache::ExprResultIdx) {
834-
if (!V->getType()->isPointerTy())
835-
continue;
836-
if (RetainedKnowledge RK = getKnowledgeFromBundle(
837-
*I, I->bundle_op_info_begin()[Elem.Index])) {
834+
if (V->getType()->isPointerTy()) {
835+
if (RetainedKnowledge RK = getKnowledgeFromBundle(
836+
*I, I->bundle_op_info_begin()[Elem.Index])) {
838837
// Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
839838
// be the producer of the pointer in the bundle. At the moment, align
840839
// assumptions aren't optimized away.
841-
if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
842-
isPowerOf2_64(RK.ArgValue) &&
843-
isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
844-
Known.Zero.setLowBits(Log2_64(RK.ArgValue));
840+
if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
841+
isPowerOf2_64(RK.ArgValue) &&
842+
isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
843+
Known.Zero.setLowBits(Log2_64(RK.ArgValue));
844+
}
845+
} else if (V->getType()->isIntOrIntVectorTy()) {
846+
if (std::optional<ConstantRange> Range =
847+
getRangeFromBundle(*I, I->bundle_op_info_begin()[Elem.Index]))
848+
if (isValidAssumeForContext(I, Q.CxtI, Q.DT))
849+
Known = Known.unionWith(Range->toKnownBits());
845850
}
846851
continue;
847852
}

llvm/test/Transforms/InstSimplify/shift-knownbits.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ define <1 x i64> @bitcast_noshift_vector_wrong_type(<2 x float> %v1, <1 x i64> %
503503
define i32 @shl_amount_is_known_bogus_range_assum(i32 %a, i32 %b) {
504504
; CHECK-LABEL: @shl_amount_is_known_bogus_range_assum(
505505
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "range"(i32 [[B:%.*]], i32 32, i32 64) ]
506-
; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A:%.*]], [[B]]
507-
; CHECK-NEXT: ret i32 [[SHL]]
506+
; CHECK-NEXT: ret i32 poison
508507
;
509508
call void @llvm.assume(i1 true) ["range"(i32 %b, i32 32, i32 64)]
510509
%shl = shl i32 %a, %b
@@ -526,8 +525,7 @@ define i32 @neg_shl_amount_is_known_bogus_range_assum(i32 %a, i32 %b) {
526525
define <2 x i32> @shl_amount_is_known_bogus_range_assum_vec(<2 x i32> %a, <2 x i32> %b) {
527526
; CHECK-LABEL: @shl_amount_is_known_bogus_range_assum_vec(
528527
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "range"(<2 x i32> [[B:%.*]], i32 32, i32 64) ]
529-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> [[A:%.*]], [[B]]
530-
; CHECK-NEXT: ret <2 x i32> [[SHL]]
528+
; CHECK-NEXT: ret <2 x i32> poison
531529
;
532530
call void @llvm.assume(i1 true) ["range"(<2 x i32> %b, i32 32, i32 64)]
533531
%shl = shl <2 x i32> %a, %b

0 commit comments

Comments
 (0)