1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -correlated-propagation -S < %s | FileCheck %s
3
4declare void @use()
5declare void @use_ptr(i8*)
6
7; test requires a mix of context sensative refinement, and analysis
8; of the originating IR pattern.  Neither part is enough in isolation.
9define void @test1(i1 %c, i1 %c2) {
10; CHECK-LABEL: @test1(
11; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], i64 -1, i64 1
12; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C2:%.*]], i64 [[SEL]], i64 0
13; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[SEL2]], 0
14; CHECK-NEXT:    br i1 [[CMP]], label [[TAKEN:%.*]], label [[UNTAKEN:%.*]]
15; CHECK:       taken:
16; CHECK-NEXT:    call void @use() [ "deopt"(i64 1) ]
17; CHECK-NEXT:    ret void
18; CHECK:       untaken:
19; CHECK-NEXT:    ret void
20;
21  %sel = select i1 %c, i64 -1, i64 1
22  %sel2 = select i1 %c2, i64 %sel, i64 0
23  %cmp = icmp sgt i64 %sel2, 0
24  br i1 %cmp, label %taken, label %untaken
25taken:
26  call void @use() ["deopt" (i64 %sel2)]
27  ret void
28untaken:
29  ret void
30}
31
32declare void @llvm.assume(i1)
33declare void @llvm.experimental.guard(i1,...)
34
35; Same as test1, but with assume not branch
36define void @test1_assume(i1 %c, i1 %c2) {
37; CHECK-LABEL: @test1_assume(
38; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], i64 -1, i64 1
39; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C2:%.*]], i64 [[SEL]], i64 0
40; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[SEL2]], 0
41; CHECK-NEXT:    call void @llvm.assume(i1 [[CMP]])
42; CHECK-NEXT:    call void @use() [ "deopt"(i64 1) ]
43; CHECK-NEXT:    ret void
44;
45  %sel = select i1 %c, i64 -1, i64 1
46  %sel2 = select i1 %c2, i64 %sel, i64 0
47  %cmp = icmp sgt i64 %sel2, 0
48  call void @llvm.assume(i1 %cmp)
49  call void @use() ["deopt" (i64 %sel2)]
50  ret void
51}
52
53; Same as test1, but with guard not branch
54define void @test1_guard(i1 %c, i1 %c2) {
55; CHECK-LABEL: @test1_guard(
56; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], i64 -1, i64 1
57; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C2:%.*]], i64 [[SEL]], i64 0
58; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[SEL2]], 0
59; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[CMP]]) [ "deopt"(i64 [[SEL2]]) ]
60; CHECK-NEXT:    call void @use() [ "deopt"(i64 1) ]
61; CHECK-NEXT:    ret void
62;
63  %sel = select i1 %c, i64 -1, i64 1
64  %sel2 = select i1 %c2, i64 %sel, i64 0
65  %cmp = icmp sgt i64 %sel2, 0
66  call void (i1, ...) @llvm.experimental.guard(i1 %cmp) ["deopt" (i64 %sel2)]
67  call void @use() ["deopt" (i64 %sel2)]
68  ret void
69}
70
71;; The rest of these are slight variations on the patterns
72;; producing 1 of several adjacent constants to test generality
73
74define void @test2(i1 %c, i1 %c2) {
75; CHECK-LABEL: @test2(
76; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], i64 0, i64 1
77; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C2:%.*]], i64 [[SEL]], i64 -1
78; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[SEL2]], 0
79; CHECK-NEXT:    br i1 [[CMP]], label [[TAKEN:%.*]], label [[UNTAKEN:%.*]]
80; CHECK:       taken:
81; CHECK-NEXT:    call void @use() [ "deopt"(i64 1) ]
82; CHECK-NEXT:    ret void
83; CHECK:       untaken:
84; CHECK-NEXT:    ret void
85;
86  %sel = select i1 %c, i64 0, i64 1
87  %sel2 = select i1 %c2, i64 %sel, i64 -1
88  %cmp = icmp sgt i64 %sel2, 0
89  br i1 %cmp, label %taken, label %untaken
90taken:
91  call void @use() ["deopt" (i64 %sel2)]
92  ret void
93untaken:
94  ret void
95}
96define void @test3(i1 %c, i1 %c2) {
97; CHECK-LABEL: @test3(
98; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], i64 0, i64 1
99; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C2:%.*]], i64 [[SEL]], i64 2
100; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[SEL2]], 1
101; CHECK-NEXT:    br i1 [[CMP]], label [[TAKEN:%.*]], label [[UNTAKEN:%.*]]
102; CHECK:       taken:
103; CHECK-NEXT:    call void @use() [ "deopt"(i64 2) ]
104; CHECK-NEXT:    ret void
105; CHECK:       untaken:
106; CHECK-NEXT:    ret void
107;
108  %sel = select i1 %c, i64 0, i64 1
109  %sel2 = select i1 %c2, i64 %sel, i64 2
110  %cmp = icmp sgt i64 %sel2, 1
111  br i1 %cmp, label %taken, label %untaken
112taken:
113  call void @use() ["deopt" (i64 %sel2)]
114  ret void
115untaken:
116  ret void
117}
118
119define void @test4(i1 %c, i1 %c2) {
120; CHECK-LABEL: @test4(
121; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[C:%.*]], i64 0, i64 1
122; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C2:%.*]], i64 0, i64 1
123; CHECK-NEXT:    [[ADD1:%.*]] = add nuw nsw i64 0, [[SEL]]
124; CHECK-NEXT:    [[ADD2:%.*]] = add nuw nsw i64 [[ADD1]], [[SEL2]]
125; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i64 [[ADD2]], 1
126; CHECK-NEXT:    br i1 [[CMP]], label [[TAKEN:%.*]], label [[UNTAKEN:%.*]]
127; CHECK:       taken:
128; CHECK-NEXT:    call void @use() [ "deopt"(i64 2) ]
129; CHECK-NEXT:    ret void
130; CHECK:       untaken:
131; CHECK-NEXT:    ret void
132;
133  %sel = select i1 %c, i64 0, i64 1
134  %sel2 = select i1 %c2, i64 0, i64 1
135  %add1 = add i64 0, %sel
136  %add2 = add i64 %add1, %sel2
137  %cmp = icmp sgt i64 %add2, 1
138  br i1 %cmp, label %taken, label %untaken
139taken:
140  call void @use() ["deopt" (i64 %add2)]
141  ret void
142untaken:
143  ret void
144}
145
146
147define void @test5(i64 %a, i8* nonnull %p) {
148; CHECK-LABEL: @test5(
149; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[A:%.*]], 0
150; CHECK-NEXT:    br i1 [[CMP]], label [[TAKEN:%.*]], label [[UNTAKEN:%.*]]
151; CHECK:       taken:
152; CHECK-NEXT:    call void @use_ptr(i8* nonnull [[P:%.*]]) [ "deopt"(i64 0) ]
153; CHECK-NEXT:    ret void
154; CHECK:       untaken:
155; CHECK-NEXT:    ret void
156;
157  %cmp = icmp eq i64 %a, 0
158  br i1 %cmp, label %taken, label %untaken
159taken:
160  call void @use_ptr(i8* %p) ["deopt" (i64 %a)]
161  ret void
162untaken:
163  ret void
164}
165