blob: 8de05335916703edbfa19d0d8498d2bc09f629ac [file] [log] [blame]
Aart Bik55b14df2016-01-12 14:12:47 -08001/*
2 * Copyright (C) 2016 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
17public class Main {
18
19 /**
20 * Method with an outer countable loop and an inner do-while loop.
21 * Since all work is done in the header of the inner loop, any invariant hoisting
22 * and deopting should be done in its proper loop preheader, not the true-block
23 * of the newly generated taken-test after dynamic BCE.
24 */
25 public static int doit(int[][] x, int j) {
26 float f = 0;
27 int acc = 0;
28 for (int i = 0; i < 2; i++) {
29 // The full body of a do-while loop is the loop header.
30 do {
31 // Some "noise" to avoid hoisting the array reference
32 // before the dynamic BCE phase runs.
33 f++;
34 // The invariant array reference with corresponding bounds check
35 // is a candidate for hoisting when dynamic BCE runs. If it is
36 // not moved to the proper loop preheader, the wrong values
37 // cause the test to fail.
38 acc += x[i][i];
39 } while (++j < i);
40 }
41 return acc;
42 }
43
44 /**
45 * Single countable loop with a clear header and a loop body. In this case,
46 * after dynamic bce, some invariant hoisting and deopting must go to the
47 * proper loop preheader and some must go to the true-block.
48 */
49 public static int foo(int[] x, int[] y, int n) {
50 float f = 0;
51 int acc = 0;
52 int i = 0;
53 while (true) {
54 // This part is the loop header.
55 // Some "noise" to avoid hoisting the array reference
56 // before the dynamic BCE phase runs.
57 f++;
58 // The invariant array reference with corresponding bounds check
59 // is a candidate for hoisting when dynamic BCE runs. If it is
60 // not moved to the proper loop preheader, the wrong values
61 // cause the test to fail.
62 acc += y[0];
63 if (++i > n)
64 break;
65 // From here on, this part is the loop body.
66 // The unit-stride array reference is a candidate for dynamic BCE.
67 // The deopting appears in the true-block.
68 acc += x[i];
69 }
70 return acc;
71 }
72
73 public static void main(String args[]) {
74 int[][] x = new int[2][2];
75 int y;
76
77 x[0][0] = 1;
78 x[1][1] = 2;
79
80 expectEquals(8, doit(x, -6));
81 expectEquals(7, doit(x, -5));
82 expectEquals(6, doit(x, -4));
83 expectEquals(5, doit(x, -3));
84 expectEquals(4, doit(x, -2));
85 expectEquals(3, doit(x, -1));
86 expectEquals(3, doit(x, 0));
87 expectEquals(3, doit(x, 1));
88 expectEquals(3, doit(x, 22));
89
90 int a[] = { 1, 2, 3, 5 };
91 int b[] = { 7 };
92
93 expectEquals(7, foo(a, b, -1));
94 expectEquals(7, foo(a, b, 0));
95 expectEquals(16, foo(a, b, 1));
96 expectEquals(26, foo(a, b, 2));
97 expectEquals(38, foo(a, b, 3));
98
99 System.out.println("passed");
100 }
101
102 private static void expectEquals(int expected, int result) {
103 if (expected != result) {
104 throw new Error("Expected: " + expected + ", found: " + result);
105 }
106 }
107}