OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/flow_graph_allocator.h" | 5 #include "vm/flow_graph_allocator.h" |
6 | 6 |
7 #include "vm/bit_vector.h" | 7 #include "vm/bit_vector.h" |
8 #include "vm/intermediate_language.h" | 8 #include "vm/intermediate_language.h" |
9 #include "vm/il_printer.h" | 9 #include "vm/il_printer.h" |
10 #include "vm/flow_graph_builder.h" | 10 #include "vm/flow_graph_builder.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 #define TRACE_ALLOC(m) do { \ | 21 #define TRACE_ALLOC(m) do { \ |
22 if (FLAG_trace_ssa_allocator) OS::Print m ; \ | 22 if (FLAG_trace_ssa_allocator) OS::Print m ; \ |
23 } while (0) | 23 } while (0) |
24 #else | 24 #else |
25 #define TRACE_ALLOC(m) | 25 #define TRACE_ALLOC(m) |
26 #endif | 26 #endif |
27 | 27 |
28 | 28 |
29 static const intptr_t kNoVirtualRegister = -1; | 29 static const intptr_t kNoVirtualRegister = -1; |
30 static const intptr_t kTempVirtualRegister = -2; | 30 static const intptr_t kTempVirtualRegister = -2; |
31 static UseInterval* const kPermanentlyBlocked = | |
32 reinterpret_cast<UseInterval*>(-1); | |
33 static const intptr_t kIllegalPosition = -1; | 31 static const intptr_t kIllegalPosition = -1; |
34 static const intptr_t kMaxPosition = 0x7FFFFFFF; | 32 static const intptr_t kMaxPosition = 0x7FFFFFFF; |
35 | 33 |
36 | 34 |
35 static intptr_t MinPosition(intptr_t a, intptr_t b) { | |
36 return (a < b) ? a : b; | |
37 } | |
38 | |
39 | |
40 static bool IsParallelMovePosition(intptr_t pos) { | |
41 return (pos & 1) == 0; | |
42 } | |
43 | |
44 | |
45 static bool IsInstructionPosition(intptr_t pos) { | |
46 return (pos & 1) == 1; | |
47 } | |
48 | |
49 | |
50 static intptr_t ToParallelMove(intptr_t pos) { | |
Kevin Millikin (Google)
2012/07/24 15:20:51
I prefer ToPreviousParallelMove.
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
This would be confusing: if position is the parall
| |
51 return (pos & ~1); | |
52 } | |
53 | |
37 FlowGraphAllocator::FlowGraphAllocator( | 54 FlowGraphAllocator::FlowGraphAllocator( |
38 const GrowableArray<BlockEntryInstr*>& block_order, | 55 const GrowableArray<BlockEntryInstr*>& block_order, |
39 FlowGraphBuilder* builder) | 56 FlowGraphBuilder* builder) |
40 : builder_(builder), | 57 : builder_(builder), |
41 block_order_(block_order), | 58 block_order_(block_order), |
42 postorder_(builder->postorder_block_entries()), | 59 postorder_(builder->postorder_block_entries()), |
43 live_out_(block_order.length()), | 60 live_out_(block_order.length()), |
44 kill_(block_order.length()), | 61 kill_(block_order.length()), |
45 live_in_(block_order.length()), | 62 live_in_(block_order.length()), |
46 vreg_count_(builder->current_ssa_temp_index()), | 63 vreg_count_(builder->current_ssa_temp_index()), |
47 live_ranges_(builder->current_ssa_temp_index()) { | 64 live_ranges_(builder->current_ssa_temp_index()) { |
48 for (intptr_t i = 0; i < vreg_count_; i++) live_ranges_.Add(NULL); | 65 for (intptr_t i = 0; i < vreg_count_; i++) live_ranges_.Add(NULL); |
49 | 66 |
50 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { | 67 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { |
51 cpu_regs_[reg] = NULL; | 68 blocked_cpu_regs_[reg] = false; |
Kevin Millikin (Google)
2012/07/24 15:20:51
blocked_cpu_regs_ will be zero initialized if you
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
Done.
| |
52 } | 69 } |
53 | 70 |
54 cpu_regs_[CTX] = kPermanentlyBlocked; | 71 blocked_cpu_regs_[CTX] = true; |
55 if (TMP != kNoRegister) { | 72 if (TMP != kNoRegister) { |
56 cpu_regs_[TMP] = kPermanentlyBlocked; | 73 blocked_cpu_regs_[TMP] = true; |
57 } | 74 } |
58 cpu_regs_[SPREG] = kPermanentlyBlocked; | 75 blocked_cpu_regs_[SPREG] = true; |
59 cpu_regs_[FPREG] = kPermanentlyBlocked; | 76 blocked_cpu_regs_[FPREG] = true; |
60 } | 77 } |
61 | 78 |
62 | 79 |
63 void FlowGraphAllocator::ComputeInitialSets() { | 80 void FlowGraphAllocator::ComputeInitialSets() { |
64 const intptr_t block_count = postorder_.length(); | 81 const intptr_t block_count = postorder_.length(); |
65 for (intptr_t i = 0; i < block_count; i++) { | 82 for (intptr_t i = 0; i < block_count; i++) { |
66 BlockEntryInstr* block = postorder_[i]; | 83 BlockEntryInstr* block = postorder_[i]; |
67 | 84 |
68 BitVector* kill = kill_[i]; | 85 BitVector* kill = kill_[i]; |
69 BitVector* live_in = live_in_[i]; | 86 BitVector* live_in = live_in_[i]; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 } | 223 } |
207 OS::Print("\n"); | 224 OS::Print("\n"); |
208 | 225 |
209 PrintBitVector(" live out", live_out_[i]); | 226 PrintBitVector(" live out", live_out_[i]); |
210 PrintBitVector(" kill", kill_[i]); | 227 PrintBitVector(" kill", kill_[i]); |
211 PrintBitVector(" live in", live_in_[i]); | 228 PrintBitVector(" live in", live_in_[i]); |
212 } | 229 } |
213 } | 230 } |
214 | 231 |
215 | 232 |
216 void UseInterval::Print() { | 233 void LiveRange::AddUse(intptr_t pos, Location* location_slot) { |
217 OS::Print(" [%d, %d) uses {", start_, end_); | 234 ASSERT((first_use_interval_->start_ <= pos) && |
218 for (UsePosition* use_pos = uses_; | 235 (pos <= first_use_interval_->end_)); |
219 use_pos != NULL && use_pos->pos() <= end(); | |
220 use_pos = use_pos->next()) { | |
221 if (use_pos != uses_) OS::Print(", "); | |
222 OS::Print("%d", use_pos->pos()); | |
223 } | |
224 OS::Print("}\n"); | |
225 } | |
226 | |
227 | |
228 void UseInterval::AddUse(Instruction* instr, | |
229 intptr_t pos, | |
230 Location* location_slot) { | |
231 ASSERT((start_ <= pos) && (pos <= end_)); | |
232 ASSERT((instr == NULL) || (instr->lifetime_position() == pos)); | |
233 if ((uses_ != NULL) && (uses_->pos() == pos)) { | 236 if ((uses_ != NULL) && (uses_->pos() == pos)) { |
234 if ((location_slot == NULL) || (uses_->location_slot() == location_slot)) { | 237 if ((location_slot == NULL) || (uses_->location_slot() == location_slot)) { |
235 return; | 238 return; |
236 } else if ((uses_->location_slot() == NULL) && (instr == NULL)) { | 239 } else if (uses_->location_slot() == NULL) { |
237 uses_->set_location_slot(location_slot); | 240 uses_->set_location_slot(location_slot); |
238 return; | 241 return; |
239 } | 242 } |
240 } | 243 } |
241 uses_ = new UsePosition(instr, pos, uses_, location_slot); | 244 uses_ = new UsePosition(pos, uses_, location_slot); |
242 } | 245 } |
243 | 246 |
244 | 247 |
245 void LiveRange::Print() { | 248 void LiveRange::AddUseInterval(intptr_t start, intptr_t end) { |
246 OS::Print("vreg %d live intervals:\n", vreg_); | 249 ASSERT(start < end); |
247 for (UseInterval* interval = head_; | 250 |
248 interval != NULL; | 251 // Live ranges are being build by visiting instructions in post-order. |
Kevin Millikin (Google)
2012/07/24 15:20:51
That doesn't have anything to do with postorder.
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
Well we are visiting blocks in post order (and ins
| |
249 interval = interval->next_) { | 252 // This implies that use intervals will be perpended in a monotonically |
250 interval->Print(); | 253 // decreasing order. |
254 if (first_use_interval() != NULL) { | |
255 // If the first use interval and the use interval we are adding | |
256 // touch then we can just extend the first interval to cover their | |
257 // union. | |
258 if (start >= first_use_interval()->start()) { | |
259 // The only case when we can add intervals with start greater than | |
260 // start of an already created interval is BlockLocation. | |
261 ASSERT((start == first_use_interval()->start()) || | |
262 (vreg() == kNoVirtualRegister)); | |
263 ASSERT(end <= first_use_interval()->end()); | |
264 return; | |
265 } else if (end == first_use_interval()->start()) { | |
266 first_use_interval()->start_ = start; | |
267 return; | |
268 } | |
269 | |
270 ASSERT(end < first_use_interval()->start()); | |
271 } | |
272 | |
273 first_use_interval_ = new UseInterval(start, end, first_use_interval_); | |
274 if (last_use_interval_ == NULL) { | |
275 ASSERT(first_use_interval_->next() == NULL); | |
276 last_use_interval_ = first_use_interval_; | |
251 } | 277 } |
252 } | 278 } |
253 | 279 |
254 | 280 |
255 void LiveRange::AddUseInterval(intptr_t start, intptr_t end) { | 281 void LiveRange::DefineAt(intptr_t pos) { |
256 if ((head_ != NULL) && (head_->start_ == end)) { | 282 // Live ranges are being build by visiting instructions in post-order. |
257 head_->start_ = start; | 283 // This implies that use intervals will be perpended in a monotonically |
258 return; | 284 // decreasing order. |
285 // When we encounter a use of a value inside a block we optimistically | |
286 // expand the first use interval to cover the block from the start | |
287 // to the last use in the block and then we shrink it if we encounter | |
288 // definition of the value inside the same block. | |
289 if (first_use_interval_ == NULL) { | |
290 // Definition without a use. | |
291 first_use_interval_ = new UseInterval(pos, pos + 1, NULL); | |
292 last_use_interval_ = first_use_interval_; | |
293 } else { | |
294 // Shrink the first use interval. It was optimistically expanded to | |
295 // cover the the block from the start to the last use in the block. | |
296 ASSERT(first_use_interval_->start_ <= pos); | |
297 first_use_interval_->start_ = pos; | |
259 } | 298 } |
260 | |
261 head_ = new UseInterval(vreg_, start, end, head_); | |
262 } | 299 } |
263 | 300 |
264 | 301 |
265 void LiveRange::DefineAt(Instruction* instr, intptr_t pos, Location* loc) { | |
266 if (head_ != NULL) { | |
267 ASSERT(head_->start_ <= pos); | |
268 head_->start_ = pos; | |
269 } else { | |
270 // Definition without a use. | |
271 head_ = new UseInterval(vreg_, pos, pos + 1, NULL); | |
272 } | |
273 head_->AddUse(instr, pos, loc); | |
274 } | |
275 | |
276 | |
277 // TODO(vegorov): encode use_at_start vs. use_at_end in the location itself? | |
278 void LiveRange::UseAt(Instruction* instr, | |
279 intptr_t def, intptr_t use, | |
280 bool use_at_end, | |
281 Location* loc) { | |
282 if (head_ == NULL || head_->start_ != def) { | |
283 AddUseInterval(def, use + (use_at_end ? 1 : 0)); | |
284 } | |
285 head_->AddUse(instr, use, loc); | |
286 } | |
287 | |
288 | |
289 LiveRange* FlowGraphAllocator::GetLiveRange(intptr_t vreg) { | 302 LiveRange* FlowGraphAllocator::GetLiveRange(intptr_t vreg) { |
290 if (live_ranges_[vreg] == NULL) { | 303 if (live_ranges_[vreg] == NULL) { |
291 live_ranges_[vreg] = new LiveRange(vreg); | 304 live_ranges_[vreg] = new LiveRange(vreg); |
292 } | 305 } |
293 return live_ranges_[vreg]; | 306 return live_ranges_[vreg]; |
294 } | 307 } |
295 | 308 |
296 | 309 |
297 void FlowGraphAllocator::BlockLocation(Location loc, intptr_t pos) { | 310 void FlowGraphAllocator::BlockLocation(Location loc, |
311 intptr_t from, | |
312 intptr_t to) { | |
298 ASSERT(loc.IsRegister()); | 313 ASSERT(loc.IsRegister()); |
299 const Register reg = loc.reg(); | 314 const Register reg = loc.reg(); |
300 UseInterval* last = cpu_regs_[reg]; | 315 if (blocked_cpu_regs_[reg]) return; |
301 if (last == kPermanentlyBlocked) return; | 316 if (cpu_regs_[reg].length() == 0) { |
302 if ((last != NULL) && (last->start() == pos)) return; | 317 cpu_regs_[reg].Add(new LiveRange(kNoVirtualRegister)); |
303 cpu_regs_[reg] = new UseInterval(kNoVirtualRegister, pos, pos + 1, last); | 318 } |
319 cpu_regs_[reg][0]->AddUseInterval(from, to); | |
304 } | 320 } |
305 | 321 |
306 | 322 |
307 void FlowGraphAllocator::Define(Instruction* instr, | 323 void LiveRange::Print() { |
308 intptr_t pos, | 324 OS::Print(" live range v%d [%d, %d)\n", vreg(), Start(), End()); |
309 intptr_t vreg, | 325 UsePosition* use_pos = uses_; |
310 Location* loc) { | 326 for (UseInterval* interval = first_use_interval_; |
311 LiveRange* range = GetLiveRange(vreg); | 327 interval != NULL; |
312 ASSERT(loc != NULL); | 328 interval = interval->next()) { |
313 if (loc->IsRegister()) { | 329 OS::Print(" use interval [%d, %d)\n", |
314 BlockLocation(*loc, pos); | 330 interval->start(), |
315 range->DefineAt(instr, pos + 1, loc); | 331 interval->end()); |
316 } else if (loc->IsUnallocated()) { | 332 while ((use_pos != NULL) && (use_pos->pos() <= interval->end())) { |
317 range->DefineAt(instr, pos, loc); | 333 OS::Print(" use at %d as %s\n", |
318 } else { | 334 use_pos->pos(), |
319 UNREACHABLE(); | 335 (use_pos->location_slot() == NULL) |
336 ? "-" : use_pos->location_slot()->Name()); | |
337 use_pos = use_pos->next(); | |
338 } | |
320 } | 339 } |
321 | 340 |
322 AddToUnallocated(range->head()); | 341 if (next_sibling() != NULL) { |
323 } | 342 next_sibling()->Print(); |
Kevin Millikin (Google)
2012/07/24 15:20:51
Ha ha. Cute. Should probably just use the while
| |
324 | |
325 | |
326 void FlowGraphAllocator::UseValue(Instruction* instr, | |
327 intptr_t def_pos, | |
328 intptr_t use_pos, | |
329 intptr_t vreg, | |
330 Location* loc, | |
331 bool use_at_end) { | |
332 LiveRange* range = GetLiveRange(vreg); | |
333 if (loc == NULL) { | |
334 range->UseAt(NULL, def_pos, use_pos, true, loc); | |
335 } else if (loc->IsRegister()) { | |
336 // We have a fixed use. | |
337 BlockLocation(*loc, use_pos); | |
338 range->UseAt(instr, def_pos, use_pos, false, loc); | |
339 } else if (loc->IsUnallocated()) { | |
340 ASSERT(loc->policy() == Location::kRequiresRegister); | |
341 range->UseAt(use_at_end ? NULL : instr, def_pos, use_pos, use_at_end, loc); | |
342 } | |
343 } | |
344 | |
345 | |
346 static void PrintChain(UseInterval* chain) { | |
347 if (chain == kPermanentlyBlocked) { | |
348 OS::Print(" not for allocation\n"); | |
349 return; | |
350 } | |
351 | |
352 while (chain != NULL) { | |
353 chain->Print(); | |
354 chain = chain->next(); | |
355 } | 343 } |
356 } | 344 } |
357 | 345 |
358 | 346 |
359 void FlowGraphAllocator::PrintLiveRanges() { | 347 void FlowGraphAllocator::PrintLiveRanges() { |
360 for (intptr_t i = 0; i < unallocated_.length(); i++) { | 348 for (intptr_t i = 0; i < unallocated_.length(); i++) { |
361 OS::Print("unallocated chain for vr%d\n", unallocated_[i]->vreg()); | 349 unallocated_[i]->Print(); |
362 PrintChain(unallocated_[i]); | |
363 } | 350 } |
364 | 351 |
365 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { | 352 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { |
366 OS::Print("blocking chain for %s\n", | 353 if (blocked_cpu_regs_[reg]) continue; |
354 if (cpu_regs_[reg].length() == 0) continue; | |
355 | |
356 ASSERT(cpu_regs_[reg].length() == 1); | |
357 OS::Print("blocking live range for %s\n", | |
367 Location::RegisterLocation(static_cast<Register>(reg)).Name()); | 358 Location::RegisterLocation(static_cast<Register>(reg)).Name()); |
368 PrintChain(cpu_regs_[reg]); | 359 cpu_regs_[reg][0]->Print(); |
369 } | 360 } |
370 } | 361 } |
371 | 362 |
372 | 363 |
373 void FlowGraphAllocator::BuildLiveRanges() { | 364 void FlowGraphAllocator::BuildLiveRanges() { |
374 NumberInstructions(); | 365 NumberInstructions(); |
375 | 366 |
376 const intptr_t block_count = postorder_.length(); | 367 const intptr_t block_count = postorder_.length(); |
377 for (intptr_t i = 0; i < block_count; i++) { | 368 ASSERT(postorder_[block_count - 1]->IsGraphEntry()); |
369 for (intptr_t i = 0; i < (block_count - 1); i++) { | |
378 BlockEntryInstr* block = postorder_[i]; | 370 BlockEntryInstr* block = postorder_[i]; |
379 | 371 |
380 // For every SSA value that is live out of this block create an interval | 372 // For every SSA value that is live out of this block create an interval |
381 // that covers the hole block. It will be shortened if we encounter a | 373 // that covers the hole block. It will be shortened if we encounter a |
382 // definition of this value in this block. | 374 // definition of this value in this block. |
383 for (BitVector::Iterator it(live_out_[i]); !it.Done(); it.Advance()) { | 375 for (BitVector::Iterator it(live_out_[i]); !it.Done(); it.Advance()) { |
384 LiveRange* range = GetLiveRange(it.Current()); | 376 LiveRange* range = GetLiveRange(it.Current()); |
385 range->AddUseInterval(block->start_pos(), block->end_pos()); | 377 range->AddUseInterval(block->start_pos(), block->end_pos()); |
386 } | 378 } |
387 | 379 |
388 // Position corresponding to the beginning of the last instruction in the | 380 // Connect outgoing phi-moves that were created in NumberInstructions |
389 // block. | 381 // and find last instruction that contributes to liveness. |
390 intptr_t pos = block->end_pos() - 1; | 382 Instruction* current = ConnectOutgoingPhiMoves(block); |
391 Instruction* current = block->last_instruction(); | 383 |
392 | 384 // Now process all instructions in reverse order. |
393 // Goto instructions do not contribute liveness information. | 385 while (current != block) { |
Kevin Millikin (Google)
2012/07/24 15:20:51
You can use the backward instruction iterator if y
| |
394 GotoInstr* goto_instr = current->AsGoto(); | 386 // Skip parallel moves that we insert while processing instructions. |
395 if (goto_instr != NULL) { | 387 if (!current->IsParallelMove()) { |
388 ProcessOneInstruction(block, current); | |
389 } | |
396 current = current->previous(); | 390 current = current->previous(); |
397 // If we have a parallel move here then the successor block must be a | 391 } |
398 // join with phis. The phi inputs contribute uses to each predecessor | 392 |
399 // block (and the phi outputs contribute definitions in the successor | 393 ConnectIncomingPhiMoves(block); |
400 // block). | 394 } |
401 // | 395 } |
402 // We record those uses at the end of the instruction preceding the | 396 |
403 // parallel move. This position is 'pos', because we do not assign | 397 // |
404 // instruction numbers to parallel moves. | 398 // When describing shape of live ranges in comments below we are going to use |
405 ParallelMoveInstr* parallel_move = current->AsParallelMove(); | 399 // the following notation: |
406 if (parallel_move != NULL) { | 400 // |
407 JoinEntryInstr* join = goto_instr->successor(); | 401 // B block entry |
408 ASSERT(join != NULL); | 402 // g goto instruction |
409 | 403 // m parallel move |
410 // Search for the index of the current block in the predecessors of | 404 // i any other instruction |
411 // the join. | 405 // |
412 // TODO(kmillikin): record the predecessor index in the goto when | 406 // - body of a use interval |
413 // building the predecessor list to avoid this search. | 407 // [ start of a use interval |
414 intptr_t pred_idx = join->IndexOfPredecessor(block); | 408 // ) end of a use interval |
415 ASSERT(pred_idx >= 0); | 409 // * use |
416 | 410 // |
417 // Record the corresponding phi input use for each phi. | 411 // For example diagram |
418 ZoneGrowableArray<PhiInstr*>* phis = join->phis(); | 412 // |
419 intptr_t move_idx = 0; | 413 // m i |
420 for (intptr_t phi_idx = 0; phi_idx < phis->length(); phi_idx++) { | 414 // value --*-) |
421 PhiInstr* phi = (*phis)[phi_idx]; | 415 // |
422 if (phi == NULL) continue; | 416 // can be read as: use interval for value starts somewhere before parallel move |
423 | 417 // and extends until currently processed instruction, there is a use of value |
424 Value* val = phi->InputAt(pred_idx); | 418 // at a position of the parallel move. |
425 MoveOperands* move = parallel_move->MoveOperandsAt(move_idx); | 419 // |
426 if (val->IsUse()) { | 420 |
427 const intptr_t virtual_register = | 421 Instruction* FlowGraphAllocator::ConnectOutgoingPhiMoves( |
428 val->AsUse()->definition()->ssa_temp_index(); | 422 BlockEntryInstr* block) { |
429 move->set_src(Location::RequiresRegister()); | 423 Instruction* last = block->last_instruction(); |
430 GetLiveRange( | 424 |
431 virtual_register)->head()->AddUse(NULL, pos, move->src_slot()); | 425 GotoInstr* goto_instr = last->AsGoto(); |
432 } else { | 426 if (goto_instr == NULL) return last; |
433 ASSERT(val->IsConstant()); | 427 |
434 move->set_src(Location::Constant(val->AsConstant()->value())); | 428 // If we have a parallel move here then the successor block must be a |
435 } | 429 // join with phis. The phi inputs contribute uses to each predecessor |
436 move_idx++; | 430 // block (and the phi outputs contribute definitions in the successor |
437 } | 431 // block). |
438 | 432 ParallelMoveInstr* parallel_move = goto_instr->previous()->AsParallelMove(); |
439 // Begin backward iteration with the instruction before the parallel | 433 if (parallel_move == NULL) return goto_instr->previous(); |
440 // move. | 434 |
441 current = current->previous(); | 435 // All uses are recorded at the position of parallel move preceding goto. |
Kevin Millikin (Google)
2012/07/24 15:20:51
I have a feeling that the implicit and explicit pa
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
There is not distinction between implicit and expl
| |
436 const intptr_t pos = goto_instr->lifetime_position() - 1; | |
437 ASSERT((pos >= 0) && IsParallelMovePosition(pos)); | |
438 | |
439 JoinEntryInstr* join = goto_instr->successor(); | |
440 ASSERT(join != NULL); | |
441 | |
442 // Search for the index of the current block in the predecessors of | |
443 // the join. | |
444 const intptr_t pred_idx = join->IndexOfPredecessor(block); | |
445 | |
446 // Record the corresponding phi input use for each phi. | |
447 ZoneGrowableArray<PhiInstr*>* phis = join->phis(); | |
448 intptr_t move_idx = 0; | |
449 for (intptr_t phi_idx = 0; phi_idx < phis->length(); phi_idx++) { | |
450 PhiInstr* phi = (*phis)[phi_idx]; | |
451 if (phi == NULL) continue; | |
452 | |
453 Value* val = phi->InputAt(pred_idx); | |
454 MoveOperands* move = parallel_move->MoveOperandsAt(move_idx); | |
455 if (val->IsUse()) { | |
456 // Expected shape of live ranges: | |
457 // | |
458 // m g | |
459 // value --* | |
460 // | |
461 | |
462 LiveRange* range = GetLiveRange( | |
463 val->AsUse()->definition()->ssa_temp_index()); | |
464 | |
465 range->AddUseInterval(block->start_pos(), pos); | |
466 range->AddUse(pos, move->src_slot()); | |
467 | |
468 move->set_src(Location::PrefersRegister()); | |
469 } else { | |
470 ASSERT(val->IsConstant()); | |
471 move->set_src(Location::Constant(val->AsConstant()->value())); | |
472 } | |
473 move_idx++; | |
474 } | |
475 | |
476 // Begin backward iteration with the instruction before the parallel | |
477 // move. | |
478 return parallel_move->previous(); | |
479 } | |
480 | |
481 | |
482 void FlowGraphAllocator::ConnectIncomingPhiMoves(BlockEntryInstr* block) { | |
483 // If this block is a join we need to add destinations of phi | |
484 // resolution moves to phi's live range so that register allocator will | |
485 // fill them with moves. | |
486 JoinEntryInstr* join = block->AsJoinEntry(); | |
487 if (join == NULL) return; | |
488 | |
489 // All uses are recorded at the start position in the block. | |
490 const intptr_t pos = join->start_pos(); | |
491 | |
492 ZoneGrowableArray<PhiInstr*>* phis = join->phis(); | |
493 if (phis != NULL) { | |
494 intptr_t move_idx = 0; | |
495 for (intptr_t j = 0; j < phis->length(); j++) { | |
Kevin Millikin (Google)
2012/07/24 15:20:51
j? How about something like phi_idx or phi_index.
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
Done.
| |
496 PhiInstr* phi = (*phis)[j]; | |
497 if (phi == NULL) continue; | |
498 | |
499 const intptr_t vreg = phi->ssa_temp_index(); | |
500 ASSERT(vreg != -1); | |
501 | |
502 // Expected shape of live range: | |
503 // | |
504 // B | |
505 // phi [-------- | |
506 // | |
507 LiveRange* range = GetLiveRange(vreg); | |
508 range->DefineAt(pos); // Shorten live range. | |
Kevin Millikin (Google)
2012/07/24 15:20:51
Is this really shortening it, or is pos == the ass
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
Actually since pos is equal to start of the block
| |
509 | |
510 for (intptr_t k = 0; k < phi->InputCount(); k++) { | |
Kevin Millikin (Google)
2012/07/24 15:20:51
k? How about pred_idx or something.
Vyacheslav Egorov (Google)
2012/07/24 16:01:00
Done.
| |
511 BlockEntryInstr* pred = block->PredecessorAt(k); | |
512 ASSERT(pred->last_instruction()->IsGoto()); | |
513 Instruction* move_instr = pred->last_instruction()->previous(); | |
514 ASSERT(move_instr->IsParallelMove()); | |
515 | |
516 MoveOperands* move = | |
517 move_instr->AsParallelMove()->MoveOperandsAt(move_idx); | |
518 move->set_dest(Location::PrefersRegister()); | |
519 range->AddUse(pos, move->dest_slot()); | |
442 } | 520 } |
443 } | 521 |
444 | 522 // All phi resolution moves are connected. Phi's live range is |
445 // Now process all instructions in reverse order. | 523 // complete. |
446 --pos; // 'pos' is now the start position for the current instruction. | 524 AddToUnallocated(range); |
447 while (current != block) { | 525 |
448 LocationSummary* locs = current->locs(); | 526 move_idx++; |
449 | 527 } |
450 const bool output_same_as_first_input = | 528 } |
451 locs->out().IsUnallocated() && | 529 } |
452 locs->out().policy() == Location::kSameAsFirstInput; | 530 |
453 | 531 |
454 // TODO(vegorov): number of inputs should match number of input locations. | 532 // Create and update live ranges corresponding to instruction's inputs, |
455 // TODO(vegorov): generic support for writable registers? | 533 // temporaries and output. |
456 for (intptr_t j = 0; j < current->InputCount(); j++) { | 534 void FlowGraphAllocator::ProcessOneInstruction(BlockEntryInstr* block, |
457 Value* input = current->InputAt(j); | 535 Instruction* current) { |
458 if (input->IsUse()) { | 536 const intptr_t pos = current->lifetime_position(); |
459 const intptr_t use = input->AsUse()->definition()->ssa_temp_index(); | 537 ASSERT(IsInstructionPosition(pos)); |
460 | 538 |
461 Location* in_ref = (j < locs->input_count()) ? | 539 LocationSummary* locs = current->locs(); |
462 locs->in_slot(j) : NULL; | 540 |
463 const bool use_at_end = (j > 0) || (in_ref == NULL) || | 541 // TODO(vegorov): number of inputs must match number of input locations. |
464 !output_same_as_first_input; | 542 if (locs->input_count() != current->InputCount()) { |
465 UseValue(current, block->start_pos(), pos, use, in_ref, use_at_end); | 543 builder_->Bailout("ssa allocator: number of input locations mismatch"); |
466 } | 544 } |
545 | |
546 const bool output_same_as_first_input = | |
547 locs->out().IsUnallocated() && | |
548 (locs->out().policy() == Location::kSameAsFirstInput); | |
549 | |
550 // Add uses from the deoptimization environment. | |
551 if (current->env() != NULL) { | |
552 // Any value mentioned in the deoptimization environment should survive | |
553 // until the end of instruction but it does not need to be in the register. | |
554 // Expected shape of live range: | |
555 // | |
556 // m i m | |
557 // value -----*--) | |
558 // | |
559 | |
560 Environment* env = current->env(); | |
561 const GrowableArray<Value*>& values = env->values(); | |
562 | |
563 for (intptr_t j = 0; j < values.length(); j++) { | |
564 Value* val = values[j]; | |
565 if (val->IsUse()) { | |
566 env->AddLocation(Location::Any()); | |
567 const intptr_t vreg = val->AsUse()->definition()->ssa_temp_index(); | |
568 | |
569 LiveRange* range = GetLiveRange(vreg); | |
570 range->AddUseInterval(block->start_pos(), pos + 1); | |
571 range->AddUse(pos, env->LocationSlotAt(j)); | |
572 } else { | |
573 ASSERT(val->IsConstant()); | |
574 env->AddLocation(Location::NoLocation()); | |
467 } | 575 } |
468 | 576 } |
469 // Add uses from the deoptimization environment. | 577 } |
470 // TODO(vegorov): these uses should _not_ require register but for now | 578 |
471 // they do because we don't support spilling at all. | 579 // Process inputs. |
472 if (current->env() != NULL) { | 580 // Skip the first input if output is specified with kSameAsFirstInput policy, |
473 Environment* env = current->env(); | 581 // they will be processed together at the very end. |
474 const GrowableArray<Value*>& values = env->values(); | 582 for (intptr_t j = output_same_as_first_input ? 1 : 0; |
475 | 583 j < current->InputCount(); |
476 for (intptr_t j = 0; j < values.length(); j++) { | 584 j++) { |
477 Value* val = values[j]; | 585 Value* input = current->InputAt(j); |
478 if (val->IsUse()) { | 586 ASSERT(input->IsUse()); // Can not be a constant currently. |
479 env->AddLocation(Location::RequiresRegister()); | 587 |
480 const intptr_t use = val->AsUse()->definition()->ssa_temp_index(); | 588 const intptr_t vreg = input->AsUse()->definition()->ssa_temp_index(); |
481 UseValue(current, | 589 LiveRange* range = GetLiveRange(vreg); |
482 block->start_pos(), | 590 |
483 pos, | 591 Location* in_ref = locs->in_slot(j); |
484 use, | 592 |
485 env->LocationSlotAt(j), | 593 if (in_ref->IsRegister()) { |
486 true); | 594 // Input is expected in a fixed register. Expected shape of |
487 } else { | 595 // live ranges: |
488 env->AddLocation(Location::NoLocation()); | 596 // |
489 } | 597 // m i m |
490 } | 598 // value --* |
491 } | 599 // register [-----) |
492 | 600 // |
493 // Process temps. | 601 MoveOperands* move = |
494 for (intptr_t j = 0; j < locs->temp_count(); j++) { | 602 AddMoveAt(pos - 1, *in_ref, Location::PrefersRegister()); |
495 Location temp = locs->temp(j); | 603 BlockLocation(*in_ref, pos - 1, pos + 1); |
496 if (temp.IsRegister()) { | 604 range->AddUseInterval(block->start_pos(), pos - 1); |
497 BlockLocation(temp, pos); | 605 range->AddUse(pos - 1, move->src_slot()); |
498 } else if (temp.IsUnallocated()) { | 606 } else { |
499 UseInterval* temp_interval = new UseInterval( | 607 // Normal unallocated input. Expected shape of |
500 kTempVirtualRegister, pos, pos + 1, NULL); | 608 // live ranges: |
501 temp_interval->AddUse(NULL, pos, locs->temp_slot(j)); | 609 // |
502 AddToUnallocated(temp_interval); | 610 // m i m |
503 } else { | 611 // value -----*--) |
504 UNREACHABLE(); | 612 // |
505 } | 613 ASSERT(in_ref->IsUnallocated()); |
506 } | 614 range->AddUseInterval(block->start_pos(), pos + 1); |
507 | 615 range->AddUse(pos, in_ref); |
508 // Block all allocatable registers for calls. | 616 } |
509 if (locs->is_call()) { | 617 } |
510 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { | 618 |
511 BlockLocation(Location::RegisterLocation(static_cast<Register>(reg)), | 619 // Process temps. |
512 pos); | 620 for (intptr_t j = 0; j < locs->temp_count(); j++) { |
513 } | 621 // Expected shape of live range: |
514 } | 622 // |
515 | 623 // m i m |
516 if (locs->out().IsRegister()) { | 624 // [--) |
517 builder_->Bailout("ssa allocator: fixed outputs are not supported"); | 625 // |
518 } | 626 |
519 | 627 Location temp = locs->temp(j); |
520 Definition* def = current->AsDefinition(); | 628 if (temp.IsRegister()) { |
521 if ((def != NULL) && (def->ssa_temp_index() >= 0)) { | 629 BlockLocation(temp, pos, pos + 1); |
522 Define(output_same_as_first_input ? current : NULL, | 630 } else if (temp.IsUnallocated()) { |
523 pos, | 631 LiveRange* range = new LiveRange(kTempVirtualRegister); |
524 def->ssa_temp_index(), | 632 range->AddUseInterval(pos, pos + 1); |
525 locs->out_slot()); | 633 range->AddUse(pos, locs->temp_slot(j)); |
526 } | 634 AddToUnallocated(range); |
527 | 635 } else { |
528 current = current->previous(); | 636 UNREACHABLE(); |
529 pos -= 2; | 637 } |
530 } | 638 } |
531 | 639 |
532 // If this block is a join we need to add destinations of phi | 640 // Block all allocatable registers for calls. |
533 // resolution moves to phi's live range so that register allocator will | 641 if (locs->is_call()) { |
534 // fill them with moves. | 642 // Expected shape of live range: |
535 JoinEntryInstr* join = block->AsJoinEntry(); | 643 // |
536 if (join != NULL) { | 644 // m i m |
537 ZoneGrowableArray<PhiInstr*>* phis = join->phis(); | 645 // [--) |
538 if (phis != NULL) { | 646 // |
539 intptr_t move_idx = 0; | 647 |
540 for (intptr_t j = 0; j < phis->length(); j++) { | 648 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { |
541 PhiInstr* phi = (*phis)[j]; | 649 BlockLocation(Location::RegisterLocation(static_cast<Register>(reg)), |
542 if (phi == NULL) continue; | 650 pos, |
543 | 651 pos + 1); |
544 const intptr_t virtual_register = phi->ssa_temp_index(); | 652 } |
545 ASSERT(virtual_register != -1); | 653 |
546 | 654 #ifdef DEBUG |
547 LiveRange* range = GetLiveRange(virtual_register); | 655 // Verify that temps, inputs and output were specified as fixed |
548 range->DefineAt(NULL, pos, NULL); | 656 // locations. Every register is blocked now so attempt to |
549 UseInterval* interval = GetLiveRange(virtual_register)->head(); | 657 // allocate will not succeed. |
550 | 658 for (intptr_t j = 0; j < locs->temp_count(); j++) { |
551 for (intptr_t k = 0; k < phi->InputCount(); k++) { | 659 ASSERT(!locs->temp(j).IsUnallocated()); |
552 BlockEntryInstr* pred = block->PredecessorAt(k); | 660 } |
553 ASSERT(pred->last_instruction()->IsGoto()); | 661 |
554 Instruction* move_instr = pred->last_instruction()->previous(); | 662 for (intptr_t j = 0; j < locs->input_count(); j++) { |
555 ParallelMoveInstr* pmove = move_instr->AsParallelMove(); | 663 ASSERT(!locs->in(j).IsUnallocated()); |
556 ASSERT(pmove != NULL); | 664 } |
557 | 665 |
558 MoveOperands* move_operands = pmove->MoveOperandsAt(move_idx); | 666 ASSERT(!locs->out().IsUnallocated()); |
559 move_operands->set_dest(Location::RequiresRegister()); | 667 #endif |
560 interval->AddUse(NULL, pos, move_operands->dest_slot()); | 668 } |
561 } | 669 |
562 | 670 Definition* def = current->AsDefinition(); |
563 // All phi resolution moves are connected. Phi's live range is | 671 if (def == NULL) { |
564 // complete. | 672 ASSERT(locs->out().IsInvalid()); |
565 AddToUnallocated(interval); | 673 return; |
566 | 674 } |
567 move_idx++; | 675 |
568 } | 676 if (locs->out().IsInvalid()) { |
569 } | 677 ASSERT(def->ssa_temp_index() < 0); |
570 } | 678 return; |
571 } | 679 } |
572 } | 680 |
573 | 681 // We might have a definition without use. We do not assign SSA index to |
574 | 682 // such definitions. |
683 LiveRange* range = (def->ssa_temp_index() >= 0) ? | |
684 GetLiveRange(def->ssa_temp_index()) : | |
685 new LiveRange(kTempVirtualRegister); | |
686 Location* out = locs->out_slot(); | |
687 | |
688 // Process output and finalize its liverange. | |
689 if (out->IsRegister()) { | |
690 // Fixed output location. Expected shape of live range: | |
691 // | |
692 // m i m | |
693 // register [--) | |
694 // output [------- | |
695 // | |
696 BlockLocation(*out, pos, pos + 1); | |
697 | |
698 if (range->vreg() == kTempVirtualRegister) return; | |
699 | |
700 // We need to emit move connecting fixed register with another location | |
701 // that will be allocated for this output's live range. | |
702 // Special case: fixed output followed by a fixed input last use. | |
703 UsePosition* use = range->first_use(); | |
704 if (use->pos() == (pos + 1)) { | |
705 // We have a use position on the parallel move. | |
706 ASSERT(use->location_slot()->IsUnallocated()); | |
707 *(use->location_slot()) = *out; | |
708 | |
709 // Remove first use. It was allocated. | |
710 range->set_first_use(range->first_use()->next()); | |
711 } | |
712 | |
713 // Shorten live range to the point of definition, this might make the range | |
714 // empty (if the only use immediately follows). If range is not empty add | |
715 // move from a fixed register to an unallocated location. | |
716 range->DefineAt(pos + 1); | |
717 if (range->Start() == range->End()) return; | |
718 | |
719 MoveOperands* move = AddMoveAt(pos + 1, Location::PrefersRegister(), *out); | |
720 range->AddUse(pos + 1, move->dest_slot()); | |
721 } else if (output_same_as_first_input) { | |
722 // Output register will contain a value of the first input at instruction's | |
723 // start. Expected shape of live ranges: | |
724 // | |
725 // m i m | |
726 // input #0 --* | |
727 // output [--*---- | |
728 // | |
729 ASSERT(locs->in_slot(0)->Equals(Location::RequiresRegister())); | |
730 | |
731 // Create move that will copy value between input and output. | |
732 locs->set_out(Location::RequiresRegister()); | |
733 MoveOperands* move = AddMoveAt(pos - 1, | |
734 Location::RequiresRegister(), | |
735 Location::PrefersRegister()); | |
736 | |
737 // Add uses to the live range of the input. | |
738 Value* input = current->InputAt(0); | |
739 ASSERT(input->IsUse()); // Can not be a constant currently. | |
740 LiveRange* input_range = GetLiveRange( | |
741 input->AsUse()->definition()->ssa_temp_index()); | |
742 input_range->AddUseInterval(block->start_pos(), pos - 1); | |
743 input_range->AddUse(pos - 1, move->src_slot()); | |
744 | |
745 // Shorten output live range to the point of definition and add both input | |
746 // and output uses slots to be filled by allocator. | |
747 range->DefineAt(pos - 1); | |
748 range->AddUse(pos - 1, out); | |
749 range->AddUse(pos - 1, move->dest_slot()); | |
750 range->AddUse(pos, locs->in_slot(0)); | |
751 } else { | |
752 // Normal unallocated location that requires a register. Expected shape of | |
753 // live range: | |
754 // | |
755 // m i m | |
756 // output [------- | |
757 // | |
758 ASSERT(out->IsUnallocated() && | |
759 (out->policy() == Location::kRequiresRegister)); | |
760 | |
761 // Shorten live range to the point of definition and add use to be filled by | |
762 // allocator. | |
763 range->DefineAt(pos); | |
764 range->AddUse(pos, out); | |
765 } | |
766 | |
767 AddToUnallocated(range); | |
768 } | |
769 | |
770 | |
771 static ParallelMoveInstr* CreateParallelMoveBefore(Instruction* instr, | |
772 intptr_t pos) { | |
773 Instruction* prev = instr->previous(); | |
774 ParallelMoveInstr* move = prev->AsParallelMove(); | |
775 if ((move == NULL) || (move->lifetime_position() != pos)) { | |
776 move = new ParallelMoveInstr(); | |
777 move->set_next(prev->next()); | |
778 prev->set_next(move); | |
779 move->next()->set_previous(move); | |
780 move->set_previous(prev); | |
781 move->set_lifetime_position(pos); | |
782 } | |
783 return move; | |
784 } | |
785 | |
786 | |
787 static ParallelMoveInstr* CreateParallelMoveAfter(Instruction* instr, | |
788 intptr_t pos) { | |
789 Instruction* next = instr->next(); | |
790 if (next->IsParallelMove() && (next->lifetime_position() == pos)) { | |
791 return next->AsParallelMove(); | |
792 } | |
793 return CreateParallelMoveBefore(next, pos); | |
794 } | |
795 | |
796 | |
575 // Linearize the control flow graph. The chosen order will be used by the | 797 // Linearize the control flow graph. The chosen order will be used by the |
576 // linear-scan register allocator. Number most instructions with a pair of | 798 // linear-scan register allocator. Number most instructions with a pair of |
577 // numbers representing lifetime positions. Introduce explicit parallel | 799 // numbers representing lifetime positions. Introduce explicit parallel |
578 // move instructions in the predecessors of join nodes. The moves are used | 800 // move instructions in the predecessors of join nodes. The moves are used |
579 // for phi resolution. | 801 // for phi resolution. |
580 void FlowGraphAllocator::NumberInstructions() { | 802 void FlowGraphAllocator::NumberInstructions() { |
581 intptr_t pos = 0; | 803 intptr_t pos = 0; |
582 | 804 |
583 // The basic block order is reverse postorder. | 805 // The basic block order is reverse postorder. |
584 const intptr_t block_count = postorder_.length(); | 806 const intptr_t block_count = postorder_.length(); |
585 for (intptr_t i = block_count - 1; i >= 0; i--) { | 807 for (intptr_t i = block_count - 1; i >= 0; i--) { |
586 BlockEntryInstr* block = postorder_[i]; | 808 BlockEntryInstr* block = postorder_[i]; |
809 | |
810 instructions_.Add(block); | |
587 block->set_start_pos(pos); | 811 block->set_start_pos(pos); |
588 block->set_lifetime_position(pos); | 812 block->set_lifetime_position(pos + 1); |
589 pos += 2; | 813 pos += 2; |
814 | |
590 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) { | 815 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) { |
591 Instruction* current = it.Current(); | 816 Instruction* current = it.Current(); |
592 // Do not assign numbers to parallel moves or goto instructions. | 817 // Do not assign numbers to parallel move instructions. |
593 if (!current->IsParallelMove() && !current->IsGoto()) { | 818 if (!current->IsParallelMove()) { |
594 current->set_lifetime_position(pos); | 819 instructions_.Add(current); |
820 current->set_lifetime_position(pos + 1); | |
595 pos += 2; | 821 pos += 2; |
596 } | 822 } |
597 } | 823 } |
598 block->set_end_pos(pos); | 824 block->set_end_pos(pos); |
599 | 825 |
600 // For join entry predecessors create phi resolution moves if | 826 // For join entry predecessors create phi resolution moves if |
601 // necessary. They will be populated by the register allocator. | 827 // necessary. They will be populated by the register allocator. |
602 JoinEntryInstr* join = block->AsJoinEntry(); | 828 JoinEntryInstr* join = block->AsJoinEntry(); |
603 if ((join != NULL) && (join->phi_count() > 0)) { | 829 if ((join != NULL) && (join->phi_count() > 0)) { |
604 const intptr_t phi_count = join->phi_count(); | 830 const intptr_t phi_count = join->phi_count(); |
605 for (intptr_t i = 0; i < block->PredecessorCount(); i++) { | 831 for (intptr_t i = 0; i < block->PredecessorCount(); i++) { |
606 ParallelMoveInstr* move = new ParallelMoveInstr(); | 832 // Insert the move between the last two instructions of the |
833 // predecessor block (all such blocks have at least two instructions: | |
834 // the block entry and goto instructions.) | |
835 Instruction* last = block->PredecessorAt(i)->last_instruction(); | |
836 ParallelMoveInstr* move = | |
837 CreateParallelMoveBefore(last, last->lifetime_position() - 1); | |
838 | |
607 // Populate the ParallelMove with empty moves. | 839 // Populate the ParallelMove with empty moves. |
608 for (intptr_t j = 0; j < phi_count; j++) { | 840 for (intptr_t j = 0; j < phi_count; j++) { |
609 move->AddMove(Location::NoLocation(), Location::NoLocation()); | 841 move->AddMove(Location::NoLocation(), Location::NoLocation()); |
610 } | 842 } |
611 | |
612 // Insert the move between the last two instructions of the | |
613 // predecessor block (all such blocks have at least two instructions: | |
614 // the block entry and goto instructions.) | |
615 BlockEntryInstr* pred = block->PredecessorAt(i); | |
616 Instruction* next = pred->last_instruction(); | |
617 Instruction* previous = next->previous(); | |
618 ASSERT(next->IsGoto()); | |
619 ASSERT(!previous->IsParallelMove()); | |
620 previous->set_next(move); | |
621 move->set_previous(previous); | |
622 move->set_next(next); | |
623 next->set_previous(move); | |
624 } | 843 } |
625 } | 844 } |
626 } | 845 } |
627 } | 846 } |
628 | 847 |
629 | 848 |
849 Instruction* FlowGraphAllocator::InstructionAt(intptr_t pos) const { | |
850 return instructions_[pos / 2]; | |
851 } | |
852 | |
853 | |
854 bool FlowGraphAllocator::IsBlockEntry(intptr_t pos) const { | |
855 return InstructionAt(pos)->IsBlockEntry(); | |
856 } | |
857 | |
858 | |
859 static UsePosition* FirstUseAfter(UsePosition* use, intptr_t after) { | |
860 while ((use != NULL) && (use->pos() < after)) { | |
861 use = use->next(); | |
862 } | |
863 return use; | |
864 } | |
865 | |
866 | |
867 Location AllocationFinger::FirstHint() { | |
868 UsePosition* use = first_hinted_use_; | |
869 | |
870 while (use != NULL) { | |
871 if (use->HasHint()) return use->hint(); | |
872 use = use->next(); | |
873 } | |
874 | |
875 return Location::NoLocation(); | |
876 } | |
877 | |
878 | |
879 UsePosition* AllocationFinger::FirstRegisterUse(intptr_t after) { | |
880 for (UsePosition* use = FirstUseAfter(first_register_use_, after); | |
881 use != NULL; | |
882 use = use->next()) { | |
883 Location* loc = use->location_slot(); | |
884 if ((loc != NULL) && | |
885 loc->IsUnallocated() && | |
886 (loc->policy() == Location::kRequiresRegister)) { | |
887 first_register_use_ = use; | |
888 return use; | |
889 } | |
890 } | |
891 return NULL; | |
892 } | |
893 | |
894 | |
895 UsePosition* AllocationFinger::FirstRegisterBeneficialUse(intptr_t after) { | |
896 for (UsePosition* use = FirstUseAfter(first_register_beneficial_use_, after); | |
897 use != NULL; | |
898 use = use->next()) { | |
899 Location* loc = use->location_slot(); | |
900 if ((loc != NULL) && | |
901 (loc->IsRegister() || | |
902 (loc->IsUnallocated() && loc->IsRegisterBeneficial()))) { | |
903 first_register_beneficial_use_ = use; | |
904 return use; | |
905 } | |
906 } | |
907 return NULL; | |
908 } | |
909 | |
910 | |
630 intptr_t UseInterval::Intersect(UseInterval* other) { | 911 intptr_t UseInterval::Intersect(UseInterval* other) { |
631 if (this->start() <= other->start()) { | 912 if (this->start() <= other->start()) { |
632 if (other->start() < this->end()) return other->start(); | 913 if (other->start() < this->end()) return other->start(); |
633 } else if (this->start() < other->end()) { | 914 } else if (this->start() < other->end()) { |
634 return this->start(); | 915 return this->start(); |
635 } | 916 } |
636 return kIllegalPosition; | 917 return kIllegalPosition; |
637 } | 918 } |
638 | 919 |
639 | 920 |
640 static intptr_t FirstIntersection(UseInterval* a, UseInterval* u) { | 921 static intptr_t FirstIntersection(UseInterval* a, UseInterval* u) { |
641 while (a != NULL && u != NULL) { | 922 while (a != NULL && u != NULL) { |
642 const intptr_t pos = a->Intersect(u); | 923 const intptr_t pos = a->Intersect(u); |
643 if (pos != kIllegalPosition) return pos; | 924 if (pos != kIllegalPosition) return pos; |
644 | 925 |
645 if (a->start() < u->start()) { | 926 if (a->start() < u->start()) { |
646 a = a->next_allocated(); | 927 a = a->next(); |
647 } else { | 928 } else { |
648 u = u->next(); | 929 u = u->next(); |
649 } | 930 } |
650 } | 931 } |
651 | 932 |
652 return kMaxPosition; | 933 return kMaxPosition; |
653 } | 934 } |
654 | 935 |
655 | 936 |
656 static Location LookAheadForHint(UseInterval* interval) { | 937 LiveRange* LiveRange::MakeTemp(intptr_t pos, Location* location_slot) { |
657 UsePosition* use = interval->first_use(); | 938 UNREACHABLE(); |
658 | 939 return NULL; |
659 while (use != NULL) { | |
660 if (use->HasHint()) return use->hint(); | |
661 use = use->next(); | |
662 } | |
663 | |
664 return Location::NoLocation(); | |
665 } | 940 } |
666 | 941 |
667 | 942 |
668 bool FlowGraphAllocator::AllocateFreeRegister(UseInterval* unallocated) { | 943 LiveRange* LiveRange::SplitAt(intptr_t split_pos) { |
944 if (Start() == split_pos) return this; | |
945 | |
946 // Ranges can only be connected by parallel moves. | |
947 split_pos = ToParallelMove(split_pos); | |
948 | |
949 UseInterval* interval = finger_.first_pending_use_interval(); | |
950 ASSERT(interval->start() < split_pos); | |
951 | |
952 // Corner case. We need to start over to find previous interval. | |
953 if (interval->start() == split_pos) interval = first_use_interval_; | |
954 | |
955 UseInterval* last_before_split = NULL; | |
956 while (interval->end() <= split_pos) { | |
957 last_before_split = interval; | |
958 interval = interval->next(); | |
959 } | |
960 | |
961 const bool split_at_start = (interval->start() == split_pos); | |
962 | |
963 UseInterval* first_after_split = interval; | |
964 if (!split_at_start && interval->Contains(split_pos)) { | |
965 first_after_split = new UseInterval(split_pos, | |
966 interval->end(), | |
967 interval->next()); | |
968 interval->end_ = split_pos; | |
969 interval->next_ = first_after_split; | |
970 last_before_split = interval; | |
971 } | |
972 | |
973 ASSERT(last_before_split->next() == first_after_split); | |
974 ASSERT(last_before_split->end() <= split_pos); | |
975 ASSERT(split_pos <= first_after_split->start()); | |
976 | |
977 UsePosition* last_use_before_split = NULL; | |
978 UsePosition* use = uses_; | |
979 if (split_at_start) { | |
980 while ((use != NULL) && (use->pos() < split_pos)) { | |
981 last_use_before_split = use; | |
982 use = use->next(); | |
983 } | |
984 } else { | |
985 while ((use != NULL) && (use->pos() <= split_pos)) { | |
986 last_use_before_split = use; | |
987 use = use->next(); | |
988 } | |
989 } | |
990 UsePosition* first_use_after_split = use; | |
991 | |
992 if (last_use_before_split == NULL) { | |
993 uses_ = NULL; | |
994 } else { | |
995 last_use_before_split->set_next(NULL); | |
996 } | |
997 | |
998 UseInterval* last_use_interval = (last_before_split == last_use_interval_) ? | |
999 first_after_split : last_use_interval_; | |
1000 next_sibling_ = new LiveRange(vreg(), | |
1001 first_use_after_split, | |
1002 first_after_split, | |
1003 last_use_interval, | |
1004 next_sibling_); | |
1005 | |
1006 TRACE_ALLOC((" split sibling [%d, %d)\n", | |
1007 next_sibling_->Start(), next_sibling_->End())); | |
1008 | |
1009 // Split sibling can only start at a parallel move. | |
1010 ASSERT(IsParallelMovePosition(next_sibling_->Start())); | |
1011 | |
1012 last_use_interval_ = last_before_split; | |
1013 last_use_interval_->next_ = NULL; | |
1014 return next_sibling_; | |
1015 } | |
1016 | |
1017 | |
1018 LiveRange* FlowGraphAllocator::SplitBetween(LiveRange* range, | |
1019 intptr_t from, | |
1020 intptr_t to) { | |
1021 // TODO(vegorov): select optimal split position based on loop structure. | |
1022 TRACE_ALLOC(("split %d [%d, %d) between [%d, %d)\n", | |
1023 range->vreg(), range->Start(), range->End(), from, to)); | |
1024 return range->SplitAt(to); | |
1025 } | |
1026 | |
1027 | |
1028 void FlowGraphAllocator::SpillBetween(LiveRange* range, | |
1029 intptr_t from, | |
1030 intptr_t to) { | |
1031 ASSERT(from < to); | |
1032 TRACE_ALLOC(("spill %d [%d, %d) between [%d, %d)\n", | |
1033 range->vreg(), range->Start(), range->End(), from, to)); | |
1034 LiveRange* tail = range->SplitAt(from); | |
1035 | |
1036 if (tail->Start() < to) { | |
1037 // There is an intersection of tail and [from, to). | |
1038 LiveRange* tail_tail = SplitBetween(tail, tail->Start(), to); | |
1039 Spill(tail); | |
1040 AddToUnallocated(tail_tail); | |
1041 } else { | |
1042 // No intersection between tail and [from, to). | |
1043 AddToUnallocated(tail); | |
1044 } | |
1045 } | |
1046 | |
1047 | |
1048 void FlowGraphAllocator::SpillAfter(LiveRange* range, intptr_t from) { | |
1049 TRACE_ALLOC(("spill %d [%d, %d) after %d\n", | |
1050 range->vreg(), range->Start(), range->End(), from)); | |
1051 LiveRange* tail = range->SplitAt(from); | |
1052 Spill(tail); | |
1053 } | |
1054 | |
1055 | |
1056 intptr_t FlowGraphAllocator::AllocateSpillSlotFor(LiveRange* range) { | |
1057 for (intptr_t i = 0; i < spill_slots_.length(); i++) { | |
1058 if (spill_slots_[i] <= range->Start()) { | |
1059 return i; | |
1060 } | |
1061 } | |
1062 spill_slots_.Add(0); | |
1063 return spill_slots_.length() - 1; | |
1064 } | |
1065 | |
1066 | |
1067 void FlowGraphAllocator::Spill(LiveRange* range) { | |
1068 const intptr_t spill_index = AllocateSpillSlotFor(range); | |
1069 ASSERT(spill_slots_[spill_index] < range->Start()); | |
1070 spill_slots_[spill_index] = range->End(); | |
1071 range->set_assigned_location(Location::SpillSlot(spill_index)); | |
1072 ConvertAllUses(range); | |
1073 } | |
1074 | |
1075 | |
1076 intptr_t FlowGraphAllocator::FirstIntersectionWithAllocated( | |
1077 Register reg, LiveRange* unallocated) { | |
1078 intptr_t intersection = kMaxPosition; | |
1079 for (intptr_t i = 0; i < cpu_regs_[reg].length(); i++) { | |
1080 LiveRange* allocated = cpu_regs_[reg][i]; | |
1081 if (allocated == NULL) continue; | |
1082 | |
1083 UseInterval* allocated_head = | |
1084 allocated->finger()->first_pending_use_interval(); | |
1085 if (allocated_head->start() >= intersection) continue; | |
1086 | |
1087 const intptr_t pos = FirstIntersection( | |
1088 unallocated->finger()->first_pending_use_interval(), | |
1089 allocated_head); | |
1090 if (pos < intersection) intersection = pos; | |
1091 } | |
1092 return intersection; | |
1093 } | |
1094 | |
1095 | |
1096 | |
1097 bool FlowGraphAllocator::AllocateFreeRegister(LiveRange* unallocated) { | |
669 Register candidate = kNoRegister; | 1098 Register candidate = kNoRegister; |
670 intptr_t free_until = 0; | 1099 intptr_t free_until = 0; |
671 | 1100 |
672 // If hint is available try hint first. | 1101 // If hint is available try hint first. |
673 // TODO(vegorov): ensure that phis are hinted on the backedge. | 1102 // TODO(vegorov): ensure that phis are hinted on the back edge. |
674 Location hint = LookAheadForHint(unallocated); | 1103 Location hint = unallocated->finger()->FirstHint(); |
675 if (!hint.IsInvalid()) { | 1104 if (!hint.IsInvalid()) { |
676 ASSERT(hint.IsRegister()); | 1105 ASSERT(hint.IsRegister()); |
677 | 1106 |
678 if (cpu_regs_[hint.reg()] != kPermanentlyBlocked) { | 1107 if (!blocked_cpu_regs_[hint.reg()]) { |
679 free_until = FirstIntersection(cpu_regs_[hint.reg()], unallocated); | 1108 free_until = FirstIntersectionWithAllocated(hint.reg(), unallocated); |
680 candidate = hint.reg(); | 1109 candidate = hint.reg(); |
681 } | 1110 } |
682 | 1111 |
683 TRACE_ALLOC(("found hint %s for %d: free until %d\n", | 1112 TRACE_ALLOC(("found hint %s for %d: free until %d\n", |
684 hint.Name(), unallocated->vreg(), free_until)); | 1113 hint.Name(), unallocated->vreg(), free_until)); |
685 } | 1114 } |
686 | 1115 |
687 if (free_until != kMaxPosition) { | 1116 if (free_until != kMaxPosition) { |
688 for (int reg = 0; reg < kNumberOfCpuRegisters; ++reg) { | 1117 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; ++reg) { |
689 if (cpu_regs_[reg] == NULL) { | 1118 if (!blocked_cpu_regs_[reg] && cpu_regs_[reg].length() == 0) { |
690 candidate = static_cast<Register>(reg); | 1119 candidate = static_cast<Register>(reg); |
691 free_until = kMaxPosition; | 1120 free_until = kMaxPosition; |
692 break; | 1121 break; |
693 } | 1122 } |
694 } | 1123 } |
695 } | 1124 } |
696 | 1125 |
697 ASSERT(0 <= kMaxPosition); | 1126 ASSERT(0 <= kMaxPosition); |
698 if (free_until != kMaxPosition) { | 1127 if (free_until != kMaxPosition) { |
699 for (int reg = 0; reg < kNumberOfCpuRegisters; ++reg) { | 1128 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; ++reg) { |
700 if (cpu_regs_[reg] == kPermanentlyBlocked) continue; | 1129 if (blocked_cpu_regs_[reg] || (reg == candidate)) continue; |
701 if (reg == candidate) continue; | 1130 |
702 | 1131 const intptr_t intersection = |
703 const intptr_t pos = FirstIntersection(cpu_regs_[reg], unallocated); | 1132 FirstIntersectionWithAllocated(static_cast<Register>(reg), unallocated); |
704 | 1133 |
705 if (pos > free_until) { | 1134 if (intersection > free_until) { |
706 candidate = static_cast<Register>(reg); | 1135 candidate = static_cast<Register>(reg); |
707 free_until = pos; | 1136 free_until = intersection; |
708 if (free_until == kMaxPosition) break; | 1137 if (free_until == kMaxPosition) break; |
709 } | 1138 } |
710 } | 1139 } |
711 } | 1140 } |
712 | 1141 |
1142 if (free_until != kMaxPosition) free_until = ToParallelMove(free_until); | |
1143 | |
713 // All registers are blocked by active ranges. | 1144 // All registers are blocked by active ranges. |
714 if (free_until <= unallocated->start()) return false; | 1145 if (free_until <= unallocated->Start()) return false; |
715 | 1146 |
716 AssignFreeRegister(unallocated, candidate); | 1147 TRACE_ALLOC(("assigning free register %s to %d\n", |
1148 Location::RegisterLocation(candidate).Name(), | |
1149 unallocated->vreg())); | |
1150 | |
1151 if (free_until != kMaxPosition) { | |
1152 // There was an intersection. Split unallocated. | |
1153 TRACE_ALLOC((" splitting at %d\n", free_until)); | |
1154 LiveRange* tail = unallocated->SplitAt(free_until); | |
1155 AddToUnallocated(tail); | |
1156 } | |
1157 | |
1158 cpu_regs_[candidate].Add(unallocated); | |
1159 unallocated->set_assigned_location(Location::RegisterLocation(candidate)); | |
1160 | |
717 return true; | 1161 return true; |
718 } | 1162 } |
719 | 1163 |
720 | 1164 |
721 UseInterval* UseInterval::Split(intptr_t pos) { | 1165 void FlowGraphAllocator::AllocateAnyRegister(LiveRange* unallocated) { |
722 if (pos == start()) return this; | 1166 UsePosition* register_use = |
723 ASSERT(Contains(pos)); | 1167 unallocated->finger()->FirstRegisterUse(unallocated->Start()); |
724 UseInterval* tail = new UseInterval(vreg(), pos, end(), next()); | 1168 if (register_use == NULL) { |
725 | 1169 Spill(unallocated); |
726 UsePosition* use = uses_; | 1170 return; |
727 while (use != NULL && use->pos() <= pos) { | 1171 } |
728 use = use->next(); | 1172 |
729 } | 1173 Register candidate = kNoRegister; |
730 | 1174 intptr_t free_until = 0; |
731 tail->uses_ = use; | 1175 intptr_t blocked_at = kMaxPosition; |
732 | 1176 |
733 end_ = pos; | 1177 for (int reg = 0; reg < kNumberOfCpuRegisters; ++reg) { |
734 | 1178 if (blocked_cpu_regs_[reg]) continue; |
735 return tail; | 1179 if (UpdateFreeUntil(static_cast<Register>(reg), |
736 } | 1180 unallocated, |
737 | 1181 &free_until, |
738 | 1182 &blocked_at)) { |
739 void FlowGraphAllocator::AssignFreeRegister(UseInterval* unallocated, | 1183 candidate = static_cast<Register>(reg); |
740 Register reg) { | 1184 } |
741 TRACE_ALLOC(("assigning free register %s to %d\n", | 1185 } |
1186 | |
1187 if (free_until < register_use->pos()) { | |
1188 // Can't acquire free register. Spill until we really need one. | |
1189 ASSERT(unallocated->Start() < ToParallelMove(register_use->pos())); | |
1190 SpillBetween(unallocated, unallocated->Start(), register_use->pos()); | |
1191 return; | |
1192 } | |
1193 | |
1194 if (blocked_at < unallocated->End()) { | |
1195 LiveRange* tail = SplitBetween(unallocated, | |
1196 unallocated->Start(), | |
1197 blocked_at); | |
1198 AddToUnallocated(tail); | |
1199 } | |
1200 | |
1201 AssignNonFreeRegister(unallocated, candidate); | |
1202 } | |
1203 | |
1204 | |
1205 bool FlowGraphAllocator::UpdateFreeUntil(Register reg, | |
1206 LiveRange* unallocated, | |
1207 intptr_t* cur_free_until, | |
1208 intptr_t* cur_blocked_at) { | |
1209 intptr_t free_until = kMaxPosition; | |
1210 intptr_t blocked_at = kMaxPosition; | |
1211 const intptr_t start = unallocated->Start(); | |
1212 | |
1213 for (intptr_t i = 0; i < cpu_regs_[reg].length(); i++) { | |
1214 LiveRange* allocated = cpu_regs_[reg][i]; | |
1215 | |
1216 UseInterval* first_pending_use_interval = | |
1217 allocated->finger()->first_pending_use_interval(); | |
1218 if (first_pending_use_interval->Contains(start)) { | |
1219 // This is an active interval. | |
1220 if (allocated->vreg() <= 0) { | |
1221 // This register blocked by an interval that | |
1222 // can't be spilled. | |
1223 return false; | |
1224 } | |
1225 | |
1226 const UsePosition* use = | |
1227 allocated->finger()->FirstRegisterBeneficialUse(unallocated->Start()); | |
1228 | |
1229 if ((use != NULL) && ((use->pos() - start) <= 1)) { | |
1230 // This register is blocked by interval that is used | |
1231 // as register in the current instruction and can't | |
1232 // be spilled. | |
1233 return false; | |
1234 } | |
1235 | |
1236 const intptr_t use_pos = (use != NULL) ? use->pos() | |
1237 : allocated->End(); | |
1238 | |
1239 if (use_pos < free_until) free_until = use_pos; | |
1240 } else { | |
1241 // This is inactive interval. | |
1242 const intptr_t intersection = FirstIntersection( | |
1243 first_pending_use_interval, unallocated->first_use_interval()); | |
1244 if (intersection != kMaxPosition) { | |
1245 if (intersection < free_until) free_until = intersection; | |
1246 if (allocated->vreg() == kNoVirtualRegister) blocked_at = intersection; | |
1247 } | |
1248 } | |
1249 | |
1250 if (free_until <= *cur_free_until) { | |
1251 return false; | |
1252 } | |
1253 } | |
1254 | |
1255 ASSERT(free_until > *cur_free_until); | |
1256 *cur_free_until = free_until; | |
1257 *cur_blocked_at = blocked_at; | |
1258 return true; | |
1259 } | |
1260 | |
1261 | |
1262 void FlowGraphAllocator::RemoveEvicted(Register reg, intptr_t first_evicted) { | |
1263 intptr_t to = first_evicted; | |
1264 intptr_t from = first_evicted + 1; | |
1265 while (from < cpu_regs_[reg].length()) { | |
1266 LiveRange* allocated = cpu_regs_[reg][from++]; | |
1267 if (allocated != NULL) cpu_regs_[reg][to++] = allocated; | |
1268 } | |
1269 cpu_regs_[reg].TruncateTo(to); | |
1270 } | |
1271 | |
1272 | |
1273 void FlowGraphAllocator::AssignNonFreeRegister(LiveRange* unallocated, | |
1274 Register reg) { | |
1275 TRACE_ALLOC(("assigning blocked register %s to live range %d\n", | |
742 Location::RegisterLocation(reg).Name(), | 1276 Location::RegisterLocation(reg).Name(), |
743 unallocated->vreg())); | 1277 unallocated->vreg())); |
744 | 1278 |
745 UseInterval* a = cpu_regs_[reg]; | 1279 intptr_t first_evicted = -1; |
746 if (a == NULL) { | 1280 for (intptr_t i = cpu_regs_[reg].length() - 1; i >= 0; i--) { |
747 // Register is completely free. | 1281 LiveRange* allocated = cpu_regs_[reg][i]; |
748 cpu_regs_[reg] = unallocated; | 1282 if (allocated->vreg() < 0) continue; // Can't be evicted. |
1283 if (EvictIntersection(allocated, | |
1284 unallocated)) { | |
1285 cpu_regs_[reg][i] = NULL; | |
1286 first_evicted = i; | |
1287 } | |
1288 } | |
1289 | |
1290 // Remove evicted ranges from the array. | |
1291 if (first_evicted != -1) RemoveEvicted(reg, first_evicted); | |
1292 | |
1293 cpu_regs_[reg].Add(unallocated); | |
1294 unallocated->set_assigned_location(Location::RegisterLocation(reg)); | |
1295 } | |
1296 | |
1297 | |
1298 bool FlowGraphAllocator::EvictIntersection(LiveRange* allocated, | |
1299 LiveRange* unallocated) { | |
1300 UseInterval* first_unallocated = | |
1301 unallocated->finger()->first_pending_use_interval(); | |
1302 const intptr_t intersection = FirstIntersection( | |
1303 allocated->finger()->first_pending_use_interval(), | |
1304 first_unallocated); | |
1305 if (intersection == kMaxPosition) return false; | |
1306 | |
1307 const intptr_t spill_position = first_unallocated->start(); | |
1308 UsePosition* use = allocated->finger()->FirstRegisterUse(spill_position); | |
1309 if (use == NULL) { | |
1310 // No register uses after this point. | |
1311 SpillAfter(allocated, spill_position); | |
1312 } else { | |
1313 const intptr_t restore_position = | |
1314 (spill_position < intersection) ? MinPosition(intersection, use->pos()) | |
1315 : use->pos(); | |
1316 | |
1317 SpillBetween(allocated, spill_position, restore_position); | |
1318 } | |
1319 | |
1320 return true; | |
1321 } | |
1322 | |
1323 | |
1324 MoveOperands* FlowGraphAllocator::AddMoveAt(intptr_t pos, | |
1325 Location to, | |
1326 Location from) { | |
1327 ASSERT(IsParallelMovePosition(pos)); | |
1328 Instruction* instr = InstructionAt(pos); | |
1329 ASSERT(!instr->IsBlockEntry()); | |
1330 return CreateParallelMoveBefore(instr, pos)->AddMove(to, from); | |
1331 } | |
1332 | |
1333 | |
1334 void FlowGraphAllocator::ConvertUseTo(UsePosition* use, Location loc) { | |
1335 ASSERT(use->location_slot() != NULL); | |
1336 Location* slot = use->location_slot(); | |
1337 ASSERT(slot->IsUnallocated()); | |
1338 ASSERT((slot->policy() == Location::kRequiresRegister) || | |
1339 (slot->policy() == Location::kPrefersRegister) || | |
1340 (slot->policy() == Location::kAny)); | |
1341 TRACE_ALLOC((" use at %d converted to %s\n", use->pos(), loc.Name())); | |
1342 *slot = loc; | |
1343 } | |
1344 | |
1345 | |
1346 void FlowGraphAllocator::ConvertAllUses(LiveRange* range) { | |
1347 if (range->vreg() == kNoVirtualRegister) return; | |
1348 TRACE_ALLOC(("range [%d, %d) for v%d has been allocated to %s:\n", | |
1349 range->Start(), | |
1350 range->End(), | |
1351 range->vreg(), | |
1352 range->assigned_location().Name())); | |
1353 ASSERT(!range->assigned_location().IsInvalid()); | |
1354 const Location loc = range->assigned_location(); | |
1355 for (UsePosition* use = range->first_use(); use != NULL; use = use->next()) { | |
1356 ConvertUseTo(use, loc); | |
1357 } | |
1358 } | |
1359 | |
1360 | |
1361 bool AllocationFinger::Advance(const intptr_t start) { | |
1362 UseInterval* a = first_pending_use_interval_; | |
1363 while (a != NULL && a->end() <= start) a = a->next(); | |
1364 first_pending_use_interval_ = a; | |
1365 if (first_pending_use_interval_ == NULL) { | |
1366 return true; | |
1367 } | |
1368 return false; | |
1369 } | |
1370 | |
1371 | |
1372 void FlowGraphAllocator::AdvanceActiveIntervals(const intptr_t start) { | |
1373 for (intptr_t reg = 0; reg < kNumberOfCpuRegisters; reg++) { | |
1374 if (cpu_regs_[reg].is_empty()) continue; | |
1375 | |
1376 intptr_t first_evicted = -1; | |
1377 for (intptr_t i = cpu_regs_[reg].length() - 1; i >= 0; i--) { | |
1378 LiveRange* range = cpu_regs_[reg][i]; | |
1379 if (range->finger()->Advance(start)) { | |
1380 ConvertAllUses(range); | |
1381 cpu_regs_[reg][i] = NULL; | |
1382 first_evicted = i; | |
1383 } | |
1384 } | |
1385 | |
1386 if (first_evicted != -1) { | |
1387 RemoveEvicted(static_cast<Register>(reg), first_evicted); | |
1388 } | |
1389 } | |
1390 } | |
1391 | |
1392 | |
1393 void AllocationFinger::Initialize(LiveRange* range) { | |
1394 first_pending_use_interval_ = range->first_use_interval(); | |
1395 first_register_use_ = range->first_use(); | |
1396 first_register_beneficial_use_ = range->first_use(); | |
1397 first_hinted_use_ = range->first_use(); | |
1398 } | |
1399 | |
1400 | |
1401 static inline bool ShouldBeAllocatedBefore(LiveRange* a, LiveRange* b) { | |
1402 return a->Start() <= b->Start(); | |
1403 } | |
1404 | |
1405 | |
1406 void FlowGraphAllocator::AddToUnallocated(LiveRange* range) { | |
1407 range->finger()->Initialize(range); | |
1408 | |
1409 if (unallocated_.is_empty()) { | |
1410 unallocated_.Add(range); | |
749 return; | 1411 return; |
750 } | 1412 } |
751 | 1413 |
752 UseInterval* u = unallocated; | 1414 for (intptr_t i = unallocated_.length() - 1; i >= 0; i--) { |
753 ASSERT(u->start() < a->start()); // Register is free. | 1415 if (ShouldBeAllocatedBefore(range, unallocated_[i])) { |
754 cpu_regs_[reg] = u; | 1416 unallocated_.InsertAt(i + 1, range); |
755 if (u->next() == NULL || u->next()->start() >= a->start()) { | |
756 u->set_next_allocated(a); | |
757 } | |
758 | |
759 while (a != NULL && u != NULL) { | |
760 const intptr_t pos = a->Intersect(u); | |
761 if (pos != kIllegalPosition) { | |
762 // TODO(vegorov): split live ranges might require control flow resolution | |
763 // which is not implemented yet. | |
764 builder_->Bailout("ssa allocator: control flow resolution required"); | |
765 | |
766 TRACE_ALLOC((" splitting at %d\n", pos)); | |
767 // Reached intersection | |
768 UseInterval* tail = u->Split(pos); | |
769 AddToUnallocated(tail); | |
770 ASSERT(tail == u || u->next_allocated() == a); | |
771 return; | 1417 return; |
772 } | 1418 } |
773 | 1419 } |
774 if (a->start() < u->start()) { | 1420 unallocated_.InsertAt(0, range); |
775 if (a->next_allocated() == NULL) { | |
776 a->set_next_allocated(u); | |
777 break; | |
778 } | |
779 | |
780 UseInterval* next = a->next_allocated(); | |
781 if (next->start() > u->start()) { | |
782 a->set_next_allocated(u); | |
783 u->set_next_allocated(next); | |
784 } | |
785 | |
786 a = next; | |
787 } else { | |
788 UseInterval* next = u->next(); | |
789 | |
790 if (next == NULL || next->start() >= a->start()) { | |
791 u->set_next_allocated(a); | |
792 } | |
793 u = next; | |
794 } | |
795 } | |
796 } | |
797 | |
798 | |
799 static void InsertMoveBefore(Instruction* instr, Location to, Location from) { | |
800 Instruction* prev = instr->previous(); | |
801 ParallelMoveInstr* move = prev->AsParallelMove(); | |
802 if (move == NULL) { | |
803 move = new ParallelMoveInstr(); | |
804 move->set_next(prev->next()); | |
805 prev->set_next(move); | |
806 move->next()->set_previous(move); | |
807 move->set_previous(prev); | |
808 } | |
809 move->AddMove(to, from); | |
810 } | |
811 | |
812 | |
813 void UsePosition::AssignLocation(Location loc) { | |
814 if (location_slot_ == NULL) return; | |
815 | |
816 if (location_slot_->IsUnallocated()) { | |
817 if (location_slot_->policy() == Location::kSameAsFirstInput) { | |
818 Instruction* instr = this->instr(); | |
819 LocationSummary* locs = instr->locs(); | |
820 if (!locs->in(0).IsUnallocated()) { | |
821 InsertMoveBefore(instr, loc, locs->in(0)); | |
822 } | |
823 locs->set_in(0, loc); | |
824 } | |
825 TRACE_ALLOC((" use at %d converted to %s\n", pos(), loc.Name())); | |
826 *location_slot_ = loc; | |
827 } else if (location_slot_->IsRegister()) { | |
828 InsertMoveBefore(this->instr(), *location_slot_, loc); | |
829 } | |
830 } | |
831 | |
832 | |
833 void FlowGraphAllocator::FinalizeInterval(UseInterval* interval, Location loc) { | |
834 if (interval->vreg() == kNoVirtualRegister) return; | |
835 | |
836 TRACE_ALLOC(("assigning location %s to interval [%d, %d)\n", loc.Name(), | |
837 interval->start(), interval->end())); | |
838 | |
839 for (UsePosition* use = interval->first_use(); | |
840 use != NULL && use->pos() <= interval->end(); | |
841 use = use->next()) { | |
842 use->AssignLocation(loc); | |
843 } | |
844 } | |
845 | |
846 | |
847 void FlowGraphAllocator::AdvanceActiveIntervals(const intptr_t start) { | |
848 for (int reg = 0; reg < kNumberOfCpuRegisters; reg++) { | |
849 if (cpu_regs_[reg] == NULL) continue; | |
850 if (cpu_regs_[reg] == kPermanentlyBlocked) continue; | |
851 | |
852 UseInterval* a = cpu_regs_[reg]; | |
853 while (a != NULL && a->end() <= start) { | |
854 FinalizeInterval(a, | |
855 Location::RegisterLocation(static_cast<Register>(reg))); | |
856 a = a->next_allocated(); | |
857 } | |
858 | |
859 cpu_regs_[reg] = a; | |
860 } | |
861 } | |
862 | |
863 | |
864 static inline bool ShouldBeAllocatedBefore(const UseInterval& a, | |
865 const UseInterval& b) { | |
866 return a.start() <= b.start(); | |
867 } | |
868 | |
869 | |
870 void FlowGraphAllocator::AddToUnallocated(UseInterval* chain) { | |
871 if (unallocated_.is_empty()) { | |
872 unallocated_.Add(chain); | |
873 return; | |
874 } | |
875 | |
876 for (intptr_t i = unallocated_.length() - 1; i >= 0; i--) { | |
877 if (ShouldBeAllocatedBefore(*chain, *unallocated_[i])) { | |
878 unallocated_.InsertAt(i + 1, chain); | |
879 return; | |
880 } | |
881 } | |
882 unallocated_.InsertAt(0, chain); | |
883 } | 1421 } |
884 | 1422 |
885 | 1423 |
886 bool FlowGraphAllocator::UnallocatedIsSorted() { | 1424 bool FlowGraphAllocator::UnallocatedIsSorted() { |
887 for (intptr_t i = unallocated_.length() - 1; i >= 1; i--) { | 1425 for (intptr_t i = unallocated_.length() - 1; i >= 1; i--) { |
888 UseInterval* a = unallocated_[i]; | 1426 LiveRange* a = unallocated_[i]; |
889 UseInterval* b = unallocated_[i - 1]; | 1427 LiveRange* b = unallocated_[i - 1]; |
890 if (!ShouldBeAllocatedBefore(*a, *b)) return false; | 1428 if (!ShouldBeAllocatedBefore(a, b)) return false; |
891 } | 1429 } |
892 return true; | 1430 return true; |
893 } | 1431 } |
894 | 1432 |
895 | 1433 |
896 void FlowGraphAllocator::AllocateCPURegisters() { | 1434 void FlowGraphAllocator::AllocateCPURegisters() { |
897 ASSERT(UnallocatedIsSorted()); | 1435 ASSERT(UnallocatedIsSorted()); |
898 | 1436 |
1437 for (intptr_t i = 0; i < kNumberOfCpuRegisters; i++) { | |
1438 if (cpu_regs_[i].length() == 1) { | |
1439 LiveRange* range = cpu_regs_[i][0]; | |
1440 range->finger()->Initialize(range); | |
1441 } | |
1442 } | |
1443 | |
899 while (!unallocated_.is_empty()) { | 1444 while (!unallocated_.is_empty()) { |
900 UseInterval* range = unallocated_.Last(); | 1445 LiveRange* range = unallocated_.Last(); |
901 unallocated_.RemoveLast(); | 1446 unallocated_.RemoveLast(); |
902 const intptr_t start = range->start(); | 1447 const intptr_t start = range->Start(); |
903 TRACE_ALLOC(("Processing interval chain for vreg %d starting at %d\n", | 1448 TRACE_ALLOC(("Processing live range for vreg %d starting at %d\n", |
904 range->vreg(), | 1449 range->vreg(), |
905 start)); | 1450 start)); |
906 | 1451 |
907 // TODO(vegorov): eagerly spill liveranges without register uses. | 1452 // TODO(vegorov): eagerly spill liveranges without register uses. |
908 AdvanceActiveIntervals(start); | 1453 AdvanceActiveIntervals(start); |
909 | 1454 |
910 if (!AllocateFreeRegister(range)) { | 1455 if (!AllocateFreeRegister(range)) { |
911 builder_->Bailout("ssa allocator: spilling required"); | 1456 AllocateAnyRegister(range); |
912 return; | |
913 } | 1457 } |
914 } | 1458 } |
915 | 1459 |
916 // All allocation decisions were done. | 1460 // All allocation decisions were done. |
917 ASSERT(unallocated_.is_empty()); | 1461 ASSERT(unallocated_.is_empty()); |
918 | 1462 |
919 // Finish allocation. | 1463 // Finish allocation. |
920 AdvanceActiveIntervals(kMaxPosition); | 1464 AdvanceActiveIntervals(kMaxPosition); |
921 TRACE_ALLOC(("Allocation completed\n")); | 1465 TRACE_ALLOC(("Allocation completed\n")); |
922 } | 1466 } |
923 | 1467 |
924 | 1468 |
1469 void FlowGraphAllocator::ConnectSplitSiblings(LiveRange* range, | |
1470 BlockEntryInstr* source_block, | |
1471 BlockEntryInstr* target_block) { | |
1472 if (range->next_sibling() == NULL) { | |
1473 // Nothing to connect. The whole range was allocated to the same location. | |
1474 TRACE_ALLOC(("range %d has no siblings\n", range->vreg())); | |
1475 return; | |
1476 } | |
1477 | |
1478 const intptr_t source_pos = source_block->end_pos() - 1; | |
1479 ASSERT(IsInstructionPosition(source_pos)); | |
1480 | |
1481 const intptr_t target_pos = target_block->start_pos(); | |
1482 | |
1483 Location target; | |
1484 Location source; | |
1485 | |
1486 #ifdef DEBUG | |
1487 LiveRange* source_cover = NULL; | |
1488 LiveRange* target_cover = NULL; | |
1489 #endif | |
1490 | |
1491 while ((range != NULL) && (source.IsInvalid() || target.IsInvalid())) { | |
1492 if (range->CanCover(source_pos)) { | |
1493 ASSERT(source.IsInvalid()); | |
1494 source = range->assigned_location(); | |
1495 #ifdef DEBUG | |
1496 source_cover = range; | |
1497 #endif | |
1498 } | |
1499 if (range->CanCover(target_pos)) { | |
1500 ASSERT(target.IsInvalid()); | |
1501 target = range->assigned_location(); | |
1502 #ifdef DEBUG | |
1503 target_cover = range; | |
1504 #endif | |
1505 } | |
1506 | |
1507 range = range->next_sibling(); | |
1508 } | |
1509 | |
1510 TRACE_ALLOC(("connecting [%d, %d) [%s] to [%d, %d) [%s]\n", | |
1511 source_cover->Start(), source_cover->End(), source.Name(), | |
1512 target_cover->Start(), target_cover->End(), target.Name())); | |
1513 | |
1514 // Siblings were allocated to the same register. | |
1515 if (source.Equals(target)) return; | |
1516 | |
1517 Instruction* last = source_block->last_instruction(); | |
1518 if (last->SuccessorCount() == 1) { | |
1519 CreateParallelMoveBefore(last, last->lifetime_position() - 1)-> | |
1520 AddMove(target, source); | |
1521 } else { | |
1522 CreateParallelMoveAfter(target_block, target_block->start_pos())-> | |
1523 AddMove(target, source); | |
1524 } | |
1525 } | |
1526 | |
1527 | |
1528 void FlowGraphAllocator::ResolveControlFlow() { | |
1529 // Resolve linear control flow between touching split siblings | |
1530 // inside basic blocks. | |
1531 for (intptr_t vreg = 0; vreg < live_ranges_.length(); vreg++) { | |
1532 LiveRange* range = live_ranges_[vreg]; | |
1533 if (range == NULL) continue; | |
1534 | |
1535 while (range->next_sibling() != NULL) { | |
1536 LiveRange* sibling = range->next_sibling(); | |
1537 if ((range->End() == sibling->Start()) && | |
1538 !range->assigned_location().Equals(sibling->assigned_location()) && | |
1539 !IsBlockEntry(range->End())) { | |
1540 AddMoveAt(sibling->Start(), | |
1541 sibling->assigned_location(), | |
1542 range->assigned_location()); | |
1543 } | |
1544 range = sibling; | |
1545 } | |
1546 } | |
1547 | |
1548 // Resolve non-linear control flow across branches. | |
1549 for (intptr_t i = 1; i < block_order_.length(); i++) { | |
1550 BlockEntryInstr* block = block_order_[i]; | |
1551 BitVector* live = live_in_[block->postorder_number()]; | |
1552 for (BitVector::Iterator it(live); !it.Done(); it.Advance()) { | |
1553 LiveRange* range = GetLiveRange(it.Current()); | |
1554 for (intptr_t j = 0; j < block->PredecessorCount(); j++) { | |
1555 ConnectSplitSiblings(range, block->PredecessorAt(j), block); | |
1556 } | |
1557 } | |
1558 } | |
1559 } | |
1560 | |
1561 | |
925 void FlowGraphAllocator::AllocateRegisters() { | 1562 void FlowGraphAllocator::AllocateRegisters() { |
926 GraphEntryInstr* entry = block_order_[0]->AsGraphEntry(); | 1563 GraphEntryInstr* entry = block_order_[0]->AsGraphEntry(); |
927 ASSERT(entry != NULL); | 1564 ASSERT(entry != NULL); |
928 | 1565 |
929 for (intptr_t i = 0; i < entry->start_env()->values().length(); i++) { | 1566 for (intptr_t i = 0; i < entry->start_env()->values().length(); i++) { |
930 if (entry->start_env()->values()[i]->IsUse()) { | 1567 if (entry->start_env()->values()[i]->IsUse()) { |
931 builder_->Bailout("ssa allocator: unsupported start environment"); | 1568 builder_->Bailout("ssa allocator: unsupported start environment"); |
932 } | 1569 } |
933 } | 1570 } |
934 | 1571 |
935 AnalyzeLiveness(); | 1572 AnalyzeLiveness(); |
936 | 1573 |
937 BuildLiveRanges(); | 1574 BuildLiveRanges(); |
938 | 1575 |
939 if (FLAG_print_ssa_liveness) { | 1576 if (FLAG_print_ssa_liveness) { |
940 DumpLiveness(); | 1577 DumpLiveness(); |
941 } | 1578 } |
942 | 1579 |
943 if (FLAG_trace_ssa_allocator) { | 1580 if (FLAG_trace_ssa_allocator) { |
944 PrintLiveRanges(); | 1581 PrintLiveRanges(); |
945 } | 1582 } |
946 | 1583 |
947 AllocateCPURegisters(); | 1584 AllocateCPURegisters(); |
948 | 1585 |
1586 ResolveControlFlow(); | |
1587 | |
949 if (FLAG_trace_ssa_allocator) { | 1588 if (FLAG_trace_ssa_allocator) { |
950 OS::Print("-- ir after allocation -------------------------\n"); | 1589 OS::Print("-- ir after allocation -------------------------\n"); |
951 FlowGraphPrinter printer(Function::Handle(), block_order_, true); | 1590 FlowGraphPrinter printer(Function::Handle(), block_order_, true); |
952 printer.PrintBlocks(); | 1591 printer.PrintBlocks(); |
953 } | 1592 } |
954 } | 1593 } |
955 | 1594 |
956 | 1595 |
957 } // namespace dart | 1596 } // namespace dart |
OLD | NEW |