blob: 395143988138de6b7fa7d64a39518d8f1048a8e9 [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);
David Brazdil5e8b1372015-01-23 14:39:08 +000041 HGraph* graph = new (&allocator) HGraph(&allocator);
42 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 Geoffray8a16d972014-09-11 10:30:02 +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);
63 HGraph* graph = new (&allocator) HGraph(&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) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000258 HGraph* graph = new (allocator) HGraph(allocator);
259 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 Geoffray8a16d972014-09-11 10:30:02 +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 Geoffray8a16d972014-09-11 10:30:02 +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 Geoffray8a16d972014-09-11 10:30:02 +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 Geoffrayaac0f392014-09-16 14:11:14 +0100422 SsaLivenessAnalysis liveness(*graph, &codegen);
423 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);
Mingyao Yang296bd602014-10-06 16:47:28 -0700429 // For SSA value intervals, only an interval resulted from a split may intersect
430 // with inactive intervals.
431 unhandled = register_allocator.Split(unhandled, 5);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100432
433 // Add three temps holding the same register, and starting at different positions.
434 // Put the one that should be picked in the middle of the inactive list to ensure
435 // we do not depend on an order.
Mingyao Yang296bd602014-10-06 16:47:28 -0700436 LiveInterval* interval = LiveInterval::MakeInterval(&allocator, Primitive::kPrimInt);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100437 interval->SetRegister(0);
438 interval->AddRange(40, 50);
439 register_allocator.inactive_.Add(interval);
440
Mingyao Yang296bd602014-10-06 16:47:28 -0700441 interval = LiveInterval::MakeInterval(&allocator, Primitive::kPrimInt);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100442 interval->SetRegister(0);
443 interval->AddRange(20, 30);
444 register_allocator.inactive_.Add(interval);
445
Mingyao Yang296bd602014-10-06 16:47:28 -0700446 interval = LiveInterval::MakeInterval(&allocator, Primitive::kPrimInt);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100447 interval->SetRegister(0);
448 interval->AddRange(60, 70);
449 register_allocator.inactive_.Add(interval);
450
451 register_allocator.number_of_registers_ = 1;
452 register_allocator.registers_array_ = allocator.AllocArray<size_t>(1);
453 register_allocator.processing_core_registers_ = true;
454 register_allocator.unhandled_ = &register_allocator.unhandled_core_intervals_;
455
Mingyao Yang296bd602014-10-06 16:47:28 -0700456 ASSERT_TRUE(register_allocator.TryAllocateFreeReg(unhandled));
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100457
458 // Check that we have split the interval.
459 ASSERT_EQ(1u, register_allocator.unhandled_->Size());
460 // Check that we know need to find a new register where the next interval
461 // that uses the register starts.
462 ASSERT_EQ(20u, register_allocator.unhandled_->Get(0)->GetStart());
463}
464
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100465static HGraph* BuildIfElseWithPhi(ArenaAllocator* allocator,
466 HPhi** phi,
467 HInstruction** input1,
468 HInstruction** input2) {
469 HGraph* graph = new (allocator) HGraph(allocator);
470 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
471 graph->AddBlock(entry);
472 graph->SetEntryBlock(entry);
473 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
474 entry->AddInstruction(parameter);
475
476 HBasicBlock* block = new (allocator) HBasicBlock(graph);
477 graph->AddBlock(block);
478 entry->AddSuccessor(block);
479
480 HInstruction* test = new (allocator) HInstanceFieldGet(
Calin Juravle52c48962014-12-16 17:02:57 +0000481 parameter, Primitive::kPrimBoolean, MemberOffset(22), false);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100482 block->AddInstruction(test);
483 block->AddInstruction(new (allocator) HIf(test));
484 HBasicBlock* then = new (allocator) HBasicBlock(graph);
485 HBasicBlock* else_ = new (allocator) HBasicBlock(graph);
486 HBasicBlock* join = new (allocator) HBasicBlock(graph);
487 graph->AddBlock(then);
488 graph->AddBlock(else_);
489 graph->AddBlock(join);
490
491 block->AddSuccessor(then);
492 block->AddSuccessor(else_);
493 then->AddSuccessor(join);
494 else_->AddSuccessor(join);
495 then->AddInstruction(new (allocator) HGoto());
496 else_->AddInstruction(new (allocator) HGoto());
497
498 *phi = new (allocator) HPhi(allocator, 0, 0, Primitive::kPrimInt);
499 join->AddPhi(*phi);
Calin Juravle52c48962014-12-16 17:02:57 +0000500 *input1 = new (allocator) HInstanceFieldGet(parameter, Primitive::kPrimInt,
501 MemberOffset(42), false);
502 *input2 = new (allocator) HInstanceFieldGet(parameter, Primitive::kPrimInt,
503 MemberOffset(42), false);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100504 then->AddInstruction(*input1);
505 else_->AddInstruction(*input2);
506 join->AddInstruction(new (allocator) HExit());
507 (*phi)->AddInput(*input1);
508 (*phi)->AddInput(*input2);
509
510 graph->BuildDominatorTree();
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000511 graph->AnalyzeNaturalLoops();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100512 return graph;
513}
514
515TEST(RegisterAllocatorTest, PhiHint) {
516 ArenaPool pool;
517 ArenaAllocator allocator(&pool);
518 HPhi *phi;
519 HInstruction *input1, *input2;
520
521 {
522 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400523 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
524 X86InstructionSetFeatures::FromCppDefines());
525 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100526 SsaLivenessAnalysis liveness(*graph, &codegen);
527 liveness.Analyze();
528
529 // Check that the register allocator is deterministic.
530 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
531 register_allocator.AllocateRegisters();
532
533 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 0);
534 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 0);
535 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 0);
536 }
537
538 {
539 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400540 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
541 X86InstructionSetFeatures::FromCppDefines());
542 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100543 SsaLivenessAnalysis liveness(*graph, &codegen);
544 liveness.Analyze();
545
546 // Set the phi to a specific register, and check that the inputs get allocated
547 // the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000548 phi->GetLocations()->UpdateOut(Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100549 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
550 register_allocator.AllocateRegisters();
551
552 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2);
553 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2);
554 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2);
555 }
556
557 {
558 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400559 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
560 X86InstructionSetFeatures::FromCppDefines());
561 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100562 SsaLivenessAnalysis liveness(*graph, &codegen);
563 liveness.Analyze();
564
565 // Set input1 to a specific register, and check that the phi and other input get allocated
566 // the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000567 input1->GetLocations()->UpdateOut(Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100568 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
569 register_allocator.AllocateRegisters();
570
571 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2);
572 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2);
573 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2);
574 }
575
576 {
577 HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400578 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
579 X86InstructionSetFeatures::FromCppDefines());
580 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100581 SsaLivenessAnalysis liveness(*graph, &codegen);
582 liveness.Analyze();
583
584 // Set input2 to a specific register, and check that the phi and other input get allocated
585 // the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000586 input2->GetLocations()->UpdateOut(Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100587 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
588 register_allocator.AllocateRegisters();
589
590 ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2);
591 ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2);
592 ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2);
593 }
594}
595
596static HGraph* BuildFieldReturn(ArenaAllocator* allocator,
597 HInstruction** field,
598 HInstruction** ret) {
599 HGraph* graph = new (allocator) HGraph(allocator);
600 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
601 graph->AddBlock(entry);
602 graph->SetEntryBlock(entry);
603 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimNot);
604 entry->AddInstruction(parameter);
605
606 HBasicBlock* block = new (allocator) HBasicBlock(graph);
607 graph->AddBlock(block);
608 entry->AddSuccessor(block);
609
Calin Juravle52c48962014-12-16 17:02:57 +0000610 *field = new (allocator) HInstanceFieldGet(parameter, Primitive::kPrimInt,
611 MemberOffset(42), false);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100612 block->AddInstruction(*field);
613 *ret = new (allocator) HReturn(*field);
614 block->AddInstruction(*ret);
615
616 HBasicBlock* exit = new (allocator) HBasicBlock(graph);
617 graph->AddBlock(exit);
618 block->AddSuccessor(exit);
619 exit->AddInstruction(new (allocator) HExit());
David Brazdil10f56cb2015-03-24 18:49:14 +0000620
621 graph->BuildDominatorTree();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100622 return graph;
623}
624
625TEST(RegisterAllocatorTest, ExpectedInRegisterHint) {
626 ArenaPool pool;
627 ArenaAllocator allocator(&pool);
628 HInstruction *field, *ret;
629
630 {
631 HGraph* graph = BuildFieldReturn(&allocator, &field, &ret);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400632 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
633 X86InstructionSetFeatures::FromCppDefines());
634 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100635 SsaLivenessAnalysis liveness(*graph, &codegen);
636 liveness.Analyze();
637
638 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
639 register_allocator.AllocateRegisters();
640
641 // Sanity check that in normal conditions, the register should be hinted to 0 (EAX).
642 ASSERT_EQ(field->GetLiveInterval()->GetRegister(), 0);
643 }
644
645 {
646 HGraph* graph = BuildFieldReturn(&allocator, &field, &ret);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400647 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
648 X86InstructionSetFeatures::FromCppDefines());
649 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100650 SsaLivenessAnalysis liveness(*graph, &codegen);
651 liveness.Analyze();
652
653 // Check that the field gets put in the register expected by its use.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000654 // Don't use SetInAt because we are overriding an already allocated location.
655 ret->GetLocations()->inputs_.Put(0, Location::RegisterLocation(2));
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100656
657 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
658 register_allocator.AllocateRegisters();
659
660 ASSERT_EQ(field->GetLiveInterval()->GetRegister(), 2);
661 }
662}
663
Mark Mendell09b84632015-02-13 17:48:38 -0500664static HGraph* BuildTwoSubs(ArenaAllocator* allocator,
665 HInstruction** first_sub,
666 HInstruction** second_sub) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100667 HGraph* graph = new (allocator) HGraph(allocator);
668 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
669 graph->AddBlock(entry);
670 graph->SetEntryBlock(entry);
671 HInstruction* parameter = new (allocator) HParameterValue(0, Primitive::kPrimInt);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100672 entry->AddInstruction(parameter);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000673
674 HInstruction* constant1 = graph->GetIntConstant(1);
675 HInstruction* constant2 = graph->GetIntConstant(2);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100676
677 HBasicBlock* block = new (allocator) HBasicBlock(graph);
678 graph->AddBlock(block);
679 entry->AddSuccessor(block);
680
Mark Mendell09b84632015-02-13 17:48:38 -0500681 *first_sub = new (allocator) HSub(Primitive::kPrimInt, parameter, constant1);
682 block->AddInstruction(*first_sub);
683 *second_sub = new (allocator) HSub(Primitive::kPrimInt, *first_sub, constant2);
684 block->AddInstruction(*second_sub);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100685
686 block->AddInstruction(new (allocator) HExit());
David Brazdil10f56cb2015-03-24 18:49:14 +0000687
688 graph->BuildDominatorTree();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100689 return graph;
690}
691
692TEST(RegisterAllocatorTest, SameAsFirstInputHint) {
693 ArenaPool pool;
694 ArenaAllocator allocator(&pool);
Mark Mendell09b84632015-02-13 17:48:38 -0500695 HInstruction *first_sub, *second_sub;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100696
697 {
Mark Mendell09b84632015-02-13 17:48:38 -0500698 HGraph* graph = BuildTwoSubs(&allocator, &first_sub, &second_sub);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400699 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
700 X86InstructionSetFeatures::FromCppDefines());
701 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100702 SsaLivenessAnalysis liveness(*graph, &codegen);
703 liveness.Analyze();
704
705 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
706 register_allocator.AllocateRegisters();
707
708 // Sanity check that in normal conditions, the registers are the same.
Mark Mendell09b84632015-02-13 17:48:38 -0500709 ASSERT_EQ(first_sub->GetLiveInterval()->GetRegister(), 1);
710 ASSERT_EQ(second_sub->GetLiveInterval()->GetRegister(), 1);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100711 }
712
713 {
Mark Mendell09b84632015-02-13 17:48:38 -0500714 HGraph* graph = BuildTwoSubs(&allocator, &first_sub, &second_sub);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400715 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
716 X86InstructionSetFeatures::FromCppDefines());
717 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100718 SsaLivenessAnalysis liveness(*graph, &codegen);
719 liveness.Analyze();
720
721 // check that both adds get the same register.
Nicolas Geoffray18c219b2015-02-04 09:38:49 +0000722 // Don't use UpdateOutput because output is already allocated.
Mark Mendell09b84632015-02-13 17:48:38 -0500723 first_sub->InputAt(0)->GetLocations()->output_ = Location::RegisterLocation(2);
724 ASSERT_EQ(first_sub->GetLocations()->Out().GetPolicy(), Location::kSameAsFirstInput);
725 ASSERT_EQ(second_sub->GetLocations()->Out().GetPolicy(), Location::kSameAsFirstInput);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100726
727 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
728 register_allocator.AllocateRegisters();
729
Mark Mendell09b84632015-02-13 17:48:38 -0500730 ASSERT_EQ(first_sub->GetLiveInterval()->GetRegister(), 2);
731 ASSERT_EQ(second_sub->GetLiveInterval()->GetRegister(), 2);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100732 }
733}
734
Calin Juravled0d48522014-11-04 16:40:20 +0000735static HGraph* BuildDiv(ArenaAllocator* allocator,
736 HInstruction** div) {
737 HGraph* graph = new (allocator) HGraph(allocator);
738 HBasicBlock* entry = new (allocator) HBasicBlock(graph);
739 graph->AddBlock(entry);
740 graph->SetEntryBlock(entry);
741 HInstruction* first = new (allocator) HParameterValue(0, Primitive::kPrimInt);
742 HInstruction* second = new (allocator) HParameterValue(0, Primitive::kPrimInt);
743 entry->AddInstruction(first);
744 entry->AddInstruction(second);
745
746 HBasicBlock* block = new (allocator) HBasicBlock(graph);
747 graph->AddBlock(block);
748 entry->AddSuccessor(block);
749
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000750 *div = new (allocator) HDiv(Primitive::kPrimInt, first, second, 0); // don't care about dex_pc.
Calin Juravled0d48522014-11-04 16:40:20 +0000751 block->AddInstruction(*div);
752
753 block->AddInstruction(new (allocator) HExit());
David Brazdil10f56cb2015-03-24 18:49:14 +0000754
755 graph->BuildDominatorTree();
Calin Juravled0d48522014-11-04 16:40:20 +0000756 return graph;
757}
758
759TEST(RegisterAllocatorTest, ExpectedExactInRegisterAndSameOutputHint) {
760 ArenaPool pool;
761 ArenaAllocator allocator(&pool);
762 HInstruction *div;
763
764 {
765 HGraph* graph = BuildDiv(&allocator, &div);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400766 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
767 X86InstructionSetFeatures::FromCppDefines());
768 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Calin Juravled0d48522014-11-04 16:40:20 +0000769 SsaLivenessAnalysis liveness(*graph, &codegen);
770 liveness.Analyze();
771
772 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
773 register_allocator.AllocateRegisters();
774
775 // div on x86 requires its first input in eax and the output be the same as the first input.
776 ASSERT_EQ(div->GetLiveInterval()->GetRegister(), 0);
777 }
778}
779
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000780// Test a bug in the register allocator, where allocating a blocked
781// register would lead to spilling an inactive interval at the wrong
782// position.
783TEST(RegisterAllocatorTest, SpillInactive) {
784 ArenaPool pool;
785
786 // Create a synthesized graph to please the register_allocator and
787 // ssa_liveness_analysis code.
788 ArenaAllocator allocator(&pool);
789 HGraph* graph = new (&allocator) HGraph(&allocator);
790 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
791 graph->AddBlock(entry);
792 graph->SetEntryBlock(entry);
793 HInstruction* one = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
794 HInstruction* two = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
795 HInstruction* three = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
796 HInstruction* four = new (&allocator) HParameterValue(0, Primitive::kPrimInt);
797 entry->AddInstruction(one);
798 entry->AddInstruction(two);
799 entry->AddInstruction(three);
800 entry->AddInstruction(four);
801
802 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
803 graph->AddBlock(block);
804 entry->AddSuccessor(block);
805 block->AddInstruction(new (&allocator) HExit());
806
807 // We create a synthesized user requesting a register, to avoid just spilling the
808 // intervals.
809 HPhi* user = new (&allocator) HPhi(&allocator, 0, 1, Primitive::kPrimInt);
810 user->AddInput(one);
811 user->SetBlock(block);
812 LocationSummary* locations = new (&allocator) LocationSummary(user, LocationSummary::kNoCall);
813 locations->SetInAt(0, Location::RequiresRegister());
814 static constexpr size_t phi_ranges[][2] = {{20, 30}};
815 BuildInterval(phi_ranges, arraysize(phi_ranges), &allocator, -1, user);
816
817 // Create an interval with lifetime holes.
818 static constexpr size_t ranges1[][2] = {{0, 2}, {4, 6}, {8, 10}};
819 LiveInterval* first = BuildInterval(ranges1, arraysize(ranges1), &allocator, -1, one);
820 first->first_use_ = new(&allocator) UsePosition(user, 0, false, 8, first->first_use_);
821 first->first_use_ = new(&allocator) UsePosition(user, 0, false, 7, first->first_use_);
822 first->first_use_ = new(&allocator) UsePosition(user, 0, false, 6, first->first_use_);
823
824 locations = new (&allocator) LocationSummary(first->GetDefinedBy(), LocationSummary::kNoCall);
825 locations->SetOut(Location::RequiresRegister());
826 first = first->SplitAt(1);
827
828 // Create an interval that conflicts with the next interval, to force the next
829 // interval to call `AllocateBlockedReg`.
830 static constexpr size_t ranges2[][2] = {{2, 4}};
831 LiveInterval* second = BuildInterval(ranges2, arraysize(ranges2), &allocator, -1, two);
832 locations = new (&allocator) LocationSummary(second->GetDefinedBy(), LocationSummary::kNoCall);
833 locations->SetOut(Location::RequiresRegister());
834
835 // Create an interval that will lead to splitting the first interval. The bug occured
836 // by splitting at a wrong position, in this case at the next intersection between
837 // this interval and the first interval. We would have then put the interval with ranges
838 // "[0, 2(, [4, 6(" in the list of handled intervals, even though we haven't processed intervals
839 // before lifetime position 6 yet.
840 static constexpr size_t ranges3[][2] = {{2, 4}, {8, 10}};
841 LiveInterval* third = BuildInterval(ranges3, arraysize(ranges3), &allocator, -1, three);
842 third->first_use_ = new(&allocator) UsePosition(user, 0, false, 8, third->first_use_);
843 third->first_use_ = new(&allocator) UsePosition(user, 0, false, 4, third->first_use_);
844 third->first_use_ = new(&allocator) UsePosition(user, 0, false, 3, third->first_use_);
845 locations = new (&allocator) LocationSummary(third->GetDefinedBy(), LocationSummary::kNoCall);
846 locations->SetOut(Location::RequiresRegister());
847 third = third->SplitAt(3);
848
849 // Because the first part of the split interval was considered handled, this interval
850 // was free to allocate the same register, even though it conflicts with it.
851 static constexpr size_t ranges4[][2] = {{4, 6}};
852 LiveInterval* fourth = BuildInterval(ranges4, arraysize(ranges4), &allocator, -1, four);
853 locations = new (&allocator) LocationSummary(fourth->GetDefinedBy(), LocationSummary::kNoCall);
854 locations->SetOut(Location::RequiresRegister());
855
Mark Mendellfb8d2792015-03-31 22:16:59 -0400856 std::unique_ptr<const X86InstructionSetFeatures> features_x86(
857 X86InstructionSetFeatures::FromCppDefines());
858 x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions());
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000859 SsaLivenessAnalysis liveness(*graph, &codegen);
860
861 RegisterAllocator register_allocator(&allocator, &codegen, liveness);
862 register_allocator.unhandled_core_intervals_.Add(fourth);
863 register_allocator.unhandled_core_intervals_.Add(third);
864 register_allocator.unhandled_core_intervals_.Add(second);
865 register_allocator.unhandled_core_intervals_.Add(first);
866
867 // Set just one register available to make all intervals compete for the same.
868 register_allocator.number_of_registers_ = 1;
869 register_allocator.registers_array_ = allocator.AllocArray<size_t>(1);
870 register_allocator.processing_core_registers_ = true;
871 register_allocator.unhandled_ = &register_allocator.unhandled_core_intervals_;
872 register_allocator.LinearScan();
873
874 // Test that there is no conflicts between intervals.
875 GrowableArray<LiveInterval*> intervals(&allocator, 0);
876 intervals.Add(first);
877 intervals.Add(second);
878 intervals.Add(third);
879 intervals.Add(fourth);
880 ASSERT_TRUE(RegisterAllocator::ValidateIntervals(
881 intervals, 0, 0, codegen, &allocator, true, false));
882}
883
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100884} // namespace art