blob: 6174dd49a177ea80807764717c38177024645264 [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
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010031class SsaPrettyPrinter : public HPrettyPrinter {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010032 public:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010033 explicit SsaPrettyPrinter(HGraph* graph) : HPrettyPrinter(graph), str_("") {}
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034
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
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010062 DISALLOW_COPY_AND_ASSIGN(SsaPrettyPrinter);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063};
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 Geoffrayf635e632014-05-14 09:43:38 +010069 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 it.Current()->SetId(id++);
71 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010072 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010073 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);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010085
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +010086 graph->BuildDominatorTree();
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000087 // Suspend checks implementation may change in the future, and this test relies
88 // on how instructions are ordered.
89 RemoveSuspendChecks(graph);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090 graph->TransformToSSA();
91 ReNumberInstructions(graph);
92
Nicolas Geoffray184d6402014-06-09 14:06:02 +010093 // Test that phis had their type set.
94 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
95 for (HInstructionIterator it(graph->GetBlocks().Get(i)->GetPhis()); !it.Done(); it.Advance()) {
96 ASSERT_NE(it.Current()->GetType(), Primitive::kPrimVoid);
97 }
98 }
99
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100100 SsaPrettyPrinter printer(graph);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 printer.VisitInsertionOrder();
102
103 ASSERT_STREQ(expected, printer.str().c_str());
104}
105
106TEST(SsaTest, CFG1) {
107 // Test that we get rid of loads and stores.
108 const char* expected =
109 "BasicBlock 0, succ: 1\n"
110 " 0: IntConstant 0 [2, 2]\n"
111 " 1: Goto\n"
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100112 "BasicBlock 1, pred: 0, succ: 5, 2\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113 " 2: Equal(0, 0) [3]\n"
114 " 3: If(2)\n"
115 "BasicBlock 2, pred: 1, succ: 3\n"
116 " 4: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100117 "BasicBlock 3, pred: 2, 5, succ: 4\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100118 " 5: ReturnVoid\n"
119 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100120 " 6: Exit\n"
121 // Synthesized block to avoid critical edge.
122 "BasicBlock 5, pred: 1, succ: 3\n"
123 " 7: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124
125 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
126 Instruction::CONST_4 | 0 | 0,
127 Instruction::IF_EQ, 3,
128 Instruction::GOTO | 0x100,
129 Instruction::RETURN_VOID);
130
131 TestCode(data, expected);
132}
133
134TEST(SsaTest, CFG2) {
135 // Test that we create a phi for the join block of an if control flow instruction
136 // when there is only code in the else branch.
137 const char* expected =
138 "BasicBlock 0, succ: 1\n"
139 " 0: IntConstant 0 [6, 3, 3]\n"
140 " 1: IntConstant 4 [6]\n"
141 " 2: Goto\n"
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100142 "BasicBlock 1, pred: 0, succ: 5, 2\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100143 " 3: Equal(0, 0) [4]\n"
144 " 4: If(3)\n"
145 "BasicBlock 2, pred: 1, succ: 3\n"
146 " 5: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100147 "BasicBlock 3, pred: 2, 5, succ: 4\n"
148 " 6: Phi(1, 0) [7]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100149 " 7: Return(6)\n"
150 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100151 " 8: Exit\n"
152 // Synthesized block to avoid critical edge.
153 "BasicBlock 5, pred: 1, succ: 3\n"
154 " 9: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155
156 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
157 Instruction::CONST_4 | 0 | 0,
158 Instruction::IF_EQ, 3,
159 Instruction::CONST_4 | 4 << 12 | 0,
160 Instruction::RETURN | 0 << 8);
161
162 TestCode(data, expected);
163}
164
165TEST(SsaTest, CFG3) {
166 // Test that we create a phi for the join block of an if control flow instruction
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 // when both branches update a local.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100168 const char* expected =
169 "BasicBlock 0, succ: 1\n"
170 " 0: IntConstant 0 [4, 4]\n"
171 " 1: IntConstant 4 [8]\n"
172 " 2: IntConstant 5 [8]\n"
173 " 3: Goto\n"
174 "BasicBlock 1, pred: 0, succ: 3, 2\n"
175 " 4: Equal(0, 0) [5]\n"
176 " 5: If(4)\n"
177 "BasicBlock 2, pred: 1, succ: 4\n"
178 " 6: Goto\n"
179 "BasicBlock 3, pred: 1, succ: 4\n"
180 " 7: Goto\n"
181 "BasicBlock 4, pred: 2, 3, succ: 5\n"
182 " 8: Phi(1, 2) [9]\n"
183 " 9: Return(8)\n"
184 "BasicBlock 5, pred: 4\n"
185 " 10: Exit\n";
186
187 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
188 Instruction::CONST_4 | 0 | 0,
189 Instruction::IF_EQ, 4,
190 Instruction::CONST_4 | 4 << 12 | 0,
191 Instruction::GOTO | 0x200,
192 Instruction::CONST_4 | 5 << 12 | 0,
193 Instruction::RETURN | 0 << 8);
194
195 TestCode(data, expected);
196}
197
198TEST(SsaTest, Loop1) {
199 // Test that we create a phi for an initialized local at entry of a loop.
200 const char* expected =
201 "BasicBlock 0, succ: 1\n"
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000202 " 0: IntConstant 0 [6, 3, 3]\n"
203 " 1: IntConstant 4 [6]\n"
204 " 2: Goto\n"
205 "BasicBlock 1, pred: 0, succ: 4, 2\n"
206 " 3: Equal(0, 0) [4]\n"
207 " 4: If(3)\n"
208 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100209 " 5: Goto\n"
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000210 "BasicBlock 3, pred: 2, 4, succ: 5\n"
211 " 6: Phi(1, 0) [9]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100212 " 7: Goto\n"
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000213 "BasicBlock 4, pred: 1, succ: 3\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100214 " 8: Goto\n"
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000215 "BasicBlock 5, pred: 3, succ: 6\n"
216 " 9: Return(6)\n"
217 "BasicBlock 6, pred: 5\n"
218 " 10: Exit\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100219
220 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
221 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000222 Instruction::IF_EQ, 4,
223 Instruction::CONST_4 | 4 << 12 | 0,
224 Instruction::GOTO | 0x200,
225 Instruction::GOTO | 0xFF00,
226 Instruction::RETURN | 0 << 8);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100227
228 TestCode(data, expected);
229}
230
231TEST(SsaTest, Loop2) {
232 // Simple loop with one preheader and one back edge.
233 const char* expected =
234 "BasicBlock 0, succ: 1\n"
235 " 0: IntConstant 0 [4]\n"
236 " 1: IntConstant 4 [4]\n"
237 " 2: Goto\n"
238 "BasicBlock 1, pred: 0, succ: 2\n"
239 " 3: Goto\n"
240 "BasicBlock 2, pred: 1, 3, succ: 4, 3\n"
241 " 4: Phi(0, 1) [5, 5]\n"
242 " 5: Equal(4, 4) [6]\n"
243 " 6: If(5)\n"
244 "BasicBlock 3, pred: 2, succ: 2\n"
245 " 7: Goto\n"
246 "BasicBlock 4, pred: 2, succ: 5\n"
247 " 8: ReturnVoid\n"
248 "BasicBlock 5, pred: 4\n"
249 " 9: Exit\n";
250
251 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
252 Instruction::CONST_4 | 0 | 0,
253 Instruction::IF_EQ, 4,
254 Instruction::CONST_4 | 4 << 12 | 0,
255 Instruction::GOTO | 0xFD00,
256 Instruction::RETURN_VOID);
257
258 TestCode(data, expected);
259}
260
261TEST(SsaTest, Loop3) {
262 // Test that a local not yet defined at the entry of a loop is handled properly.
263 const char* expected =
264 "BasicBlock 0, succ: 1\n"
265 " 0: IntConstant 0 [5]\n"
266 " 1: IntConstant 4 [5]\n"
267 " 2: IntConstant 5 [9]\n"
268 " 3: Goto\n"
269 "BasicBlock 1, pred: 0, succ: 2\n"
270 " 4: Goto\n"
271 "BasicBlock 2, pred: 1, 3, succ: 4, 3\n"
272 " 5: Phi(0, 1) [6, 6]\n"
273 " 6: Equal(5, 5) [7]\n"
274 " 7: If(6)\n"
275 "BasicBlock 3, pred: 2, succ: 2\n"
276 " 8: Goto\n"
277 "BasicBlock 4, pred: 2, succ: 5\n"
278 " 9: Return(2)\n"
279 "BasicBlock 5, pred: 4\n"
280 " 10: Exit\n";
281
282 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
283 Instruction::CONST_4 | 0 | 0,
284 Instruction::IF_EQ, 4,
285 Instruction::CONST_4 | 4 << 12 | 0,
286 Instruction::GOTO | 0xFD00,
287 Instruction::CONST_4 | 5 << 12 | 1 << 8,
288 Instruction::RETURN | 1 << 8);
289
290 TestCode(data, expected);
291}
292
293TEST(SsaTest, Loop4) {
294 // Make sure we support a preheader of a loop not being the first predecessor
295 // in the predecessor list of the header.
296 const char* expected =
297 "BasicBlock 0, succ: 1\n"
298 " 0: IntConstant 0 [4]\n"
299 " 1: IntConstant 4 [4]\n"
300 " 2: Goto\n"
301 "BasicBlock 1, pred: 0, succ: 4\n"
302 " 3: Goto\n"
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100303 "BasicBlock 2, pred: 4, 3, succ: 5, 3\n"
304 " 4: Phi(0, 1) [9, 5, 5]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100305 " 5: Equal(4, 4) [6]\n"
306 " 6: If(5)\n"
307 "BasicBlock 3, pred: 2, succ: 2\n"
308 " 7: Goto\n"
309 "BasicBlock 4, pred: 1, succ: 2\n"
310 " 8: Goto\n"
311 "BasicBlock 5, pred: 2, succ: 6\n"
312 " 9: Return(4)\n"
313 "BasicBlock 6, pred: 5\n"
314 " 10: Exit\n";
315
316 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
317 Instruction::CONST_4 | 0 | 0,
318 Instruction::GOTO | 0x500,
319 Instruction::IF_EQ, 5,
320 Instruction::CONST_4 | 4 << 12 | 0,
321 Instruction::GOTO | 0xFD00,
322 Instruction::GOTO | 0xFC00,
323 Instruction::RETURN | 0 << 8);
324
325 TestCode(data, expected);
326}
327
328TEST(SsaTest, Loop5) {
329 // Make sure we create a preheader of a loop when a header originally has two
330 // incoming blocks and one back edge.
331 const char* expected =
332 "BasicBlock 0, succ: 1\n"
333 " 0: IntConstant 0 [4, 4]\n"
334 " 1: IntConstant 4 [14]\n"
335 " 2: IntConstant 5 [14]\n"
336 " 3: Goto\n"
337 "BasicBlock 1, pred: 0, succ: 3, 2\n"
338 " 4: Equal(0, 0) [5]\n"
339 " 5: If(4)\n"
340 "BasicBlock 2, pred: 1, succ: 8\n"
341 " 6: Goto\n"
342 "BasicBlock 3, pred: 1, succ: 8\n"
343 " 7: Goto\n"
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100344 "BasicBlock 4, pred: 8, 5, succ: 6, 5\n"
345 " 8: Phi(14, 8) [8, 12, 9, 9]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100346 " 9: Equal(8, 8) [10]\n"
347 " 10: If(9)\n"
348 "BasicBlock 5, pred: 4, succ: 4\n"
349 " 11: Goto\n"
350 "BasicBlock 6, pred: 4, succ: 7\n"
351 " 12: Return(8)\n"
352 "BasicBlock 7, pred: 6\n"
353 " 13: Exit\n"
354 "BasicBlock 8, pred: 2, 3, succ: 4\n"
355 " 14: Phi(1, 2) [8]\n"
356 " 15: Goto\n";
357
358 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
359 Instruction::CONST_4 | 0 | 0,
360 Instruction::IF_EQ, 4,
361 Instruction::CONST_4 | 4 << 12 | 0,
362 Instruction::GOTO | 0x200,
363 Instruction::CONST_4 | 5 << 12 | 0,
364 Instruction::IF_EQ, 3,
365 Instruction::GOTO | 0xFE00,
366 Instruction::RETURN | 0 << 8);
367
368 TestCode(data, expected);
369}
370
371TEST(SsaTest, Loop6) {
372 // Test a loop with one preheader and two back edges (e.g. continue).
373 const char* expected =
374 "BasicBlock 0, succ: 1\n"
375 " 0: IntConstant 0 [5]\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100376 " 1: IntConstant 4 [14, 8, 8]\n"
377 " 2: IntConstant 5 [14]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100378 " 3: Goto\n"
379 "BasicBlock 1, pred: 0, succ: 2\n"
380 " 4: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381 "BasicBlock 2, pred: 1, 8, succ: 6, 3\n"
382 " 5: Phi(0, 14) [12, 6, 6]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100383 " 6: Equal(5, 5) [7]\n"
384 " 7: If(6)\n"
385 "BasicBlock 3, pred: 2, succ: 5, 4\n"
386 " 8: Equal(1, 1) [9]\n"
387 " 9: If(8)\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 "BasicBlock 4, pred: 3, succ: 8\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100389 " 10: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100390 "BasicBlock 5, pred: 3, succ: 8\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100391 " 11: Goto\n"
392 "BasicBlock 6, pred: 2, succ: 7\n"
393 " 12: Return(5)\n"
394 "BasicBlock 7, pred: 6\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100395 " 13: Exit\n"
396 // Synthesized single back edge of loop.
397 "BasicBlock 8, pred: 5, 4, succ: 2\n"
398 " 14: Phi(1, 2) [5]\n"
399 " 15: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100400
401 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
402 Instruction::CONST_4 | 0 | 0,
403 Instruction::IF_EQ, 8,
404 Instruction::CONST_4 | 4 << 12 | 0,
405 Instruction::IF_EQ, 4,
406 Instruction::CONST_4 | 5 << 12 | 0,
407 Instruction::GOTO | 0xFA00,
408 Instruction::GOTO | 0xF900,
409 Instruction::RETURN | 0 << 8);
410
411 TestCode(data, expected);
412}
413
414TEST(SsaTest, Loop7) {
415 // Test a loop with one preheader, one back edge, and two exit edges (e.g. break).
416 const char* expected =
417 "BasicBlock 0, succ: 1\n"
418 " 0: IntConstant 0 [5]\n"
419 " 1: IntConstant 4 [5, 8, 8]\n"
420 " 2: IntConstant 5 [12]\n"
421 " 3: Goto\n"
422 "BasicBlock 1, pred: 0, succ: 2\n"
423 " 4: Goto\n"
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100424 "BasicBlock 2, pred: 1, 5, succ: 8, 3\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 " 5: Phi(0, 1) [12, 6, 6]\n"
426 " 6: Equal(5, 5) [7]\n"
427 " 7: If(6)\n"
428 "BasicBlock 3, pred: 2, succ: 5, 4\n"
429 " 8: Equal(1, 1) [9]\n"
430 " 9: If(8)\n"
431 "BasicBlock 4, pred: 3, succ: 6\n"
432 " 10: Goto\n"
433 "BasicBlock 5, pred: 3, succ: 2\n"
434 " 11: Goto\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 "BasicBlock 6, pred: 4, 8, succ: 7\n"
436 " 12: Phi(2, 5) [13]\n"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100437 " 13: Return(12)\n"
438 "BasicBlock 7, pred: 6\n"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 " 14: Exit\n"
440 "BasicBlock 8, pred: 2, succ: 6\n"
441 " 15: Goto\n";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442
443 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
444 Instruction::CONST_4 | 0 | 0,
445 Instruction::IF_EQ, 8,
446 Instruction::CONST_4 | 4 << 12 | 0,
447 Instruction::IF_EQ, 4,
448 Instruction::CONST_4 | 5 << 12 | 0,
449 Instruction::GOTO | 0x0200,
450 Instruction::GOTO | 0xF900,
451 Instruction::RETURN | 0 << 8);
452
453 TestCode(data, expected);
454}
455
456TEST(SsaTest, DeadLocal) {
457 // Test that we correctly handle a local not being used.
458 const char* expected =
459 "BasicBlock 0, succ: 1\n"
460 " 0: IntConstant 0\n"
461 " 1: Goto\n"
462 "BasicBlock 1, pred: 0, succ: 2\n"
463 " 2: ReturnVoid\n"
464 "BasicBlock 2, pred: 1\n"
465 " 3: Exit\n";
466
467 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
468 Instruction::CONST_4 | 0 | 0,
469 Instruction::RETURN_VOID);
470
471 TestCode(data, expected);
472}
473
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100474TEST(SsaTest, LocalInIf) {
475 // Test that we do not create a phi in the join block when one predecessor
476 // does not update the local.
477 const char* expected =
478 "BasicBlock 0, succ: 1\n"
479 " 0: IntConstant 0 [3, 3]\n"
480 " 1: IntConstant 4\n"
481 " 2: Goto\n"
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100482 "BasicBlock 1, pred: 0, succ: 5, 2\n"
Nicolas Geoffray7c3560f2014-06-04 12:12:08 +0100483 " 3: Equal(0, 0) [4]\n"
484 " 4: If(3)\n"
485 "BasicBlock 2, pred: 1, succ: 3\n"
486 " 5: Goto\n"
487 "BasicBlock 3, pred: 2, 5, succ: 4\n"
488 " 6: ReturnVoid\n"
489 "BasicBlock 4, pred: 3\n"
490 " 7: Exit\n"
491 // Synthesized block to avoid critical edge.
492 "BasicBlock 5, pred: 1, succ: 3\n"
493 " 8: Goto\n";
494
495 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
496 Instruction::CONST_4 | 0 | 0,
497 Instruction::IF_EQ, 3,
498 Instruction::CONST_4 | 4 << 12 | 1 << 8,
499 Instruction::RETURN_VOID);
500
501 TestCode(data, expected);
502}
503
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100504TEST(SsaTest, MultiplePredecessors) {
505 // Test that we do not create a phi when one predecessor
506 // does not update the local.
507 const char* expected =
508 "BasicBlock 0, succ: 1\n"
509 " 0: IntConstant 0 [4, 8, 6, 6, 2, 2, 8, 4]\n"
510 " 1: Goto\n"
511 "BasicBlock 1, pred: 0, succ: 3, 2\n"
512 " 2: Equal(0, 0) [3]\n"
513 " 3: If(2)\n"
514 "BasicBlock 2, pred: 1, succ: 5\n"
515 " 4: Add(0, 0)\n"
516 " 5: Goto\n"
517 "BasicBlock 3, pred: 1, succ: 7, 4\n"
518 " 6: Equal(0, 0) [7]\n"
519 " 7: If(6)\n"
520 "BasicBlock 4, pred: 3, succ: 5\n"
521 " 8: Add(0, 0)\n"
522 " 9: Goto\n"
523 // This block should not get a phi for local 1.
524 "BasicBlock 5, pred: 2, 4, 7, succ: 6\n"
525 " 10: ReturnVoid\n"
526 "BasicBlock 6, pred: 5\n"
527 " 11: Exit\n"
528 "BasicBlock 7, pred: 3, succ: 5\n"
529 " 12: Goto\n";
530
531 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
532 Instruction::CONST_4 | 0 | 0,
533 Instruction::IF_EQ, 5,
534 Instruction::ADD_INT_LIT8 | 1 << 8, 0 << 8,
535 Instruction::GOTO | 0x0500,
536 Instruction::IF_EQ, 4,
537 Instruction::ADD_INT_LIT8 | 1 << 8, 0 << 8,
538 Instruction::RETURN_VOID);
539
540 TestCode(data, expected);
541}
542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100543} // namespace art