blob: 81e28877ce3f18701d1e8bbd81eb0201798dbbcb [file] [log] [blame]
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +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 "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010018#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010019#include "code_generator_x86.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010020#include "dex_file.h"
21#include "dex_instruction.h"
22#include "nodes.h"
23#include "optimizing_unit_test.h"
24#include "ssa_liveness_analysis.h"
25#include "utils/arena_allocator.h"
26
27#include "gtest/gtest.h"
28
29namespace art {
30
31static HGraph* BuildGraph(const uint16_t* data, ArenaAllocator* allocator) {
32 HGraphBuilder builder(allocator);
33 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
34 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000035 // Suspend checks implementation may change in the future, and this test relies
36 // on how instructions are ordered.
37 RemoveSuspendChecks(graph);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038 graph->BuildDominatorTree();
39 graph->TransformToSSA();
40 graph->FindNaturalLoops();
41 return graph;
42}
43
44TEST(LiveRangesTest, CFG1) {
45 /*
46 * Test the following snippet:
47 * return 0;
48 *
49 * Which becomes the following graph (numbered by lifetime position):
50 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010051 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053 * 8: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 * 12: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 */
57 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
58 Instruction::CONST_4 | 0 | 0,
59 Instruction::RETURN);
60
61 ArenaPool pool;
62 ArenaAllocator allocator(&pool);
63 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010064
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010065 x86::CodeGeneratorX86 codegen(graph);
66 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067 liveness.Analyze();
68
69 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010070 LiveRange* range = interval->GetFirstRange();
71 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010072 // Last use is the return instruction.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010073 ASSERT_EQ(9u, range->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010074 HBasicBlock* block = graph->GetBlocks().Get(1);
Roland Levillain476df552014-10-09 17:51:36 +010075 ASSERT_TRUE(block->GetLastInstruction()->IsReturn());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition());
77 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010078}
79
80TEST(LiveRangesTest, CFG2) {
81 /*
82 * Test the following snippet:
83 * var a = 0;
84 * if (0 == 0) {
85 * } else {
86 * }
87 * return a;
88 *
89 * Which becomes the following graph (numbered by lifetime position):
90 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010091 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010092 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010093 * 8: equal
94 * 10: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010095 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010096 * 14: goto 18: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010097 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010098 * 22: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010099 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100 * 26: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100101 */
102 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
103 Instruction::CONST_4 | 0 | 0,
104 Instruction::IF_EQ, 3,
105 Instruction::GOTO | 0x100,
106 Instruction::RETURN | 0 << 8);
107
108 ArenaPool pool;
109 ArenaAllocator allocator(&pool);
110 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100111 x86::CodeGeneratorX86 codegen(graph);
112 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100113 liveness.Analyze();
114
115 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100116 LiveRange* range = interval->GetFirstRange();
117 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100118 // Last use is the return instruction.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100119 ASSERT_EQ(23u, range->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100120 HBasicBlock* block = graph->GetBlocks().Get(3);
Roland Levillain476df552014-10-09 17:51:36 +0100121 ASSERT_TRUE(block->GetLastInstruction()->IsReturn());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100122 ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition());
123 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100124}
125
126TEST(LiveRangesTest, CFG3) {
127 /*
128 * Test the following snippet:
129 * var a = 0;
130 * if (0 == 0) {
131 * } else {
132 * a = 4;
133 * }
134 * return a;
135 *
136 * Which becomes the following graph (numbered by lifetime position):
137 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 * 4: constant4
139 * 6: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100140 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141 * 10: equal
142 * 12: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100143 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100144 * 16: goto 20: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100145 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 * 22: phi
147 * 24: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148 * |
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100149 * 28: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100150 */
151 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
152 Instruction::CONST_4 | 0 | 0,
153 Instruction::IF_EQ, 3,
154 Instruction::CONST_4 | 4 << 12 | 0,
155 Instruction::RETURN | 0 << 8);
156
157 ArenaPool pool;
158 ArenaAllocator allocator(&pool);
159 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100160 x86::CodeGeneratorX86 codegen(graph);
161 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100162 liveness.Analyze();
163
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100164 // Test for the 4 constant.
165 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100166 LiveRange* range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100167 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100168 // Last use is the phi at the return block so instruction is live until
169 // the end of the then block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100170 ASSERT_EQ(18u, range->GetEnd());
171 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100172
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100173 // Test for the 0 constant.
174 interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100175 // The then branch is a hole for this constant, therefore its interval has 2 ranges.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100176 // First range starts from the definition and ends at the if block.
177 range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100178 ASSERT_EQ(2u, range->GetStart());
179 // 14 is the end of the if block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100180 ASSERT_EQ(14u, range->GetEnd());
181 // Second range is the else block.
182 range = range->GetNext();
183 ASSERT_EQ(18u, range->GetStart());
184 // Last use is the phi at the return block.
185 ASSERT_EQ(22u, range->GetEnd());
186 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100187
188 // Test for the phi.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100189 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100190 range = interval->GetFirstRange();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100191 ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100192 ASSERT_EQ(22u, range->GetStart());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100193 ASSERT_EQ(25u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100194 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100195}
196
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100197TEST(LiveRangesTest, Loop1) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100198 /*
199 * Test the following snippet:
200 * var a = 0;
201 * while (a == a) {
202 * a = 4;
203 * }
204 * return 5;
205 *
206 * Which becomes the following graph (numbered by lifetime position):
207 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100208 * 4: constant4
209 * 6: constant5
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100210 * 8: goto
211 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100212 * 12: goto
213 * |
214 * 14: phi
215 * 16: equal
216 * 18: if +++++
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100217 * | \ +
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100218 * | 22: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100220 * 26: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100221 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100222 * 30: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100223 */
224
225 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
226 Instruction::CONST_4 | 0 | 0,
227 Instruction::IF_EQ, 4,
228 Instruction::CONST_4 | 4 << 12 | 0,
229 Instruction::GOTO | 0xFD00,
230 Instruction::CONST_4 | 5 << 12 | 1 << 8,
231 Instruction::RETURN | 1 << 8);
232
233 ArenaPool pool;
234 ArenaAllocator allocator(&pool);
235 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +0100236 RemoveSuspendChecks(graph);
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100237 x86::CodeGeneratorX86 codegen(graph);
238 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100239 liveness.Analyze();
240
241 // Test for the 0 constant.
242 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100243 LiveRange* range = interval->GetFirstRange();
244 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100245 // Last use is the loop phi so instruction is live until
246 // the end of the pre loop header.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100247 ASSERT_EQ(14u, range->GetEnd());
248 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100249
250 // Test for the 4 constant.
251 interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100252 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100253 // The instruction is live until the end of the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100254 ASSERT_EQ(4u, range->GetStart());
255 ASSERT_EQ(24u, range->GetEnd());
256 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100257
258 // Test for the 5 constant.
259 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100260 range = interval->GetFirstRange();
261 // The instruction is live until the return instruction after the loop.
262 ASSERT_EQ(6u, range->GetStart());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100263 ASSERT_EQ(27u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100264 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100265
266 // Test for the phi.
267 interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100268 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100269 // Instruction is consumed by the if.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100270 ASSERT_EQ(14u, range->GetStart());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100271 ASSERT_EQ(16u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100272 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100273}
274
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100275TEST(LiveRangesTest, Loop2) {
276 /*
277 * Test the following snippet:
278 * var a = 0;
279 * while (a == a) {
280 * a = a + a;
281 * }
282 * return a;
283 *
284 * Which becomes the following graph (numbered by lifetime position):
285 * 2: constant0
286 * 4: goto
287 * |
288 * 8: goto
289 * |
290 * 10: phi
291 * 12: equal
292 * 14: if +++++
293 * | \ +
294 * | 18: suspend
295 * | 20: add
296 * | 22: goto
297 * |
298 * 26: return
299 * |
300 * 30: exit
301 *
302 * We want to make sure the phi at 10 has a lifetime hole after the add at 20.
303 */
304
305 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
306 Instruction::CONST_4 | 0 | 0,
307 Instruction::IF_EQ, 6,
308 Instruction::ADD_INT, 0, 0,
309 Instruction::GOTO | 0xFB00,
310 Instruction::RETURN | 0 << 8);
311
312 ArenaPool pool;
313 ArenaAllocator allocator(&pool);
314 HGraph* graph = BuildGraph(data, &allocator);
315 x86::CodeGeneratorX86 codegen(graph);
316 SsaLivenessAnalysis liveness(*graph, &codegen);
317 liveness.Analyze();
318
319 // Test for the 0 constant.
320 HIntConstant* constant = liveness.GetInstructionFromSsaIndex(0)->AsIntConstant();
321 LiveInterval* interval = constant->GetLiveInterval();
322 LiveRange* range = interval->GetFirstRange();
323 ASSERT_EQ(2u, range->GetStart());
324 // Last use is the loop phi so instruction is live until
325 // the end of the pre loop header.
326 ASSERT_EQ(10u, range->GetEnd());
327 ASSERT_TRUE(range->GetNext() == nullptr);
328
329 // Test for the loop phi.
330 HPhi* phi = liveness.GetInstructionFromSsaIndex(1)->AsPhi();
331 interval = phi->GetLiveInterval();
332 range = interval->GetFirstRange();
333 ASSERT_EQ(10u, range->GetStart());
334 ASSERT_EQ(21u, range->GetEnd());
335 range = range->GetNext();
336 ASSERT_TRUE(range != nullptr);
337 ASSERT_EQ(24u, range->GetStart());
338 ASSERT_EQ(27u, range->GetEnd());
339
340 // Test for the add instruction.
341 HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd();
342 interval = add->GetLiveInterval();
343 range = interval->GetFirstRange();
344 ASSERT_EQ(20u, range->GetStart());
345 ASSERT_EQ(24u, range->GetEnd());
346 ASSERT_TRUE(range->GetNext() == nullptr);
347}
348
349TEST(LiveRangesTest, CFG4) {
350 /*
351 * Test the following snippet:
352 * var a = 0;
353 * var b = 4;
354 * if (a == a) {
355 * a = b + a;
356 * } else {
357 * a = b + a
358 * }
359 * return b;
360 *
361 * Which becomes the following graph (numbered by lifetime position):
362 * 2: constant0
363 * 4: constant4
364 * 6: goto
365 * |
366 * 10: equal
367 * 12: if
368 * / \
369 * 16: add 22: add
370 * 18: goto 24: goto
371 * \ /
372 * 26: phi
373 * 28: return
374 * |
375 * 32: exit
376 *
377 * We want to make sure the constant0 has a lifetime hole after the 16: add.
378 */
379 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
380 Instruction::CONST_4 | 0 | 0,
381 Instruction::CONST_4 | 4 << 12 | 1 << 8,
382 Instruction::IF_EQ, 5,
383 Instruction::ADD_INT, 1 << 8,
384 Instruction::GOTO | 0x300,
385 Instruction::ADD_INT, 1 << 8,
386 Instruction::RETURN | 1 << 8);
387
388 ArenaPool pool;
389 ArenaAllocator allocator(&pool);
390 HGraph* graph = BuildGraph(data, &allocator);
391 x86::CodeGeneratorX86 codegen(graph);
392 SsaLivenessAnalysis liveness(*graph, &codegen);
393 liveness.Analyze();
394
395 // Test for the 0 constant.
396 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
397 LiveRange* range = interval->GetFirstRange();
398 ASSERT_EQ(2u, range->GetStart());
399 ASSERT_EQ(16u, range->GetEnd());
400 range = range->GetNext();
401 ASSERT_TRUE(range != nullptr);
402 ASSERT_EQ(20u, range->GetStart());
403 ASSERT_EQ(22u, range->GetEnd());
404 ASSERT_TRUE(range->GetNext() == nullptr);
405
406 // Test for the 4 constant.
407 interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
408 range = interval->GetFirstRange();
409 ASSERT_EQ(4u, range->GetStart());
410 ASSERT_EQ(29u, range->GetEnd());
411 ASSERT_TRUE(range->GetNext() == nullptr);
412
413 // Test for the first add.
414 HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd();
415 interval = add->GetLiveInterval();
416 range = interval->GetFirstRange();
417 ASSERT_EQ(16u, range->GetStart());
418 ASSERT_EQ(20u, range->GetEnd());
419 ASSERT_TRUE(range->GetNext() == nullptr);
420
421 // Test for the second add.
422 add = liveness.GetInstructionFromSsaIndex(3)->AsAdd();
423 interval = add->GetLiveInterval();
424 range = interval->GetFirstRange();
425 ASSERT_EQ(22u, range->GetStart());
426 ASSERT_EQ(26u, range->GetEnd());
427 ASSERT_TRUE(range->GetNext() == nullptr);
428
429 // Test for the phi, which is unused.
430 HPhi* phi = liveness.GetInstructionFromSsaIndex(4)->AsPhi();
431 ASSERT_EQ(phi->NumberOfUses(), 0u);
432 interval = phi->GetLiveInterval();
433 range = interval->GetFirstRange();
434 ASSERT_EQ(26u, range->GetStart());
435 ASSERT_EQ(28u, range->GetEnd());
436 ASSERT_TRUE(range->GetNext() == nullptr);
437}
438
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100439} // namespace art