Debian Codegraph @

test/2255-checker-branch-redirection/src/Main.java

raw
1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 public class Main {
18 public static void main(String[] args) throws Exception {
19 assertEquals(40, $noinline$testEliminateIf(20, 40));
20 assertEquals(30, $noinline$testEliminateIf(20, 10));
21 assertEquals(40, $noinline$testEliminateIfTwiceInARow(20, 40));
22 assertEquals(30, $noinline$testEliminateIfTwiceInARow(20, 10));
23 assertEquals(40, $noinline$testEliminateIfThreePredecessors(20, 40));
24 assertEquals(30, $noinline$testEliminateIfThreePredecessors(20, 10));
25 assertEquals(40, $noinline$testEliminateIfOppositeCondition(20, 40));
26 assertEquals(30, $noinline$testEliminateIfOppositeCondition(20, 10));
27 assertEquals(40, $noinline$testEliminateIfParameter(20, 40, 20 < 40));
28 assertEquals(30, $noinline$testEliminateIfParameter(20, 10, 20 < 10));
29 assertEquals(40, $noinline$testEliminateIfParameterReverseCondition(20, 40, 20 < 40));
30 assertEquals(30, $noinline$testEliminateIfParameterReverseCondition(20, 10, 20 < 10));
31 assertEquals(40, $noinline$testEliminateIfParameterOppositeCondition(20, 40, 20 < 40));
32 assertEquals(30, $noinline$testEliminateIfParameterOppositeCondition(20, 10, 20 < 10));
33 assertEquals(40, $noinline$testEliminateIfParameterOppositeCondition_2(20, 40, 20 < 40));
34 assertEquals(30, $noinline$testEliminateIfParameterOppositeCondition_2(20, 10, 20 < 10));
35 }
36
37 private static int $noinline$emptyMethod(int a) {
38 return a;
39 }
40
41 /// CHECK-START: int Main.$noinline$testEliminateIf(int, int) dead_code_elimination$after_gvn (before)
42 /// CHECK: If
43 /// CHECK: If
44
45 /// CHECK-START: int Main.$noinline$testEliminateIf(int, int) dead_code_elimination$after_gvn (after)
46 /// CHECK: If
47 /// CHECK-NOT: If
48 private static int $noinline$testEliminateIf(int a, int b) {
49 int result = 0;
50 if (a < b) {
51 $noinline$emptyMethod(a + b);
52 } else {
53 $noinline$emptyMethod(a - b);
54 }
55 if (a < b) {
56 result += $noinline$emptyMethod(a * 2);
57 } else {
58 result += $noinline$emptyMethod(b * 3);
59 }
60 return result;
61 }
62
63 /// CHECK-START: int Main.$noinline$testEliminateIfTwiceInARow(int, int) dead_code_elimination$after_gvn (before)
64 /// CHECK: If
65 /// CHECK: If
66 /// CHECK: If
67
68 /// CHECK-START: int Main.$noinline$testEliminateIfTwiceInARow(int, int) dead_code_elimination$after_gvn (after)
69 /// CHECK: If
70 /// CHECK-NOT: If
71 private static int $noinline$testEliminateIfTwiceInARow(int a, int b) {
72 int result = 0;
73 if (a < b) {
74 $noinline$emptyMethod(a + b);
75 } else {
76 $noinline$emptyMethod(a - b);
77 }
78 if (a < b) {
79 $noinline$emptyMethod(a * 2);
80 } else {
81 $noinline$emptyMethod(b * 3);
82 }
83 if (a < b) {
84 result += $noinline$emptyMethod(40);
85 } else {
86 result += $noinline$emptyMethod(30);
87 }
88 return result;
89 }
90
91 /// CHECK-START: int Main.$noinline$testEliminateIfThreePredecessors(int, int) dead_code_elimination$after_gvn (before)
92 /// CHECK: If
93 /// CHECK: If
94 /// CHECK: If
95
96 /// CHECK-START: int Main.$noinline$testEliminateIfThreePredecessors(int, int) dead_code_elimination$after_gvn (after)
97 /// CHECK: If
98 /// CHECK: If
99 /// CHECK-NOT: If
100 private static int $noinline$testEliminateIfThreePredecessors(int a, int b) {
101 int result = 0;
102 if (a < b) {
103 $noinline$emptyMethod(a + b);
104 } else {
105 if (b < 5) {
106 $noinline$emptyMethod(a - b);
107 } else {
108 $noinline$emptyMethod(a * b);
109 }
110 }
111 if (a < b) {
112 result += $noinline$emptyMethod(a * 2);
113 } else {
114 result += $noinline$emptyMethod(b * 3);
115 }
116 return result;
117 }
118
119 // Note that we can perform this optimization in dead_code_elimination$initial since we don't
120 // rely on gvn to de-duplicate the values.
121
122 /// CHECK-START: int Main.$noinline$testEliminateIfOppositeCondition(int, int) dead_code_elimination$initial (before)
123 /// CHECK: If
124 /// CHECK: If
125
126 /// CHECK-START: int Main.$noinline$testEliminateIfOppositeCondition(int, int) dead_code_elimination$initial (after)
127 /// CHECK: If
128 /// CHECK-NOT: If
129 private static int $noinline$testEliminateIfOppositeCondition(int a, int b) {
130 int result = 0;
131 if (a < b) {
132 $noinline$emptyMethod(a + b);
133 } else {
134 $noinline$emptyMethod(a - b);
135 }
136 if (a >= b) {
137 result += $noinline$emptyMethod(b * 3);
138 } else {
139 result += $noinline$emptyMethod(a * 2);
140 }
141 return result;
142 }
143
144 // In this scenario, we have a BooleanNot before the If instructions so we have to wait until
145 // the following pass to perform the optimization. The BooleanNot is dead at this time (even
146 // when starting DCE), but RemoveDeadInstructions runs after SimplifyIfs so the optimization
147 // doesn't trigger.
148
149 /// CHECK-START: int Main.$noinline$testEliminateIfParameter(int, int, boolean) dead_code_elimination$initial (before)
150 /// CHECK: BooleanNot
151 /// CHECK: If
152 /// CHECK: BooleanNot
153 /// CHECK: If
154
155 /// CHECK-START: int Main.$noinline$testEliminateIfParameter(int, int, boolean) dead_code_elimination$initial (after)
156 /// CHECK: If
157 /// CHECK: Phi
158 /// CHECK: If
159
160 /// CHECK-START: int Main.$noinline$testEliminateIfParameter(int, int, boolean) dead_code_elimination$initial (after)
161 /// CHECK-NOT: BooleanNot
162
163 /// CHECK-START: int Main.$noinline$testEliminateIfParameter(int, int, boolean) dead_code_elimination$after_gvn (before)
164 /// CHECK: If
165 /// CHECK: Phi
166 /// CHECK: If
167
168 /// CHECK-START: int Main.$noinline$testEliminateIfParameter(int, int, boolean) dead_code_elimination$after_gvn (after)
169 /// CHECK: If
170 /// CHECK-NOT: If
171 private static int $noinline$testEliminateIfParameter(int a, int b, boolean condition) {
172 int result = 0;
173 if (condition) {
174 $noinline$emptyMethod(a + b);
175 } else {
176 $noinline$emptyMethod(a - b);
177 }
178 if (condition) {
179 result += $noinline$emptyMethod(a * 2);
180 } else {
181 result += $noinline$emptyMethod(b * 3);
182 }
183 return result;
184 }
185
186 // Same in the following two cases: we do it in dead_code_elimination$initial since GVN is not
187 // needed.
188
189 /// CHECK-START: int Main.$noinline$testEliminateIfParameterReverseCondition(int, int, boolean) dead_code_elimination$initial (before)
190 /// CHECK: If
191 /// CHECK: If
192
193 /// CHECK-START: int Main.$noinline$testEliminateIfParameterReverseCondition(int, int, boolean) dead_code_elimination$initial (after)
194 /// CHECK: If
195 /// CHECK-NOT: If
196 private static int $noinline$testEliminateIfParameterReverseCondition(
197 int a, int b, boolean condition) {
198 int result = 0;
199 if (!condition) {
200 $noinline$emptyMethod(a + b);
201 } else {
202 $noinline$emptyMethod(a - b);
203 }
204 if (!condition) {
205 result += $noinline$emptyMethod(b * 3);
206 } else {
207 result += $noinline$emptyMethod(a * 2);
208 }
209 return result;
210 }
211
212 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition(int, int, boolean) dead_code_elimination$initial (before)
213 /// CHECK: If
214 /// CHECK: If
215
216 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition(int, int, boolean) dead_code_elimination$initial (after)
217 /// CHECK: If
218 /// CHECK-NOT: If
219 private static int $noinline$testEliminateIfParameterOppositeCondition(
220 int a, int b, boolean condition) {
221 int result = 0;
222 if (condition) {
223 $noinline$emptyMethod(a + b);
224 } else {
225 $noinline$emptyMethod(a - b);
226 }
227 if (!condition) {
228 result += $noinline$emptyMethod(b * 3);
229 } else {
230 result += $noinline$emptyMethod(a * 2);
231 }
232 return result;
233 }
234
235 // In this scenario, we have a BooleanNot before the If instructions so we have to wait until
236 // the following pass to perform the optimization. The BooleanNot is dead at this time (even
237 // when starting DCE), but RemoveDeadInstructions runs after SimplifyIfs so the optimization
238 // doesn't trigger.
239
240 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition_2(int, int, boolean) dead_code_elimination$initial (before)
241 /// CHECK: If
242 /// CHECK: BooleanNot
243 /// CHECK: If
244
245 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition_2(int, int, boolean) dead_code_elimination$initial (after)
246 /// CHECK: If
247 /// CHECK: Phi
248 /// CHECK: If
249
250 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition_2(int, int, boolean) dead_code_elimination$initial (after)
251 /// CHECK-NOT: BooleanNot
252
253 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition_2(int, int, boolean) dead_code_elimination$after_gvn (before)
254 /// CHECK: If
255 /// CHECK: Phi
256 /// CHECK: If
257
258 /// CHECK-START: int Main.$noinline$testEliminateIfParameterOppositeCondition_2(int, int, boolean) dead_code_elimination$after_gvn (after)
259 /// CHECK: If
260 /// CHECK-NOT: If
261 private static int $noinline$testEliminateIfParameterOppositeCondition_2(
262 int a, int b, boolean condition) {
263 int result = 0;
264 if (!condition) {
265 $noinline$emptyMethod(a + b);
266 } else {
267 $noinline$emptyMethod(a - b);
268 }
269 if (condition) {
270 result += $noinline$emptyMethod(a * 2);
271 } else {
272 result += $noinline$emptyMethod(b * 3);
273 }
274 return result;
275 }
276
277 public static void assertEquals(int expected, int result) {
278 if (expected != result) {
279 throw new Error("Expected: " + expected + ", found: " + result);
280 }
281 }
282 }