Skip to content

Commit 39562de

Browse files
authored
[ValueTracking] Handle assume(trunc x to i1) in ComputeKnownBits (#118406)
proof: https://alive2.llvm.org/ce/z/zAspzb
1 parent 3954d25 commit 39562de

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,16 @@ void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
910910
Known.setAllZero();
911911
return;
912912
}
913+
auto *Trunc = dyn_cast<TruncInst>(Arg);
914+
if (Trunc && Trunc->getOperand(0) == V &&
915+
isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
916+
if (Trunc->hasNoUnsignedWrap()) {
917+
Known = KnownBits::makeConstant(APInt(BitWidth, 1));
918+
return;
919+
}
920+
Known.One.setBit(0);
921+
return;
922+
}
913923

914924
// The remaining tests are all recursive, so bail out if we hit the limit.
915925
if (Depth == MaxAnalysisRecursionDepth)

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,7 @@ define i1 @assume_trunc_nuw_eq_one(i8 %x) {
10131013
; CHECK-LABEL: @assume_trunc_nuw_eq_one(
10141014
; CHECK-NEXT: [[A:%.*]] = trunc nuw i8 [[X:%.*]] to i1
10151015
; CHECK-NEXT: call void @llvm.assume(i1 [[A]])
1016-
; CHECK-NEXT: [[Q:%.*]] = icmp eq i8 [[X]], 1
1017-
; CHECK-NEXT: ret i1 [[Q]]
1016+
; CHECK-NEXT: ret i1 true
10181017
;
10191018
%a = trunc nuw i8 %x to i1
10201019
call void @llvm.assume(i1 %a)

llvm/test/Transforms/InstSimplify/shr-nop.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ define i8 @exact_lshr_lowbit_set_assume_trunc(i8 %x) {
385385
; CHECK-LABEL: @exact_lshr_lowbit_set_assume_trunc(
386386
; CHECK-NEXT: [[COND:%.*]] = trunc i8 [[X:%.*]] to i1
387387
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]])
388-
; CHECK-NEXT: [[SHR:%.*]] = lshr exact i8 [[X]], 1
389-
; CHECK-NEXT: ret i8 [[SHR]]
388+
; CHECK-NEXT: ret i8 [[X]]
390389
;
391390
%cond = trunc i8 %x to i1
392391
call void @llvm.assume(i1 %cond)

0 commit comments

Comments
 (0)