Skip to content

Commit 80e2d84

Browse files
dtcxzywtstellar
authored andcommitted
[InstCombine] Fix assertion failure in issue80597 (llvm#80614)
The assertion in llvm#80597 failed when we were trying to compute known bits of a value in an unreachable BB. https://github.com/llvm/llvm-project/blob/859b09da08c2a47026ba0a7d2f21b7dca705864d/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L749-L810 In this case, `SignBits` is 30 (deduced from instr info), but `Known` is `10000101010111010011110101000?0?00000000000000000000000000000000` (deduced from dom cond). Setting high bits of `lshr Known, 1` will lead to conflict. This patch masks out high bits of `Known.Zero` to address this problem. Fixes llvm#80597. (cherry picked from commit cb8d83a)
1 parent b6f208b commit 80e2d84

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,9 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
802802
return InsertNewInstWith(LShr, I->getIterator());
803803
} else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one.
804804
Known.One |= HighBits;
805+
// SignBits may be out-of-sync with Known.countMinSignBits(). Mask out
806+
// high bits of Known.Zero to avoid conflicts.
807+
Known.Zero &= ~HighBits;
805808
}
806809
} else {
807810
computeKnownBits(I, Known, Depth, CxtI);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
3+
4+
define i64 @pr80597(i1 %cond) {
5+
; CHECK-LABEL: define i64 @pr80597(
6+
; CHECK-SAME: i1 [[COND:%.*]]) {
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[COND]], i64 0, i64 -12884901888
9+
; CHECK-NEXT: [[SEXT1:%.*]] = add nsw i64 [[ADD]], 8836839514384105472
10+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[SEXT1]], -34359738368
11+
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
12+
; CHECK: if.else:
13+
; CHECK-NEXT: [[SEXT2:%.*]] = ashr exact i64 [[ADD]], 1
14+
; CHECK-NEXT: [[ASHR:%.*]] = or i64 [[SEXT2]], 4418419761487020032
15+
; CHECK-NEXT: ret i64 [[ASHR]]
16+
; CHECK: if.then:
17+
; CHECK-NEXT: ret i64 0
18+
;
19+
entry:
20+
%add = select i1 %cond, i64 0, i64 4294967293
21+
%add8 = shl i64 %add, 32
22+
%sext1 = add i64 %add8, 8836839514384105472
23+
%cmp = icmp ult i64 %sext1, -34359738368
24+
br i1 %cmp, label %if.then, label %if.else
25+
26+
if.else:
27+
%sext2 = or i64 %add8, 8836839522974040064
28+
%ashr = ashr i64 %sext2, 1
29+
ret i64 %ashr
30+
31+
if.then:
32+
ret i64 0
33+
}

0 commit comments

Comments
 (0)