blob: 7cc0b8b6524a8c6537e4b2394b497b406f67642d [file] [log] [blame]
Aart Bik807868e2016-11-03 17:51:43 -07001/*
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
17/**
18 * Regression tests for loop optimizations.
19 */
20public class Main {
21
22 /// CHECK-START: int Main.earlyExitFirst(int) loop_optimization (before)
23 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
24 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
25 //
26 /// CHECK-START: int Main.earlyExitFirst(int) loop_optimization (after)
27 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
28 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
29 static int earlyExitFirst(int m) {
30 int k = 0;
31 for (int i = 0; i < 10; i++) {
32 if (i == m) {
33 return k;
34 }
35 k++;
36 }
37 return k;
38 }
39
40 /// CHECK-START: int Main.earlyExitLast(int) loop_optimization (before)
41 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
42 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
43 //
44 /// CHECK-START: int Main.earlyExitLast(int) loop_optimization (after)
45 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
46 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
47 static int earlyExitLast(int m) {
48 int k = 0;
49 for (int i = 0; i < 10; i++) {
50 k++;
51 if (i == m) {
52 return k;
53 }
54 }
55 return k;
56 }
57
58 /// CHECK-START: int Main.earlyExitNested() loop_optimization (before)
59 /// CHECK-DAG: Phi loop:<<Loop1:B\d+>> outer_loop:none
60 /// CHECK-DAG: Phi loop:<<Loop1>> outer_loop:none
61 /// CHECK-DAG: Phi loop:<<Loop2:B\d+>> outer_loop:<<Loop1>>
62 /// CHECK-DAG: Phi loop:<<Loop2>> outer_loop:<<Loop1>>
63 //
64 /// CHECK-START: int Main.earlyExitNested() loop_optimization (after)
65 /// CHECK-DAG: Phi loop:<<Loop1:B\d+>> outer_loop:none
66 /// CHECK-DAG: Phi loop:<<Loop1>> outer_loop:none
67 //
68 /// CHECK-START: int Main.earlyExitNested() loop_optimization (after)
69 /// CHECK-NOT: Phi loop:{{B\d+}} outer_loop:{{B\d+}}
70 static int earlyExitNested() {
71 int offset = 0;
72 for (int i = 0; i < 2; i++) {
73 int start = offset;
74 // This loop can be removed.
75 for (int j = 0; j < 2; j++) {
76 offset++;
77 }
78 if (i == 1) {
79 return start;
80 }
81 }
82 return 0;
83 }
84
Aart Bik74da5292016-12-20 11:13:03 -080085 // Regression test for b/33774618: transfer operations involving
86 // narrowing linear induction should be done correctly.
87 //
88 /// CHECK-START: int Main.transferNarrowWrap() loop_optimization (before)
89 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
90 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
91 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
92 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
93 //
94 /// CHECK-START: int Main.transferNarrowWrap() loop_optimization (after)
95 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
96 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
97 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
98 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
99 static int transferNarrowWrap() {
100 short x = 0;
101 int w = 10;
102 int v = 3;
103 for (int i = 0; i < 10; i++) {
104 v = w + 1; // transfer on wrap-around
105 w = x; // wrap-around
106 x += 2; // narrowing linear
107 }
108 return v;
109 }
110
111 // Regression test for b/33774618: transfer operations involving
112 // narrowing linear induction should be done correctly
113 // (currently rejected, could be improved).
114 //
115 /// CHECK-START: int Main.polynomialShort() loop_optimization (before)
116 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
117 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
118 //
119 /// CHECK-START: int Main.polynomialShort() loop_optimization (after)
120 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
121 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
122 static int polynomialShort() {
123 int x = 0;
124 for (short i = 0; i < 10; i++) {
125 x = x - i; // polynomial on narrowing linear
126 }
127 return x;
128 }
129
130 // Regression test for b/33774618: transfer operations involving
131 // narrowing linear induction should be done correctly
132 // (currently rejected, could be improved).
133 //
134 /// CHECK-START: int Main.polynomialIntFromLong() loop_optimization (before)
135 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
136 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
137 //
138 /// CHECK-START: int Main.polynomialIntFromLong() loop_optimization (after)
139 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
140 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
141 static int polynomialIntFromLong() {
142 int x = 0;
143 for (long i = 0; i < 10; i++) {
144 x = x - (int) i; // polynomial on narrowing linear
145 }
146 return x;
147 }
148
149 /// CHECK-START: int Main.polynomialInt() loop_optimization (before)
150 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
151 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
152 //
153 /// CHECK-START: int Main.polynomialInt() loop_optimization (after)
154 /// CHECK-NOT: Phi
155 //
156 /// CHECK-START: int Main.polynomialInt() instruction_simplifier$after_bce (after)
157 /// CHECK-DAG: <<Int:i\d+>> IntConstant -45 loop:none
158 /// CHECK-DAG: Return [<<Int>>] loop:none
159 static int polynomialInt() {
160 int x = 0;
161 for (int i = 0; i < 10; i++) {
162 x = x - i;
163 }
164 return x;
165 }
166
Aart Bik807868e2016-11-03 17:51:43 -0700167 public static void main(String[] args) {
168 expectEquals(10, earlyExitFirst(-1));
169 for (int i = 0; i <= 10; i++) {
170 expectEquals(i, earlyExitFirst(i));
171 }
172 expectEquals(10, earlyExitFirst(11));
173
174 expectEquals(10, earlyExitLast(-1));
175 for (int i = 0; i < 10; i++) {
176 expectEquals(i + 1, earlyExitLast(i));
177 }
178 expectEquals(10, earlyExitLast(10));
179 expectEquals(10, earlyExitLast(11));
180
181 expectEquals(2, earlyExitNested());
182
Aart Bik74da5292016-12-20 11:13:03 -0800183 expectEquals(17, transferNarrowWrap());
184 expectEquals(-45, polynomialShort());
185 expectEquals(-45, polynomialIntFromLong());
186 expectEquals(-45, polynomialInt());
187
Aart Bik807868e2016-11-03 17:51:43 -0700188 System.out.println("passed");
189 }
190
191 private static void expectEquals(int expected, int result) {
192 if (expected != result) {
193 throw new Error("Expected: " + expected + ", found: " + result);
194 }
195 }
196}