blob: de2c5c7473debf8af3a79cb5eb96956d7df1ffa5 [file] [log] [blame]
David Brazdilee690a32014-12-01 17:04:16 +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 Brazdilee690a32014-12-01 17:04:16 +000018
19 /**
20 * Tiny three-register program exercising int constant folding
21 * on negation.
22 */
23
David Brazdil4846d132015-01-15 19:07:08 +000024 // CHECK-START: int Main.IntNegation() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +000025 // CHECK-DAG: [[Const42:i\d+]] IntConstant 42
26 // CHECK-DAG: [[Neg:i\d+]] Neg [ [[Const42]] ]
27 // CHECK-DAG: Return [ [[Neg]] ]
David Brazdilee690a32014-12-01 17:04:16 +000028
David Brazdil4846d132015-01-15 19:07:08 +000029 // CHECK-START: int Main.IntNegation() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +000030 // CHECK-DAG: [[ConstN42:i\d+]] IntConstant -42
31 // CHECK-DAG: Return [ [[ConstN42]] ]
David Brazdilee690a32014-12-01 17:04:16 +000032
33 public static int IntNegation() {
34 int x, y;
35 x = 42;
36 y = -x;
37 return y;
38 }
39
40 /**
41 * Tiny three-register program exercising int constant folding
42 * on addition.
43 */
44
David Brazdil4846d132015-01-15 19:07:08 +000045 // CHECK-START: int Main.IntAddition1() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +000046 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
47 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
48 // CHECK-DAG: [[Add:i\d+]] Add [ [[Const1]] [[Const2]] ]
49 // CHECK-DAG: Return [ [[Add]] ]
David Brazdilee690a32014-12-01 17:04:16 +000050
David Brazdil4846d132015-01-15 19:07:08 +000051 // CHECK-START: int Main.IntAddition1() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +000052 // CHECK-DAG: [[Const3:i\d+]] IntConstant 3
53 // CHECK-DAG: Return [ [[Const3]] ]
David Brazdilee690a32014-12-01 17:04:16 +000054
55 public static int IntAddition1() {
56 int a, b, c;
57 a = 1;
58 b = 2;
59 c = a + b;
60 return c;
61 }
62
63 /**
64 * Small three-register program exercising int constant folding
65 * on addition.
66 */
67
David Brazdil4846d132015-01-15 19:07:08 +000068 // CHECK-START: int Main.IntAddition2() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +000069 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
70 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
71 // CHECK-DAG: [[Const5:i\d+]] IntConstant 5
72 // CHECK-DAG: [[Const6:i\d+]] IntConstant 6
73 // CHECK-DAG: [[Add1:i\d+]] Add [ [[Const1]] [[Const2]] ]
74 // CHECK-DAG: [[Add2:i\d+]] Add [ [[Const5]] [[Const6]] ]
75 // CHECK-DAG: [[Add3:i\d+]] Add [ [[Add1]] [[Add2]] ]
76 // CHECK-DAG: Return [ [[Add3]] ]
David Brazdilee690a32014-12-01 17:04:16 +000077
David Brazdil4846d132015-01-15 19:07:08 +000078 // CHECK-START: int Main.IntAddition2() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +000079 // CHECK-DAG: [[Const14:i\d+]] IntConstant 14
80 // CHECK-DAG: Return [ [[Const14]] ]
David Brazdilee690a32014-12-01 17:04:16 +000081
82 public static int IntAddition2() {
83 int a, b, c;
84 a = 1;
85 b = 2;
86 a += b;
87 b = 5;
88 c = 6;
89 b += c;
90 c = a + b;
91 return c;
92 }
93
94 /**
95 * Tiny three-register program exercising int constant folding
96 * on subtraction.
97 */
98
David Brazdil4846d132015-01-15 19:07:08 +000099 // CHECK-START: int Main.IntSubtraction() constant_folding (before)
100 // CHECK-DAG: [[Const6:i\d+]] IntConstant 6
David Brazdilbe0cc082014-12-31 11:49:30 +0000101 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
David Brazdil4846d132015-01-15 19:07:08 +0000102 // CHECK-DAG: [[Sub:i\d+]] Sub [ [[Const6]] [[Const2]] ]
David Brazdilbe0cc082014-12-31 11:49:30 +0000103 // CHECK-DAG: Return [ [[Sub]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000104
David Brazdil4846d132015-01-15 19:07:08 +0000105 // CHECK-START: int Main.IntSubtraction() constant_folding (after)
106 // CHECK-DAG: [[Const4:i\d+]] IntConstant 4
107 // CHECK-DAG: Return [ [[Const4]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000108
109 public static int IntSubtraction() {
110 int a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000111 a = 6;
David Brazdilee690a32014-12-01 17:04:16 +0000112 b = 2;
113 c = a - b;
114 return c;
115 }
116
117 /**
118 * Tiny three-register program exercising long constant folding
119 * on addition.
120 */
121
David Brazdil4846d132015-01-15 19:07:08 +0000122 // CHECK-START: long Main.LongAddition() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +0000123 // CHECK-DAG: [[Const1:j\d+]] LongConstant 1
124 // CHECK-DAG: [[Const2:j\d+]] LongConstant 2
125 // CHECK-DAG: [[Add:j\d+]] Add [ [[Const1]] [[Const2]] ]
126 // CHECK-DAG: Return [ [[Add]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000127
David Brazdil4846d132015-01-15 19:07:08 +0000128 // CHECK-START: long Main.LongAddition() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +0000129 // CHECK-DAG: [[Const3:j\d+]] LongConstant 3
130 // CHECK-DAG: Return [ [[Const3]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000131
132 public static long LongAddition() {
133 long a, b, c;
134 a = 1L;
135 b = 2L;
136 c = a + b;
137 return c;
138 }
139
140 /**
141 * Tiny three-register program exercising long constant folding
142 * on subtraction.
143 */
144
David Brazdil4846d132015-01-15 19:07:08 +0000145 // CHECK-START: long Main.LongSubtraction() constant_folding (before)
146 // CHECK-DAG: [[Const6:j\d+]] LongConstant 6
David Brazdilbe0cc082014-12-31 11:49:30 +0000147 // CHECK-DAG: [[Const2:j\d+]] LongConstant 2
David Brazdil4846d132015-01-15 19:07:08 +0000148 // CHECK-DAG: [[Sub:j\d+]] Sub [ [[Const6]] [[Const2]] ]
David Brazdilbe0cc082014-12-31 11:49:30 +0000149 // CHECK-DAG: Return [ [[Sub]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000150
David Brazdil4846d132015-01-15 19:07:08 +0000151 // CHECK-START: long Main.LongSubtraction() constant_folding (after)
152 // CHECK-DAG: [[Const4:j\d+]] LongConstant 4
153 // CHECK-DAG: Return [ [[Const4]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000154
155 public static long LongSubtraction() {
156 long a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000157 a = 6L;
David Brazdilee690a32014-12-01 17:04:16 +0000158 b = 2L;
159 c = a - b;
160 return c;
161 }
162
163 /**
164 * Three-register program with a constant (static) condition.
165 */
166
David Brazdil4846d132015-01-15 19:07:08 +0000167 // CHECK-START: int Main.StaticCondition() constant_folding (before)
168 // CHECK-DAG: [[Const7:i\d+]] IntConstant 7
David Brazdilbe0cc082014-12-31 11:49:30 +0000169 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
David Brazdil4846d132015-01-15 19:07:08 +0000170 // CHECK-DAG: [[Cond:z\d+]] GreaterThanOrEqual [ [[Const7]] [[Const2]] ]
David Brazdilbe0cc082014-12-31 11:49:30 +0000171 // CHECK-DAG: If [ [[Cond]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000172
David Brazdil4846d132015-01-15 19:07:08 +0000173 // CHECK-START: int Main.StaticCondition() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +0000174 // CHECK-DAG: [[Const1:i\d+]] IntConstant 1
175 // CHECK-DAG: If [ [[Const1]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000176
177 public static int StaticCondition() {
178 int a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000179 a = 7;
David Brazdilee690a32014-12-01 17:04:16 +0000180 b = 2;
181 if (a < b)
182 c = a + b;
183 else
184 c = a - b;
185 return c;
186 }
187
188 /**
189 * Four-variable program with jumps leading to the creation of many
190 * blocks.
191 *
192 * The intent of this test is to ensure that all constant expressions
193 * are actually evaluated at compile-time, thanks to the reverse
194 * (forward) post-order traversal of the the dominator tree.
195 */
196
David Brazdil4846d132015-01-15 19:07:08 +0000197 // CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +0000198 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
199 // CHECK-DAG: [[Const5:i\d+]] IntConstant 5
200 // CHECK-DAG: [[Add:i\d+]] Add [ [[Const5]] [[Const2]] ]
201 // CHECK-DAG: [[Sub:i\d+]] Sub [ [[Const5]] [[Const2]] ]
202 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Add]] [[Sub]] ]
203 // CHECK-DAG: Return [ [[Phi]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000204
David Brazdil4846d132015-01-15 19:07:08 +0000205 // CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +0000206 // CHECK-DAG: [[Const3:i\d+]] IntConstant 3
207 // CHECK-DAG: [[Const7:i\d+]] IntConstant 7
208 // CHECK-DAG: [[Phi:i\d+]] Phi [ [[Const7]] [[Const3]] ]
209 // CHECK-DAG: Return [ [[Phi]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000210
211 public static int JumpsAndConditionals(boolean cond) {
212 int a, b, c;
213 a = 5;
214 b = 2;
215 if (cond)
216 c = a + b;
217 else
218 c = a - b;
219 return c;
220 }
David Brazdil4846d132015-01-15 19:07:08 +0000221
222 public static void main(String[] args) {
223 if (IntNegation() != -42) {
224 throw new Error();
225 }
226
227 if (IntAddition1() != 3) {
228 throw new Error();
229 }
230
231 if (IntAddition2() != 14) {
232 throw new Error();
233 }
234
235 if (IntSubtraction() != 4) {
236 throw new Error();
237 }
238
239 if (LongAddition() != 3L) {
240 throw new Error();
241 }
242
243 if (LongSubtraction() != 4L) {
244 throw new Error();
245 }
246
247 if (StaticCondition() != 5) {
248 throw new Error();
249 }
250
251 if (JumpsAndConditionals(true) != 7) {
252 throw new Error();
253 }
254
255 if (JumpsAndConditionals(false) != 3) {
256 throw new Error();
257 }
258 }
David Brazdilee690a32014-12-01 17:04:16 +0000259}