summaryrefslogtreecommitdiff
path: root/test/2244-checker-remove-try-boundary/src/Main.java
blob: 1b616a5ecc3ec37ff338e255f77a5bda6d521a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
  public static void main(String[] args) throws Exception {
    assertEquals(2, $noinline$testDivideOverTen(20));
    assertEquals(-2, $noinline$testDivideOverTen(-20));
    assertEquals(0, $noinline$testSimpleDivisionInLoop(0));
    assertEquals(1, $noinline$testSimpleDivisionInLoop(81));
    assertEquals(10, $noinline$testOptimizeSeparateBranches(60, true));
    assertEquals(10, $noinline$testOptimizeSeparateBranches(80, false));
    assertEquals(1, $noinline$testDoNotOptimizeOneBranchThrows(81, false));
    assertEquals(-1000, $noinline$testDoNotOptimizeOneBranchThrows(81, true));
    assertEquals(1, $noinline$testOptimizeAfterOneBranchDisappears(81, false));
    assertEquals(10, $noinline$testRemoveTryBoundaryNested(60));
    assertEquals(-2000, $noinline$testRemoveTryBoundaryNestedButNotCatch(60, true));
    assertEquals(30, $noinline$testRemoveTryBoundaryNestedButNotCatch(60, false));
    assertEquals(30, $noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(60, false));
  }

  public static void assertEquals(int expected, int result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  // Check that this version cannot remove the TryBoundary instructions since we may throw.

  /// CHECK-START: int Main.$inline$division(int, int) register (after)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$inline$division(int, int) register (after)
  /// CHECK:     flags "catch_block"
  private static int $inline$division(int a, int b) {
    try {
      return a / b;
    } catch (Error unexpected) {
      return -1000;
    }
  }

  // Check that we can remove the TryBoundary afer inlining since we know we can't throw.

  /// CHECK-START: int Main.$noinline$testDivideOverTen(int) inliner (after)
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testDivideOverTen(int) inliner (after)
  /// CHECK-NOT: flags "catch_block"
  private static int $noinline$testDivideOverTen(int a) {
    return $inline$division(a, 10);
  }

  /// CHECK-START: int Main.$noinline$testSimpleDivisionInLoop(int) dead_code_elimination$initial (before)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testSimpleDivisionInLoop(int) dead_code_elimination$initial (before)
  /// CHECK:     flags "catch_block"

  /// CHECK-START: int Main.$noinline$testSimpleDivisionInLoop(int) dead_code_elimination$initial (after)
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testSimpleDivisionInLoop(int) dead_code_elimination$initial (after)
  /// CHECK-NOT: flags "catch_block"
  private static int $noinline$testSimpleDivisionInLoop(int a) {
    try {
      for (int i = 0; i < 4; i++) {
        a /= 3;
      }
    } catch (Error unexpected) {
      return -1000;
    }
    return a;
  }

  // Even though the `TryBoundary`s are split, we can remove them as nothing in the try can throw.

  /// CHECK-START: int Main.$noinline$testOptimizeSeparateBranches(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testOptimizeSeparateBranches(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"

  /// CHECK-START: int Main.$noinline$testOptimizeSeparateBranches(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testOptimizeSeparateBranches(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT: flags "catch_block"
  private static int $noinline$testOptimizeSeparateBranches(int a, boolean val) {
    try {
      if (val) {
        // TryBoundary kind:entry
        a /= 3;
      } else {
        // TryBoundary kind:entry
        a /= 4;
      }
      a /= 2;
      // TryBoundary kind:exit
    } catch (Error unexpected) {
      return -1000;
    }
    return a;
  }

  // Even though the `a /= 3;` can't throw, we don't eliminate any `TryBoundary` instructions. This
  // is because we have the `throw new Error();` in the try as well. We could potentially support
  // removing some `TryBoundary` instructions and not all in the try, but this would complicate the
  // code and wouldn't bring code size reductions since we would be unable to remove the catch
  // block.

  /// CHECK-START: int Main.$noinline$testDoNotOptimizeOneBranchThrows(int, boolean) register (after)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testDoNotOptimizeOneBranchThrows(int, boolean) register (after)
  /// CHECK:     flags "catch_block"
  public static int $noinline$testDoNotOptimizeOneBranchThrows(int a, boolean val) {
    try {
      for (int i = 0; i < 4; i++) {
        // TryBoundary kind:entry
        a /= 3;
        // TryBoundary kind:exit
      }

      if (val) {
        // TryBoundary kind:entry
        throw new Error();
        // TryBoundary kind:exit
      }
    } catch (Error e) {
      return -1000;
    }
    return a;
  }

  // The throw gets eliminated by `SimplifyIfs` in DCE, so we can detect that nothing can throw in
  // the graph and eliminate the `TryBoundary` instructions.

  /// CHECK-START: int Main.$noinline$testOptimizeAfterOneBranchDisappears(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     Throw

  /// CHECK-START: int Main.$noinline$testOptimizeAfterOneBranchDisappears(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testOptimizeAfterOneBranchDisappears(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"

  /// CHECK-START: int Main.$noinline$testOptimizeAfterOneBranchDisappears(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT: Throw

  /// CHECK-START: int Main.$noinline$testOptimizeAfterOneBranchDisappears(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testOptimizeAfterOneBranchDisappears(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT: flags "catch_block"
  public static int $noinline$testOptimizeAfterOneBranchDisappears(int a, boolean val) {
    try {
      for (int i = 0; i < 4; i++) {
        // TryBoundary kind:entry
        a /= 3;
        // TryBoundary kind:exit
      }

      if (val && !val) {
        // TryBoundary kind:entry
        throw new Error();
        // TryBoundary kind:exit
      }
    } catch (Error e) {
      return -1000;
    }
    return a;
  }

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNested(int) dead_code_elimination$initial (before)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNested(int) dead_code_elimination$initial (before)
  /// CHECK:     flags "catch_block"
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNested(int) dead_code_elimination$initial (after)
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNested(int) dead_code_elimination$initial (after)
  /// CHECK-NOT: flags "catch_block"
  public static int $noinline$testRemoveTryBoundaryNested(int a) {
    try {
      // TryBoundary kind:entry
      a /= 2;
      // TryBoundary kind:exit
      try {
        // TryBoundary kind:entry
        a /= 3;
        // TryBoundary kind:exit
      } catch (Error e) {
        return -2000;
      }
    } catch (Exception e) {
      return -1000;
    }
    return a;
  }

  // We can remove the `TryBoundary` instructions surrounding `a /= 2;` but since the inner try can
  // throw, we must keep both the inner and outer catches as they are catch handlers of the inner
  // try.

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNestedButNotCatch(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNestedButNotCatch(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     flags "catch_block"
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNestedButNotCatch(int, boolean) dead_code_elimination$initial (after)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testRemoveTryBoundaryNestedButNotCatch(int, boolean) dead_code_elimination$initial (after)
  /// CHECK:     flags "catch_block"
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"
  public static int $noinline$testRemoveTryBoundaryNestedButNotCatch(int a, boolean val) {
    try {
      // TryBoundary kind:entry
      a /= 2;
      // TryBoundary kind:exit
      try {
        if (val) {
          // TryBoundary kind:entry
          throw new Error();
          // TryBoundary kind:exit
        }
        // TryBoundary kind:exit
      } catch (Error e) {
        return -2000;
      }
    } catch (Exception e) {
      return -1000;
    }
    return a;
  }

  // We eliminate the return -1000 catch block which is outside of the loop in
  // dead_code_elimination$initial. We can do so since we eliminated the TryBoundary of `a /= 2;`.

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     flags "catch_block"
  /// CHECK:     flags "catch_block"
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     IntConstant -1000

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (after)
  /// CHECK:     TryBoundary
  /// CHECK:     TryBoundary
  /// CHECK-NOT: TryBoundary

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (after)
  /// CHECK:     flags "catch_block"
  /// CHECK:     flags "catch_block"
  /// CHECK-NOT: flags "catch_block"

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT: IntConstant -1000

  // When removing that block, we are removing a block outside of a loop but we still need to update
  // the loop information in the graph since we removed TryBoundary instructions inside of a loop
  // and now `a /= 2;` is not considered part of a loop (Cannot throw so it will not `continue` and
  // will always return).

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (before)
  /// CHECK:     Div loop:B2

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (after)
  /// CHECK-NOT:  Div loop:B2

  /// CHECK-START: int Main.$noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(int, boolean) dead_code_elimination$initial (after)
  /// CHECK:      Div
  /// CHECK-NOT:  Div
  public static int $noinline$testNestedTryBoundariesWithLoopAndCatchOutsideOfLoop(
          int a, boolean val) {
    try {
      for (int i = 0; i < 4; ++i) {
        try {
          try {
            if (val) {
              // TryBoundary kind:entry
              throw new Error();
              // TryBoundary kind:exit
            }
            // TryBoundary kind:exit
          } catch (Exception e) {
              continue;
          }
          // TryBoundary kind:entry
          a /= 2;
          // TryBoundary kind:exit
          return a;
        } catch (Error e) {
          continue;
        }
      }
    } catch (Exception e) {
      return -1000;
    }
    return a;
  }
}