xref: /llvm-project/llvm/test/Transforms/BDCE/invalidate-assumptions.ll (revision cb6240d247b3419dea29eb99261171ea239b1c5c)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2; RUN: opt -passes=bdce %s -S | FileCheck %s
3
4; The 'nuw' on the subtract allows us to deduce that %setbit is not demanded.
5; But if we change that value to '0', then the 'nuw' is no longer valid. If we don't
6; remove the 'nuw', another pass (-instcombine) may make a transform based on an
7; that incorrect assumption and we can miscompile:
8; https://bugs.llvm.org/show_bug.cgi?id=33695
9
10define i1 @PR33695(i1 %b, i8 %x) {
11; CHECK-LABEL: define i1 @PR33695(
12; CHECK-SAME: i1 [[B:%.*]], i8 [[X:%.*]]) {
13; CHECK-NEXT:    [[LITTLE_NUMBER:%.*]] = zext i1 [[B]] to i8
14; CHECK-NEXT:    [[BIG_NUMBER:%.*]] = shl i8 0, 1
15; CHECK-NEXT:    [[SUB:%.*]] = sub i8 [[BIG_NUMBER]], [[LITTLE_NUMBER]]
16; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i8 [[SUB]] to i1
17; CHECK-NEXT:    ret i1 [[TRUNC]]
18;
19  %setbit = or i8 %x, 64
20  %little_number = zext i1 %b to i8
21  %big_number = shl i8 %setbit, 1
22  %sub = sub nuw i8 %big_number, %little_number
23  %trunc = trunc i8 %sub to i1
24  ret i1 %trunc
25}
26
27; Similar to above, but now with more no-wrap.
28; https://bugs.llvm.org/show_bug.cgi?id=34037
29
30define i64 @PR34037(i64 %m, i32 %r, i64 %j, i1 %b, i32 %k, i64 %p) {
31; CHECK-LABEL: define i64 @PR34037(
32; CHECK-SAME: i64 [[M:%.*]], i32 [[R:%.*]], i64 [[J:%.*]], i1 [[B:%.*]], i32 [[K:%.*]], i64 [[P:%.*]]) {
33; CHECK-NEXT:    [[SHL:%.*]] = shl i64 0, 29
34; CHECK-NEXT:    [[CONV1:%.*]] = select i1 [[B]], i64 7, i64 0
35; CHECK-NEXT:    [[SUB:%.*]] = sub i64 [[SHL]], [[CONV1]]
36; CHECK-NEXT:    [[CONV2:%.*]] = zext i32 [[K]] to i64
37; CHECK-NEXT:    [[MUL:%.*]] = mul i64 [[SUB]], [[CONV2]]
38; CHECK-NEXT:    [[CONV4:%.*]] = and i64 [[P]], 65535
39; CHECK-NEXT:    [[AND5:%.*]] = and i64 [[MUL]], [[CONV4]]
40; CHECK-NEXT:    ret i64 [[AND5]]
41;
42  %conv = zext i32 %r to i64
43  %and = and i64 %m, %conv
44  %neg = xor i64 %and, 34359738367
45  %or = or i64 %j, %neg
46  %shl = shl i64 %or, 29
47  %conv1 = select i1 %b, i64 7, i64 0
48  %sub = sub nuw nsw i64 %shl, %conv1
49  %conv2 = zext i32 %k to i64
50  %mul = mul nsw i64 %sub, %conv2
51  %conv4 = and i64 %p, 65535
52  %and5 = and i64 %mul, %conv4
53  ret i64 %and5
54}
55
56; This is a manufactured example based on the 1st test to prove that the
57; assumption-killing algorithm stops at the call. Ie, it does not remove
58; nsw/nuw from the 'add' because a call demands all bits of its argument.
59
60declare i1 @foo(i1)
61
62define i1 @poison_on_call_user_is_ok(i1 %b, i8 %x) {
63; CHECK-LABEL: define i1 @poison_on_call_user_is_ok(
64; CHECK-SAME: i1 [[B:%.*]], i8 [[X:%.*]]) {
65; CHECK-NEXT:    [[LITTLE_NUMBER:%.*]] = zext i1 [[B]] to i8
66; CHECK-NEXT:    [[BIG_NUMBER:%.*]] = shl i8 0, 1
67; CHECK-NEXT:    [[SUB:%.*]] = sub i8 [[BIG_NUMBER]], [[LITTLE_NUMBER]]
68; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i8 [[SUB]] to i1
69; CHECK-NEXT:    [[CALL_RESULT:%.*]] = call i1 @foo(i1 [[TRUNC]])
70; CHECK-NEXT:    [[ADD:%.*]] = add nuw nsw i1 [[CALL_RESULT]], true
71; CHECK-NEXT:    [[MUL:%.*]] = mul i1 [[TRUNC]], [[ADD]]
72; CHECK-NEXT:    ret i1 [[MUL]]
73;
74  %setbit = or i8 %x, 64
75  %little_number = zext i1 %b to i8
76  %big_number = shl i8 %setbit, 1
77  %sub = sub nuw i8 %big_number, %little_number
78  %trunc = trunc i8 %sub to i1
79  %call_result = call i1 @foo(i1 %trunc)
80  %add = add nsw nuw i1 %call_result, 1
81  %mul = mul i1 %trunc, %add
82  ret i1 %mul
83}
84
85
86; We were asserting that all users of a trivialized integer-type instruction were
87; also integer-typed, but that's too strong. The alloca has a pointer-type result.
88
89define void @PR34179(ptr %a) {
90; CHECK-LABEL: define void @PR34179(
91; CHECK-SAME: ptr [[A:%.*]]) {
92; CHECK-NEXT:    [[T0:%.*]] = load volatile i32, ptr [[A]], align 4
93; CHECK-NEXT:    ret void
94;
95  %t0 = load volatile i32, ptr %a
96  %vla = alloca i32, i32 %t0
97  ret void
98}
99
100define i64 @disjoint(i64 %x) {
101; CHECK-LABEL: define i64 @disjoint(
102; CHECK-SAME: i64 [[X:%.*]]) {
103; CHECK-NEXT:    [[OR:%.*]] = or i64 [[X]], -2
104; CHECK-NEXT:    ret i64 [[OR]]
105;
106  %and = and i64 %x, 1
107  %or = or disjoint i64 %and, -2
108  ret i64 %or
109}
110
111define i32 @disjoint_indirect(i64 %x) {
112; CHECK-LABEL: define i32 @disjoint_indirect(
113; CHECK-SAME: i64 [[X:%.*]]) {
114; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i64 [[X]] to i32
115; CHECK-NEXT:    [[OR:%.*]] = or i32 [[TRUNC]], -2
116; CHECK-NEXT:    ret i32 [[OR]]
117;
118  %and = and i64 %x, 1
119  %trunc = trunc i64 %and to i32
120  %or = or disjoint i32 %trunc, -2
121  ret i32 %or
122}
123
124define i32 @range(i32 %x) {
125; CHECK-LABEL: define i32 @range(
126; CHECK-SAME: i32 [[X:%.*]]) {
127; CHECK-NEXT:    [[UMIN:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 100)
128; CHECK-NEXT:    [[AND:%.*]] = and i32 [[UMIN]], -2
129; CHECK-NEXT:    ret i32 [[AND]]
130;
131  %or = or i32 %x, 1
132  %umin = call i32 @llvm.umin.i32(i32 %or, i32 100), !range !{i32 1, i32 101}
133  %and = and i32 %umin, -2
134  ret i32 %and
135}
136