blob: b7da36299db87771908d4d3d90905ebf957a5098 [file] [log] [blame]
Nicolas Geoffraya7062e02014-05-22 12:50:17 +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 Geoffraya7062e02014-05-22 12:50:17 +010019#include "builder.h"
20#include "code_generator.h"
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010021#include "code_generator_x86.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010022#include "dex_file.h"
23#include "dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000024#include "driver/compiler_options.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010025#include "nodes.h"
26#include "optimizing_unit_test.h"
27#include "register_allocator.h"
28#include "ssa_liveness_analysis.h"
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +010029#include "ssa_phi_elimination.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010030
31#include "gtest/gtest.h"
32
33namespace art {
34
35// Note: the register allocator tests rely on the fact that constants have live
36// intervals and registers get allocated to them.
37
38static bool Check(const uint16_t* data) {
39 ArenaPool pool;
40 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010041 HGraph* graph = CreateGraph(&allocator);
David Brazdil5e8b1372015-01-23 14:39:08 +000042 HGraphBuilder builder(graph);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010043 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000044 builder.BuildGraph(*item);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000045 graph->TryBuildingSsa();
Mark Mendellfb8d2792015-03-31 22:16:59 -040046 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
47 X86InstructionSetFeatures::FromCppDefines());
48 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010049 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010050 liveness.Analyze();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010051 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010052 register_allocator.AllocateRegisters();
53 return register_allocator.Validate(false);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010054}
55
56/**
57 * Unit testing of RegisterAllocator::ValidateIntervals. Register allocator
58 * tests are based on this validation method.
59 */
60TEST(RegisterAllocatorTest, ValidateIntervals) {
61 ArenaPool pool;
62 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010063 HGraph* graph = CreateGraph(&allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -040064 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
65 X86InstructionSetFeatures::FromCppDefines());
66 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010067 GrowableArray<LiveInterval*> intervals(&allocator, 0);
68
69 // Test with two intervals of the same range.
70 {
71 static constexpr size_t ranges[][2] = {{0, 42}};
72 intervals.Add(BuildInterval(ranges, arraysize(ranges), &allocator, 0));
73 intervals.Add(BuildInterval(ranges, arraysize(ranges), &allocator, 1));
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010074 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010075 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076
77 intervals.Get(1)->SetRegister(0);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010078 ASSERT_FALSE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010079 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 intervals.Reset();
81 }
82
83 // Test with two non-intersecting intervals.
84 {
85 static constexpr size_t ranges1[][2] = {{0, 42}};
86 intervals.Add(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0));
87 static constexpr size_t ranges2[][2] = {{42, 43}};
88 intervals.Add(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1));
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010089 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010090 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010091
92 intervals.Get(1)->SetRegister(0);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010093 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010094 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010095 intervals.Reset();
96 }
97
98 // Test with two non-intersecting intervals, with one with a lifetime hole.
99 {
100 static constexpr size_t ranges1[][2] = {{0, 42}, {45, 48}};
101 intervals.Add(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0));
102 static constexpr size_t ranges2[][2] = {{42, 43}};
103 intervals.Add(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1));
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100104 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100105 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100106
107 intervals.Get(1)->SetRegister(0);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100108 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100109 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100110 intervals.Reset();
111 }
112
113 // Test with intersecting intervals.
114 {
115 static constexpr size_t ranges1[][2] = {{0, 42}, {44, 48}};
116 intervals.Add(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0));
117 static constexpr size_t ranges2[][2] = {{42, 47}};
118 intervals.Add(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1));
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100119 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100120 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100121
122 intervals.Get(1)->SetRegister(0);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100123 ASSERT_FALSE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100124 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125 intervals.Reset();
126 }
127
128 // Test with siblings.
129 {
130 static constexpr size_t ranges1[][2] = {{0, 42}, {44, 48}};
131 intervals.Add(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0));
132 intervals.Get(0)->SplitAt(43);
133 static constexpr size_t ranges2[][2] = {{42, 47}};
134 intervals.Add(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1));
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100135 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100136 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137
138 intervals.Get(1)->SetRegister(0);
139 // Sibling of the first interval has no register allocated to it.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100140 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100141 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100142
143 intervals.Get(0)->GetNextSibling()->SetRegister(0);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100144 ASSERT_FALSE(RegisterAllocator::ValidateIntervals(
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100145 intervals, 0, 0, codegen, &allocator, true, false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 }
147}
148
149TEST(RegisterAllocatorTest, CFG1) {
150 /*
151 * Test the following snippet:
152 * return 0;
153 *
154 * Which becomes the following graph:
155 * constant0
156 * goto
157 * |
158 * return
159 * |
160 * exit
161 */
162 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
163 Instruction::CONST_4 | 0 | 0,
164 Instruction::RETURN);
165
166 ASSERT_TRUE(Check(data));
167}
168
169TEST(RegisterAllocatorTest, Loop1) {
170 /*
171 * Test the following snippet:
172 * int a = 0;
173 * while (a == a) {
174 * a = 4;
175 * }
176 * return 5;
177 *
178 * Which becomes the following graph:
179 * constant0
180 * constant4
181 * constant5
182 * goto
183 * |
184 * goto
185 * |
186 * phi
187 * equal
188 * if +++++
189 * | \ +
190 * | goto
191 * |
192 * return
193 * |
194 * exit
195 */
196
197 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
198 Instruction::CONST_4 | 0 | 0,
199 Instruction::IF_EQ, 4,
200 Instruction::CONST_4 | 4 << 12 | 0,
201 Instruction::GOTO | 0xFD00,
202 Instruction::CONST_4 | 5 << 12 | 1 << 8,
203 Instruction::RETURN | 1 << 8);
204
205 ASSERT_TRUE(Check(data));
206}
207
208TEST(RegisterAllocatorTest, Loop2) {
209 /*
210 * Test the following snippet:
211 * int a = 0;
212 * while (a == 8) {
213 * a = 4 + 5;
214 * }
215 * return 6 + 7;
216 *
217 * Which becomes the following graph:
218 * constant0
219 * constant4
220 * constant5
221 * constant6
222 * constant7
223 * constant8
224 * goto
225 * |
226 * goto
227 * |
228 * phi
229 * equal
230 * if +++++
231 * | \ +
232 * | 4 + 5
233 * | goto
234 * |
235 * 6 + 7
236 * return
237 * |
238 * exit
239 */
240
241 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
242 Instruction::CONST_4 | 0 | 0,
243 Instruction::CONST_4 | 8 << 12 | 1 << 8,
244 Instruction::IF_EQ | 1 << 8, 7,
245 Instruction::CONST_4 | 4 << 12 | 0 << 8,
246 Instruction::CONST_4 | 5 << 12 | 1 << 8,
247 Instruction::ADD_INT, 1 << 8 | 0,
248 Instruction::GOTO | 0xFA00,
249 Instruction::CONST_4 | 6 << 12 | 1 << 8,
250 Instruction::CONST_4 | 7 << 12 | 1 << 8,
251 Instruction::ADD_INT, 1 << 8 | 0,
252 Instruction::RETURN | 1 << 8);
253
254 ASSERT_TRUE(Check(data));
255}
256
257static HGraph* BuildSSAGraph(const uint16_t* data, ArenaAllocator* allocator) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100258 HGraph* graph = CreateGraph(allocator);
David Brazdil5e8b1372015-01-23 14:39:08 +0000259 HGraphBuilder builder(graph);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100260 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +0000261 builder.BuildGraph(*item);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000262 graph->TryBuildingSsa();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100263 return graph;
264}
265
266TEST(RegisterAllocatorTest, Loop3) {
267 /*
268 * Test the following snippet:
269 * int a = 0
270 * do {
271 * b = a;
272 * a++;
273 * } while (a != 5)
274 * return b;
275 *
276 * Which becomes the following graph:
277 * constant0
278 * constant1
279 * constant5
280 * goto
281 * |
282 * goto
283 * |++++++++++++
284 * phi +
285 * a++ +
286 * equals +
287 * if +
288 * |++++++++++++
289 * return
290 * |
291 * exit
292 */
293
294 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
295 Instruction::CONST_4 | 0 | 0,
296 Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8,
297 Instruction::CONST_4 | 5 << 12 | 2 << 8,
298 Instruction::IF_NE | 1 << 8 | 2 << 12, 3,
299 Instruction::RETURN | 0 << 8,
300 Instruction::MOVE | 1 << 12 | 0 << 8,
301 Instruction::GOTO | 0xF900);
302
303 ArenaPool pool;
304 ArenaAllocator allocator(&pool);
305 HGraph* graph = BuildSSAGraph(data, &allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400306 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
307 X86InstructionSetFeatures::FromCppDefines());
308 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100309 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100310 liveness.Analyze();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100311 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100312 register_allocator.AllocateRegisters();
313 ASSERT_TRUE(register_allocator.Validate(false));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100314
315 HBasicBlock* loop_header = graph->GetBlocks().Get(2);
316 HPhi* phi = loop_header->GetFirstPhi()->AsPhi();
317
318 LiveInterval* phi_interval = phi->GetLiveInterval();
319 LiveInterval* loop_update = phi->InputAt(1)->GetLiveInterval();
320 ASSERT_TRUE(phi_interval->HasRegister());
321 ASSERT_TRUE(loop_update->HasRegister());
322 ASSERT_NE(phi_interval->GetRegister(), loop_update->GetRegister());
323
324 HBasicBlock* return_block = graph->GetBlocks().Get(3);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100325 HReturn* ret = return_block->GetLastInstruction()->AsReturn();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100326 ASSERT_EQ(phi_interval->GetRegister(), ret->InputAt(0)->GetLiveInterval()->GetRegister());
327}
328
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100329TEST(RegisterAllocatorTest, FirstRegisterUse) {
330 const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
331 Instruction::CONST_4 | 0 | 0,
Mark Mendell09b84632015-02-13 17:48:38 -0500332 Instruction::XOR_INT_LIT8 | 1 << 8, 1 << 8,
333 Instruction::XOR_INT_LIT8 | 0 << 8, 1 << 8,
334 Instruction::XOR_INT_LIT8 | 1 << 8, 1 << 8 | 1,
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100335 Instruction::RETURN_VOID);
336
337 ArenaPool pool;
338 ArenaAllocator allocator(&pool);
339 HGraph* graph = BuildSSAGraph(data, &allocator);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400340 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
341 X86InstructionSetFeatures::FromCppDefines());
342 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100343 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100344 liveness.Analyze();
345
Mark Mendell09b84632015-02-13 17:48:38 -0500346 HXor* first_xor = graph->GetBlocks().Get(1)->GetFirstInstruction()->AsXor();
347 HXor* last_xor = graph->GetBlocks().Get(1)->GetLastInstruction()->GetPrevious()->AsXor();
348 ASSERT_EQ(last_xor->InputAt(0), first_xor);
349 LiveInterval* interval = first_xor->GetLiveInterval();
350 ASSERT_EQ(interval->GetEnd(), last_xor->GetLifetimePosition());
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100351 ASSERT_TRUE(interval->GetNextSibling() == nullptr);
352
353 // We need a register for the output of the instruction.
Mark Mendell09b84632015-02-13 17:48:38 -0500354 ASSERT_EQ(interval->FirstRegisterUse(), first_xor->GetLifetimePosition());
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100355
356 // Split at the next instruction.
Mark Mendell09b84632015-02-13 17:48:38 -0500357 interval = interval->SplitAt(first_xor->GetLifetimePosition() + 2);
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100358 // The user of the split is the last add.
Mark Mendell09b84632015-02-13 17:48:38 -0500359 ASSERT_EQ(interval->FirstRegisterUse(), last_xor->GetLifetimePosition());
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100360
361 // Split before the last add.
Mark Mendell09b84632015-02-13 17:48:38 -0500362 LiveInterval* new_interval = interval->SplitAt(last_xor->GetLifetimePosition() - 1);
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100363 // Ensure the current interval has no register use...
364 ASSERT_EQ(interval->FirstRegisterUse(), kNoLifetime);
365 // And the new interval has it for the last add.
Mark Mendell09b84632015-02-13 17:48:38 -0500366 ASSERT_EQ(new_interval->FirstRegisterUse(), last_xor->GetLifetimePosition());
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100367}
368
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100369TEST(RegisterAllocatorTest, DeadPhi) {
370 /* Test for a dead loop phi taking as back-edge input a phi that also has
371 * this loop phi as input. Walking backwards in SsaDeadPhiElimination
372 * does not solve the problem because the loop phi will be visited last.
373 *
374 * Test the following snippet:
375 * int a = 0
376 * do {
377 * if (true) {
378 * a = 2;
379 * }
380 * } while (true);
381 */
382
383 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
384 Instruction::CONST_4 | 0 | 0,
385 Instruction::CONST_4 | 1 << 8 | 0,
386 Instruction::IF_NE | 1 << 8 | 1 << 12, 3,
387 Instruction::CONST_4 | 2 << 12 | 0 << 8,
388 Instruction::GOTO | 0xFD00,
389 Instruction::RETURN_VOID);
390
391 ArenaPool pool;
392 ArenaAllocator allocator(&pool);
393 HGraph* graph = BuildSSAGraph(data, &allocator);
394 SsaDeadPhiElimination(graph).Run();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400395 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
396 X86InstructionSetFeatures::FromCppDefines());
397 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100398 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100399 liveness.Analyze();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100400 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100401 register_allocator.AllocateRegisters();
402 ASSERT_TRUE(register_allocator.Validate(false));
403}
404
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100405/**
406 * Test that the TryAllocateFreeReg method works in the presence of inactive intervals
407 * that share the same register. It should split the interval it is currently
408 * allocating for at the minimum lifetime position between the two inactive intervals.
409 */
410TEST(RegisterAllocatorTest, FreeUntil) {
411 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
412 Instruction::CONST_4 | 0 | 0,
413 Instruction::RETURN);
414
415 ArenaPool pool;
416 ArenaAllocator allocator(&pool);
417 HGraph* graph = BuildSSAGraph(data, &allocator);
418 SsaDeadPhiElimination(graph).Run();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400419 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
420 X86InstructionSetFeatures::FromCppDefines());
421 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100422 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100423 liveness.Analyze();
424 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
425
426 // Add an artifical range to cover the temps that will be put in the unhandled list.
427 LiveInterval* unhandled = graph->GetEntryBlock()->GetFirstInstruction()->GetLiveInterval();
428 unhandled->AddLoopRange(0, 60);
Nicolas Geoffray23a81882015-06-01 18:12:38 +0100429
Nicolas Geoffray30971d62015-06-01 18:37:24 +0100430 // Populate the instructions in the liveness object, to please the register allocator.
Nicolas Geoffray23a81882015-06-01 18:12:38 +0100431 for (size_t i = 0; i < 60; ++i) {
432 liveness.instructions_from_lifetime_position_.Add(
433 graph->GetEntryBlock()->GetFirstInstruction());
434 }
435
Mingyao Yang296bd602014-10-06 16:47:28 -0700436 // For SSA value intervals, only an interval resulted from a split may intersect
437 // with inactive intervals.
438 unhandled = register_allocator.Split(unhandled, 5);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100439
440 // Add three temps holding the same register, and starting at different positions.
441 // Put the one that should be picked in the middle of the inactive list to ensure
442 // we do not depend on an order.
David Brazdilf4eb9ae2015-04-17 18:19:30 +0100443 LiveInterval* interval = LiveInterval::MakeFixedInterval(&allocator, 0, Primitive::kPrimInt);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100444 interval->AddRange(40, 50);
445 register_allocator.inactive_.Add(interval);
446
David Brazdilf4eb9ae2015-04-17 18:19:30 +0100447 interval = LiveInterval::MakeFixedInterval(&allocator, 0, Primitive::kPrimInt);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100448 interval->AddRange(20, 30);
449 register_allocator.inactive_.Add(interval);
450
David Brazdilf4eb9ae2015-04-17 18:19:30 +0100451 interval = LiveInterval::MakeFixedInterval(&allocator, 0, Primitive::kPrimInt);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100452 interval->AddRange(60, 70);
453 register_allocator.inactive_.Add(interval);
454
455 register_allocator.number_of_registers_ = 1;
456 register_allocator.registers_array_ = allocator.AllocArray<size_t>(1);
457 register_allocator.processing_core_registers_ = true;
458 register_allocator.unhandled_ = &register_allocator.unhandled_core_intervals_;
459
Mingyao Yang296bd602014-10-06 16:47:28 -0700460 ASSERT_TRUE(register_allocator.TryAllocateFreeReg(unhandled));
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100461
462 // Check that we have split the interval.
463 ASSERT_EQ(1u, register_allocator.unhandled_->Size());
464 // Check that we know need to find a new register where the next interval
465 // that uses the register starts.
466 ASSERT_EQ(20u, register_allocator.unhandled_->Get(0)->GetStart());
467}
468
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100469static HGraph* BuildIfElseWithPhi(ArenaAllocator* allocator,
470 HPhi** phi,
471 HInstruction** input1,
472 HInstruction** input2) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100473 HGraph* graph = CreateGraph(allocator);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100474 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
475 graph->AddBlock(entry);
476 graph->SetEntryBlock(entry);
477 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
478 entry->AddInstruction(parameter);
479
480 HBasicBlock* block = new (allocator) HBasicBlock(graph);
481 graph->AddBlock(block);
482 entry->AddSuccessor(block);
483
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100484 HInstruction* test = new (allocator) HInstanceFieldGet(parameter,
485 Primitive::kPrimBoolean,
486 MemberOffset(22),
487 false,
488 kUnknownFieldIndex,
489 graph->GetDexFile());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100490 block->AddInstruction(test);
491 block->AddInstruction(new (allocator) HIf(test));
492 HBasicBlock* then = new (allocator) HBasicBlock(graph);
493 HBasicBlock* else_ = new (allocator) HBasicBlock(graph);
494 HBasicBlock* join = new (allocator) HBasicBlock(graph);
495 graph->AddBlock(then);
496 graph->AddBlock(else_);
497 graph->AddBlock(join);
498
499 block->AddSuccessor(then);
500 block->AddSuccessor(else_);
501 then->AddSuccessor(join);
502 else_->AddSuccessor(join);
503 then->AddInstruction(new (allocator) HGoto());
504 else_->AddInstruction(new (allocator) HGoto());
505
506 *phi = new (allocator) HPhi(allocator, 0, 0, Primitive::kPrimInt);
507 join->AddPhi(*phi);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100508 *input1 = new (allocator) HInstanceFieldGet(parameter,
509 Primitive::kPrimInt,
510 MemberOffset(42),
511 false,
512 kUnknownFieldIndex,
513 graph->GetDexFile());
514*input2 = new (allocator) HInstanceFieldGet(parameter,
515 Primitive::kPrimInt,
516 MemberOffset(42),
517 false,
518 kUnknownFieldIndex,
519 graph->GetDexFile());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100520 then->AddInstruction(*input1);
521 else_->AddInstruction(*input2);
522 join->AddInstruction(new (allocator) HExit());
523 (*phi)->AddInput(*input1);
524 (*phi)->AddInput(*input2);
525
526 graph->BuildDominatorTree();
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000527 graph->AnalyzeNaturalLoops();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100528 return graph;
529}
530
531TEST(RegisterAllocatorTest, PhiHint) {
532 ArenaPool pool;
533 ArenaAllocator allocator(&pool);
534 HPhi *phi;
535 HInstruction *input1, *input2;
536
537 {
538 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400539 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
540 X86InstructionSetFeatures::FromCppDefines());
541 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100542 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100543 liveness.Analyze();
544
545 // Check that the register allocator is deterministic.
546 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
547 register_allocator.AllocateRegisters();
548
549 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 0);
550 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 0);
551 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 0);
552 }
553
554 {
555 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400556 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
557 X86InstructionSetFeatures::FromCppDefines());
558 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100559 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100560 liveness.Analyze();
561
562 // Set the phi to a specific register, and check that the inputs get allocated
563 // the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000564 phi->GetLocations()->UpdateOut(Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100565 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
566 register_allocator.AllocateRegisters();
567
568 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2);
569 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2);
570 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2);
571 }
572
573 {
574 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400575 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
576 X86InstructionSetFeatures::FromCppDefines());
577 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100578 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100579 liveness.Analyze();
580
581 // Set input1 to a specific register, and check that the phi and other input get allocated
582 // the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000583 input1->GetLocations()->UpdateOut(Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100584 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
585 register_allocator.AllocateRegisters();
586
587 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2);
588 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2);
589 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2);
590 }
591
592 {
593 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400594 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
595 X86InstructionSetFeatures::FromCppDefines());
596 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100597 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100598 liveness.Analyze();
599
600 // Set input2 to a specific register, and check that the phi and other input get allocated
601 // the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000602 input2->GetLocations()->UpdateOut(Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100603 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
604 register_allocator.AllocateRegisters();
605
606 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2);
607 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2);
608 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2);
609 }
610}
611
612static HGraph* BuildFieldReturn(ArenaAllocator* allocator,
613 HInstruction** field,
614 HInstruction** ret) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100615 HGraph* graph = CreateGraph(allocator);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100616 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
617 graph->AddBlock(entry);
618 graph->SetEntryBlock(entry);
619 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
620 entry->AddInstruction(parameter);
621
622 HBasicBlock* block = new (allocator) HBasicBlock(graph);
623 graph->AddBlock(block);
624 entry->AddSuccessor(block);
625
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100626 *field = new (allocator) HInstanceFieldGet(parameter,
627 Primitive::kPrimInt,
628 MemberOffset(42),
629 false,
630 kUnknownFieldIndex,
631 graph->GetDexFile());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100632 block->AddInstruction(*field);
633 *ret = new (allocator) HReturn(*field);
634 block->AddInstruction(*ret);
635
636 HBasicBlock* exit = new (allocator) HBasicBlock(graph);
637 graph->AddBlock(exit);
638 block->AddSuccessor(exit);
639 exit->AddInstruction(new (allocator) HExit());
David Brazdil10f56cb2015-03-24 18:49:14 +0000640
641 graph->BuildDominatorTree();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100642 return graph;
643}
644
645TEST(RegisterAllocatorTest, ExpectedInRegisterHint) {
646 ArenaPool pool;
647 ArenaAllocator allocator(&pool);
648 HInstruction *field, *ret;
649
650 {
651 HGraph* graph = BuildFieldReturn(&allocator, &field, &ret);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400652 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
653 X86InstructionSetFeatures::FromCppDefines());
654 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100655 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100656 liveness.Analyze();
657
658 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
659 register_allocator.AllocateRegisters();
660
661 // Sanity check that in normal conditions, the register should be hinted to 0 (EAX).
662 ASSERT_EQ(field->GetLiveInterval()->GetRegister(), 0);
663 }
664
665 {
666 HGraph* graph = BuildFieldReturn(&allocator, &field, &ret);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400667 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
668 X86InstructionSetFeatures::FromCppDefines());
669 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100670 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100671 liveness.Analyze();
672
673 // Check that the field gets put in the register expected by its use.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000674 // Don't use SetInAt because we are overriding an already allocated location.
675 ret->GetLocations()->inputs_.Put(0, Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100676
677 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
678 register_allocator.AllocateRegisters();
679
680 ASSERT_EQ(field->GetLiveInterval()->GetRegister(), 2);
681 }
682}
683
Mark Mendell09b84632015-02-13 17:48:38 -0500684static HGraph* BuildTwoSubs(ArenaAllocator* allocator,
685 HInstruction** first_sub,
686 HInstruction** second_sub) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100687 HGraph* graph = CreateGraph(allocator);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100688 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
689 graph->AddBlock(entry);
690 graph->SetEntryBlock(entry);
691 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimInt);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100692 entry->AddInstruction(parameter);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000693
694 HInstruction* constant1 = graph->GetIntConstant(1);
695 HInstruction* constant2 = graph->GetIntConstant(2);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100696
697 HBasicBlock* block = new (allocator) HBasicBlock(graph);
698 graph->AddBlock(block);
699 entry->AddSuccessor(block);
700
Mark Mendell09b84632015-02-13 17:48:38 -0500701 *first_sub = new (allocator) HSub(Primitive::kPrimInt, parameter, constant1);
702 block->AddInstruction(*first_sub);
703 *second_sub = new (allocator) HSub(Primitive::kPrimInt, *first_sub, constant2);
704 block->AddInstruction(*second_sub);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100705
706 block->AddInstruction(new (allocator) HExit());
David Brazdil10f56cb2015-03-24 18:49:14 +0000707
708 graph->BuildDominatorTree();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100709 return graph;
710}
711
712TEST(RegisterAllocatorTest, SameAsFirstInputHint) {
713 ArenaPool pool;
714 ArenaAllocator allocator(&pool);
Mark Mendell09b84632015-02-13 17:48:38 -0500715 HInstruction *first_sub, *second_sub;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100716
717 {
Mark Mendell09b84632015-02-13 17:48:38 -0500718 HGraph* graph = BuildTwoSubs(&allocator, &first_sub, &second_sub);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400719 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
720 X86InstructionSetFeatures::FromCppDefines());
721 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100722 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100723 liveness.Analyze();
724
725 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
726 register_allocator.AllocateRegisters();
727
728 // Sanity check that in normal conditions, the registers are the same.
Mark Mendell09b84632015-02-13 17:48:38 -0500729 ASSERT_EQ(first_sub->GetLiveInterval()->GetRegister(), 1);
730 ASSERT_EQ(second_sub->GetLiveInterval()->GetRegister(), 1);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100731 }
732
733 {
Mark Mendell09b84632015-02-13 17:48:38 -0500734 HGraph* graph = BuildTwoSubs(&allocator, &first_sub, &second_sub);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400735 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
736 X86InstructionSetFeatures::FromCppDefines());
737 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100738 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100739 liveness.Analyze();
740
741 // check that both adds get the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000742 // Don't use UpdateOutput because output is already allocated.
Mark Mendell09b84632015-02-13 17:48:38 -0500743 first_sub->InputAt(0)->GetLocations()->output_ = Location::RegisterLocation(2);
744 ASSERT_EQ(first_sub->GetLocations()->Out().GetPolicy(), Location::kSameAsFirstInput);
745 ASSERT_EQ(second_sub->GetLocations()->Out().GetPolicy(), Location::kSameAsFirstInput);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100746
747 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
748 register_allocator.AllocateRegisters();
749
Mark Mendell09b84632015-02-13 17:48:38 -0500750 ASSERT_EQ(first_sub->GetLiveInterval()->GetRegister(), 2);
751 ASSERT_EQ(second_sub->GetLiveInterval()->GetRegister(), 2);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100752 }
753}
754
Calin Juravled0d48522014-11-04 16:40:20 +0000755static HGraph* BuildDiv(ArenaAllocator* allocator,
756 HInstruction** div) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100757 HGraph* graph = CreateGraph(allocator);
Calin Juravled0d48522014-11-04 16:40:20 +0000758 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
759 graph->AddBlock(entry);
760 graph->SetEntryBlock(entry);
761 HInstruction* first = new (allocator) HParameterValue(0, Primitive::kPrimInt);
762 HInstruction* second = new (allocator) HParameterValue(0, Primitive::kPrimInt);
763 entry->AddInstruction(first);
764 entry->AddInstruction(second);
765
766 HBasicBlock* block = new (allocator) HBasicBlock(graph);
767 graph->AddBlock(block);
768 entry->AddSuccessor(block);
769
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000770 *div = new (allocator) HDiv(Primitive::kPrimInt, first, second, 0); // don't care about dex_pc.
Calin Juravled0d48522014-11-04 16:40:20 +0000771 block->AddInstruction(*div);
772
773 block->AddInstruction(new (allocator) HExit());
David Brazdil10f56cb2015-03-24 18:49:14 +0000774
775 graph->BuildDominatorTree();
Calin Juravled0d48522014-11-04 16:40:20 +0000776 return graph;
777}
778
779TEST(RegisterAllocatorTest, ExpectedExactInRegisterAndSameOutputHint) {
780 ArenaPool pool;
781 ArenaAllocator allocator(&pool);
782 HInstruction *div;
783
784 {
785 HGraph* graph = BuildDiv(&allocator, &div);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400786 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
787 X86InstructionSetFeatures::FromCppDefines());
788 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100789 SsaLivenessAnalysis liveness(graph, &codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000790 liveness.Analyze();
791
792 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
793 register_allocator.AllocateRegisters();
794
795 // div on x86 requires its first input in eax and the output be the same as the first input.
796 ASSERT_EQ(div->GetLiveInterval()->GetRegister(), 0);
797 }
798}
799
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000800// Test a bug in the register allocator, where allocating a blocked
801// register would lead to spilling an inactive interval at the wrong
802// position.
803TEST(RegisterAllocatorTest, SpillInactive) {
804 ArenaPool pool;
805
806 // Create a synthesized graph to please the register_allocator and
807 // ssa_liveness_analysis code.
808 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100809 HGraph* graph = CreateGraph(&allocator);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000810 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
811 graph->AddBlock(entry);
812 graph->SetEntryBlock(entry);
813 HInstruction* one = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
814 HInstruction* two = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
815 HInstruction* three = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
816 HInstruction* four = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
817 entry->AddInstruction(one);
818 entry->AddInstruction(two);
819 entry->AddInstruction(three);
820 entry->AddInstruction(four);
821
822 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
823 graph->AddBlock(block);
824 entry->AddSuccessor(block);
825 block->AddInstruction(new (&allocator) HExit());
826
827 // We create a synthesized user requesting a register, to avoid just spilling the
828 // intervals.
829 HPhi* user = new (&allocator) HPhi(&allocator, 0, 1, Primitive::kPrimInt);
830 user->AddInput(one);
831 user->SetBlock(block);
832 LocationSummary* locations = new (&allocator) LocationSummary(user, LocationSummary::kNoCall);
833 locations->SetInAt(0, Location::RequiresRegister());
834 static constexpr size_t phi_ranges[][2] = {{20, 30}};
835 BuildInterval(phi_ranges, arraysize(phi_ranges), &allocator, -1, user);
836
837 // Create an interval with lifetime holes.
838 static constexpr size_t ranges1[][2] = {{0, 2}, {4, 6}, {8, 10}};
839 LiveInterval* first = BuildInterval(ranges1, arraysize(ranges1), &allocator, -1, one);
840 first->first_use_ = new(&allocator) UsePosition(user, 0, false, 8, first->first_use_);
841 first->first_use_ = new(&allocator) UsePosition(user, 0, false, 7, first->first_use_);
842 first->first_use_ = new(&allocator) UsePosition(user, 0, false, 6, first->first_use_);
843
844 locations = new (&allocator) LocationSummary(first->GetDefinedBy(), LocationSummary::kNoCall);
845 locations->SetOut(Location::RequiresRegister());
846 first = first->SplitAt(1);
847
848 // Create an interval that conflicts with the next interval, to force the next
849 // interval to call `AllocateBlockedReg`.
850 static constexpr size_t ranges2[][2] = {{2, 4}};
851 LiveInterval* second = BuildInterval(ranges2, arraysize(ranges2), &allocator, -1, two);
852 locations = new (&allocator) LocationSummary(second->GetDefinedBy(), LocationSummary::kNoCall);
853 locations->SetOut(Location::RequiresRegister());
854
855 // Create an interval that will lead to splitting the first interval. The bug occured
856 // by splitting at a wrong position, in this case at the next intersection between
857 // this interval and the first interval. We would have then put the interval with ranges
858 // "[0, 2(, [4, 6(" in the list of handled intervals, even though we haven't processed intervals
859 // before lifetime position 6 yet.
860 static constexpr size_t ranges3[][2] = {{2, 4}, {8, 10}};
861 LiveInterval* third = BuildInterval(ranges3, arraysize(ranges3), &allocator, -1, three);
862 third->first_use_ = new(&allocator) UsePosition(user, 0, false, 8, third->first_use_);
863 third->first_use_ = new(&allocator) UsePosition(user, 0, false, 4, third->first_use_);
864 third->first_use_ = new(&allocator) UsePosition(user, 0, false, 3, third->first_use_);
865 locations = new (&allocator) LocationSummary(third->GetDefinedBy(), LocationSummary::kNoCall);
866 locations->SetOut(Location::RequiresRegister());
867 third = third->SplitAt(3);
868
869 // Because the first part of the split interval was considered handled, this interval
870 // was free to allocate the same register, even though it conflicts with it.
871 static constexpr size_t ranges4[][2] = {{4, 6}};
872 LiveInterval* fourth = BuildInterval(ranges4, arraysize(ranges4), &allocator, -1, four);
873 locations = new (&allocator) LocationSummary(fourth->GetDefinedBy(), LocationSummary::kNoCall);
874 locations->SetOut(Location::RequiresRegister());
875
Mark Mendellfb8d2792015-03-31 22:16:59 -0400876 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
877 X86InstructionSetFeatures::FromCppDefines());
878 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100879 SsaLivenessAnalysis liveness(graph, &codegen);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +0100880 // Populate the instructions in the liveness object, to please the register allocator.
881 for (size_t i = 0; i < 32; ++i) {
882 liveness.instructions_from_lifetime_position_.Add(user);
883 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000884
885 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
886 register_allocator.unhandled_core_intervals_.Add(fourth);
887 register_allocator.unhandled_core_intervals_.Add(third);
888 register_allocator.unhandled_core_intervals_.Add(second);
889 register_allocator.unhandled_core_intervals_.Add(first);
890
891 // Set just one register available to make all intervals compete for the same.
892 register_allocator.number_of_registers_ = 1;
893 register_allocator.registers_array_ = allocator.AllocArray<size_t>(1);
894 register_allocator.processing_core_registers_ = true;
895 register_allocator.unhandled_ = &register_allocator.unhandled_core_intervals_;
896 register_allocator.LinearScan();
897
898 // Test that there is no conflicts between intervals.
899 GrowableArray<LiveInterval*> intervals(&allocator, 0);
900 intervals.Add(first);
901 intervals.Add(second);
902 intervals.Add(third);
903 intervals.Add(fourth);
904 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
905 intervals, 0, 0, codegen, &allocator, true, false));
906}
907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100908} // namespace art