blob: 6853d54c48829a1194493a8e2a0ab711880a5f10 [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
Roland Levillain75be2832014-10-17 17:02:00 +010019#include "code_generator_x86.h"
20#include "constant_folding.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010021#include "dead_code_elimination.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000022#include "driver/compiler_options.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010023#include "graph_checker.h"
24#include "optimizing_unit_test.h"
Roland Levillain75be2832014-10-17 17:02:00 +010025#include "pretty_printer.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010026
27#include "gtest/gtest.h"
28
29namespace art {
30
31static void TestCode(const uint16_t* data,
32 const std::string& expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +010033 const std::string& expected_after_cf,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010034 const std::string& expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +010035 std::function<void(HGraph*)> check_after_cf,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010036 Primitive::Type return_type = Primitive::kPrimInt) {
Roland Levillain556c3d12014-09-18 15:25:07 +010037 ArenaPool pool;
38 ArenaAllocator allocator(&pool);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010039 HGraph* graph = CreateCFG(&allocator, data, return_type);
Roland Levillain556c3d12014-09-18 15:25:07 +010040 ASSERT_NE(graph, nullptr);
41
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000042 graph->TryBuildingSsa();
Roland Levillain556c3d12014-09-18 15:25:07 +010043
44 StringPrettyPrinter printer_before(graph);
45 printer_before.VisitInsertionOrder();
46 std::string actual_before = printer_before.str();
47 ASSERT_EQ(expected_before, actual_before);
48
Calin Juravlecd6dffe2015-01-08 17:35:35 +000049 x86::CodeGeneratorX86 codegen(graph, CompilerOptions());
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000050 HConstantFolding(graph).Run();
Nicolas Geoffray942a3782014-12-17 17:10:47 +000051 SSAChecker ssa_checker_cf(&allocator, graph);
52 ssa_checker_cf.Run();
53 ASSERT_TRUE(ssa_checker_cf.IsValid());
Roland Levillain556c3d12014-09-18 15:25:07 +010054
Roland Levillain75be2832014-10-17 17:02:00 +010055 StringPrettyPrinter printer_after_cf(graph);
56 printer_after_cf.VisitInsertionOrder();
57 std::string actual_after_cf = printer_after_cf.str();
58 ASSERT_EQ(expected_after_cf, actual_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +010059
Roland Levillain75be2832014-10-17 17:02:00 +010060 check_after_cf(graph);
Roland Levillain93445682014-10-06 19:24:02 +010061
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +000062 HDeadCodeElimination(graph).Run();
Nicolas Geoffray942a3782014-12-17 17:10:47 +000063 SSAChecker ssa_checker_dce(&allocator, graph);
64 ssa_checker_dce.Run();
65 ASSERT_TRUE(ssa_checker_dce.IsValid());
Roland Levillain556c3d12014-09-18 15:25:07 +010066
67 StringPrettyPrinter printer_after_dce(graph);
68 printer_after_dce.VisitInsertionOrder();
69 std::string actual_after_dce = printer_after_dce.str();
70 ASSERT_EQ(expected_after_dce, actual_after_dce);
Roland Levillain556c3d12014-09-18 15:25:07 +010071}
72
73
74/**
Roland Levillain9240d6a2014-10-20 16:47:04 +010075 * Tiny three-register program exercising int constant folding on negation.
76 *
77 * 16-bit
78 * offset
79 * ------
80 * v0 <- 1 0. const/4 v0, #+1
81 * v1 <- -v0 1. neg-int v0, v1
82 * return v1 2. return v1
83 */
84TEST(ConstantFolding, IntConstantFoldingNegation) {
85 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
86 Instruction::CONST_4 | 0 << 8 | 1 << 12,
87 Instruction::NEG_INT | 1 << 8 | 0 << 12,
88 Instruction::RETURN | 1 << 8);
89
90 std::string expected_before =
91 "BasicBlock 0, succ: 1\n"
92 " 2: IntConstant [5]\n"
93 " 10: SuspendCheck\n"
94 " 11: Goto 1\n"
95 "BasicBlock 1, pred: 0, succ: 2\n"
96 " 5: Neg(2) [8]\n"
97 " 8: Return(5)\n"
98 "BasicBlock 2, pred: 1\n"
99 " 9: Exit\n";
100
101 // Expected difference after constant folding.
102 diff_t expected_cf_diff = {
103 { " 2: IntConstant [5]\n", " 2: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000104 { " 10: SuspendCheck\n", " 10: SuspendCheck\n"
105 " 12: IntConstant [8]\n" },
106 { " 5: Neg(2) [8]\n", removed },
Roland Levillain9240d6a2014-10-20 16:47:04 +0100107 { " 8: Return(5)\n", " 8: Return(12)\n" }
108 };
109 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
110
111 // Check the value of the computed constant.
112 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000113 HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain9240d6a2014-10-20 16:47:04 +0100114 ASSERT_TRUE(inst->IsIntConstant());
115 ASSERT_EQ(inst->AsIntConstant()->GetValue(), -1);
116 };
117
118 // Expected difference after dead code elimination.
119 diff_t expected_dce_diff = {
120 { " 2: IntConstant\n", removed },
121 };
122 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
123
124 TestCode(data,
125 expected_before,
126 expected_after_cf,
127 expected_after_dce,
128 check_after_cf);
129}
130
131/**
Roland Levillain556c3d12014-09-18 15:25:07 +0100132 * Tiny three-register program exercising int constant folding on addition.
133 *
134 * 16-bit
135 * offset
136 * ------
137 * v0 <- 1 0. const/4 v0, #+1
138 * v1 <- 2 1. const/4 v1, #+2
139 * v2 <- v0 + v1 2. add-int v2, v0, v1
140 * return v2 4. return v2
141 */
Roland Levillain75be2832014-10-17 17:02:00 +0100142TEST(ConstantFolding, IntConstantFoldingOnAddition1) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100143 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
144 Instruction::CONST_4 | 0 << 8 | 1 << 12,
145 Instruction::CONST_4 | 1 << 8 | 2 << 12,
146 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
147 Instruction::RETURN | 2 << 8);
148
149 std::string expected_before =
150 "BasicBlock 0, succ: 1\n"
151 " 3: IntConstant [9]\n"
152 " 5: IntConstant [9]\n"
153 " 14: SuspendCheck\n"
154 " 15: Goto 1\n"
155 "BasicBlock 1, pred: 0, succ: 2\n"
156 " 9: Add(3, 5) [12]\n"
157 " 12: Return(9)\n"
158 "BasicBlock 2, pred: 1\n"
159 " 13: Exit\n";
160
Roland Levillain75be2832014-10-17 17:02:00 +0100161 // Expected difference after constant folding.
162 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100163 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
164 { " 5: IntConstant [9]\n", " 5: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000165 { " 14: SuspendCheck\n", " 14: SuspendCheck\n"
166 " 16: IntConstant [12]\n" },
167 { " 9: Add(3, 5) [12]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100168 { " 12: Return(9)\n", " 12: Return(16)\n" }
169 };
Roland Levillain75be2832014-10-17 17:02:00 +0100170 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100171
Roland Levillain93445682014-10-06 19:24:02 +0100172 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100173 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000174 HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100175 ASSERT_TRUE(inst->IsIntConstant());
176 ASSERT_EQ(inst->AsIntConstant()->GetValue(), 3);
177 };
178
Roland Levillain556c3d12014-09-18 15:25:07 +0100179 // Expected difference after dead code elimination.
180 diff_t expected_dce_diff = {
181 { " 3: IntConstant\n", removed },
182 { " 5: IntConstant\n", removed }
183 };
Roland Levillain75be2832014-10-17 17:02:00 +0100184 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100185
Roland Levillain93445682014-10-06 19:24:02 +0100186 TestCode(data,
187 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100188 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100189 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100190 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100191}
192
193/**
194 * Small three-register program exercising int constant folding on addition.
195 *
196 * 16-bit
197 * offset
198 * ------
199 * v0 <- 1 0. const/4 v0, #+1
200 * v1 <- 2 1. const/4 v1, #+2
201 * v0 <- v0 + v1 2. add-int/2addr v0, v1
David Brazdil8d5b8b22015-03-24 10:51:52 +0000202 * v1 <- 4 3. const/4 v1, #+4
203 * v2 <- 5 4. const/4 v2, #+5
Roland Levillain556c3d12014-09-18 15:25:07 +0100204 * v1 <- v1 + v2 5. add-int/2addr v1, v2
205 * v2 <- v0 + v1 6. add-int v2, v0, v1
206 * return v2 8. return v2
207 */
Roland Levillain75be2832014-10-17 17:02:00 +0100208TEST(ConstantFolding, IntConstantFoldingOnAddition2) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100209 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
210 Instruction::CONST_4 | 0 << 8 | 1 << 12,
211 Instruction::CONST_4 | 1 << 8 | 2 << 12,
212 Instruction::ADD_INT_2ADDR | 0 << 8 | 1 << 12,
David Brazdil8d5b8b22015-03-24 10:51:52 +0000213 Instruction::CONST_4 | 1 << 8 | 4 << 12,
214 Instruction::CONST_4 | 2 << 8 | 5 << 12,
Roland Levillain556c3d12014-09-18 15:25:07 +0100215 Instruction::ADD_INT_2ADDR | 1 << 8 | 2 << 12,
216 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
217 Instruction::RETURN | 2 << 8);
218
219 std::string expected_before =
220 "BasicBlock 0, succ: 1\n"
221 " 3: IntConstant [9]\n"
222 " 5: IntConstant [9]\n"
223 " 11: IntConstant [17]\n"
224 " 13: IntConstant [17]\n"
225 " 26: SuspendCheck\n"
226 " 27: Goto 1\n"
227 "BasicBlock 1, pred: 0, succ: 2\n"
228 " 9: Add(3, 5) [21]\n"
229 " 17: Add(11, 13) [21]\n"
230 " 21: Add(9, 17) [24]\n"
231 " 24: Return(21)\n"
232 "BasicBlock 2, pred: 1\n"
233 " 25: Exit\n";
234
Roland Levillain75be2832014-10-17 17:02:00 +0100235 // Expected difference after constant folding.
236 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100237 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
238 { " 5: IntConstant [9]\n", " 5: IntConstant\n" },
239 { " 11: IntConstant [17]\n", " 11: IntConstant\n" },
240 { " 13: IntConstant [17]\n", " 13: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000241 { " 26: SuspendCheck\n", " 26: SuspendCheck\n"
242 " 28: IntConstant\n"
243 " 29: IntConstant\n"
244 " 30: IntConstant [24]\n" },
245 { " 9: Add(3, 5) [21]\n", removed },
246 { " 17: Add(11, 13) [21]\n", removed },
247 { " 21: Add(9, 17) [24]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100248 { " 24: Return(21)\n", " 24: Return(30)\n" }
249 };
Roland Levillain75be2832014-10-17 17:02:00 +0100250 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100251
Roland Levillain93445682014-10-06 19:24:02 +0100252 // Check the values of the computed constants.
Roland Levillain75be2832014-10-17 17:02:00 +0100253 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000254 HInstruction* inst1 = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100255 ASSERT_TRUE(inst1->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000256 ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 12);
257 HInstruction* inst2 = inst1->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100258 ASSERT_TRUE(inst2->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000259 ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 9);
260 HInstruction* inst3 = inst2->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100261 ASSERT_TRUE(inst3->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000262 ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 3);
Roland Levillain93445682014-10-06 19:24:02 +0100263 };
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 { " 11: IntConstant\n", removed },
270 { " 13: IntConstant\n", removed },
271 { " 28: IntConstant\n", removed },
272 { " 29: IntConstant\n", removed }
273 };
Roland Levillain75be2832014-10-17 17:02:00 +0100274 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100275
Roland Levillain93445682014-10-06 19:24:02 +0100276 TestCode(data,
277 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100278 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100279 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100280 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100281}
282
283/**
284 * Tiny three-register program exercising int constant folding on subtraction.
285 *
286 * 16-bit
287 * offset
288 * ------
289 * v0 <- 3 0. const/4 v0, #+3
290 * v1 <- 2 1. const/4 v1, #+2
291 * v2 <- v0 - v1 2. sub-int v2, v0, v1
292 * return v2 4. return v2
293 */
Roland Levillain75be2832014-10-17 17:02:00 +0100294TEST(ConstantFolding, IntConstantFoldingOnSubtraction) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100295 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
296 Instruction::CONST_4 | 0 << 8 | 3 << 12,
297 Instruction::CONST_4 | 1 << 8 | 2 << 12,
298 Instruction::SUB_INT | 2 << 8, 0 | 1 << 8,
299 Instruction::RETURN | 2 << 8);
300
301 std::string expected_before =
302 "BasicBlock 0, succ: 1\n"
303 " 3: IntConstant [9]\n"
304 " 5: IntConstant [9]\n"
305 " 14: SuspendCheck\n"
306 " 15: Goto 1\n"
307 "BasicBlock 1, pred: 0, succ: 2\n"
308 " 9: Sub(3, 5) [12]\n"
309 " 12: Return(9)\n"
310 "BasicBlock 2, pred: 1\n"
311 " 13: Exit\n";
312
Roland Levillain75be2832014-10-17 17:02:00 +0100313 // Expected difference after constant folding.
314 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100315 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
316 { " 5: IntConstant [9]\n", " 5: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000317 { " 14: SuspendCheck\n", " 14: SuspendCheck\n"
318 " 16: IntConstant [12]\n" },
319 { " 9: Sub(3, 5) [12]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100320 { " 12: Return(9)\n", " 12: Return(16)\n" }
321 };
Roland Levillain75be2832014-10-17 17:02:00 +0100322 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100323
Roland Levillain93445682014-10-06 19:24:02 +0100324 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100325 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000326 HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100327 ASSERT_TRUE(inst->IsIntConstant());
328 ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1);
329 };
330
Roland Levillain556c3d12014-09-18 15:25:07 +0100331 // Expected difference after dead code elimination.
332 diff_t expected_dce_diff = {
333 { " 3: IntConstant\n", removed },
334 { " 5: IntConstant\n", removed }
335 };
Roland Levillain75be2832014-10-17 17:02:00 +0100336 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100337
Roland Levillain93445682014-10-06 19:24:02 +0100338 TestCode(data,
339 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100340 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100341 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100342 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100343}
344
Roland Levillain556c3d12014-09-18 15:25:07 +0100345/**
346 * Tiny three-register-pair program exercising long constant folding
347 * on addition.
348 *
349 * 16-bit
350 * offset
351 * ------
352 * (v0, v1) <- 1 0. const-wide/16 v0, #+1
353 * (v2, v3) <- 2 2. const-wide/16 v2, #+2
354 * (v4, v5) <-
355 * (v0, v1) + (v1, v2) 4. add-long v4, v0, v2
356 * return (v4, v5) 6. return-wide v4
357 */
Roland Levillain75be2832014-10-17 17:02:00 +0100358TEST(ConstantFolding, LongConstantFoldingOnAddition) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100359 const uint16_t data[] = SIX_REGISTERS_CODE_ITEM(
360 Instruction::CONST_WIDE_16 | 0 << 8, 1,
361 Instruction::CONST_WIDE_16 | 2 << 8, 2,
362 Instruction::ADD_LONG | 4 << 8, 0 | 2 << 8,
363 Instruction::RETURN_WIDE | 4 << 8);
364
365 std::string expected_before =
366 "BasicBlock 0, succ: 1\n"
367 " 6: LongConstant [12]\n"
368 " 8: LongConstant [12]\n"
369 " 17: SuspendCheck\n"
370 " 18: Goto 1\n"
371 "BasicBlock 1, pred: 0, succ: 2\n"
372 " 12: Add(6, 8) [15]\n"
373 " 15: Return(12)\n"
374 "BasicBlock 2, pred: 1\n"
375 " 16: Exit\n";
376
Roland Levillain75be2832014-10-17 17:02:00 +0100377 // Expected difference after constant folding.
378 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100379 { " 6: LongConstant [12]\n", " 6: LongConstant\n" },
380 { " 8: LongConstant [12]\n", " 8: LongConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000381 { " 17: SuspendCheck\n", " 17: SuspendCheck\n"
382 " 19: LongConstant [15]\n" },
383 { " 12: Add(6, 8) [15]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100384 { " 15: Return(12)\n", " 15: Return(19)\n" }
385 };
Roland Levillain75be2832014-10-17 17:02:00 +0100386 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100387
Roland Levillain93445682014-10-06 19:24:02 +0100388 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100389 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000390 HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100391 ASSERT_TRUE(inst->IsLongConstant());
392 ASSERT_EQ(inst->AsLongConstant()->GetValue(), 3);
393 };
394
Roland Levillain556c3d12014-09-18 15:25:07 +0100395 // Expected difference after dead code elimination.
396 diff_t expected_dce_diff = {
397 { " 6: LongConstant\n", removed },
398 { " 8: LongConstant\n", removed }
399 };
Roland Levillain75be2832014-10-17 17:02:00 +0100400 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100401
Roland Levillain93445682014-10-06 19:24:02 +0100402 TestCode(data,
403 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100404 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100405 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100406 check_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100407 Primitive::kPrimLong);
Roland Levillain556c3d12014-09-18 15:25:07 +0100408}
409
410/**
411 * Tiny three-register-pair program exercising long constant folding
412 * on subtraction.
413 *
414 * 16-bit
415 * offset
416 * ------
417 * (v0, v1) <- 3 0. const-wide/16 v0, #+3
418 * (v2, v3) <- 2 2. const-wide/16 v2, #+2
419 * (v4, v5) <-
420 * (v0, v1) - (v1, v2) 4. sub-long v4, v0, v2
421 * return (v4, v5) 6. return-wide v4
422 */
Roland Levillain75be2832014-10-17 17:02:00 +0100423TEST(ConstantFolding, LongConstantFoldingOnSubtraction) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100424 const uint16_t data[] = SIX_REGISTERS_CODE_ITEM(
425 Instruction::CONST_WIDE_16 | 0 << 8, 3,
426 Instruction::CONST_WIDE_16 | 2 << 8, 2,
427 Instruction::SUB_LONG | 4 << 8, 0 | 2 << 8,
428 Instruction::RETURN_WIDE | 4 << 8);
429
430 std::string expected_before =
431 "BasicBlock 0, succ: 1\n"
432 " 6: LongConstant [12]\n"
433 " 8: LongConstant [12]\n"
434 " 17: SuspendCheck\n"
435 " 18: Goto 1\n"
436 "BasicBlock 1, pred: 0, succ: 2\n"
437 " 12: Sub(6, 8) [15]\n"
438 " 15: Return(12)\n"
439 "BasicBlock 2, pred: 1\n"
440 " 16: Exit\n";
441
Roland Levillain75be2832014-10-17 17:02:00 +0100442 // Expected difference after constant folding.
443 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100444 { " 6: LongConstant [12]\n", " 6: LongConstant\n" },
445 { " 8: LongConstant [12]\n", " 8: LongConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000446 { " 17: SuspendCheck\n", " 17: SuspendCheck\n"
447 " 19: LongConstant [15]\n" },
448 { " 12: Sub(6, 8) [15]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100449 { " 15: Return(12)\n", " 15: Return(19)\n" }
450 };
Roland Levillain75be2832014-10-17 17:02:00 +0100451 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100452
Roland Levillain93445682014-10-06 19:24:02 +0100453 // Check the value of the computed constant.
Roland Levillain75be2832014-10-17 17:02:00 +0100454 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100456 ASSERT_TRUE(inst->IsLongConstant());
457 ASSERT_EQ(inst->AsLongConstant()->GetValue(), 1);
458 };
459
Roland Levillain556c3d12014-09-18 15:25:07 +0100460 // Expected difference after dead code elimination.
461 diff_t expected_dce_diff = {
462 { " 6: LongConstant\n", removed },
463 { " 8: LongConstant\n", removed }
464 };
Roland Levillain75be2832014-10-17 17:02:00 +0100465 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100466
Roland Levillain93445682014-10-06 19:24:02 +0100467 TestCode(data,
468 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100469 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100470 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100471 check_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100472 Primitive::kPrimLong);
Roland Levillain556c3d12014-09-18 15:25:07 +0100473}
474
475/**
476 * Three-register program with jumps leading to the creation of many
477 * blocks.
478 *
479 * The intent of this test is to ensure that all constant expressions
480 * are actually evaluated at compile-time, thanks to the reverse
481 * (forward) post-order traversal of the the dominator tree.
482 *
483 * 16-bit
484 * offset
485 * ------
David Brazdil8d5b8b22015-03-24 10:51:52 +0000486 * v0 <- 1 0. const/4 v0, #+1
487 * v1 <- 2 1. const/4 v1, #+2
Roland Levillain556c3d12014-09-18 15:25:07 +0100488 * v2 <- v0 + v1 2. add-int v2, v0, v1
489 * goto L2 4. goto +4
David Brazdil8d5b8b22015-03-24 10:51:52 +0000490 * L1: v1 <- v0 + 5 5. add-int/lit16 v1, v0, #+5
Roland Levillain556c3d12014-09-18 15:25:07 +0100491 * goto L3 7. goto +4
David Brazdil8d5b8b22015-03-24 10:51:52 +0000492 * L2: v0 <- v2 + 4 8. add-int/lit16 v0, v2, #+4
Roland Levillain556c3d12014-09-18 15:25:07 +0100493 * goto L1 10. goto +(-5)
David Brazdil8d5b8b22015-03-24 10:51:52 +0000494 * L3: v2 <- v1 + 8 11. add-int/lit16 v2, v1, #+8
Roland Levillain556c3d12014-09-18 15:25:07 +0100495 * return v2 13. return v2
496 */
Roland Levillain75be2832014-10-17 17:02:00 +0100497TEST(ConstantFolding, IntConstantFoldingAndJumps) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100498 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
David Brazdil8d5b8b22015-03-24 10:51:52 +0000499 Instruction::CONST_4 | 0 << 8 | 1 << 12,
500 Instruction::CONST_4 | 1 << 8 | 2 << 12,
Roland Levillain556c3d12014-09-18 15:25:07 +0100501 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
502 Instruction::GOTO | 4 << 8,
David Brazdil8d5b8b22015-03-24 10:51:52 +0000503 Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 5,
Roland Levillain556c3d12014-09-18 15:25:07 +0100504 Instruction::GOTO | 4 << 8,
David Brazdil8d5b8b22015-03-24 10:51:52 +0000505 Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 4,
Roland Levillain556c3d12014-09-18 15:25:07 +0100506 static_cast<uint16_t>(Instruction::GOTO | -5 << 8),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000507 Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 8,
Roland Levillain556c3d12014-09-18 15:25:07 +0100508 Instruction::RETURN | 2 << 8);
509
510 std::string expected_before =
511 "BasicBlock 0, succ: 1\n"
David Brazdil8d5b8b22015-03-24 10:51:52 +0000512 " 3: IntConstant [9]\n" // v0 <- 1
513 " 5: IntConstant [9]\n" // v1 <- 2
514 " 13: IntConstant [14]\n" // const 5
515 " 18: IntConstant [19]\n" // const 4
516 " 24: IntConstant [25]\n" // const 8
Roland Levillain556c3d12014-09-18 15:25:07 +0100517 " 30: SuspendCheck\n"
518 " 31: Goto 1\n"
519 "BasicBlock 1, pred: 0, succ: 3\n"
David Brazdil8d5b8b22015-03-24 10:51:52 +0000520 " 9: Add(3, 5) [19]\n" // v2 <- v0 + v1 = 1 + 2 = 3
Roland Levillain93445682014-10-06 19:24:02 +0100521 " 11: Goto 3\n" // goto L2
522 "BasicBlock 2, pred: 3, succ: 4\n" // L1:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000523 " 14: Add(19, 13) [25]\n" // v1 <- v0 + 3 = 7 + 5 = 12
Roland Levillain93445682014-10-06 19:24:02 +0100524 " 16: Goto 4\n" // goto L3
525 "BasicBlock 3, pred: 1, succ: 2\n" // L2:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 " 19: Add(9, 18) [14]\n" // v0 <- v2 + 2 = 3 + 4 = 7
Roland Levillain556c3d12014-09-18 15:25:07 +0100527 " 21: SuspendCheck\n"
Roland Levillain93445682014-10-06 19:24:02 +0100528 " 22: Goto 2\n" // goto L1
529 "BasicBlock 4, pred: 2, succ: 5\n" // L3:
David Brazdil8d5b8b22015-03-24 10:51:52 +0000530 " 25: Add(14, 24) [28]\n" // v2 <- v1 + 4 = 12 + 8 = 20
Roland Levillain93445682014-10-06 19:24:02 +0100531 " 28: Return(25)\n" // return v2
Roland Levillain556c3d12014-09-18 15:25:07 +0100532 "BasicBlock 5, pred: 4\n"
533 " 29: Exit\n";
534
Roland Levillain75be2832014-10-17 17:02:00 +0100535 // Expected difference after constant folding.
536 diff_t expected_cf_diff = {
Roland Levillain556c3d12014-09-18 15:25:07 +0100537 { " 3: IntConstant [9]\n", " 3: IntConstant\n" },
538 { " 5: IntConstant [9]\n", " 5: IntConstant []\n" },
539 { " 13: IntConstant [14]\n", " 13: IntConstant\n" },
540 { " 18: IntConstant [19]\n", " 18: IntConstant\n" },
541 { " 24: IntConstant [25]\n", " 24: IntConstant\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542 { " 30: SuspendCheck\n", " 30: SuspendCheck\n"
543 " 32: IntConstant []\n"
544 " 33: IntConstant []\n"
545 " 34: IntConstant\n"
546 " 35: IntConstant [28]\n" },
547 { " 9: Add(3, 5) [19]\n", removed },
548 { " 14: Add(19, 13) [25]\n", removed },
549 { " 19: Add(9, 18) [14]\n", removed },
550 { " 25: Add(14, 24) [28]\n", removed },
Roland Levillain556c3d12014-09-18 15:25:07 +0100551 { " 28: Return(25)\n", " 28: Return(35)\n"}
552 };
Roland Levillain75be2832014-10-17 17:02:00 +0100553 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100554
Roland Levillain93445682014-10-06 19:24:02 +0100555 // Check the values of the computed constants.
Roland Levillain75be2832014-10-17 17:02:00 +0100556 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000557 HInstruction* inst1 = graph->GetBlock(4)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100558 ASSERT_TRUE(inst1->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000559 ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 20);
560 HInstruction* inst2 = inst1->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100561 ASSERT_TRUE(inst2->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000562 ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 12);
563 HInstruction* inst3 = inst2->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100564 ASSERT_TRUE(inst3->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000565 ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 7);
566 HInstruction* inst4 = inst3->GetPrevious();
Roland Levillain93445682014-10-06 19:24:02 +0100567 ASSERT_TRUE(inst4->IsIntConstant());
David Brazdil8d5b8b22015-03-24 10:51:52 +0000568 ASSERT_EQ(inst4->AsIntConstant()->GetValue(), 3);
Roland Levillain93445682014-10-06 19:24:02 +0100569 };
570
Roland Levillain556c3d12014-09-18 15:25:07 +0100571 // Expected difference after dead code elimination.
572 diff_t expected_dce_diff = {
573 { " 3: IntConstant\n", removed },
574 { " 13: IntConstant\n", removed },
575 { " 18: IntConstant\n", removed },
576 { " 24: IntConstant\n", removed },
577 { " 34: IntConstant\n", removed },
578 };
Roland Levillain75be2832014-10-17 17:02:00 +0100579 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100580
Roland Levillain93445682014-10-06 19:24:02 +0100581 TestCode(data,
582 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100583 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100584 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100585 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100586}
587
588
589/**
590 * Three-register program with a constant (static) condition.
591 *
592 * 16-bit
593 * offset
594 * ------
595 * v1 <- 1 0. const/4 v1, #+1
596 * v0 <- 0 1. const/4 v0, #+0
597 * if v1 >= 0 goto L1 2. if-gez v1, +3
598 * v0 <- v1 4. move v0, v1
599 * L1: v2 <- v0 + v1 5. add-int v2, v0, v1
600 * return-void 7. return
601 */
Roland Levillain75be2832014-10-17 17:02:00 +0100602TEST(ConstantFolding, ConstantCondition) {
Roland Levillain556c3d12014-09-18 15:25:07 +0100603 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
604 Instruction::CONST_4 | 1 << 8 | 1 << 12,
605 Instruction::CONST_4 | 0 << 8 | 0 << 12,
606 Instruction::IF_GEZ | 1 << 8, 3,
607 Instruction::MOVE | 0 << 8 | 1 << 12,
608 Instruction::ADD_INT | 2 << 8, 0 | 1 << 8,
609 Instruction::RETURN_VOID);
610
611 std::string expected_before =
612 "BasicBlock 0, succ: 1\n"
613 " 3: IntConstant [15, 22, 8]\n"
614 " 5: IntConstant [22, 8]\n"
615 " 19: SuspendCheck\n"
616 " 20: Goto 1\n"
617 "BasicBlock 1, pred: 0, succ: 5, 2\n"
618 " 8: GreaterThanOrEqual(3, 5) [9]\n"
619 " 9: If(8)\n"
620 "BasicBlock 2, pred: 1, succ: 3\n"
621 " 12: Goto 3\n"
622 "BasicBlock 3, pred: 2, 5, succ: 4\n"
623 " 22: Phi(3, 5) [15]\n"
624 " 15: Add(22, 3)\n"
625 " 17: ReturnVoid\n"
626 "BasicBlock 4, pred: 3\n"
627 " 18: Exit\n"
628 "BasicBlock 5, pred: 1, succ: 3\n"
629 " 21: Goto 3\n";
630
Roland Levillain75be2832014-10-17 17:02:00 +0100631 // Expected difference after constant folding.
632 diff_t expected_cf_diff = {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000633 { " 3: IntConstant [15, 22, 8]\n", " 3: IntConstant [9, 15, 22]\n" },
Roland Levillain556c3d12014-09-18 15:25:07 +0100634 { " 5: IntConstant [22, 8]\n", " 5: IntConstant [22]\n" },
David Brazdil8d5b8b22015-03-24 10:51:52 +0000635 { " 8: GreaterThanOrEqual(3, 5) [9]\n", removed },
636 { " 9: If(8)\n", " 9: If(3)\n" }
Roland Levillain556c3d12014-09-18 15:25:07 +0100637 };
Roland Levillain75be2832014-10-17 17:02:00 +0100638 std::string expected_after_cf = Patch(expected_before, expected_cf_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100639
Roland Levillain93445682014-10-06 19:24:02 +0100640 // Check the values of the computed constants.
Roland Levillain75be2832014-10-17 17:02:00 +0100641 auto check_after_cf = [](HGraph* graph) {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000642 HInstruction* inst = graph->GetBlock(1)->GetFirstInstruction()->InputAt(0);
Roland Levillain93445682014-10-06 19:24:02 +0100643 ASSERT_TRUE(inst->IsIntConstant());
644 ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1);
645 };
646
Roland Levillain556c3d12014-09-18 15:25:07 +0100647 // Expected difference after dead code elimination.
648 diff_t expected_dce_diff = {
David Brazdil8d5b8b22015-03-24 10:51:52 +0000649 { " 3: IntConstant [9, 15, 22]\n", " 3: IntConstant [9, 22]\n" },
650 { " 22: Phi(3, 5) [15]\n", " 22: Phi(3, 5)\n" },
651 { " 15: Add(22, 3)\n", removed }
Roland Levillain556c3d12014-09-18 15:25:07 +0100652 };
Roland Levillain75be2832014-10-17 17:02:00 +0100653 std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff);
Roland Levillain556c3d12014-09-18 15:25:07 +0100654
Roland Levillain93445682014-10-06 19:24:02 +0100655 TestCode(data,
656 expected_before,
Roland Levillain75be2832014-10-17 17:02:00 +0100657 expected_after_cf,
Roland Levillain93445682014-10-06 19:24:02 +0100658 expected_after_dce,
Roland Levillain75be2832014-10-17 17:02:00 +0100659 check_after_cf);
Roland Levillain556c3d12014-09-18 15:25:07 +0100660}
661
662} // namespace art