blob: d08006b4d542728f65480104eaaf71e0cb64a32e [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
17public class ConstantFolding {
18
19 /**
20 * Tiny three-register program exercising int constant folding
21 * on negation.
22 */
23
24 // CHECK-START: int ConstantFolding.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
29 // CHECK-START: int ConstantFolding.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
45 // CHECK-START: int ConstantFolding.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
51 // CHECK-START: int ConstantFolding.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
68 // CHECK-START: int ConstantFolding.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
78 // CHECK-START: int ConstantFolding.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
99 // CHECK-START: int ConstantFolding.IntSubtraction() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +0000100 // CHECK-DAG: [[Const5:i\d+]] IntConstant 5
101 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
102 // CHECK-DAG: [[Sub:i\d+]] Sub [ [[Const5]] [[Const2]] ]
103 // CHECK-DAG: Return [ [[Sub]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000104
105 // CHECK-START: int ConstantFolding.IntSubtraction() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +0000106 // CHECK-DAG: [[Const3:i\d+]] IntConstant 3
107 // CHECK-DAG: Return [ [[Const3]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000108
109 public static int IntSubtraction() {
110 int a, b, c;
111 a = 5;
112 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
122 // CHECK-START: long ConstantFolding.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
128 // CHECK-START: long ConstantFolding.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
145 // CHECK-START: long ConstantFolding.LongSubtraction() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +0000146 // CHECK-DAG: [[Const5:j\d+]] LongConstant 5
147 // CHECK-DAG: [[Const2:j\d+]] LongConstant 2
148 // CHECK-DAG: [[Sub:j\d+]] Sub [ [[Const5]] [[Const2]] ]
149 // CHECK-DAG: Return [ [[Sub]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000150
151 // CHECK-START: long ConstantFolding.LongSubtraction() constant_folding (after)
David Brazdilbe0cc082014-12-31 11:49:30 +0000152 // CHECK-DAG: [[Const3:j\d+]] LongConstant 3
153 // CHECK-DAG: Return [ [[Const3]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000154
155 public static long LongSubtraction() {
156 long a, b, c;
157 a = 5L;
158 b = 2L;
159 c = a - b;
160 return c;
161 }
162
163 /**
164 * Three-register program with a constant (static) condition.
165 */
166
167 // CHECK-START: int ConstantFolding.StaticCondition() constant_folding (before)
David Brazdilbe0cc082014-12-31 11:49:30 +0000168 // CHECK-DAG: [[Const5:i\d+]] IntConstant 5
169 // CHECK-DAG: [[Const2:i\d+]] IntConstant 2
170 // CHECK-DAG: [[Cond:z\d+]] GreaterThanOrEqual [ [[Const5]] [[Const2]] ]
171 // CHECK-DAG: If [ [[Cond]] ]
David Brazdilee690a32014-12-01 17:04:16 +0000172
173 // CHECK-START: int ConstantFolding.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;
179 a = 5;
180 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
197 // CHECK-START: int ConstantFolding.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
205 // CHECK-START: int ConstantFolding.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 }
221}