1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instsimplify -S | FileCheck %s
3
4define i64 @test0(i64 %x) {
5; CHECK-LABEL: @test0(
6; CHECK-NEXT:  start:
7; CHECK-NEXT:    [[A:%.*]] = icmp eq i64 [[X:%.*]], 0
8; CHECK-NEXT:    br i1 [[A]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
9; CHECK:       non_zero:
10; CHECK-NEXT:    br i1 false, label [[UNREACHABLE:%.*]], label [[EXIT]]
11; CHECK:       unreachable:
12; CHECK-NEXT:    br label [[EXIT]]
13; CHECK:       exit:
14; CHECK-NEXT:    [[C:%.*]] = phi i64 [ 0, [[START:%.*]] ], [ 1, [[NON_ZERO]] ], [ 2, [[UNREACHABLE]] ]
15; CHECK-NEXT:    ret i64 [[C]]
16;
17start:
18  %a = icmp eq i64 %x, 0
19  br i1 %a, label %exit, label %non_zero
20
21non_zero:
22  %b = icmp eq i64 %x, 0
23  br i1 %b, label %unreachable, label %exit
24
25unreachable:
26  br label %exit
27
28exit:
29  %c = phi i64 [ 0, %start ], [ 1, %non_zero ], [ 2, %unreachable ]
30  ret i64 %c
31}
32
33define i64 @test1(i64 %x) {
34; CHECK-LABEL: @test1(
35; CHECK-NEXT:  start:
36; CHECK-NEXT:    [[A:%.*]] = icmp eq i64 [[X:%.*]], 0
37; CHECK-NEXT:    br i1 [[A]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
38; CHECK:       non_zero:
39; CHECK-NEXT:    br i1 true, label [[EXIT]], label [[UNREACHABLE:%.*]]
40; CHECK:       unreachable:
41; CHECK-NEXT:    br label [[EXIT]]
42; CHECK:       exit:
43; CHECK-NEXT:    [[C:%.*]] = phi i64 [ 0, [[START:%.*]] ], [ [[X]], [[NON_ZERO]] ], [ 0, [[UNREACHABLE]] ]
44; CHECK-NEXT:    ret i64 [[C]]
45;
46start:
47  %a = icmp eq i64 %x, 0
48  br i1 %a, label %exit, label %non_zero
49
50non_zero:
51  %b = icmp ugt i64 %x, 0
52  br i1 %b, label %exit, label %unreachable
53
54unreachable:
55  br label %exit
56
57exit:
58  %c = phi i64 [ 0, %start ], [ %x, %non_zero ], [ 0, %unreachable ]
59  ret i64 %c
60}
61
62define i1 @test2(i64 %x, i1 %y) {
63; CHECK-LABEL: @test2(
64; CHECK-NEXT:  start:
65; CHECK-NEXT:    [[A:%.*]] = icmp eq i64 [[X:%.*]], 0
66; CHECK-NEXT:    br i1 [[A]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
67; CHECK:       non_zero:
68; CHECK-NEXT:    br i1 [[Y:%.*]], label [[ONE:%.*]], label [[TWO:%.*]]
69; CHECK:       one:
70; CHECK-NEXT:    br label [[MAINBLOCK:%.*]]
71; CHECK:       two:
72; CHECK-NEXT:    br label [[MAINBLOCK]]
73; CHECK:       mainblock:
74; CHECK-NEXT:    br label [[EXIT]]
75; CHECK:       exit:
76; CHECK-NEXT:    [[RES:%.*]] = phi i1 [ false, [[MAINBLOCK]] ], [ true, [[START:%.*]] ]
77; CHECK-NEXT:    ret i1 [[RES]]
78;
79start:
80  %a = icmp eq i64 %x, 0
81  br i1 %a, label %exit, label %non_zero
82
83non_zero:
84  br i1 %y, label %one, label %two
85
86one:
87  br label %mainblock
88
89two:
90  br label %mainblock
91
92mainblock:
93  %p = phi i64 [ %x, %one ], [ 42, %two ]
94  %cmp = icmp eq i64 %p, 0
95  br label %exit
96
97exit:
98  %res = phi i1 [ %cmp, %mainblock ], [ 1, %start ]
99  ret i1 %res
100}
101
102
103; The code below exposed a bug similar to the one exposed by D60846, see the commit 6ea477590085.
104; In a nutshell, we should not replace %result.0 with 0 here.
105
106define zeroext i8 @update_phi_query_loc_in_recursive_call(i8* nocapture readonly %p){
107; CHECK-LABEL: @update_phi_query_loc_in_recursive_call(
108; CHECK-NEXT:  entry:
109; CHECK-NEXT:    br label [[FOR_COND:%.*]]
110; CHECK:       for.cond:
111; CHECK-NEXT:    [[RESULT_0:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[CONV2:%.*]], [[FOR_BODY:%.*]] ]
112; CHECK-NEXT:    [[SHIFT_0:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ 1, [[FOR_BODY]] ]
113; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[SHIFT_0]], 0
114; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_COND_CLEANUP:%.*]]
115; CHECK:       for.cond.cleanup:
116; CHECK-NEXT:    ret i8 [[RESULT_0]]
117; CHECK:       for.body:
118; CHECK-NEXT:    [[TMP0:%.*]] = load i8, i8* [[P:%.*]], align 1
119; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP0]] to i32
120; CHECK-NEXT:    [[MUL:%.*]] = shl nuw nsw i32 [[SHIFT_0]], 3
121; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw i32 [[CONV]], [[MUL]]
122; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[SHL]] to i8
123; CHECK-NEXT:    [[CONV2]] = or i8 [[RESULT_0]], [[TMP1]]
124; CHECK-NEXT:    br label [[FOR_COND]]
125;
126entry:
127  br label %for.cond
128
129for.cond:                                         ; preds = %for.body, %entry
130  %result.0 = phi i8 [ 0, %entry ], [ %conv2, %for.body ]
131  %shift.0 = phi i32 [ 0, %entry ], [ 1, %for.body ]
132  %cmp = icmp eq i32 %shift.0, 0
133  br i1 %cmp, label %for.body, label %for.cond.cleanup
134
135for.cond.cleanup:                                 ; preds = %for.cond
136  ret i8 %result.0
137
138for.body:                                         ; preds = %for.cond
139  %0 = load i8, i8* %p, align 1
140  %conv = zext i8 %0 to i32
141  %mul = shl nuw nsw i32 %shift.0, 3
142  %shl = shl nuw nsw i32 %conv, %mul
143  %1 = trunc i32 %shl to i8
144  %conv2 = or i8 %result.0, %1
145  %inc = add nuw nsw i32 %shift.0, 1
146  br label %for.cond
147}
148
149define i1 @freeze_nonzero(i8 %x, i8 %mask) {
150; CHECK-LABEL: @freeze_nonzero(
151; CHECK-NEXT:    [[Y:%.*]] = or i8 [[X:%.*]], [[MASK:%.*]]
152; CHECK-NEXT:    [[C:%.*]] = icmp ne i8 [[Y]], 0
153; CHECK-NEXT:    br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
154; CHECK:       A:
155; CHECK-NEXT:    ret i1 false
156; CHECK:       B:
157; CHECK-NEXT:    ret i1 false
158;
159  %y = or i8 %x, %mask
160  %c = icmp ne i8 %y, 0
161  br i1 %c, label %A, label %B
162A:
163  %fr = freeze i8 %y
164  %c2 = icmp eq i8 %fr, 0
165  ret i1 %c2
166B:
167  ret i1 0
168}
169