• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -analyze -branch-prob | FileCheck %s
2; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
3
4define i32 @test1(i32 %i, i32* %a) {
5; CHECK: Printing analysis {{.*}} for function 'test1'
6entry:
7  br label %body
8; CHECK: edge entry -> body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
9
10body:
11  %iv = phi i32 [ 0, %entry ], [ %next, %body ]
12  %base = phi i32 [ 0, %entry ], [ %sum, %body ]
13  %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
14  %0 = load i32, i32* %arrayidx
15  %sum = add nsw i32 %0, %base
16  %next = add i32 %iv, 1
17  %exitcond = icmp eq i32 %next, %i
18  br i1 %exitcond, label %exit, label %body
19; CHECK: edge body -> exit probability is 0x04000000 / 0x80000000 = 3.12%
20; CHECK: edge body -> body probability is 0x7c000000 / 0x80000000 = 96.88% [HOT edge]
21
22exit:
23  ret i32 %sum
24}
25
26define i32 @test2(i32 %i, i32 %a, i32 %b) {
27; CHECK: Printing analysis {{.*}} for function 'test2'
28entry:
29  %cond = icmp ult i32 %i, 42
30  br i1 %cond, label %then, label %else, !prof !0
31; CHECK: edge entry -> then probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
32; CHECK: edge entry -> else probability is 0x07878788 / 0x80000000 = 5.88%
33
34then:
35  br label %exit
36; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
37
38else:
39  br label %exit
40; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
41
42exit:
43  %result = phi i32 [ %a, %then ], [ %b, %else ]
44  ret i32 %result
45}
46
47!0 = !{!"branch_weights", i32 64, i32 4}
48
49define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
50; CHECK: Printing analysis {{.*}} for function 'test3'
51entry:
52  switch i32 %i, label %case_a [ i32 1, label %case_b
53                                 i32 2, label %case_c
54                                 i32 3, label %case_d
55                                 i32 4, label %case_e ], !prof !1
56; CHECK: edge entry -> case_a probability is 0x06666666 / 0x80000000 = 5.00%
57; CHECK: edge entry -> case_b probability is 0x06666666 / 0x80000000 = 5.00%
58; CHECK: edge entry -> case_c probability is 0x66666666 / 0x80000000 = 80.00%
59; CHECK: edge entry -> case_d probability is 0x06666666 / 0x80000000 = 5.00%
60; CHECK: edge entry -> case_e probability is 0x06666666 / 0x80000000 = 5.00%
61
62case_a:
63  br label %exit
64; CHECK: edge case_a -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
65
66case_b:
67  br label %exit
68; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
69
70case_c:
71  br label %exit
72; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
73
74case_d:
75  br label %exit
76; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
77
78case_e:
79  br label %exit
80; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
81
82exit:
83  %result = phi i32 [ %a, %case_a ],
84                    [ %b, %case_b ],
85                    [ %c, %case_c ],
86                    [ %d, %case_d ],
87                    [ %e, %case_e ]
88  ret i32 %result
89}
90
91!1 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
92
93define i32 @test4(i32 %x) nounwind uwtable readnone ssp {
94; CHECK: Printing analysis {{.*}} for function 'test4'
95entry:
96  %conv = sext i32 %x to i64
97  switch i64 %conv, label %return [
98    i64 0, label %sw.bb
99    i64 1, label %sw.bb
100    i64 2, label %sw.bb
101    i64 5, label %sw.bb1
102  ], !prof !2
103; CHECK: edge entry -> return probability is 0x0a8a8a8b / 0x80000000 = 8.24%
104; CHECK: edge entry -> sw.bb probability is 0x15151515 / 0x80000000 = 16.47%
105; CHECK: edge entry -> sw.bb1 probability is 0x60606060 / 0x80000000 = 75.29%
106
107sw.bb:
108  br label %return
109
110sw.bb1:
111  br label %return
112
113return:
114  %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ]
115  ret i32 %retval.0
116}
117
118!2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64}
119
120declare void @coldfunc() cold
121
122define i32 @test5(i32 %a, i32 %b, i1 %flag) {
123; CHECK: Printing analysis {{.*}} for function 'test5'
124entry:
125  br i1 %flag, label %then, label %else
126; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88%
127; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
128
129then:
130  call void @coldfunc()
131  br label %exit
132; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
133
134else:
135  br label %exit
136; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
137
138exit:
139  %result = phi i32 [ %a, %then ], [ %b, %else ]
140  ret i32 %result
141}
142
143declare i32 @regular_function(i32 %i)
144
145define i32 @test_cold_call_sites(i32* %a) {
146; Test that edges to blocks post-dominated by cold call sites
147; are marked as not expected to be taken.
148; TODO(dnovillo) The calls to regular_function should not be merged, but
149; they are currently being merged. Convert this into a code generation test
150; after that is fixed.
151
152; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites'
153; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88%
154; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
155
156entry:
157  %gep1 = getelementptr i32, i32* %a, i32 1
158  %val1 = load i32, i32* %gep1
159  %cond1 = icmp ugt i32 %val1, 1
160  br i1 %cond1, label %then, label %else
161
162then:
163  ; This function is not declared cold, but this call site is.
164  %val4 = call i32 @regular_function(i32 %val1) cold
165  br label %exit
166
167else:
168  %gep2 = getelementptr i32, i32* %a, i32 2
169  %val2 = load i32, i32* %gep2
170  %val3 = call i32 @regular_function(i32 %val2)
171  br label %exit
172
173exit:
174  %ret = phi i32 [ %val4, %then ], [ %val3, %else ]
175  ret i32 %ret
176}
177
178define i32 @zero1(i32 %i, i32 %a, i32 %b) {
179; CHECK: Printing analysis {{.*}} for function 'zero1'
180entry:
181  %cond = icmp eq i32 %i, 0
182  br i1 %cond, label %then, label %else
183; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
184; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
185
186then:
187  br label %exit
188
189else:
190  br label %exit
191
192exit:
193  %result = phi i32 [ %a, %then ], [ %b, %else ]
194  ret i32 %result
195}
196
197define i32 @zero2(i32 %i, i32 %a, i32 %b) {
198; CHECK: Printing analysis {{.*}} for function 'zero2'
199entry:
200  %cond = icmp ne i32 %i, -1
201  br i1 %cond, label %then, label %else
202; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
203; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
204
205then:
206  br label %exit
207
208else:
209  br label %exit
210
211exit:
212  %result = phi i32 [ %a, %then ], [ %b, %else ]
213  ret i32 %result
214}
215
216define i32 @zero3(i32 %i, i32 %a, i32 %b) {
217; CHECK: Printing analysis {{.*}} for function 'zero3'
218entry:
219; AND'ing with a single bit bitmask essentially leads to a bool comparison,
220; meaning we don't have probability information.
221  %and = and i32 %i, 2
222  %tobool = icmp eq i32 %and, 0
223  br i1 %tobool, label %then, label %else
224; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
225; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
226
227then:
228; AND'ing with other bitmask might be something else, so we still assume the
229; usual probabilities.
230  %and2 = and i32 %i, 5
231  %tobool2 = icmp eq i32 %and2, 0
232  br i1 %tobool2, label %else, label %exit
233; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50%
234; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50%
235
236else:
237  br label %exit
238
239exit:
240  %result = phi i32 [ %a, %then ], [ %b, %else ]
241  ret i32 %result
242}
243
244