blob: 9be2197ad7c890b79e34fe7550f8b88d6cd2b106 [file] [log] [blame]
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +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
17#include "base/stringprintf.h"
18#include "builder.h"
19#include "dex_file.h"
20#include "dex_instruction.h"
21#include "nodes.h"
22#include "optimizing_unit_test.h"
23#include "pretty_printer.h"
24#include "ssa_builder.h"
25#include "utils/arena_allocator.h"
26
27#include "gtest/gtest.h"
28
29namespace art {
30
31class StringPrettyPrinter : public HPrettyPrinter {
32 public:
33 explicit StringPrettyPrinter(HGraph* graph) : HPrettyPrinter(graph), str_("") {}
34
35 virtual void PrintInt(int value) {
36 str_ += StringPrintf("%d", value);
37 }
38
39 virtual void PrintString(const char* value) {
40 str_ += value;
41 }
42
43 virtual void PrintNewLine() {
44 str_ += '\n';
45 }
46
47 void Clear() { str_.clear(); }
48
49 std::string str() const { return str_; }
50
51 virtual void VisitIntConstant(HIntConstant* constant) {
52 PrintPreInstruction(constant);
53 str_ += constant->DebugName();
54 str_ += " ";
55 PrintInt(constant->GetValue());
56 PrintPostInstruction(constant);
57 }
58
59 private:
60 std::string str_;
61
62 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
63};
64
65static void ReNumberInstructions(HGraph* graph) {
66 int id = 0;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010067 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
68 HBasicBlock* block = graph->GetBlocks().Get(i);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010069 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
70 it.Current()->SetId(id++);
71 }
72 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
73 it.Current()->SetId(id++);
74 }
75 }
76}
77
78static void TestCode(const uint16_t* data, const char* expected) {
79 ArenaPool pool;
80 ArenaAllocator allocator(&pool);
81 HGraphBuilder builder(&allocator);
82 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
83 HGraph* graph = builder.BuildGraph(*item);
84 ASSERT_NE(graph, nullptr);
85 graph->BuildDominatorTree();
86 graph->TransformToSSA();
87 ReNumberInstructions(graph);
88
89 StringPrettyPrinter printer(graph);
90 printer.VisitInsertionOrder();
91
92 ASSERT_STREQ(expected, printer.str().c_str());
93}
94
95TEST(SsaTest, CFG1) {
96 // Test that we get rid of loads and stores.
97 const char* expected =
98 "BasicBlock 0, succ: 1\n"
99 " 0: IntConstant 0 [2, 2]\n"
100 " 1: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100101 "BasicBlock 1, pred: 0, succ: 2, 5\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 " 2: Equal(0, 0) [3]\n"
103 " 3: If(2)\n"
104 "BasicBlock 2, pred: 1, succ: 3\n"
105 " 4: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100106 "BasicBlock 3, pred: 2, 5, succ: 4\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 " 5: ReturnVoid\n"
108 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 " 6: Exit\n"
110 // Synthesized block to avoid critical edge.
111 "BasicBlock 5, pred: 1, succ: 3\n"
112 " 7: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113
114 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
115 Instruction::CONST_4 | 0 | 0,
116 Instruction::IF_EQ, 3,
117 Instruction::GOTO | 0x100,
118 Instruction::RETURN_VOID);
119
120 TestCode(data, expected);
121}
122
123TEST(SsaTest, CFG2) {
124 // Test that we create a phi for the join block of an if control flow instruction
125 // when there is only code in the else branch.
126 const char* expected =
127 "BasicBlock 0, succ: 1\n"
128 " 0: IntConstant 0 [6, 3, 3]\n"
129 " 1: IntConstant 4 [6]\n"
130 " 2: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131 "BasicBlock 1, pred: 0, succ: 2, 5\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100132 " 3: Equal(0, 0) [4]\n"
133 " 4: If(3)\n"
134 "BasicBlock 2, pred: 1, succ: 3\n"
135 " 5: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100136 "BasicBlock 3, pred: 2, 5, succ: 4\n"
137 " 6: Phi(1, 0) [7]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 " 7: Return(6)\n"
139 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100140 " 8: Exit\n"
141 // Synthesized block to avoid critical edge.
142 "BasicBlock 5, pred: 1, succ: 3\n"
143 " 9: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144
145 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
146 Instruction::CONST_4 | 0 | 0,
147 Instruction::IF_EQ, 3,
148 Instruction::CONST_4 | 4 << 12 | 0,
149 Instruction::RETURN | 0 << 8);
150
151 TestCode(data, expected);
152}
153
154TEST(SsaTest, CFG3) {
155 // Test that we create a phi for the join block of an if control flow instruction
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100156 // when both branches update a local.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100157 const char* expected =
158 "BasicBlock 0, succ: 1\n"
159 " 0: IntConstant 0 [4, 4]\n"
160 " 1: IntConstant 4 [8]\n"
161 " 2: IntConstant 5 [8]\n"
162 " 3: Goto\n"
163 "BasicBlock 1, pred: 0, succ: 3, 2\n"
164 " 4: Equal(0, 0) [5]\n"
165 " 5: If(4)\n"
166 "BasicBlock 2, pred: 1, succ: 4\n"
167 " 6: Goto\n"
168 "BasicBlock 3, pred: 1, succ: 4\n"
169 " 7: Goto\n"
170 "BasicBlock 4, pred: 2, 3, succ: 5\n"
171 " 8: Phi(1, 2) [9]\n"
172 " 9: Return(8)\n"
173 "BasicBlock 5, pred: 4\n"
174 " 10: Exit\n";
175
176 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
177 Instruction::CONST_4 | 0 | 0,
178 Instruction::IF_EQ, 4,
179 Instruction::CONST_4 | 4 << 12 | 0,
180 Instruction::GOTO | 0x200,
181 Instruction::CONST_4 | 5 << 12 | 0,
182 Instruction::RETURN | 0 << 8);
183
184 TestCode(data, expected);
185}
186
187TEST(SsaTest, Loop1) {
188 // Test that we create a phi for an initialized local at entry of a loop.
189 const char* expected =
190 "BasicBlock 0, succ: 1\n"
191 " 0: IntConstant 0 [6, 4, 2, 2]\n"
192 " 1: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 "BasicBlock 1, pred: 0, succ: 5, 6\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100194 " 2: Equal(0, 0) [3]\n"
195 " 3: If(2)\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100196 "BasicBlock 2, pred: 3, 6, succ: 3\n"
197 " 4: Phi(6, 0) [6]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100198 " 5: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100199 "BasicBlock 3, pred: 2, 5, succ: 2\n"
200 " 6: Phi(4, 0) [4]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100201 " 7: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100202 "BasicBlock 4\n"
203 // Synthesized blocks to avoid critical edge.
204 "BasicBlock 5, pred: 1, succ: 3\n"
205 " 8: Goto\n"
206 "BasicBlock 6, pred: 1, succ: 2\n"
207 " 9: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100208
209 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
210 Instruction::CONST_4 | 0 | 0,
211 Instruction::IF_EQ, 3,
212 Instruction::GOTO | 0x100,
213 Instruction::GOTO | 0xFF00);
214
215 TestCode(data, expected);
216}
217
218TEST(SsaTest, Loop2) {
219 // Simple loop with one preheader and one back edge.
220 const char* expected =
221 "BasicBlock 0, succ: 1\n"
222 " 0: IntConstant 0 [4]\n"
223 " 1: IntConstant 4 [4]\n"
224 " 2: Goto\n"
225 "BasicBlock 1, pred: 0, succ: 2\n"
226 " 3: Goto\n"
227 "BasicBlock 2, pred: 1, 3, succ: 4, 3\n"
228 " 4: Phi(0, 1) [5, 5]\n"
229 " 5: Equal(4, 4) [6]\n"
230 " 6: If(5)\n"
231 "BasicBlock 3, pred: 2, succ: 2\n"
232 " 7: Goto\n"
233 "BasicBlock 4, pred: 2, succ: 5\n"
234 " 8: ReturnVoid\n"
235 "BasicBlock 5, pred: 4\n"
236 " 9: Exit\n";
237
238 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
239 Instruction::CONST_4 | 0 | 0,
240 Instruction::IF_EQ, 4,
241 Instruction::CONST_4 | 4 << 12 | 0,
242 Instruction::GOTO | 0xFD00,
243 Instruction::RETURN_VOID);
244
245 TestCode(data, expected);
246}
247
248TEST(SsaTest, Loop3) {
249 // Test that a local not yet defined at the entry of a loop is handled properly.
250 const char* expected =
251 "BasicBlock 0, succ: 1\n"
252 " 0: IntConstant 0 [5]\n"
253 " 1: IntConstant 4 [5]\n"
254 " 2: IntConstant 5 [9]\n"
255 " 3: Goto\n"
256 "BasicBlock 1, pred: 0, succ: 2\n"
257 " 4: Goto\n"
258 "BasicBlock 2, pred: 1, 3, succ: 4, 3\n"
259 " 5: Phi(0, 1) [6, 6]\n"
260 " 6: Equal(5, 5) [7]\n"
261 " 7: If(6)\n"
262 "BasicBlock 3, pred: 2, succ: 2\n"
263 " 8: Goto\n"
264 "BasicBlock 4, pred: 2, succ: 5\n"
265 " 9: Return(2)\n"
266 "BasicBlock 5, pred: 4\n"
267 " 10: Exit\n";
268
269 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
270 Instruction::CONST_4 | 0 | 0,
271 Instruction::IF_EQ, 4,
272 Instruction::CONST_4 | 4 << 12 | 0,
273 Instruction::GOTO | 0xFD00,
274 Instruction::CONST_4 | 5 << 12 | 1 << 8,
275 Instruction::RETURN | 1 << 8);
276
277 TestCode(data, expected);
278}
279
280TEST(SsaTest, Loop4) {
281 // Make sure we support a preheader of a loop not being the first predecessor
282 // in the predecessor list of the header.
283 const char* expected =
284 "BasicBlock 0, succ: 1\n"
285 " 0: IntConstant 0 [4]\n"
286 " 1: IntConstant 4 [4]\n"
287 " 2: Goto\n"
288 "BasicBlock 1, pred: 0, succ: 4\n"
289 " 3: Goto\n"
290 "BasicBlock 2, pred: 3, 4, succ: 5, 3\n"
291 " 4: Phi(1, 0) [9, 5, 5]\n"
292 " 5: Equal(4, 4) [6]\n"
293 " 6: If(5)\n"
294 "BasicBlock 3, pred: 2, succ: 2\n"
295 " 7: Goto\n"
296 "BasicBlock 4, pred: 1, succ: 2\n"
297 " 8: Goto\n"
298 "BasicBlock 5, pred: 2, succ: 6\n"
299 " 9: Return(4)\n"
300 "BasicBlock 6, pred: 5\n"
301 " 10: Exit\n";
302
303 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
304 Instruction::CONST_4 | 0 | 0,
305 Instruction::GOTO | 0x500,
306 Instruction::IF_EQ, 5,
307 Instruction::CONST_4 | 4 << 12 | 0,
308 Instruction::GOTO | 0xFD00,
309 Instruction::GOTO | 0xFC00,
310 Instruction::RETURN | 0 << 8);
311
312 TestCode(data, expected);
313}
314
315TEST(SsaTest, Loop5) {
316 // Make sure we create a preheader of a loop when a header originally has two
317 // incoming blocks and one back edge.
318 const char* expected =
319 "BasicBlock 0, succ: 1\n"
320 " 0: IntConstant 0 [4, 4]\n"
321 " 1: IntConstant 4 [14]\n"
322 " 2: IntConstant 5 [14]\n"
323 " 3: Goto\n"
324 "BasicBlock 1, pred: 0, succ: 3, 2\n"
325 " 4: Equal(0, 0) [5]\n"
326 " 5: If(4)\n"
327 "BasicBlock 2, pred: 1, succ: 8\n"
328 " 6: Goto\n"
329 "BasicBlock 3, pred: 1, succ: 8\n"
330 " 7: Goto\n"
331 "BasicBlock 4, pred: 5, 8, succ: 6, 5\n"
332 " 8: Phi(8, 14) [8, 12, 9, 9]\n"
333 " 9: Equal(8, 8) [10]\n"
334 " 10: If(9)\n"
335 "BasicBlock 5, pred: 4, succ: 4\n"
336 " 11: Goto\n"
337 "BasicBlock 6, pred: 4, succ: 7\n"
338 " 12: Return(8)\n"
339 "BasicBlock 7, pred: 6\n"
340 " 13: Exit\n"
341 "BasicBlock 8, pred: 2, 3, succ: 4\n"
342 " 14: Phi(1, 2) [8]\n"
343 " 15: Goto\n";
344
345 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
346 Instruction::CONST_4 | 0 | 0,
347 Instruction::IF_EQ, 4,
348 Instruction::CONST_4 | 4 << 12 | 0,
349 Instruction::GOTO | 0x200,
350 Instruction::CONST_4 | 5 << 12 | 0,
351 Instruction::IF_EQ, 3,
352 Instruction::GOTO | 0xFE00,
353 Instruction::RETURN | 0 << 8);
354
355 TestCode(data, expected);
356}
357
358TEST(SsaTest, Loop6) {
359 // Test a loop with one preheader and two back edges (e.g. continue).
360 const char* expected =
361 "BasicBlock 0, succ: 1\n"
362 " 0: IntConstant 0 [5]\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100363 " 1: IntConstant 4 [14, 8, 8]\n"
364 " 2: IntConstant 5 [14]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365 " 3: Goto\n"
366 "BasicBlock 1, pred: 0, succ: 2\n"
367 " 4: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368 "BasicBlock 2, pred: 1, 8, succ: 6, 3\n"
369 " 5: Phi(0, 14) [12, 6, 6]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100370 " 6: Equal(5, 5) [7]\n"
371 " 7: If(6)\n"
372 "BasicBlock 3, pred: 2, succ: 5, 4\n"
373 " 8: Equal(1, 1) [9]\n"
374 " 9: If(8)\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100375 "BasicBlock 4, pred: 3, succ: 8\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100376 " 10: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100377 "BasicBlock 5, pred: 3, succ: 8\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100378 " 11: Goto\n"
379 "BasicBlock 6, pred: 2, succ: 7\n"
380 " 12: Return(5)\n"
381 "BasicBlock 7, pred: 6\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382 " 13: Exit\n"
383 // Synthesized single back edge of loop.
384 "BasicBlock 8, pred: 5, 4, succ: 2\n"
385 " 14: Phi(1, 2) [5]\n"
386 " 15: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100387
388 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
389 Instruction::CONST_4 | 0 | 0,
390 Instruction::IF_EQ, 8,
391 Instruction::CONST_4 | 4 << 12 | 0,
392 Instruction::IF_EQ, 4,
393 Instruction::CONST_4 | 5 << 12 | 0,
394 Instruction::GOTO | 0xFA00,
395 Instruction::GOTO | 0xF900,
396 Instruction::RETURN | 0 << 8);
397
398 TestCode(data, expected);
399}
400
401TEST(SsaTest, Loop7) {
402 // Test a loop with one preheader, one back edge, and two exit edges (e.g. break).
403 const char* expected =
404 "BasicBlock 0, succ: 1\n"
405 " 0: IntConstant 0 [5]\n"
406 " 1: IntConstant 4 [5, 8, 8]\n"
407 " 2: IntConstant 5 [12]\n"
408 " 3: Goto\n"
409 "BasicBlock 1, pred: 0, succ: 2\n"
410 " 4: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100411 "BasicBlock 2, pred: 1, 5, succ: 3, 8\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100412 " 5: Phi(0, 1) [12, 6, 6]\n"
413 " 6: Equal(5, 5) [7]\n"
414 " 7: If(6)\n"
415 "BasicBlock 3, pred: 2, succ: 5, 4\n"
416 " 8: Equal(1, 1) [9]\n"
417 " 9: If(8)\n"
418 "BasicBlock 4, pred: 3, succ: 6\n"
419 " 10: Goto\n"
420 "BasicBlock 5, pred: 3, succ: 2\n"
421 " 11: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 "BasicBlock 6, pred: 4, 8, succ: 7\n"
423 " 12: Phi(2, 5) [13]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 " 13: Return(12)\n"
425 "BasicBlock 7, pred: 6\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 " 14: Exit\n"
427 "BasicBlock 8, pred: 2, succ: 6\n"
428 " 15: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100429
430 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
431 Instruction::CONST_4 | 0 | 0,
432 Instruction::IF_EQ, 8,
433 Instruction::CONST_4 | 4 << 12 | 0,
434 Instruction::IF_EQ, 4,
435 Instruction::CONST_4 | 5 << 12 | 0,
436 Instruction::GOTO | 0x0200,
437 Instruction::GOTO | 0xF900,
438 Instruction::RETURN | 0 << 8);
439
440 TestCode(data, expected);
441}
442
443TEST(SsaTest, DeadLocal) {
444 // Test that we correctly handle a local not being used.
445 const char* expected =
446 "BasicBlock 0, succ: 1\n"
447 " 0: IntConstant 0\n"
448 " 1: Goto\n"
449 "BasicBlock 1, pred: 0, succ: 2\n"
450 " 2: ReturnVoid\n"
451 "BasicBlock 2, pred: 1\n"
452 " 3: Exit\n";
453
454 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
455 Instruction::CONST_4 | 0 | 0,
456 Instruction::RETURN_VOID);
457
458 TestCode(data, expected);
459}
460
461} // namespace art