blob: 61d6593f2bbb2e8f5fe77210f5d88f712fc416a5 [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
Mark Mendellfb8d2792015-03-31 22:16:59 -040017#include "arch/x86/instruction_set_features_x86.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080018#include "base/arena_allocator.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010019#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010020#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010021#include "code_generator_x86.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010022#include "dex_file.h"
23#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000024#include "driver/compiler_options.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010025#include "nodes.h"
26#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010027#include "prepare_for_register_allocation.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010028#include "ssa_liveness_analysis.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010029
30#include "gtest/gtest.h"
31
32namespace art {
33
34static HGraph* BuildGraph(const uint16_t* data, ArenaAllocator* allocator) {
David Brazdil5e8b1372015-01-23 14:39:08 +000035 HGraph* graph = new (allocator) HGraph(allocator);
36 HGraphBuilder builder(graph);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010037 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000038 builder.BuildGraph(*item);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000039 // Suspend checks implementation may change in the future, and this test relies
40 // on how instructions are ordered.
41 RemoveSuspendChecks(graph);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000042 graph->TryBuildingSsa();
Nicolas Geoffray360231a2014-10-08 21:07:48 +010043 // `Inline` conditions into ifs.
44 PrepareForRegisterAllocation(graph).Run();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010045 return graph;
46}
47
48TEST(LiveRangesTest, CFG1) {
49 /*
50 * Test the following snippet:
51 * return 0;
52 *
53 * Which becomes the following graph (numbered by lifetime position):
54 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057 * 8: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 * 12: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060 */
61 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
62 Instruction::CONST_4 | 0 | 0,
63 Instruction::RETURN);
64
65 ArenaPool pool;
66 ArenaAllocator allocator(&pool);
67 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010068
Mark Mendellfb8d2792015-03-31 22:16:59 -040069 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
70 X86InstructionSetFeatures::FromCppDefines());
71 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010072 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010073 liveness.Analyze();
74
75 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 LiveRange* range = interval->GetFirstRange();
77 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010078 // Last use is the return instruction.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +010079 ASSERT_EQ(8u, range->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010080 HBasicBlock* block = graph->GetBlocks().Get(1);
Roland Levillain476df552014-10-09 17:51:36 +010081 ASSERT_TRUE(block->GetLastInstruction()->IsReturn());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010082 ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition());
83 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010084}
85
86TEST(LiveRangesTest, CFG2) {
87 /*
88 * Test the following snippet:
89 * var a = 0;
90 * if (0 == 0) {
91 * } else {
92 * }
93 * return a;
94 *
95 * Which becomes the following graph (numbered by lifetime position):
96 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010097 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010098 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010099 * 8: equal
100 * 10: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100101 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100102 * 14: goto 18: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100103 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100104 * 22: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100105 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100106 * 26: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100107 */
108 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
109 Instruction::CONST_4 | 0 | 0,
110 Instruction::IF_EQ, 3,
111 Instruction::GOTO | 0x100,
112 Instruction::RETURN | 0 << 8);
113
114 ArenaPool pool;
115 ArenaAllocator allocator(&pool);
116 HGraph* graph = BuildGraph(data, &allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400117 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
118 X86InstructionSetFeatures::FromCppDefines());
119 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100120 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100121 liveness.Analyze();
122
123 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124 LiveRange* range = interval->GetFirstRange();
125 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100126 // Last use is the return instruction.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100127 ASSERT_EQ(22u, range->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100128 HBasicBlock* block = graph->GetBlocks().Get(3);
Roland Levillain476df552014-10-09 17:51:36 +0100129 ASSERT_TRUE(block->GetLastInstruction()->IsReturn());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100130 ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition());
131 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100132}
133
134TEST(LiveRangesTest, CFG3) {
135 /*
136 * Test the following snippet:
137 * var a = 0;
138 * if (0 == 0) {
139 * } else {
140 * a = 4;
141 * }
142 * return a;
143 *
144 * Which becomes the following graph (numbered by lifetime position):
145 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 * 4: constant4
147 * 6: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100149 * 10: equal
150 * 12: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100151 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100152 * 16: goto 20: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100153 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100154 * 22: phi
155 * 24: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100156 * |
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100157 * 28: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100158 */
159 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
160 Instruction::CONST_4 | 0 | 0,
161 Instruction::IF_EQ, 3,
162 Instruction::CONST_4 | 4 << 12 | 0,
163 Instruction::RETURN | 0 << 8);
164
165 ArenaPool pool;
166 ArenaAllocator allocator(&pool);
167 HGraph* graph = BuildGraph(data, &allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400168 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
169 X86InstructionSetFeatures::FromCppDefines());
170 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100171 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100172 liveness.Analyze();
173
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100174 // Test for the 4 constant.
175 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100176 LiveRange* range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100177 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100178 // Last use is the phi at the return block so instruction is live until
179 // the end of the then block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100180 ASSERT_EQ(18u, range->GetEnd());
181 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100182
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100183 // Test for the 0 constant.
184 interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185 // The then branch is a hole for this constant, therefore its interval has 2 ranges.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100186 // First range starts from the definition and ends at the if block.
187 range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100188 ASSERT_EQ(2u, range->GetStart());
189 // 14 is the end of the if block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100190 ASSERT_EQ(14u, range->GetEnd());
191 // Second range is the else block.
192 range = range->GetNext();
193 ASSERT_EQ(18u, range->GetStart());
194 // Last use is the phi at the return block.
195 ASSERT_EQ(22u, range->GetEnd());
196 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100197
198 // Test for the phi.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100199 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100200 range = interval->GetFirstRange();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100201 ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100202 ASSERT_EQ(22u, range->GetStart());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100203 ASSERT_EQ(24u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100204 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100205}
206
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100207TEST(LiveRangesTest, Loop1) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100208 /*
209 * Test the following snippet:
210 * var a = 0;
211 * while (a == a) {
212 * a = 4;
213 * }
214 * return 5;
215 *
216 * Which becomes the following graph (numbered by lifetime position):
217 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100218 * 4: constant4
219 * 6: constant5
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100220 * 8: goto
221 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100222 * 12: goto
223 * |
224 * 14: phi
225 * 16: equal
226 * 18: if +++++
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100227 * | \ +
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100228 * | 22: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100229 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100230 * 26: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100231 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100232 * 30: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100233 */
234
235 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
236 Instruction::CONST_4 | 0 | 0,
237 Instruction::IF_EQ, 4,
238 Instruction::CONST_4 | 4 << 12 | 0,
239 Instruction::GOTO | 0xFD00,
240 Instruction::CONST_4 | 5 << 12 | 1 << 8,
241 Instruction::RETURN | 1 << 8);
242
243 ArenaPool pool;
244 ArenaAllocator allocator(&pool);
245 HGraph* graph = BuildGraph(data, &allocator);
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +0100246 RemoveSuspendChecks(graph);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400247 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
248 X86InstructionSetFeatures::FromCppDefines());
249 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100250 SsaLivenessAnalysis liveness(*graph, &codegen);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100251 liveness.Analyze();
252
253 // Test for the 0 constant.
254 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100255 LiveRange* range = interval->GetFirstRange();
256 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100257 // Last use is the loop phi so instruction is live until
258 // the end of the pre loop header.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100259 ASSERT_EQ(14u, range->GetEnd());
260 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100261
262 // Test for the 4 constant.
263 interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100264 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100265 // The instruction is live until the end of the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100266 ASSERT_EQ(4u, range->GetStart());
267 ASSERT_EQ(24u, range->GetEnd());
268 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100269
270 // Test for the 5 constant.
271 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100272 range = interval->GetFirstRange();
273 // The instruction is live until the return instruction after the loop.
274 ASSERT_EQ(6u, range->GetStart());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100275 ASSERT_EQ(26u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100276 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100277
278 // Test for the phi.
279 interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100280 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100281 // Instruction is consumed by the if.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100282 ASSERT_EQ(14u, range->GetStart());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100283 ASSERT_EQ(17u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100284 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100285}
286
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100287TEST(LiveRangesTest, Loop2) {
288 /*
289 * Test the following snippet:
290 * var a = 0;
291 * while (a == a) {
292 * a = a + a;
293 * }
294 * return a;
295 *
296 * Which becomes the following graph (numbered by lifetime position):
297 * 2: constant0
298 * 4: goto
299 * |
300 * 8: goto
301 * |
302 * 10: phi
303 * 12: equal
304 * 14: if +++++
305 * | \ +
306 * | 18: suspend
307 * | 20: add
308 * | 22: goto
309 * |
310 * 26: return
311 * |
312 * 30: exit
313 *
314 * We want to make sure the phi at 10 has a lifetime hole after the add at 20.
315 */
316
317 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
318 Instruction::CONST_4 | 0 | 0,
319 Instruction::IF_EQ, 6,
320 Instruction::ADD_INT, 0, 0,
321 Instruction::GOTO | 0xFB00,
322 Instruction::RETURN | 0 << 8);
323
324 ArenaPool pool;
325 ArenaAllocator allocator(&pool);
326 HGraph* graph = BuildGraph(data, &allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400327 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
328 X86InstructionSetFeatures::FromCppDefines());
329 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100330 SsaLivenessAnalysis liveness(*graph, &codegen);
331 liveness.Analyze();
332
333 // Test for the 0 constant.
334 HIntConstant* constant = liveness.GetInstructionFromSsaIndex(0)->AsIntConstant();
335 LiveInterval* interval = constant->GetLiveInterval();
336 LiveRange* range = interval->GetFirstRange();
337 ASSERT_EQ(2u, range->GetStart());
338 // Last use is the loop phi so instruction is live until
339 // the end of the pre loop header.
340 ASSERT_EQ(10u, range->GetEnd());
341 ASSERT_TRUE(range->GetNext() == nullptr);
342
343 // Test for the loop phi.
344 HPhi* phi = liveness.GetInstructionFromSsaIndex(1)->AsPhi();
345 interval = phi->GetLiveInterval();
346 range = interval->GetFirstRange();
347 ASSERT_EQ(10u, range->GetStart());
348 ASSERT_EQ(21u, range->GetEnd());
349 range = range->GetNext();
350 ASSERT_TRUE(range != nullptr);
351 ASSERT_EQ(24u, range->GetStart());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100352 ASSERT_EQ(26u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100353
354 // Test for the add instruction.
355 HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd();
356 interval = add->GetLiveInterval();
357 range = interval->GetFirstRange();
358 ASSERT_EQ(20u, range->GetStart());
359 ASSERT_EQ(24u, range->GetEnd());
360 ASSERT_TRUE(range->GetNext() == nullptr);
361}
362
363TEST(LiveRangesTest, CFG4) {
364 /*
365 * Test the following snippet:
366 * var a = 0;
367 * var b = 4;
368 * if (a == a) {
369 * a = b + a;
370 * } else {
371 * a = b + a
372 * }
373 * return b;
374 *
375 * Which becomes the following graph (numbered by lifetime position):
376 * 2: constant0
377 * 4: constant4
378 * 6: goto
379 * |
380 * 10: equal
381 * 12: if
382 * / \
383 * 16: add 22: add
384 * 18: goto 24: goto
385 * \ /
386 * 26: phi
387 * 28: return
388 * |
389 * 32: exit
390 *
391 * We want to make sure the constant0 has a lifetime hole after the 16: add.
392 */
393 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
394 Instruction::CONST_4 | 0 | 0,
395 Instruction::CONST_4 | 4 << 12 | 1 << 8,
396 Instruction::IF_EQ, 5,
397 Instruction::ADD_INT, 1 << 8,
398 Instruction::GOTO | 0x300,
399 Instruction::ADD_INT, 1 << 8,
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000400 Instruction::RETURN);
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100401
402 ArenaPool pool;
403 ArenaAllocator allocator(&pool);
404 HGraph* graph = BuildGraph(data, &allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400405 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
406 X86InstructionSetFeatures::FromCppDefines());
407 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100408 SsaLivenessAnalysis liveness(*graph, &codegen);
409 liveness.Analyze();
410
411 // Test for the 0 constant.
412 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
413 LiveRange* range = interval->GetFirstRange();
414 ASSERT_EQ(2u, range->GetStart());
Mark Mendell09b84632015-02-13 17:48:38 -0500415 ASSERT_EQ(17u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100416 range = range->GetNext();
417 ASSERT_TRUE(range != nullptr);
418 ASSERT_EQ(20u, range->GetStart());
Mark Mendell09b84632015-02-13 17:48:38 -0500419 ASSERT_EQ(23u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100420 ASSERT_TRUE(range->GetNext() == nullptr);
421
422 // Test for the 4 constant.
423 interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
424 range = interval->GetFirstRange();
425 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000426 ASSERT_EQ(17u, range->GetEnd());
427 range = range->GetNext();
428 ASSERT_EQ(20u, range->GetStart());
429 ASSERT_EQ(23u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100430 ASSERT_TRUE(range->GetNext() == nullptr);
431
432 // Test for the first add.
433 HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd();
434 interval = add->GetLiveInterval();
435 range = interval->GetFirstRange();
436 ASSERT_EQ(16u, range->GetStart());
437 ASSERT_EQ(20u, range->GetEnd());
438 ASSERT_TRUE(range->GetNext() == nullptr);
439
440 // Test for the second add.
441 add = liveness.GetInstructionFromSsaIndex(3)->AsAdd();
442 interval = add->GetLiveInterval();
443 range = interval->GetFirstRange();
444 ASSERT_EQ(22u, range->GetStart());
445 ASSERT_EQ(26u, range->GetEnd());
446 ASSERT_TRUE(range->GetNext() == nullptr);
447
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100448 HPhi* phi = liveness.GetInstructionFromSsaIndex(4)->AsPhi();
David Brazdilea55b932015-01-27 17:12:29 +0000449 ASSERT_TRUE(phi->GetUses().HasOnlyOneUse());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100450 interval = phi->GetLiveInterval();
451 range = interval->GetFirstRange();
452 ASSERT_EQ(26u, range->GetStart());
453 ASSERT_EQ(28u, range->GetEnd());
454 ASSERT_TRUE(range->GetNext() == nullptr);
455}
456
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100457} // namespace art