blob: 87459e2fa641ac722b86c528da223ab97aadde13 [file] [log] [blame]
David Brazdil68427b12015-01-07 19:22:04 +00001/*
2* Copyright (C) 2014 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
David Brazdil4846d132015-01-15 19:07:08 +000017public class Main {
David Brazdil9a6f20e2014-12-19 11:17:21 +000018
David Brazdila06d66a2015-05-28 11:14:54 +010019 /// CHECK-START: void Main.InlineVoid() inliner (before)
20 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42
21 /// CHECK-DAG: InvokeStaticOrDirect
Nicolas Geoffray94015b92015-06-04 18:21:04 +010022 /// CHECK-DAG: InvokeStaticOrDirect [<<Const42>>,{{[ij]\d+}}]
David Brazdil9a6f20e2014-12-19 11:17:21 +000023
David Brazdila06d66a2015-05-28 11:14:54 +010024 /// CHECK-START: void Main.InlineVoid() inliner (after)
25 /// CHECK-NOT: InvokeStaticOrDirect
David Brazdil9a6f20e2014-12-19 11:17:21 +000026
27 public static void InlineVoid() {
28 returnVoid();
29 returnVoidWithOneParameter(42);
30 }
31
David Brazdila06d66a2015-05-28 11:14:54 +010032 /// CHECK-START: int Main.InlineParameter(int) inliner (before)
33 /// CHECK-DAG: <<Param:i\d+>> ParameterValue
Nicolas Geoffray94015b92015-06-04 18:21:04 +010034 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect [<<Param>>,{{[ij]\d+}}]
David Brazdila06d66a2015-05-28 11:14:54 +010035 /// CHECK-DAG: Return [<<Result>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000036
David Brazdila06d66a2015-05-28 11:14:54 +010037 /// CHECK-START: int Main.InlineParameter(int) inliner (after)
38 /// CHECK-DAG: <<Param:i\d+>> ParameterValue
39 /// CHECK-DAG: Return [<<Param>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000040
41 public static int InlineParameter(int a) {
42 return returnParameter(a);
43 }
44
David Brazdila06d66a2015-05-28 11:14:54 +010045 /// CHECK-START: long Main.InlineWideParameter(long) inliner (before)
46 /// CHECK-DAG: <<Param:j\d+>> ParameterValue
Nicolas Geoffray94015b92015-06-04 18:21:04 +010047 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect [<<Param>>,{{[ij]\d+}}]
David Brazdila06d66a2015-05-28 11:14:54 +010048 /// CHECK-DAG: Return [<<Result>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000049
David Brazdila06d66a2015-05-28 11:14:54 +010050 /// CHECK-START: long Main.InlineWideParameter(long) inliner (after)
51 /// CHECK-DAG: <<Param:j\d+>> ParameterValue
52 /// CHECK-DAG: Return [<<Param>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000053
54 public static long InlineWideParameter(long a) {
55 return returnWideParameter(a);
56 }
57
David Brazdila06d66a2015-05-28 11:14:54 +010058 /// CHECK-START: java.lang.Object Main.InlineReferenceParameter(java.lang.Object) inliner (before)
59 /// CHECK-DAG: <<Param:l\d+>> ParameterValue
Nicolas Geoffray94015b92015-06-04 18:21:04 +010060 /// CHECK-DAG: <<Result:l\d+>> InvokeStaticOrDirect [<<Param>>,{{[ij]\d+}}]
David Brazdila06d66a2015-05-28 11:14:54 +010061 /// CHECK-DAG: Return [<<Result>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000062
David Brazdila06d66a2015-05-28 11:14:54 +010063 /// CHECK-START: java.lang.Object Main.InlineReferenceParameter(java.lang.Object) inliner (after)
64 /// CHECK-DAG: <<Param:l\d+>> ParameterValue
65 /// CHECK-DAG: Return [<<Param>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000066
67 public static Object InlineReferenceParameter(Object o) {
68 return returnReferenceParameter(o);
69 }
70
David Brazdila06d66a2015-05-28 11:14:54 +010071 /// CHECK-START: int Main.InlineInt() inliner (before)
72 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect
73 /// CHECK-DAG: Return [<<Result>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000074
David Brazdila06d66a2015-05-28 11:14:54 +010075 /// CHECK-START: int Main.InlineInt() inliner (after)
76 /// CHECK-DAG: <<Const4:i\d+>> IntConstant 4
77 /// CHECK-DAG: Return [<<Const4>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000078
79 public static int InlineInt() {
80 return returnInt();
81 }
82
David Brazdila06d66a2015-05-28 11:14:54 +010083 /// CHECK-START: long Main.InlineWide() inliner (before)
84 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect
85 /// CHECK-DAG: Return [<<Result>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000086
David Brazdila06d66a2015-05-28 11:14:54 +010087 /// CHECK-START: long Main.InlineWide() inliner (after)
88 /// CHECK-DAG: <<Const8:j\d+>> LongConstant 8
89 /// CHECK-DAG: Return [<<Const8>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +000090
91 public static long InlineWide() {
92 return returnWide();
93 }
94
David Brazdila06d66a2015-05-28 11:14:54 +010095 /// CHECK-START: int Main.InlineAdd() inliner (before)
96 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
97 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
98 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect
99 /// CHECK-DAG: Return [<<Result>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +0000100
David Brazdila06d66a2015-05-28 11:14:54 +0100101 /// CHECK-START: int Main.InlineAdd() inliner (after)
102 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
103 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
104 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const3>>,<<Const5>>]
105 /// CHECK-DAG: Return [<<Add>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +0000106
107 public static int InlineAdd() {
108 return returnAdd(3, 5);
109 }
110
David Brazdila06d66a2015-05-28 11:14:54 +0100111 /// CHECK-START: int Main.InlineFieldAccess() inliner (before)
112 /// CHECK-DAG: <<After:i\d+>> InvokeStaticOrDirect
113 /// CHECK-DAG: Return [<<After>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +0000114
David Brazdila06d66a2015-05-28 11:14:54 +0100115 /// CHECK-START: int Main.InlineFieldAccess() inliner (after)
116 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
117 /// CHECK-DAG: <<Before:i\d+>> StaticFieldGet
118 /// CHECK-DAG: <<After:i\d+>> Add [<<Before>>,<<Const1>>]
119 /// CHECK-DAG: StaticFieldSet [{{l\d+}},<<After>>]
120 /// CHECK-DAG: Return [<<After>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +0000121
David Brazdila06d66a2015-05-28 11:14:54 +0100122 /// CHECK-START: int Main.InlineFieldAccess() inliner (after)
123 /// CHECK-NOT: InvokeStaticOrDirect
David Brazdil9a6f20e2014-12-19 11:17:21 +0000124
125 public static int InlineFieldAccess() {
126 return incCounter();
127 }
128
David Brazdila06d66a2015-05-28 11:14:54 +0100129 /// CHECK-START: int Main.InlineWithControlFlow(boolean) inliner (before)
130 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
131 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
132 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100133 /// CHECK-DAG: <<Add:i\d+>> InvokeStaticOrDirect [<<Const1>>,<<Const3>>,{{[ij]\d+}}]
134 /// CHECK-DAG: <<Sub:i\d+>> InvokeStaticOrDirect [<<Const5>>,<<Const3>>,{{[ij]\d+}}]
David Brazdila06d66a2015-05-28 11:14:54 +0100135 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Add>>,<<Sub>>]
136 /// CHECK-DAG: Return [<<Phi>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +0000137
David Brazdila06d66a2015-05-28 11:14:54 +0100138 /// CHECK-START: int Main.InlineWithControlFlow(boolean) inliner (after)
139 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
140 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
141 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
142 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const1>>,<<Const3>>]
143 /// CHECK-DAG: <<Sub:i\d+>> Sub [<<Const5>>,<<Const3>>]
144 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Add>>,<<Sub>>]
145 /// CHECK-DAG: Return [<<Phi>>]
David Brazdil9a6f20e2014-12-19 11:17:21 +0000146
147 public static int InlineWithControlFlow(boolean cond) {
148 int x, const1, const3, const5;
149 const1 = 1;
150 const3 = 3;
151 const5 = 5;
152 if (cond) {
153 x = returnAdd(const1, const3);
154 } else {
155 x = returnSub(const5, const3);
156 }
157 return x;
158 }
159
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100160 /// CHECK-START: int Main.returnAbs(int) intrinsics_recognition (before)
161 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect
162 /// CHECK-DAG: Return [<<Result>>]
163
164 /// CHECK-START: int Main.returnAbs(int) intrinsics_recognition (after)
165 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:MathAbsInt
166 /// CHECK-DAG: Return [<<Result>>]
167
168 private static int returnAbs(int i) {
169 return Math.abs(i);
170 }
171
172 /// CHECK-START: int Main.InlinedIntrinsicsAreStillIntrinsic() inliner (before)
173 /// CHECK-DAG: <<ConstMinus1:i\d+>> IntConstant -1
174 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect
175 /// CHECK-DAG: Return [<<Result>>]
176
177 /// CHECK-START: int Main.InlinedIntrinsicsAreStillIntrinsic() inliner (after)
178 /// CHECK-DAG: <<ConstMinus1:i\d+>> IntConstant -1
179 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:MathAbsInt
180 /// CHECK-DAG: Return [<<Result>>]
181
182 public static int InlinedIntrinsicsAreStillIntrinsic() {
183 return returnAbs(-1);
184 }
David Brazdil9a6f20e2014-12-19 11:17:21 +0000185
186 private static void returnVoid() {
187 return;
188 }
189
190 private static void returnVoidWithOneParameter(int a) {
191 return;
192 }
193
194 private static int returnParameter(int a) {
195 return a;
196 }
197
198 private static long returnWideParameter(long a) {
199 return a;
200 }
201
202 private static Object returnReferenceParameter(Object o) {
203 return o;
204 }
205
206 private static int returnInt() {
207 return 4;
208 }
209
210 private static long returnWide() {
211 return 8L;
212 }
213
214 private static int returnAdd(int a, int b) {
215 return a + b;
216 }
217
218 private static int returnSub(int a, int b) {
219 return a - b;
220 }
221
222 private static int counter = 42;
223
224 private static int incCounter() {
225 return ++counter;
226 }
David Brazdil4846d132015-01-15 19:07:08 +0000227
228 public static void main(String[] args) {
229 InlineVoid();
230
231 if (InlineInt() != 4) {
232 throw new Error();
233 }
234
235 if (InlineWide() != 8L) {
236 throw new Error();
237 }
238
239 if (InlineParameter(42) != 42) {
240 throw new Error();
241 }
242
243 if (InlineWideParameter(0x100000001L) != 0x100000001L) {
244 throw new Error();
245 }
246
247 if (InlineReferenceParameter(Main.class) != Main.class) {
248 throw new Error();
249 }
250
251 if (InlineAdd() != 8) {
252 throw new Error();
253 }
254
255 if (InlineFieldAccess() != 43 || InlineFieldAccess() != 44) {
256 throw new Error();
257 }
258
259 if (InlineWithControlFlow(true) != 4) {
260 throw new Error();
261 }
262
263 if (InlineWithControlFlow(false) != 2) {
264 throw new Error();
265 }
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100266
267 if (InlinedIntrinsicsAreStillIntrinsic() != 1) {
268 throw new Error();
269 }
270
271 if (returnAbs(-1) != 1) {
272 throw new Error();
273 }
David Brazdil4846d132015-01-15 19:07:08 +0000274 }
David Brazdil9a6f20e2014-12-19 11:17:21 +0000275}