blob: a8f65bf516017de71685aa174fa7518c38e098d4 [file] [log] [blame]
Roland Levillain556c3d12014-09-18 15:25:07 +01001/*
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
Roland Levillain93445682014-10-06 19:24:02 +010017#include <functional>
18
Mark Mendellfb8d2792015-03-31 22:16:59 -040019#include "arch/x86/instruction_set_features_x86.h"
Roland Levillain75be2832014-10-17 17:02:00 +010020#include "code_generator_x86.h"
21#include "constant_folding.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010022#include "dead_code_elimination.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000023#include "driver/compiler_options.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010024#include "graph_checker.h"
25#include "optimizing_unit_test.h"
Roland Levillain75be2832014-10-17 17:02:00 +010026#include "pretty_printer.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010027
28#include "gtest/gtest.h"
29
30namespace art {
31
Aart Bik96709f12015-10-28 17:49:07 -070032/**
33 * Fixture class for the constant folding and dce tests.
34 */
David Brazdil4833f5a2015-12-16 10:37:39 +000035class ConstantFoldingTest : public CommonCompilerTest {
Aart Bik96709f12015-10-28 17:49:07 -070036 public:
37 ConstantFoldingTest() : pool_(), allocator_(&pool_) {
38 graph_ = CreateGraph(&allocator_);
39 }
Roland Levillain556c3d12014-09-18 15:25:07 +010040
Aart Bik96709f12015-10-28 17:49:07 -070041 void TestCode(const uint16_t* data,
42 const std::string& expected_before,
43 const std::string& expected_after_cf,
44 const std::string& expected_after_dce,
45 std::function<void(HGraph*)> check_after_cf,
46 Primitive::Type return_type = Primitive::kPrimInt) {
47 graph_ = CreateCFG(&allocator_, data, return_type);
48 TestCodeOnReadyGraph(expected_before,
49 expected_after_cf,
50 expected_after_dce,
51 check_after_cf);
52 }
Roland Levillain556c3d12014-09-18 15:25:07 +010053
Aart Bik96709f12015-10-28 17:49:07 -070054 void TestCodeOnReadyGraph(const std::string& expected_before,
55 const std::string& expected_after_cf,
56 const std::string& expected_after_dce,
57 std::function<void(HGraph*)> check_after_cf) {
58 ASSERT_NE(graph_, nullptr);
David Brazdil4833f5a2015-12-16 10:37:39 +000059 TransformToSsa(graph_);
Roland Levillain556c3d12014-09-18 15:25:07 +010060
Aart Bik96709f12015-10-28 17:49:07 -070061 StringPrettyPrinter printer_before(graph_);
62 printer_before.VisitInsertionOrder();
63 std::string actual_before = printer_before.str();
64 EXPECT_EQ(expected_before, actual_before);
Roland Levillain556c3d12014-09-18 15:25:07 +010065
Aart Bik96709f12015-10-28 17:49:07 -070066 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
67 X86InstructionSetFeatures::FromCppDefines());
68 x86::CodeGeneratorX86 codegenX86(graph_, *features_x86.get(), CompilerOptions());
69 HConstantFolding(graph_).Run();
70 SSAChecker ssa_checker_cf(graph_);
71 ssa_checker_cf.Run();
72 ASSERT_TRUE(ssa_checker_cf.IsValid());
Roland Levillain556c3d12014-09-18 15:25:07 +010073
Aart Bik96709f12015-10-28 17:49:07 -070074 StringPrettyPrinter printer_after_cf(graph_);
75 printer_after_cf.VisitInsertionOrder();
76 std::string actual_after_cf = printer_after_cf.str();
77 EXPECT_EQ(expected_after_cf, actual_after_cf);
Roland Levillain93445682014-10-06 19:24:02 +010078
Aart Bik96709f12015-10-28 17:49:07 -070079 check_after_cf(graph_);
Roland Levillain556c3d12014-09-18 15:25:07 +010080
Aart Bik96709f12015-10-28 17:49:07 -070081 HDeadCodeElimination(graph_).Run();
82 SSAChecker ssa_checker_dce(graph_);
83 ssa_checker_dce.Run();
84 ASSERT_TRUE(ssa_checker_dce.IsValid());
Roland Levillain556c3d12014-09-18 15:25:07 +010085
Aart Bik96709f12015-10-28 17:49:07 -070086 StringPrettyPrinter printer_after_dce(graph_);
87 printer_after_dce.VisitInsertionOrder();
88 std::string actual_after_dce = printer_after_dce.str();
89 EXPECT_EQ(expected_after_dce, actual_after_dce);
90 }
91
92 ArenaPool pool_;
93 ArenaAllocator allocator_;
94 HGraph* graph_;
95};
Roland Levillain556c3d12014-09-18 15:25:07 +010096
97/**
Roland Levillain9240d6a2014-10-20 16:47:04 +010098 * Tiny three-register program exercising int constant folding on negation.
99 *
100 * 16-bit
101 * offset
102 * ------
103 * v0 <- 1 0. const/4 v0, #+1
Roland Levillainc90bc7c2014-12-11 12:14:33 +0000104 * v1 <- -v0 1. neg-int v1, v0
Roland Levillain9240d6a2014-10-20 16:47:04 +0100105 * return v1 2. return v1
106 */
Aart Bik96709f12015-10-28 17:49:07 -0700107TEST_F(ConstantFoldingTest, IntConstantFoldingNegation) {
Roland Levillain9240d6a2014-10-20 16:47:04 +0100108 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
109 Instruction::CONST_4 | 0 << 8 | 1 << 12,
110 Instruction::NEG_INT | 1 << 8 | 0 << 12,
111 Instruction::RETURN | 1 << 8);
112
113 std::string expected_before =
114 "BasicBlock 0, succ: 1\n"
115 " 2: IntConstant [5]\n"
116 " 10: SuspendCheck\n"
117 " 11: Goto 1\n"
118 "BasicBlock 1, pred: 0, succ: 2\n"
119 " 5: Neg(2) [8]\n"
120 " 8: Return(5)\n"
121 "BasicBlock 2, pred: 1\n"
122 " 9: Exit\n";
123
124 // Expected difference after constant folding.
125 diff_t expected_cf_diff = {
126 { " 2: IntConstant [5]\n", " 2: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000127 { " 10: SuspendCheck\n", " 10: SuspendCheck\n"
128 " 12: IntConstant [8]\n" },
129 { " 5: Neg(2) [8]\n", removed },
Roland Levillain9240d6a2014-10-20 16:47:04 +0100130 { " 8: Return(5)\n", " 8: Return(12)\n" }
131 };
132 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
133
134 // Check the value of the computed constant.
135 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100136 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100137 ASSERT_TRUE(inst->IsIntConstant());
138 ASSERT_EQ(inst->AsIntConstant()->GetValue(), -1);
139 };
140
141 // Expected difference after dead code elimination.
142 diff_t expected_dce_diff = {
143 { " 2: IntConstant\n", removed },
144 };
145 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
146
147 TestCode(data,
148 expected_before,
149 expected_after_cf,
150 expected_after_dce,
151 check_after_cf);
152}
153
154/**
Roland Levillainc90bc7c2014-12-11 12:14:33 +0000155 * Tiny three-register program exercising long constant folding on negation.
156 *
157 * 16-bit
158 * offset
159 * ------
160 * (v0, v1) <- 4294967296 0. const-wide v0 #+4294967296
161 * (v2, v3) <- -(v0, v1) 1. neg-long v2, v0
162 * return (v2, v3) 2. return-wide v2
163 */
Aart Bik96709f12015-10-28 17:49:07 -0700164TEST_F(ConstantFoldingTest, LongConstantFoldingNegation) {
Roland Levillainc90bc7c2014-12-11 12:14:33 +0000165 const int64_t input = INT64_C(4294967296); // 2^32
166 const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW.
167 const uint16_t word1 = High16Bits(Low32Bits(input));
168 const uint16_t word2 = Low16Bits(High32Bits(input));
169 const uint16_t word3 = High16Bits(High32Bits(input)); // MSW.
170 const uint16_t data[] = FOUR_REGISTERS_CODE_ITEM(
171 Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3,
172 Instruction::NEG_LONG | 2 << 8 | 0 << 12,
173 Instruction::RETURN_WIDE | 2 << 8);
174
175 std::string expected_before =
176 "BasicBlock 0, succ: 1\n"
177 " 4: LongConstant [7]\n"
178 " 12: SuspendCheck\n"
179 " 13: Goto 1\n"
180 "BasicBlock 1, pred: 0, succ: 2\n"
181 " 7: Neg(4) [10]\n"
182 " 10: Return(7)\n"
183 "BasicBlock 2, pred: 1\n"
184 " 11: Exit\n";
185
186 // Expected difference after constant folding.
187 diff_t expected_cf_diff = {
188 { " 4: LongConstant [7]\n", " 4: LongConstant\n" },
189 { " 12: SuspendCheck\n", " 12: SuspendCheck\n"
190 " 14: LongConstant [10]\n" },
191 { " 7: Neg(4) [10]\n", removed },
192 { " 10: Return(7)\n", " 10: Return(14)\n" }
193 };
194 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
195
196 // Check the value of the computed constant.
197 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100198 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillainc90bc7c2014-12-11 12:14:33 +0000199 ASSERT_TRUE(inst->IsLongConstant());
200 ASSERT_EQ(inst->AsLongConstant()->GetValue(), INT64_C(-4294967296));
201 };
202
203 // Expected difference after dead code elimination.
204 diff_t expected_dce_diff = {
205 { " 4: LongConstant\n", removed },
206 };
207 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
208
209 TestCode(data,
210 expected_before,
211 expected_after_cf,
212 expected_after_dce,
213 check_after_cf,
214 Primitive::kPrimLong);
215}
216
217/**
Roland Levillain556c3d12014-09-18 15:25:07 +0100218 * Tiny three-register program exercising int constant folding on addition.
219 *
220 * 16-bit
221 * offset
222 * ------
223 * v0 <- 1 0. const/4 v0, #+1
224 * v1 <- 2 1. const/4 v1, #+2
225 * v2 <- v0 + v1 2. add-int v2, v0, v1
226 * return v2 4. return v2
227 */
Aart Bik96709f12015-10-28 17:49:07 -0700228TEST_F(ConstantFoldingTest, IntConstantFoldingOnAddition1) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100229 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
230 Instruction::CONST_4 | 0 << 8 | 1 << 12,
231 Instruction::CONST_4 | 1 << 8 | 2 << 12,
232 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
233 Instruction::RETURN | 2 << 8);
234
235 std::string expected_before =
236 "BasicBlock 0, succ: 1\n"
237 " 3: IntConstant [9]\n"
238 " 5: IntConstant [9]\n"
239 " 14: SuspendCheck\n"
240 " 15: Goto 1\n"
241 "BasicBlock 1, pred: 0, succ: 2\n"
242 " 9: Add(3, 5) [12]\n"
243 " 12: Return(9)\n"
244 "BasicBlock 2, pred: 1\n"
245 " 13: Exit\n";
246
Roland Levillain75be2832014-10-17 17:02:00 +0100247 // Expected difference after constant folding.
248 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100249 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
250 { " 5: IntConstant [9]\n", " 5: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000251 { " 14: SuspendCheck\n", " 14: SuspendCheck\n"
252 " 16: IntConstant [12]\n" },
253 { " 9: Add(3, 5) [12]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100254 { " 12: Return(9)\n", " 12: Return(16)\n" }
255 };
Roland Levillain75be2832014-10-17 17:02:00 +0100256 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100257
Roland Levillain93445682014-10-06 19:24:02 +0100258 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100259 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100260 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100261 ASSERT_TRUE(inst->IsIntConstant());
262 ASSERT_EQ(inst->AsIntConstant()->GetValue(), 3);
263 };
264
Roland Levillain556c3d12014-09-18 15:25:07 +0100265 // Expected difference after dead code elimination.
266 diff_t expected_dce_diff = {
267 { " 3: IntConstant\n", removed },
268 { " 5: IntConstant\n", removed }
269 };
Roland Levillain75be2832014-10-17 17:02:00 +0100270 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100271
Roland Levillain93445682014-10-06 19:24:02 +0100272 TestCode(data,
273 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100274 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100275 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100276 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100277}
278
279/**
280 * Small three-register program exercising int constant folding on addition.
281 *
282 * 16-bit
283 * offset
284 * ------
285 * v0 <- 1 0. const/4 v0, #+1
286 * v1 <- 2 1. const/4 v1, #+2
287 * v0 <- v0 + v1 2. add-int/2addr v0, v1
David Brazdil8d5b8b22015-03-24 10:51:52 +0000288 * v1 <- 4 3. const/4 v1, #+4
289 * v2 <- 5 4. const/4 v2, #+5
Roland Levillain556c3d12014-09-18 15:25:07 +0100290 * v1 <- v1 + v2 5. add-int/2addr v1, v2
291 * v2 <- v0 + v1 6. add-int v2, v0, v1
292 * return v2 8. return v2
293 */
Aart Bik96709f12015-10-28 17:49:07 -0700294TEST_F(ConstantFoldingTest, IntConstantFoldingOnAddition2) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100295 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
296 Instruction::CONST_4 | 0 << 8 | 1 << 12,
297 Instruction::CONST_4 | 1 << 8 | 2 << 12,
298 Instruction::ADD_INT_2ADDR | 0 << 8 | 1 << 12,
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 Instruction::CONST_4 | 1 << 8 | 4 << 12,
300 Instruction::CONST_4 | 2 << 8 | 5 << 12,
Roland Levillain556c3d12014-09-18 15:25:07 +0100301 Instruction::ADD_INT_2ADDR | 1 << 8 | 2 << 12,
302 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
303 Instruction::RETURN | 2 << 8);
304
305 std::string expected_before =
306 "BasicBlock 0, succ: 1\n"
307 " 3: IntConstant [9]\n"
308 " 5: IntConstant [9]\n"
309 " 11: IntConstant [17]\n"
310 " 13: IntConstant [17]\n"
311 " 26: SuspendCheck\n"
312 " 27: Goto 1\n"
313 "BasicBlock 1, pred: 0, succ: 2\n"
314 " 9: Add(3, 5) [21]\n"
315 " 17: Add(11, 13) [21]\n"
316 " 21: Add(9, 17) [24]\n"
317 " 24: Return(21)\n"
318 "BasicBlock 2, pred: 1\n"
319 " 25: Exit\n";
320
Roland Levillain75be2832014-10-17 17:02:00 +0100321 // Expected difference after constant folding.
322 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100323 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
324 { " 5: IntConstant [9]\n", " 5: IntConstant\n" },
325 { " 11: IntConstant [17]\n", " 11: IntConstant\n" },
326 { " 13: IntConstant [17]\n", " 13: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 { " 26: SuspendCheck\n", " 26: SuspendCheck\n"
328 " 28: IntConstant\n"
329 " 29: IntConstant\n"
330 " 30: IntConstant [24]\n" },
331 { " 9: Add(3, 5) [21]\n", removed },
332 { " 17: Add(11, 13) [21]\n", removed },
333 { " 21: Add(9, 17) [24]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100334 { " 24: Return(21)\n", " 24: Return(30)\n" }
335 };
Roland Levillain75be2832014-10-17 17:02:00 +0100336 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100337
Roland Levillain93445682014-10-06 19:24:02 +0100338 // Check the values of the computed constants.
Roland Levillain75be2832014-10-17 17:02:00 +0100339 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100340 HInstruction* inst1 = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100341 ASSERT_TRUE(inst1->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 12);
343 HInstruction* inst2 = inst1->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100344 ASSERT_TRUE(inst2->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 9);
346 HInstruction* inst3 = inst2->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100347 ASSERT_TRUE(inst3->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000348 ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 3);
Roland Levillain93445682014-10-06 19:24:02 +0100349 };
350
Roland Levillain556c3d12014-09-18 15:25:07 +0100351 // Expected difference after dead code elimination.
352 diff_t expected_dce_diff = {
353 { " 3: IntConstant\n", removed },
354 { " 5: IntConstant\n", removed },
355 { " 11: IntConstant\n", removed },
356 { " 13: IntConstant\n", removed },
357 { " 28: IntConstant\n", removed },
358 { " 29: IntConstant\n", removed }
359 };
Roland Levillain75be2832014-10-17 17:02:00 +0100360 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100361
Roland Levillain93445682014-10-06 19:24:02 +0100362 TestCode(data,
363 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100364 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100365 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100366 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100367}
368
369/**
370 * Tiny three-register program exercising int constant folding on subtraction.
371 *
372 * 16-bit
373 * offset
374 * ------
375 * v0 <- 3 0. const/4 v0, #+3
376 * v1 <- 2 1. const/4 v1, #+2
377 * v2 <- v0 - v1 2. sub-int v2, v0, v1
378 * return v2 4. return v2
379 */
Aart Bik96709f12015-10-28 17:49:07 -0700380TEST_F(ConstantFoldingTest, IntConstantFoldingOnSubtraction) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100381 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
382 Instruction::CONST_4 | 0 << 8 | 3 << 12,
383 Instruction::CONST_4 | 1 << 8 | 2 << 12,
384 Instruction::SUB_INT | 2 << 8, 0 | 1 << 8,
385 Instruction::RETURN | 2 << 8);
386
387 std::string expected_before =
388 "BasicBlock 0, succ: 1\n"
389 " 3: IntConstant [9]\n"
390 " 5: IntConstant [9]\n"
391 " 14: SuspendCheck\n"
392 " 15: Goto 1\n"
393 "BasicBlock 1, pred: 0, succ: 2\n"
394 " 9: Sub(3, 5) [12]\n"
395 " 12: Return(9)\n"
396 "BasicBlock 2, pred: 1\n"
397 " 13: Exit\n";
398
Roland Levillain75be2832014-10-17 17:02:00 +0100399 // Expected difference after constant folding.
400 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100401 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
402 { " 5: IntConstant [9]\n", " 5: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000403 { " 14: SuspendCheck\n", " 14: SuspendCheck\n"
404 " 16: IntConstant [12]\n" },
405 { " 9: Sub(3, 5) [12]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100406 { " 12: Return(9)\n", " 12: Return(16)\n" }
407 };
Roland Levillain75be2832014-10-17 17:02:00 +0100408 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100409
Roland Levillain93445682014-10-06 19:24:02 +0100410 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100411 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100412 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100413 ASSERT_TRUE(inst->IsIntConstant());
414 ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1);
415 };
416
Roland Levillain556c3d12014-09-18 15:25:07 +0100417 // Expected difference after dead code elimination.
418 diff_t expected_dce_diff = {
419 { " 3: IntConstant\n", removed },
420 { " 5: IntConstant\n", removed }
421 };
Roland Levillain75be2832014-10-17 17:02:00 +0100422 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100423
Roland Levillain93445682014-10-06 19:24:02 +0100424 TestCode(data,
425 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100426 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100427 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100428 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100429}
430
Roland Levillain556c3d12014-09-18 15:25:07 +0100431/**
432 * Tiny three-register-pair program exercising long constant folding
433 * on addition.
434 *
435 * 16-bit
436 * offset
437 * ------
438 * (v0, v1) <- 1 0. const-wide/16 v0, #+1
439 * (v2, v3) <- 2 2. const-wide/16 v2, #+2
440 * (v4, v5) <-
441 * (v0, v1) + (v1, v2) 4. add-long v4, v0, v2
442 * return (v4, v5) 6. return-wide v4
443 */
Aart Bik96709f12015-10-28 17:49:07 -0700444TEST_F(ConstantFoldingTest, LongConstantFoldingOnAddition) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100445 const uint16_t data[] = SIX_REGISTERS_CODE_ITEM(
446 Instruction::CONST_WIDE_16 | 0 << 8, 1,
447 Instruction::CONST_WIDE_16 | 2 << 8, 2,
448 Instruction::ADD_LONG | 4 << 8, 0 | 2 << 8,
449 Instruction::RETURN_WIDE | 4 << 8);
450
451 std::string expected_before =
452 "BasicBlock 0, succ: 1\n"
453 " 6: LongConstant [12]\n"
454 " 8: LongConstant [12]\n"
455 " 17: SuspendCheck\n"
456 " 18: Goto 1\n"
457 "BasicBlock 1, pred: 0, succ: 2\n"
458 " 12: Add(6, 8) [15]\n"
459 " 15: Return(12)\n"
460 "BasicBlock 2, pred: 1\n"
461 " 16: Exit\n";
462
Roland Levillain75be2832014-10-17 17:02:00 +0100463 // Expected difference after constant folding.
464 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100465 { " 6: LongConstant [12]\n", " 6: LongConstant\n" },
466 { " 8: LongConstant [12]\n", " 8: LongConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 { " 17: SuspendCheck\n", " 17: SuspendCheck\n"
468 " 19: LongConstant [15]\n" },
469 { " 12: Add(6, 8) [15]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100470 { " 15: Return(12)\n", " 15: Return(19)\n" }
471 };
Roland Levillain75be2832014-10-17 17:02:00 +0100472 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100473
Roland Levillain93445682014-10-06 19:24:02 +0100474 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100475 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100476 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100477 ASSERT_TRUE(inst->IsLongConstant());
478 ASSERT_EQ(inst->AsLongConstant()->GetValue(), 3);
479 };
480
Roland Levillain556c3d12014-09-18 15:25:07 +0100481 // Expected difference after dead code elimination.
482 diff_t expected_dce_diff = {
483 { " 6: LongConstant\n", removed },
484 { " 8: LongConstant\n", removed }
485 };
Roland Levillain75be2832014-10-17 17:02:00 +0100486 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100487
Roland Levillain93445682014-10-06 19:24:02 +0100488 TestCode(data,
489 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100490 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100491 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100492 check_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100493 Primitive::kPrimLong);
Roland Levillain556c3d12014-09-18 15:25:07 +0100494}
495
496/**
497 * Tiny three-register-pair program exercising long constant folding
498 * on subtraction.
499 *
500 * 16-bit
501 * offset
502 * ------
503 * (v0, v1) <- 3 0. const-wide/16 v0, #+3
504 * (v2, v3) <- 2 2. const-wide/16 v2, #+2
505 * (v4, v5) <-
506 * (v0, v1) - (v1, v2) 4. sub-long v4, v0, v2
507 * return (v4, v5) 6. return-wide v4
508 */
Aart Bik96709f12015-10-28 17:49:07 -0700509TEST_F(ConstantFoldingTest, LongConstantFoldingOnSubtraction) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100510 const uint16_t data[] = SIX_REGISTERS_CODE_ITEM(
511 Instruction::CONST_WIDE_16 | 0 << 8, 3,
512 Instruction::CONST_WIDE_16 | 2 << 8, 2,
513 Instruction::SUB_LONG | 4 << 8, 0 | 2 << 8,
514 Instruction::RETURN_WIDE | 4 << 8);
515
516 std::string expected_before =
517 "BasicBlock 0, succ: 1\n"
518 " 6: LongConstant [12]\n"
519 " 8: LongConstant [12]\n"
520 " 17: SuspendCheck\n"
521 " 18: Goto 1\n"
522 "BasicBlock 1, pred: 0, succ: 2\n"
523 " 12: Sub(6, 8) [15]\n"
524 " 15: Return(12)\n"
525 "BasicBlock 2, pred: 1\n"
526 " 16: Exit\n";
527
Roland Levillain75be2832014-10-17 17:02:00 +0100528 // Expected difference after constant folding.
529 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100530 { " 6: LongConstant [12]\n", " 6: LongConstant\n" },
531 { " 8: LongConstant [12]\n", " 8: LongConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000532 { " 17: SuspendCheck\n", " 17: SuspendCheck\n"
533 " 19: LongConstant [15]\n" },
534 { " 12: Sub(6, 8) [15]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100535 { " 15: Return(12)\n", " 15: Return(19)\n" }
536 };
Roland Levillain75be2832014-10-17 17:02:00 +0100537 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100538
Roland Levillain93445682014-10-06 19:24:02 +0100539 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100540 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100541 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100542 ASSERT_TRUE(inst->IsLongConstant());
543 ASSERT_EQ(inst->AsLongConstant()->GetValue(), 1);
544 };
545
Roland Levillain556c3d12014-09-18 15:25:07 +0100546 // Expected difference after dead code elimination.
547 diff_t expected_dce_diff = {
548 { " 6: LongConstant\n", removed },
549 { " 8: LongConstant\n", removed }
550 };
Roland Levillain75be2832014-10-17 17:02:00 +0100551 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100552
Roland Levillain93445682014-10-06 19:24:02 +0100553 TestCode(data,
554 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100555 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100556 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100557 check_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100558 Primitive::kPrimLong);
Roland Levillain556c3d12014-09-18 15:25:07 +0100559}
560
561/**
562 * Three-register program with jumps leading to the creation of many
563 * blocks.
564 *
565 * The intent of this test is to ensure that all constant expressions
566 * are actually evaluated at compile-time, thanks to the reverse
567 * (forward) post-order traversal of the the dominator tree.
568 *
569 * 16-bit
570 * offset
571 * ------
David Brazdil8d5b8b22015-03-24 10:51:52 +0000572 * v0 <- 1 0. const/4 v0, #+1
573 * v1 <- 2 1. const/4 v1, #+2
Roland Levillain556c3d12014-09-18 15:25:07 +0100574 * v2 <- v0 + v1 2. add-int v2, v0, v1
575 * goto L2 4. goto +4
David Brazdil8d5b8b22015-03-24 10:51:52 +0000576 * L1: v1 <- v0 + 5 5. add-int/lit16 v1, v0, #+5
Roland Levillain556c3d12014-09-18 15:25:07 +0100577 * goto L3 7. goto +4
David Brazdil8d5b8b22015-03-24 10:51:52 +0000578 * L2: v0 <- v2 + 4 8. add-int/lit16 v0, v2, #+4
Roland Levillain556c3d12014-09-18 15:25:07 +0100579 * goto L1 10. goto +(-5)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000580 * L3: v2 <- v1 + 8 11. add-int/lit16 v2, v1, #+8
Roland Levillain556c3d12014-09-18 15:25:07 +0100581 * return v2 13. return v2
582 */
Aart Bik96709f12015-10-28 17:49:07 -0700583TEST_F(ConstantFoldingTest, IntConstantFoldingAndJumps) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100584 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
David Brazdil8d5b8b22015-03-24 10:51:52 +0000585 Instruction::CONST_4 | 0 << 8 | 1 << 12,
586 Instruction::CONST_4 | 1 << 8 | 2 << 12,
Roland Levillain556c3d12014-09-18 15:25:07 +0100587 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
588 Instruction::GOTO | 4 << 8,
David Brazdil8d5b8b22015-03-24 10:51:52 +0000589 Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 5,
Roland Levillain556c3d12014-09-18 15:25:07 +0100590 Instruction::GOTO | 4 << 8,
David Brazdil8d5b8b22015-03-24 10:51:52 +0000591 Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 4,
Andreas Gampe58554b72015-10-20 21:08:52 -0700592 static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000593 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 8,
Roland Levillain556c3d12014-09-18 15:25:07 +0100594 Instruction::RETURN | 2 << 8);
595
596 std::string expected_before =
597 "BasicBlock 0, succ: 1\n"
David Brazdil8d5b8b22015-03-24 10:51:52 +0000598 " 3: IntConstant [9]\n" // v0 <- 1
599 " 5: IntConstant [9]\n" // v1 <- 2
600 " 13: IntConstant [14]\n" // const 5
601 " 18: IntConstant [19]\n" // const 4
602 " 24: IntConstant [25]\n" // const 8
Roland Levillain556c3d12014-09-18 15:25:07 +0100603 " 30: SuspendCheck\n"
604 " 31: Goto 1\n"
605 "BasicBlock 1, pred: 0, succ: 3\n"
David Brazdil8d5b8b22015-03-24 10:51:52 +0000606 " 9: Add(3, 5) [19]\n" // v2 <- v0 + v1 = 1 + 2 = 3
Roland Levillain93445682014-10-06 19:24:02 +0100607 " 11: Goto 3\n" // goto L2
608 "BasicBlock 2, pred: 3, succ: 4\n" // L1:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000609 " 14: Add(19, 13) [25]\n" // v1 <- v0 + 3 = 7 + 5 = 12
Roland Levillain93445682014-10-06 19:24:02 +0100610 " 16: Goto 4\n" // goto L3
611 "BasicBlock 3, pred: 1, succ: 2\n" // L2:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000612 " 19: Add(9, 18) [14]\n" // v0 <- v2 + 2 = 3 + 4 = 7
Roland Levillain556c3d12014-09-18 15:25:07 +0100613 " 21: SuspendCheck\n"
Roland Levillain93445682014-10-06 19:24:02 +0100614 " 22: Goto 2\n" // goto L1
615 "BasicBlock 4, pred: 2, succ: 5\n" // L3:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000616 " 25: Add(14, 24) [28]\n" // v2 <- v1 + 4 = 12 + 8 = 20
Roland Levillain93445682014-10-06 19:24:02 +0100617 " 28: Return(25)\n" // return v2
Roland Levillain556c3d12014-09-18 15:25:07 +0100618 "BasicBlock 5, pred: 4\n"
619 " 29: Exit\n";
620
Roland Levillain75be2832014-10-17 17:02:00 +0100621 // Expected difference after constant folding.
622 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100623 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
624 { " 5: IntConstant [9]\n", " 5: IntConstant []\n" },
625 { " 13: IntConstant [14]\n", " 13: IntConstant\n" },
626 { " 18: IntConstant [19]\n", " 18: IntConstant\n" },
627 { " 24: IntConstant [25]\n", " 24: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000628 { " 30: SuspendCheck\n", " 30: SuspendCheck\n"
629 " 32: IntConstant []\n"
630 " 33: IntConstant []\n"
631 " 34: IntConstant\n"
632 " 35: IntConstant [28]\n" },
633 { " 9: Add(3, 5) [19]\n", removed },
634 { " 14: Add(19, 13) [25]\n", removed },
635 { " 19: Add(9, 18) [14]\n", removed },
636 { " 25: Add(14, 24) [28]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100637 { " 28: Return(25)\n", " 28: Return(35)\n"}
638 };
Roland Levillain75be2832014-10-17 17:02:00 +0100639 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100640
Roland Levillain93445682014-10-06 19:24:02 +0100641 // Check the values of the computed constants.
Roland Levillain75be2832014-10-17 17:02:00 +0100642 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100643 HInstruction* inst1 = graph->GetBlocks()[4]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100644 ASSERT_TRUE(inst1->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000645 ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 20);
646 HInstruction* inst2 = inst1->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100647 ASSERT_TRUE(inst2->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000648 ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 12);
649 HInstruction* inst3 = inst2->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100650 ASSERT_TRUE(inst3->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000651 ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 7);
652 HInstruction* inst4 = inst3->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100653 ASSERT_TRUE(inst4->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000654 ASSERT_EQ(inst4->AsIntConstant()->GetValue(), 3);
Roland Levillain93445682014-10-06 19:24:02 +0100655 };
656
Roland Levillain556c3d12014-09-18 15:25:07 +0100657 // Expected difference after dead code elimination.
David Brazdil1c533c12015-04-24 17:04:38 +0100658 std::string expected_after_dce =
659 "BasicBlock 0, succ: 1\n"
660 " 5: IntConstant []\n"
661 " 30: SuspendCheck\n"
662 " 32: IntConstant []\n"
663 " 33: IntConstant []\n"
664 " 35: IntConstant [28]\n"
665 " 31: Goto 1\n"
666 "BasicBlock 1, pred: 0, succ: 5\n"
667 " 21: SuspendCheck\n"
668 " 28: Return(35)\n"
669 "BasicBlock 5, pred: 1\n"
670 " 29: Exit\n";
Roland Levillain556c3d12014-09-18 15:25:07 +0100671
Roland Levillain93445682014-10-06 19:24:02 +0100672 TestCode(data,
673 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100674 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100675 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100676 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100677}
678
Roland Levillain556c3d12014-09-18 15:25:07 +0100679/**
680 * Three-register program with a constant (static) condition.
681 *
682 * 16-bit
683 * offset
684 * ------
685 * v1 <- 1 0. const/4 v1, #+1
686 * v0 <- 0 1. const/4 v0, #+0
687 * if v1 >= 0 goto L1 2. if-gez v1, +3
688 * v0 <- v1 4. move v0, v1
689 * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
690 * return-void 7. return
691 */
Aart Bik96709f12015-10-28 17:49:07 -0700692TEST_F(ConstantFoldingTest, ConstantCondition) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100693 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
694 Instruction::CONST_4 | 1 << 8 | 1 << 12,
695 Instruction::CONST_4 | 0 << 8 | 0 << 12,
696 Instruction::IF_GEZ | 1 << 8, 3,
697 Instruction::MOVE | 0 << 8 | 1 << 12,
698 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
699 Instruction::RETURN_VOID);
700
701 std::string expected_before =
702 "BasicBlock 0, succ: 1\n"
703 " 3: IntConstant [15, 22, 8]\n"
704 " 5: IntConstant [22, 8]\n"
705 " 19: SuspendCheck\n"
706 " 20: Goto 1\n"
707 "BasicBlock 1, pred: 0, succ: 5, 2\n"
708 " 8: GreaterThanOrEqual(3, 5) [9]\n"
709 " 9: If(8)\n"
710 "BasicBlock 2, pred: 1, succ: 3\n"
711 " 12: Goto 3\n"
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100712 "BasicBlock 3, pred: 5, 2, succ: 4\n"
713 " 22: Phi(5, 3) [15]\n"
Roland Levillain556c3d12014-09-18 15:25:07 +0100714 " 15: Add(22, 3)\n"
715 " 17: ReturnVoid\n"
716 "BasicBlock 4, pred: 3\n"
717 " 18: Exit\n"
718 "BasicBlock 5, pred: 1, succ: 3\n"
719 " 21: Goto 3\n";
720
Roland Levillain75be2832014-10-17 17:02:00 +0100721 // Expected difference after constant folding.
722 diff_t expected_cf_diff = {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000723 { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [9, 15, 22]\n" },
Roland Levillain556c3d12014-09-18 15:25:07 +0100724 { " 5: IntConstant [22, 8]\n", " 5: IntConstant [22]\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000725 { " 8: GreaterThanOrEqual(3, 5) [9]\n", removed },
726 { " 9: If(8)\n", " 9: If(3)\n" }
Roland Levillain556c3d12014-09-18 15:25:07 +0100727 };
Roland Levillain75be2832014-10-17 17:02:00 +0100728 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100729
Roland Levillain93445682014-10-06 19:24:02 +0100730 // Check the values of the computed constants.
Roland Levillain75be2832014-10-17 17:02:00 +0100731 auto check_after_cf = [](HGraph* graph) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100732 HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100733 ASSERT_TRUE(inst->IsIntConstant());
734 ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1);
735 };
736
David Brazdil1c533c12015-04-24 17:04:38 +0100737 // Expected graph after dead code elimination.
738 std::string expected_after_dce =
739 "BasicBlock 0, succ: 1\n"
740 " 19: SuspendCheck\n"
741 " 20: Goto 1\n"
742 "BasicBlock 1, pred: 0, succ: 4\n"
743 " 17: ReturnVoid\n"
744 "BasicBlock 4, pred: 1\n"
745 " 18: Exit\n";
Roland Levillain556c3d12014-09-18 15:25:07 +0100746
Roland Levillain93445682014-10-06 19:24:02 +0100747 TestCode(data,
748 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100749 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100750 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100751 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100752}
753
Aart Bik96709f12015-10-28 17:49:07 -0700754/**
755 * Unsigned comparisons with zero. Since these instructions are not present
756 * in the bytecode, we need to set up the graph explicitly.
757 */
758TEST_F(ConstantFoldingTest, UnsignedComparisonsWithZero) {
759 graph_ = CreateGraph(&allocator_);
760 HBasicBlock* entry_block = new (&allocator_) HBasicBlock(graph_);
761 graph_->AddBlock(entry_block);
762 graph_->SetEntryBlock(entry_block);
763 HBasicBlock* block = new (&allocator_) HBasicBlock(graph_);
764 graph_->AddBlock(block);
765 HBasicBlock* exit_block = new (&allocator_) HBasicBlock(graph_);
766 graph_->AddBlock(exit_block);
767 graph_->SetExitBlock(exit_block);
768 entry_block->AddSuccessor(block);
769 block->AddSuccessor(exit_block);
770
771 // Make various unsigned comparisons with zero against a parameter.
772 HInstruction* parameter = new (&allocator_) HParameterValue(
773 graph_->GetDexFile(), 0, 0, Primitive::kPrimInt, true);
774 entry_block->AddInstruction(parameter);
775 HInstruction* zero = graph_->GetIntConstant(0);
776 HInstruction* last;
777 block->AddInstruction(last = new (&allocator_) HAbove(zero, parameter));
778 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
779 block->AddInstruction(last = new (&allocator_) HAbove(parameter, zero));
780 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
781 block->AddInstruction(last = new (&allocator_) HAboveOrEqual(zero, parameter));
782 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
783 block->AddInstruction(last = new (&allocator_) HAboveOrEqual(parameter, zero));
784 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
785 block->AddInstruction(last = new (&allocator_) HBelow(zero, parameter));
786 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
787 block->AddInstruction(last = new (&allocator_) HBelow(parameter, zero));
788 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
789 block->AddInstruction(last = new (&allocator_) HBelowOrEqual(zero, parameter));
790 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
791 block->AddInstruction(last = new (&allocator_) HBelowOrEqual(parameter, zero));
792 block->AddInstruction(new (&allocator_) HDeoptimize(last, 0));
793
794 entry_block->AddInstruction(new (&allocator_) HGoto());
795 block->AddInstruction(new (&allocator_) HReturn(zero));
796 exit_block->AddInstruction(new (&allocator_) HExit());
797
798 const std::string expected_before =
799 "BasicBlock 0, succ: 1\n"
800 " 0: ParameterValue [16, 14, 12, 10, 8, 6, 4, 2]\n"
801 " 1: IntConstant [19, 16, 14, 12, 10, 8, 6, 4, 2]\n"
802 " 18: Goto 1\n"
803 "BasicBlock 1, pred: 0, succ: 2\n"
804 " 2: Above(1, 0) [3]\n"
805 " 3: Deoptimize(2)\n"
806 " 4: Above(0, 1) [5]\n"
807 " 5: Deoptimize(4)\n"
808 " 6: AboveOrEqual(1, 0) [7]\n"
809 " 7: Deoptimize(6)\n"
810 " 8: AboveOrEqual(0, 1) [9]\n"
811 " 9: Deoptimize(8)\n"
812 " 10: Below(1, 0) [11]\n"
813 " 11: Deoptimize(10)\n"
814 " 12: Below(0, 1) [13]\n"
815 " 13: Deoptimize(12)\n"
816 " 14: BelowOrEqual(1, 0) [15]\n"
817 " 15: Deoptimize(14)\n"
818 " 16: BelowOrEqual(0, 1) [17]\n"
819 " 17: Deoptimize(16)\n"
820 " 19: Return(1)\n"
821 "BasicBlock 2, pred: 1\n"
822 " 20: Exit\n";
823
824 const std::string expected_after_cf =
825 "BasicBlock 0, succ: 1\n"
826 " 0: ParameterValue [16, 10, 6, 4]\n"
827 " 1: IntConstant [13, 3, 19, 16, 10, 6, 4]\n"
828 " 21: IntConstant [15, 9]\n"
829 " 18: Goto 1\n"
830 "BasicBlock 1, pred: 0, succ: 2\n"
831 " 3: Deoptimize(1)\n"
832 " 4: Above(0, 1) [5]\n"
833 " 5: Deoptimize(4)\n"
834 " 6: AboveOrEqual(1, 0) [7]\n"
835 " 7: Deoptimize(6)\n"
836 " 9: Deoptimize(21)\n"
837 " 10: Below(1, 0) [11]\n"
838 " 11: Deoptimize(10)\n"
839 " 13: Deoptimize(1)\n"
840 " 15: Deoptimize(21)\n"
841 " 16: BelowOrEqual(0, 1) [17]\n"
842 " 17: Deoptimize(16)\n"
843 " 19: Return(1)\n"
844 "BasicBlock 2, pred: 1\n"
845 " 20: Exit\n";
846
847 const std::string expected_after_dce = expected_after_cf;
848
849 auto check_after_cf = [](HGraph* graph) {
850 CHECK(graph != nullptr);
851 };
852
853 TestCodeOnReadyGraph(expected_before,
854 expected_after_cf,
855 expected_after_dce,
856 check_after_cf);
857}
858
Roland Levillain556c3d12014-09-18 15:25:07 +0100859} // namespace art