blob: 91a98112b61bd60a50b56d98f8ca4eebacdf01ff [file] [log] [blame]
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000028#include "v8.h"
ricow@chromium.org83aa5492011-02-07 12:42:56 +000029#include "lithium-allocator-inl.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000030
kasperl@chromium.orga5551262010-12-07 12:49:48 +000031#include "hydrogen.h"
32#include "string-stream.h"
33
34#if V8_TARGET_ARCH_IA32
35#include "ia32/lithium-ia32.h"
36#elif V8_TARGET_ARCH_X64
37#include "x64/lithium-x64.h"
38#elif V8_TARGET_ARCH_ARM
39#include "arm/lithium-arm.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000040#elif V8_TARGET_ARCH_MIPS
41#include "mips/lithium-mips.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042#else
43#error "Unknown architecture."
44#endif
45
46namespace v8 {
47namespace internal {
48
kasperl@chromium.orga5551262010-12-07 12:49:48 +000049static inline LifetimePosition Min(LifetimePosition a, LifetimePosition b) {
50 return a.Value() < b.Value() ? a : b;
51}
52
53
54static inline LifetimePosition Max(LifetimePosition a, LifetimePosition b) {
55 return a.Value() > b.Value() ? a : b;
56}
57
58
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000059UsePosition::UsePosition(LifetimePosition pos, LOperand* operand)
60 : operand_(operand),
61 hint_(NULL),
62 pos_(pos),
63 next_(NULL),
64 requires_reg_(false),
65 register_beneficial_(true) {
66 if (operand_ != NULL && operand_->IsUnallocated()) {
67 LUnallocated* unalloc = LUnallocated::cast(operand_);
68 requires_reg_ = unalloc->HasRegisterPolicy();
69 register_beneficial_ = !unalloc->HasAnyPolicy();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000070 }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000071 ASSERT(pos_.IsValid());
kasperl@chromium.orga5551262010-12-07 12:49:48 +000072}
73
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000074
75bool UsePosition::HasHint() const {
76 return hint_ != NULL && !hint_->IsUnallocated();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000077}
78
79
80bool UsePosition::RequiresRegister() const {
81 return requires_reg_;
82}
83
84
85bool UsePosition::RegisterIsBeneficial() const {
86 return register_beneficial_;
87}
88
89
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +000090void UseInterval::SplitAt(LifetimePosition pos, Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000091 ASSERT(Contains(pos) && pos.Value() != start().Value());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +000092 UseInterval* after = new(zone) UseInterval(pos, end_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000093 after->next_ = next_;
94 next_ = after;
95 end_ = pos;
96}
97
98
99#ifdef DEBUG
100
101
102void LiveRange::Verify() const {
103 UsePosition* cur = first_pos_;
104 while (cur != NULL) {
105 ASSERT(Start().Value() <= cur->pos().Value() &&
106 cur->pos().Value() <= End().Value());
107 cur = cur->next();
108 }
109}
110
111
112bool LiveRange::HasOverlap(UseInterval* target) const {
113 UseInterval* current_interval = first_interval_;
114 while (current_interval != NULL) {
115 // Intervals overlap if the start of one is contained in the other.
116 if (current_interval->Contains(target->start()) ||
117 target->Contains(current_interval->start())) {
118 return true;
119 }
120 current_interval = current_interval->next();
121 }
122 return false;
123}
124
125
126#endif
127
128
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000129LiveRange::LiveRange(int id, Zone* zone)
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000130 : id_(id),
131 spilled_(false),
danno@chromium.org2c456792011-11-11 12:00:53 +0000132 is_double_(false),
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000133 assigned_register_(kInvalidAssignment),
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000134 last_interval_(NULL),
135 first_interval_(NULL),
136 first_pos_(NULL),
137 parent_(NULL),
138 next_(NULL),
139 current_interval_(NULL),
140 last_processed_use_(NULL),
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000141 spill_operand_(new(zone) LOperand()),
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000142 spill_start_index_(kMaxInt) { }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000143
144
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000145void LiveRange::set_assigned_register(int reg,
146 RegisterKind register_kind,
147 Zone* zone) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000148 ASSERT(!HasRegisterAssigned() && !IsSpilled());
149 assigned_register_ = reg;
danno@chromium.org2c456792011-11-11 12:00:53 +0000150 is_double_ = (register_kind == DOUBLE_REGISTERS);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000151 ConvertOperands(zone);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000152}
153
154
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000155void LiveRange::MakeSpilled(Zone* zone) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000156 ASSERT(!IsSpilled());
157 ASSERT(TopLevel()->HasAllocatedSpillOperand());
158 spilled_ = true;
159 assigned_register_ = kInvalidAssignment;
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000160 ConvertOperands(zone);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000161}
162
163
164bool LiveRange::HasAllocatedSpillOperand() const {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000165 ASSERT(spill_operand_ != NULL);
166 return !spill_operand_->IsIgnored();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000167}
168
169
170void LiveRange::SetSpillOperand(LOperand* operand) {
171 ASSERT(!operand->IsUnallocated());
172 ASSERT(spill_operand_ != NULL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000173 ASSERT(spill_operand_->IsIgnored());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000174 spill_operand_->ConvertTo(operand->kind(), operand->index());
175}
176
177
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000178UsePosition* LiveRange::NextUsePosition(LifetimePosition start) {
179 UsePosition* use_pos = last_processed_use_;
180 if (use_pos == NULL) use_pos = first_pos();
181 while (use_pos != NULL && use_pos->pos().Value() < start.Value()) {
182 use_pos = use_pos->next();
183 }
184 last_processed_use_ = use_pos;
185 return use_pos;
186}
187
188
189UsePosition* LiveRange::NextUsePositionRegisterIsBeneficial(
190 LifetimePosition start) {
191 UsePosition* pos = NextUsePosition(start);
192 while (pos != NULL && !pos->RegisterIsBeneficial()) {
193 pos = pos->next();
194 }
195 return pos;
196}
197
198
199UsePosition* LiveRange::NextRegisterPosition(LifetimePosition start) {
200 UsePosition* pos = NextUsePosition(start);
201 while (pos != NULL && !pos->RequiresRegister()) {
202 pos = pos->next();
203 }
204 return pos;
205}
206
207
208bool LiveRange::CanBeSpilled(LifetimePosition pos) {
209 // TODO(kmillikin): Comment. Now.
210 if (pos.Value() <= Start().Value() && HasRegisterAssigned()) return false;
211
212 // We cannot spill a live range that has a use requiring a register
213 // at the current or the immediate next position.
214 UsePosition* use_pos = NextRegisterPosition(pos);
215 if (use_pos == NULL) return true;
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000216 return
217 use_pos->pos().Value() > pos.NextInstruction().InstructionEnd().Value();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000218}
219
220
221UsePosition* LiveRange::FirstPosWithHint() const {
222 UsePosition* pos = first_pos_;
223 while (pos != NULL && !pos->HasHint()) pos = pos->next();
224 return pos;
225}
226
227
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000228LOperand* LiveRange::CreateAssignedOperand(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000229 LOperand* op = NULL;
230 if (HasRegisterAssigned()) {
231 ASSERT(!IsSpilled());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000232 if (IsDouble()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000233 op = LDoubleRegister::Create(assigned_register(), zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000234 } else {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000235 op = LRegister::Create(assigned_register(), zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000236 }
237 } else if (IsSpilled()) {
238 ASSERT(!HasRegisterAssigned());
239 op = TopLevel()->GetSpillOperand();
240 ASSERT(!op->IsUnallocated());
241 } else {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000242 LUnallocated* unalloc = new(zone) LUnallocated(LUnallocated::NONE);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000243 unalloc->set_virtual_register(id_);
244 op = unalloc;
245 }
246 return op;
247}
248
249
250UseInterval* LiveRange::FirstSearchIntervalForPosition(
251 LifetimePosition position) const {
252 if (current_interval_ == NULL) return first_interval_;
253 if (current_interval_->start().Value() > position.Value()) {
254 current_interval_ = NULL;
255 return first_interval_;
256 }
257 return current_interval_;
258}
259
260
261void LiveRange::AdvanceLastProcessedMarker(
262 UseInterval* to_start_of, LifetimePosition but_not_past) const {
263 if (to_start_of == NULL) return;
264 if (to_start_of->start().Value() > but_not_past.Value()) return;
265 LifetimePosition start =
266 current_interval_ == NULL ? LifetimePosition::Invalid()
267 : current_interval_->start();
268 if (to_start_of->start().Value() > start.Value()) {
269 current_interval_ = to_start_of;
270 }
271}
272
273
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000274void LiveRange::SplitAt(LifetimePosition position,
275 LiveRange* result,
276 Zone* zone) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000277 ASSERT(Start().Value() < position.Value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000278 ASSERT(result->IsEmpty());
279 // Find the last interval that ends before the position. If the
280 // position is contained in one of the intervals in the chain, we
281 // split that interval and use the first part.
282 UseInterval* current = FirstSearchIntervalForPosition(position);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000283
284 // If the split position coincides with the beginning of a use interval
285 // we need to split use positons in a special way.
286 bool split_at_start = false;
287
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000288 if (current->start().Value() == position.Value()) {
289 // When splitting at start we need to locate the previous use interval.
290 current = first_interval_;
291 }
292
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000293 while (current != NULL) {
294 if (current->Contains(position)) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000295 current->SplitAt(position, zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000296 break;
297 }
298 UseInterval* next = current->next();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000299 if (next->start().Value() >= position.Value()) {
300 split_at_start = (next->start().Value() == position.Value());
301 break;
302 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000303 current = next;
304 }
305
306 // Partition original use intervals to the two live ranges.
307 UseInterval* before = current;
308 UseInterval* after = before->next();
309 result->last_interval_ = (last_interval_ == before)
310 ? after // Only interval in the range after split.
311 : last_interval_; // Last interval of the original range.
312 result->first_interval_ = after;
313 last_interval_ = before;
314
315 // Find the last use position before the split and the first use
316 // position after it.
317 UsePosition* use_after = first_pos_;
318 UsePosition* use_before = NULL;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000319 if (split_at_start) {
320 // The split position coincides with the beginning of a use interval (the
321 // end of a lifetime hole). Use at this position should be attributed to
322 // the split child because split child owns use interval covering it.
323 while (use_after != NULL && use_after->pos().Value() < position.Value()) {
324 use_before = use_after;
325 use_after = use_after->next();
326 }
327 } else {
328 while (use_after != NULL && use_after->pos().Value() <= position.Value()) {
329 use_before = use_after;
330 use_after = use_after->next();
331 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000332 }
333
334 // Partition original use positions to the two live ranges.
335 if (use_before != NULL) {
336 use_before->next_ = NULL;
337 } else {
338 first_pos_ = NULL;
339 }
340 result->first_pos_ = use_after;
341
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000342 // Discard cached iteration state. It might be pointing
343 // to the use that no longer belongs to this live range.
344 last_processed_use_ = NULL;
345 current_interval_ = NULL;
346
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000347 // Link the new live range in the chain before any of the other
348 // ranges linked from the range before the split.
349 result->parent_ = (parent_ == NULL) ? this : parent_;
350 result->next_ = next_;
351 next_ = result;
352
353#ifdef DEBUG
354 Verify();
355 result->Verify();
356#endif
357}
358
359
360// This implements an ordering on live ranges so that they are ordered by their
361// start positions. This is needed for the correctness of the register
362// allocation algorithm. If two live ranges start at the same offset then there
363// is a tie breaker based on where the value is first used. This part of the
364// ordering is merely a heuristic.
365bool LiveRange::ShouldBeAllocatedBefore(const LiveRange* other) const {
366 LifetimePosition start = Start();
367 LifetimePosition other_start = other->Start();
368 if (start.Value() == other_start.Value()) {
369 UsePosition* pos = FirstPosWithHint();
370 if (pos == NULL) return false;
371 UsePosition* other_pos = other->first_pos();
372 if (other_pos == NULL) return true;
373 return pos->pos().Value() < other_pos->pos().Value();
374 }
375 return start.Value() < other_start.Value();
376}
377
378
379void LiveRange::ShortenTo(LifetimePosition start) {
380 LAllocator::TraceAlloc("Shorten live range %d to [%d\n", id_, start.Value());
381 ASSERT(first_interval_ != NULL);
382 ASSERT(first_interval_->start().Value() <= start.Value());
383 ASSERT(start.Value() < first_interval_->end().Value());
384 first_interval_->set_start(start);
385}
386
387
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000388void LiveRange::EnsureInterval(LifetimePosition start,
389 LifetimePosition end,
390 Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000391 LAllocator::TraceAlloc("Ensure live range %d in interval [%d %d[\n",
392 id_,
393 start.Value(),
394 end.Value());
395 LifetimePosition new_end = end;
396 while (first_interval_ != NULL &&
397 first_interval_->start().Value() <= end.Value()) {
398 if (first_interval_->end().Value() > end.Value()) {
399 new_end = first_interval_->end();
400 }
401 first_interval_ = first_interval_->next();
402 }
403
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000404 UseInterval* new_interval = new(zone) UseInterval(start, new_end);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000405 new_interval->next_ = first_interval_;
406 first_interval_ = new_interval;
407 if (new_interval->next() == NULL) {
408 last_interval_ = new_interval;
409 }
410}
411
412
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000413void LiveRange::AddUseInterval(LifetimePosition start,
414 LifetimePosition end,
415 Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000416 LAllocator::TraceAlloc("Add to live range %d interval [%d %d[\n",
417 id_,
418 start.Value(),
419 end.Value());
420 if (first_interval_ == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000421 UseInterval* interval = new(zone) UseInterval(start, end);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000422 first_interval_ = interval;
423 last_interval_ = interval;
424 } else {
425 if (end.Value() == first_interval_->start().Value()) {
426 first_interval_->set_start(start);
427 } else if (end.Value() < first_interval_->start().Value()) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000428 UseInterval* interval = new(zone) UseInterval(start, end);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429 interval->set_next(first_interval_);
430 first_interval_ = interval;
431 } else {
432 // Order of instruction's processing (see ProcessInstructions) guarantees
433 // that each new use interval either precedes or intersects with
434 // last added interval.
435 ASSERT(start.Value() < first_interval_->end().Value());
436 first_interval_->start_ = Min(start, first_interval_->start_);
437 first_interval_->end_ = Max(end, first_interval_->end_);
438 }
439 }
440}
441
442
443UsePosition* LiveRange::AddUsePosition(LifetimePosition pos,
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000444 LOperand* operand,
445 Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000446 LAllocator::TraceAlloc("Add to live range %d use position %d\n",
447 id_,
448 pos.Value());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000449 UsePosition* use_pos = new(zone) UsePosition(pos, operand);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000450 UsePosition* prev = NULL;
451 UsePosition* current = first_pos_;
452 while (current != NULL && current->pos().Value() < pos.Value()) {
453 prev = current;
454 current = current->next();
455 }
456
457 if (prev == NULL) {
458 use_pos->set_next(first_pos_);
459 first_pos_ = use_pos;
460 } else {
461 use_pos->next_ = prev->next_;
462 prev->next_ = use_pos;
463 }
464
465 return use_pos;
466}
467
468
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000469void LiveRange::ConvertOperands(Zone* zone) {
470 LOperand* op = CreateAssignedOperand(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000471 UsePosition* use_pos = first_pos();
472 while (use_pos != NULL) {
473 ASSERT(Start().Value() <= use_pos->pos().Value() &&
474 use_pos->pos().Value() <= End().Value());
475
476 if (use_pos->HasOperand()) {
477 ASSERT(op->IsRegister() || op->IsDoubleRegister() ||
478 !use_pos->RequiresRegister());
479 use_pos->operand()->ConvertTo(op->kind(), op->index());
480 }
481 use_pos = use_pos->next();
482 }
483}
484
485
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000486bool LiveRange::CanCover(LifetimePosition position) const {
487 if (IsEmpty()) return false;
488 return Start().Value() <= position.Value() &&
489 position.Value() < End().Value();
490}
491
492
493bool LiveRange::Covers(LifetimePosition position) {
494 if (!CanCover(position)) return false;
495 UseInterval* start_search = FirstSearchIntervalForPosition(position);
496 for (UseInterval* interval = start_search;
497 interval != NULL;
498 interval = interval->next()) {
499 ASSERT(interval->next() == NULL ||
500 interval->next()->start().Value() >= interval->start().Value());
501 AdvanceLastProcessedMarker(interval, position);
502 if (interval->Contains(position)) return true;
503 if (interval->start().Value() > position.Value()) return false;
504 }
505 return false;
506}
507
508
509LifetimePosition LiveRange::FirstIntersection(LiveRange* other) {
510 UseInterval* b = other->first_interval();
511 if (b == NULL) return LifetimePosition::Invalid();
512 LifetimePosition advance_last_processed_up_to = b->start();
513 UseInterval* a = FirstSearchIntervalForPosition(b->start());
514 while (a != NULL && b != NULL) {
515 if (a->start().Value() > other->End().Value()) break;
516 if (b->start().Value() > End().Value()) break;
517 LifetimePosition cur_intersection = a->Intersect(b);
518 if (cur_intersection.IsValid()) {
519 return cur_intersection;
520 }
521 if (a->start().Value() < b->start().Value()) {
522 a = a->next();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000523 if (a == NULL || a->start().Value() > other->End().Value()) break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000524 AdvanceLastProcessedMarker(a, advance_last_processed_up_to);
525 } else {
526 b = b->next();
527 }
528 }
529 return LifetimePosition::Invalid();
530}
531
532
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000533LAllocator::LAllocator(int num_values, HGraph* graph)
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000534 : zone_(graph->zone()),
535 chunk_(NULL),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000536 live_in_sets_(graph->blocks()->length(), zone_),
537 live_ranges_(num_values * 2, zone_),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000538 fixed_live_ranges_(NULL),
539 fixed_double_live_ranges_(NULL),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000540 unhandled_live_ranges_(num_values * 2, zone_),
541 active_live_ranges_(8, zone_),
542 inactive_live_ranges_(8, zone_),
543 reusable_slots_(8, zone_),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000544 next_virtual_register_(num_values),
545 first_artificial_register_(num_values),
danno@chromium.org2c456792011-11-11 12:00:53 +0000546 mode_(GENERAL_REGISTERS),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000547 num_registers_(-1),
548 graph_(graph),
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000549 has_osr_entry_(false),
550 allocation_ok_(true) { }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000551
552
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000553void LAllocator::InitializeLivenessAnalysis() {
554 // Initialize the live_in sets for each block to NULL.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000555 int block_count = graph_->blocks()->length();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000556 live_in_sets_.Initialize(block_count, zone());
557 live_in_sets_.AddBlock(NULL, block_count, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000558}
559
560
561BitVector* LAllocator::ComputeLiveOut(HBasicBlock* block) {
562 // Compute live out for the given block, except not including backward
563 // successor edges.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000564 BitVector* live_out = new(zone_) BitVector(next_virtual_register_, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000565
566 // Process all successor blocks.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000567 for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000568 // Add values live on entry to the successor. Note the successor's
569 // live_in will not be computed yet for backwards edges.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000570 HBasicBlock* successor = it.Current();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000571 BitVector* live_in = live_in_sets_[successor->block_id()];
572 if (live_in != NULL) live_out->Union(*live_in);
573
574 // All phi input operands corresponding to this successor edge are live
575 // out from this block.
576 int index = successor->PredecessorIndexOf(block);
577 const ZoneList<HPhi*>* phis = successor->phis();
578 for (int i = 0; i < phis->length(); ++i) {
579 HPhi* phi = phis->at(i);
580 if (!phi->OperandAt(index)->IsConstant()) {
581 live_out->Add(phi->OperandAt(index)->id());
582 }
583 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000584 }
585
586 return live_out;
587}
588
589
590void LAllocator::AddInitialIntervals(HBasicBlock* block,
591 BitVector* live_out) {
592 // Add an interval that includes the entire block to the live range for
593 // each live_out value.
594 LifetimePosition start = LifetimePosition::FromInstructionIndex(
595 block->first_instruction_index());
596 LifetimePosition end = LifetimePosition::FromInstructionIndex(
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000597 block->last_instruction_index()).NextInstruction();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000598 BitVector::Iterator iterator(live_out);
599 while (!iterator.Done()) {
600 int operand_index = iterator.Current();
601 LiveRange* range = LiveRangeFor(operand_index);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000602 range->AddUseInterval(start, end, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000603 iterator.Advance();
604 }
605}
606
607
608int LAllocator::FixedDoubleLiveRangeID(int index) {
609 return -index - 1 - Register::kNumAllocatableRegisters;
610}
611
612
613LOperand* LAllocator::AllocateFixed(LUnallocated* operand,
614 int pos,
615 bool is_tagged) {
616 TraceAlloc("Allocating fixed reg for op %d\n", operand->virtual_register());
617 ASSERT(operand->HasFixedPolicy());
618 if (operand->policy() == LUnallocated::FIXED_SLOT) {
619 operand->ConvertTo(LOperand::STACK_SLOT, operand->fixed_index());
620 } else if (operand->policy() == LUnallocated::FIXED_REGISTER) {
621 int reg_index = operand->fixed_index();
622 operand->ConvertTo(LOperand::REGISTER, reg_index);
623 } else if (operand->policy() == LUnallocated::FIXED_DOUBLE_REGISTER) {
624 int reg_index = operand->fixed_index();
625 operand->ConvertTo(LOperand::DOUBLE_REGISTER, reg_index);
626 } else {
627 UNREACHABLE();
628 }
629 if (is_tagged) {
630 TraceAlloc("Fixed reg is tagged at %d\n", pos);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000631 LInstruction* instr = InstructionAt(pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000632 if (instr->HasPointerMap()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000633 instr->pointer_map()->RecordPointer(operand, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000634 }
635 }
636 return operand;
637}
638
639
640LiveRange* LAllocator::FixedLiveRangeFor(int index) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000641 ASSERT(index < Register::kNumAllocatableRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000642 LiveRange* result = fixed_live_ranges_[index];
643 if (result == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000644 result = new(zone_) LiveRange(FixedLiveRangeID(index), zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000645 ASSERT(result->IsFixed());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000646 result->set_assigned_register(index, GENERAL_REGISTERS, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000647 fixed_live_ranges_[index] = result;
648 }
649 return result;
650}
651
652
653LiveRange* LAllocator::FixedDoubleLiveRangeFor(int index) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000654 ASSERT(index < DoubleRegister::kNumAllocatableRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000655 LiveRange* result = fixed_double_live_ranges_[index];
656 if (result == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000657 result = new(zone_) LiveRange(FixedDoubleLiveRangeID(index), zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000658 ASSERT(result->IsFixed());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000659 result->set_assigned_register(index, DOUBLE_REGISTERS, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000660 fixed_double_live_ranges_[index] = result;
661 }
662 return result;
663}
664
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000665
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000666LiveRange* LAllocator::LiveRangeFor(int index) {
667 if (index >= live_ranges_.length()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000668 live_ranges_.AddBlock(NULL, index - live_ranges_.length() + 1, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000669 }
670 LiveRange* result = live_ranges_[index];
671 if (result == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000672 result = new(zone_) LiveRange(index, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000673 live_ranges_[index] = result;
674 }
675 return result;
676}
677
678
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000679LGap* LAllocator::GetLastGap(HBasicBlock* block) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000680 int last_instruction = block->last_instruction_index();
681 int index = chunk_->NearestGapPos(last_instruction);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000682 return GapAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000683}
684
685
686HPhi* LAllocator::LookupPhi(LOperand* operand) const {
687 if (!operand->IsUnallocated()) return NULL;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000688 int index = LUnallocated::cast(operand)->virtual_register();
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000689 HValue* instr = graph_->LookupValue(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000690 if (instr != NULL && instr->IsPhi()) {
691 return HPhi::cast(instr);
692 }
693 return NULL;
694}
695
696
697LiveRange* LAllocator::LiveRangeFor(LOperand* operand) {
698 if (operand->IsUnallocated()) {
699 return LiveRangeFor(LUnallocated::cast(operand)->virtual_register());
700 } else if (operand->IsRegister()) {
701 return FixedLiveRangeFor(operand->index());
702 } else if (operand->IsDoubleRegister()) {
703 return FixedDoubleLiveRangeFor(operand->index());
704 } else {
705 return NULL;
706 }
707}
708
709
710void LAllocator::Define(LifetimePosition position,
711 LOperand* operand,
712 LOperand* hint) {
713 LiveRange* range = LiveRangeFor(operand);
714 if (range == NULL) return;
715
716 if (range->IsEmpty() || range->Start().Value() > position.Value()) {
717 // Can happen if there is a definition without use.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000718 range->AddUseInterval(position, position.NextInstruction(), zone_);
719 range->AddUsePosition(position.NextInstruction(), NULL, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000720 } else {
721 range->ShortenTo(position);
722 }
723
724 if (operand->IsUnallocated()) {
725 LUnallocated* unalloc_operand = LUnallocated::cast(operand);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000726 range->AddUsePosition(position, unalloc_operand, zone_)->set_hint(hint);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000727 }
728}
729
730
731void LAllocator::Use(LifetimePosition block_start,
732 LifetimePosition position,
733 LOperand* operand,
734 LOperand* hint) {
735 LiveRange* range = LiveRangeFor(operand);
736 if (range == NULL) return;
737 if (operand->IsUnallocated()) {
738 LUnallocated* unalloc_operand = LUnallocated::cast(operand);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000739 range->AddUsePosition(position, unalloc_operand, zone_)->set_hint(hint);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000740 }
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000741 range->AddUseInterval(block_start, position, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000742}
743
744
745void LAllocator::AddConstraintsGapMove(int index,
746 LOperand* from,
747 LOperand* to) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000748 LGap* gap = GapAt(index);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000749 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000750 if (from->IsUnallocated()) {
751 const ZoneList<LMoveOperands>* move_operands = move->move_operands();
752 for (int i = 0; i < move_operands->length(); ++i) {
753 LMoveOperands cur = move_operands->at(i);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000754 LOperand* cur_to = cur.destination();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000755 if (cur_to->IsUnallocated()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000756 if (LUnallocated::cast(cur_to)->virtual_register() ==
757 LUnallocated::cast(from)->virtual_register()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000758 move->AddMove(cur.source(), to, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000759 return;
760 }
761 }
762 }
763 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000764 move->AddMove(from, to, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000765}
766
767
768void LAllocator::MeetRegisterConstraints(HBasicBlock* block) {
769 int start = block->first_instruction_index();
770 int end = block->last_instruction_index();
771 for (int i = start; i <= end; ++i) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000772 if (IsGapAt(i)) {
773 LInstruction* instr = NULL;
774 LInstruction* prev_instr = NULL;
775 if (i < end) instr = InstructionAt(i + 1);
776 if (i > start) prev_instr = InstructionAt(i - 1);
777 MeetConstraintsBetween(prev_instr, instr, i);
rossberg@chromium.org994edf62012-02-06 10:12:55 +0000778 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000779 }
780 }
781}
782
783
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000784void LAllocator::MeetConstraintsBetween(LInstruction* first,
785 LInstruction* second,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000786 int gap_index) {
787 // Handle fixed temporaries.
788 if (first != NULL) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000789 for (TempIterator it(first); !it.Done(); it.Advance()) {
790 LUnallocated* temp = LUnallocated::cast(it.Current());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000791 if (temp->HasFixedPolicy()) {
792 AllocateFixed(temp, gap_index - 1, false);
793 }
794 }
795 }
796
797 // Handle fixed output operand.
798 if (first != NULL && first->Output() != NULL) {
799 LUnallocated* first_output = LUnallocated::cast(first->Output());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000800 LiveRange* range = LiveRangeFor(first_output->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000801 bool assigned = false;
802 if (first_output->HasFixedPolicy()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000803 LUnallocated* output_copy = first_output->CopyUnconstrained(zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000804 bool is_tagged = HasTaggedValue(first_output->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000805 AllocateFixed(first_output, gap_index, is_tagged);
806
807 // This value is produced on the stack, we never need to spill it.
808 if (first_output->IsStackSlot()) {
809 range->SetSpillOperand(first_output);
810 range->SetSpillStartIndex(gap_index - 1);
811 assigned = true;
812 }
813 chunk_->AddGapMove(gap_index, first_output, output_copy);
814 }
815
816 if (!assigned) {
817 range->SetSpillStartIndex(gap_index);
818
819 // This move to spill operand is not a real use. Liveness analysis
820 // and splitting of live ranges do not account for it.
821 // Thus it should be inserted to a lifetime position corresponding to
822 // the instruction end.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000823 LGap* gap = GapAt(gap_index);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000824 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::BEFORE, zone());
825 move->AddMove(first_output, range->GetSpillOperand(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000826 }
827 }
828
829 // Handle fixed input operands of second instruction.
830 if (second != NULL) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000831 for (UseIterator it(second); !it.Done(); it.Advance()) {
832 LUnallocated* cur_input = LUnallocated::cast(it.Current());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000833 if (cur_input->HasFixedPolicy()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000834 LUnallocated* input_copy = cur_input->CopyUnconstrained(zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000835 bool is_tagged = HasTaggedValue(cur_input->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000836 AllocateFixed(cur_input, gap_index + 1, is_tagged);
837 AddConstraintsGapMove(gap_index, input_copy, cur_input);
838 } else if (cur_input->policy() == LUnallocated::WRITABLE_REGISTER) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000839 // The live range of writable input registers always goes until the end
840 // of the instruction.
841 ASSERT(!cur_input->IsUsedAtStart());
842
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000843 LUnallocated* input_copy = cur_input->CopyUnconstrained(zone());
rossberg@chromium.org994edf62012-02-06 10:12:55 +0000844 cur_input->set_virtual_register(GetVirtualRegister());
845 if (!AllocationOk()) return;
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000846
847 if (RequiredRegisterKind(input_copy->virtual_register()) ==
848 DOUBLE_REGISTERS) {
849 double_artificial_registers_.Add(
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000850 cur_input->virtual_register() - first_artificial_register_,
851 zone_);
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000852 }
853
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000854 AddConstraintsGapMove(gap_index, input_copy, cur_input);
855 }
856 }
857 }
858
859 // Handle "output same as input" for second instruction.
860 if (second != NULL && second->Output() != NULL) {
861 LUnallocated* second_output = LUnallocated::cast(second->Output());
862 if (second_output->HasSameAsInputPolicy()) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000863 LUnallocated* cur_input = LUnallocated::cast(second->FirstInput());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000864 int output_vreg = second_output->virtual_register();
865 int input_vreg = cur_input->virtual_register();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000866
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000867 LUnallocated* input_copy = cur_input->CopyUnconstrained(zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000868 cur_input->set_virtual_register(second_output->virtual_register());
869 AddConstraintsGapMove(gap_index, input_copy, cur_input);
870
871 if (HasTaggedValue(input_vreg) && !HasTaggedValue(output_vreg)) {
872 int index = gap_index + 1;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000873 LInstruction* instr = InstructionAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000874 if (instr->HasPointerMap()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000875 instr->pointer_map()->RecordPointer(input_copy, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000876 }
877 } else if (!HasTaggedValue(input_vreg) && HasTaggedValue(output_vreg)) {
878 // The input is assumed to immediately have a tagged representation,
879 // before the pointer map can be used. I.e. the pointer map at the
880 // instruction will include the output operand (whose value at the
881 // beginning of the instruction is equal to the input operand). If
882 // this is not desired, then the pointer map at this instruction needs
883 // to be adjusted manually.
884 }
885 }
886 }
887}
888
889
890void LAllocator::ProcessInstructions(HBasicBlock* block, BitVector* live) {
891 int block_start = block->first_instruction_index();
892 int index = block->last_instruction_index();
893
894 LifetimePosition block_start_position =
895 LifetimePosition::FromInstructionIndex(block_start);
896
897 while (index >= block_start) {
898 LifetimePosition curr_position =
899 LifetimePosition::FromInstructionIndex(index);
900
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000901 if (IsGapAt(index)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000902 // We have a gap at this position.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000903 LGap* gap = GapAt(index);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000904 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000905 const ZoneList<LMoveOperands>* move_operands = move->move_operands();
906 for (int i = 0; i < move_operands->length(); ++i) {
907 LMoveOperands* cur = &move_operands->at(i);
908 if (cur->IsIgnored()) continue;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000909 LOperand* from = cur->source();
910 LOperand* to = cur->destination();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000911 HPhi* phi = LookupPhi(to);
912 LOperand* hint = to;
913 if (phi != NULL) {
914 // This is a phi resolving move.
915 if (!phi->block()->IsLoopHeader()) {
916 hint = LiveRangeFor(phi->id())->FirstHint();
917 }
918 } else {
919 if (to->IsUnallocated()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000920 if (live->Contains(LUnallocated::cast(to)->virtual_register())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000921 Define(curr_position, to, from);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000922 live->Remove(LUnallocated::cast(to)->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000923 } else {
924 cur->Eliminate();
925 continue;
926 }
927 } else {
928 Define(curr_position, to, from);
929 }
930 }
931 Use(block_start_position, curr_position, from, hint);
932 if (from->IsUnallocated()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000933 live->Add(LUnallocated::cast(from)->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000934 }
935 }
936 } else {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000937 ASSERT(!IsGapAt(index));
938 LInstruction* instr = InstructionAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000939
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000940 if (instr != NULL) {
941 LOperand* output = instr->Output();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000942 if (output != NULL) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000943 if (output->IsUnallocated()) {
944 live->Remove(LUnallocated::cast(output)->virtual_register());
945 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000946 Define(curr_position, output, NULL);
947 }
948
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000949 if (instr->IsMarkedAsCall()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000950 for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
951 if (output == NULL || !output->IsRegister() ||
952 output->index() != i) {
953 LiveRange* range = FixedLiveRangeFor(i);
954 range->AddUseInterval(curr_position,
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000955 curr_position.InstructionEnd(),
956 zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000957 }
958 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000959 }
960
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000961 if (instr->IsMarkedAsCall()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000962 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; ++i) {
963 if (output == NULL || !output->IsDoubleRegister() ||
964 output->index() != i) {
965 LiveRange* range = FixedDoubleLiveRangeFor(i);
966 range->AddUseInterval(curr_position,
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000967 curr_position.InstructionEnd(),
968 zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000969 }
970 }
971 }
972
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000973 for (UseIterator it(instr); !it.Done(); it.Advance()) {
974 LOperand* input = it.Current();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000975
976 LifetimePosition use_pos;
977 if (input->IsUnallocated() &&
978 LUnallocated::cast(input)->IsUsedAtStart()) {
979 use_pos = curr_position;
980 } else {
981 use_pos = curr_position.InstructionEnd();
982 }
983
984 Use(block_start_position, use_pos, input, NULL);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000985 if (input->IsUnallocated()) {
986 live->Add(LUnallocated::cast(input)->virtual_register());
987 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000988 }
989
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000990 for (TempIterator it(instr); !it.Done(); it.Advance()) {
991 LOperand* temp = it.Current();
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000992 if (instr->IsMarkedAsCall()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000993 if (temp->IsRegister()) continue;
994 if (temp->IsUnallocated()) {
995 LUnallocated* temp_unalloc = LUnallocated::cast(temp);
996 if (temp_unalloc->HasFixedPolicy()) {
997 continue;
998 }
999 }
1000 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001001 Use(block_start_position, curr_position.InstructionEnd(), temp, NULL);
1002 Define(curr_position, temp, NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001003 }
1004 }
1005 }
1006
1007 index = index - 1;
1008 }
1009}
1010
1011
1012void LAllocator::ResolvePhis(HBasicBlock* block) {
1013 const ZoneList<HPhi*>* phis = block->phis();
1014 for (int i = 0; i < phis->length(); ++i) {
1015 HPhi* phi = phis->at(i);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001016 LUnallocated* phi_operand = new(zone_) LUnallocated(LUnallocated::NONE);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001017 phi_operand->set_virtual_register(phi->id());
1018 for (int j = 0; j < phi->OperandCount(); ++j) {
1019 HValue* op = phi->OperandAt(j);
1020 LOperand* operand = NULL;
1021 if (op->IsConstant() && op->EmitAtUses()) {
1022 HConstant* constant = HConstant::cast(op);
1023 operand = chunk_->DefineConstantOperand(constant);
1024 } else {
1025 ASSERT(!op->EmitAtUses());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001026 LUnallocated* unalloc = new(zone_) LUnallocated(LUnallocated::ANY);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001027 unalloc->set_virtual_register(op->id());
1028 operand = unalloc;
1029 }
1030 HBasicBlock* cur_block = block->predecessors()->at(j);
1031 // The gap move must be added without any special processing as in
1032 // the AddConstraintsGapMove.
1033 chunk_->AddGapMove(cur_block->last_instruction_index() - 1,
1034 operand,
1035 phi_operand);
ricow@chromium.orgdcebac02011-04-20 09:44:50 +00001036
1037 // We are going to insert a move before the branch instruction.
1038 // Some branch instructions (e.g. loops' back edges)
1039 // can potentially cause a GC so they have a pointer map.
1040 // By inserting a move we essentially create a copy of a
1041 // value which is invisible to PopulatePointerMaps(), because we store
1042 // it into a location different from the operand of a live range
1043 // covering a branch instruction.
1044 // Thus we need to manually record a pointer.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001045 LInstruction* branch =
1046 InstructionAt(cur_block->last_instruction_index());
1047 if (branch->HasPointerMap()) {
1048 if (phi->representation().IsTagged()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001049 branch->pointer_map()->RecordPointer(phi_operand, zone());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001050 } else if (!phi->representation().IsDouble()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001051 branch->pointer_map()->RecordUntagged(phi_operand, zone());
ricow@chromium.orgdcebac02011-04-20 09:44:50 +00001052 }
1053 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001054 }
1055
1056 LiveRange* live_range = LiveRangeFor(phi->id());
1057 LLabel* label = chunk_->GetLabel(phi->block()->block_id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001058 label->GetOrCreateParallelMove(LGap::START, zone())->
1059 AddMove(phi_operand, live_range->GetSpillOperand(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001060 live_range->SetSpillStartIndex(phi->block()->first_instruction_index());
1061 }
1062}
1063
1064
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001065bool LAllocator::Allocate(LChunk* chunk) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001066 ASSERT(chunk_ == NULL);
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001067 chunk_ = static_cast<LPlatformChunk*>(chunk);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001068 MeetRegisterConstraints();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001069 if (!AllocationOk()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001070 ResolvePhis();
1071 BuildLiveRanges();
1072 AllocateGeneralRegisters();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001073 if (!AllocationOk()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001074 AllocateDoubleRegisters();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001075 if (!AllocationOk()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001076 PopulatePointerMaps();
1077 if (has_osr_entry_) ProcessOsrEntry();
1078 ConnectRanges();
1079 ResolveControlFlow();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001080 return true;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001081}
1082
1083
1084void LAllocator::MeetRegisterConstraints() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001085 HPhase phase("L_Register constraints", chunk_);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001086 first_artificial_register_ = next_virtual_register_;
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001087 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001088 for (int i = 0; i < blocks->length(); ++i) {
1089 HBasicBlock* block = blocks->at(i);
1090 MeetRegisterConstraints(block);
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001091 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001092 }
1093}
1094
1095
1096void LAllocator::ResolvePhis() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001097 HPhase phase("L_Resolve phis", chunk_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001098
1099 // Process the blocks in reverse order.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001100 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001101 for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) {
1102 HBasicBlock* block = blocks->at(block_id);
1103 ResolvePhis(block);
1104 }
1105}
1106
1107
1108void LAllocator::ResolveControlFlow(LiveRange* range,
1109 HBasicBlock* block,
1110 HBasicBlock* pred) {
1111 LifetimePosition pred_end =
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001112 LifetimePosition::FromInstructionIndex(pred->last_instruction_index());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001113 LifetimePosition cur_start =
1114 LifetimePosition::FromInstructionIndex(block->first_instruction_index());
1115 LiveRange* pred_cover = NULL;
1116 LiveRange* cur_cover = NULL;
1117 LiveRange* cur_range = range;
1118 while (cur_range != NULL && (cur_cover == NULL || pred_cover == NULL)) {
1119 if (cur_range->CanCover(cur_start)) {
1120 ASSERT(cur_cover == NULL);
1121 cur_cover = cur_range;
1122 }
1123 if (cur_range->CanCover(pred_end)) {
1124 ASSERT(pred_cover == NULL);
1125 pred_cover = cur_range;
1126 }
1127 cur_range = cur_range->next();
1128 }
1129
1130 if (cur_cover->IsSpilled()) return;
1131 ASSERT(pred_cover != NULL && cur_cover != NULL);
1132 if (pred_cover != cur_cover) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001133 LOperand* pred_op = pred_cover->CreateAssignedOperand(zone_);
1134 LOperand* cur_op = cur_cover->CreateAssignedOperand(zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001135 if (!pred_op->Equals(cur_op)) {
1136 LGap* gap = NULL;
1137 if (block->predecessors()->length() == 1) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001138 gap = GapAt(block->first_instruction_index());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001139 } else {
1140 ASSERT(pred->end()->SecondSuccessor() == NULL);
1141 gap = GetLastGap(pred);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001142
1143 // We are going to insert a move before the branch instruction.
1144 // Some branch instructions (e.g. loops' back edges)
1145 // can potentially cause a GC so they have a pointer map.
ricow@chromium.orgdcebac02011-04-20 09:44:50 +00001146 // By inserting a move we essentially create a copy of a
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001147 // value which is invisible to PopulatePointerMaps(), because we store
1148 // it into a location different from the operand of a live range
1149 // covering a branch instruction.
1150 // Thus we need to manually record a pointer.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001151 LInstruction* branch = InstructionAt(pred->last_instruction_index());
1152 if (branch->HasPointerMap()) {
1153 if (HasTaggedValue(range->id())) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001154 branch->pointer_map()->RecordPointer(cur_op, zone());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001155 } else if (!cur_op->IsDoubleStackSlot() &&
1156 !cur_op->IsDoubleRegister()) {
1157 branch->pointer_map()->RemovePointer(cur_op);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001158 }
1159 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001160 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001161 gap->GetOrCreateParallelMove(
1162 LGap::START, zone())->AddMove(pred_op, cur_op, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001163 }
1164 }
1165}
1166
1167
1168LParallelMove* LAllocator::GetConnectingParallelMove(LifetimePosition pos) {
1169 int index = pos.InstructionIndex();
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001170 if (IsGapAt(index)) {
1171 LGap* gap = GapAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001172 return gap->GetOrCreateParallelMove(
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001173 pos.IsInstructionStart() ? LGap::START : LGap::END, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001174 }
1175 int gap_pos = pos.IsInstructionStart() ? (index - 1) : (index + 1);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001176 return GapAt(gap_pos)->GetOrCreateParallelMove(
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001177 (gap_pos < index) ? LGap::AFTER : LGap::BEFORE, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001178}
1179
1180
1181HBasicBlock* LAllocator::GetBlock(LifetimePosition pos) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001182 LGap* gap = GapAt(chunk_->NearestGapPos(pos.InstructionIndex()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001183 return gap->block();
1184}
1185
1186
1187void LAllocator::ConnectRanges() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001188 HPhase phase("L_Connect ranges", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001189 for (int i = 0; i < live_ranges()->length(); ++i) {
1190 LiveRange* first_range = live_ranges()->at(i);
1191 if (first_range == NULL || first_range->parent() != NULL) continue;
1192
1193 LiveRange* second_range = first_range->next();
1194 while (second_range != NULL) {
1195 LifetimePosition pos = second_range->Start();
1196
1197 if (!second_range->IsSpilled()) {
1198 // Add gap move if the two live ranges touch and there is no block
1199 // boundary.
1200 if (first_range->End().Value() == pos.Value()) {
1201 bool should_insert = true;
1202 if (IsBlockBoundary(pos)) {
1203 should_insert = CanEagerlyResolveControlFlow(GetBlock(pos));
1204 }
1205 if (should_insert) {
1206 LParallelMove* move = GetConnectingParallelMove(pos);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001207 LOperand* prev_operand = first_range->CreateAssignedOperand(zone_);
1208 LOperand* cur_operand = second_range->CreateAssignedOperand(zone_);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001209 move->AddMove(prev_operand, cur_operand, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001210 }
1211 }
1212 }
1213
1214 first_range = second_range;
1215 second_range = second_range->next();
1216 }
1217 }
1218}
1219
1220
1221bool LAllocator::CanEagerlyResolveControlFlow(HBasicBlock* block) const {
1222 if (block->predecessors()->length() != 1) return false;
1223 return block->predecessors()->first()->block_id() == block->block_id() - 1;
1224}
1225
1226
1227void LAllocator::ResolveControlFlow() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001228 HPhase phase("L_Resolve control flow", this);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001229 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001230 for (int block_id = 1; block_id < blocks->length(); ++block_id) {
1231 HBasicBlock* block = blocks->at(block_id);
1232 if (CanEagerlyResolveControlFlow(block)) continue;
1233 BitVector* live = live_in_sets_[block->block_id()];
1234 BitVector::Iterator iterator(live);
1235 while (!iterator.Done()) {
1236 int operand_index = iterator.Current();
1237 for (int i = 0; i < block->predecessors()->length(); ++i) {
1238 HBasicBlock* cur = block->predecessors()->at(i);
1239 LiveRange* cur_range = LiveRangeFor(operand_index);
1240 ResolveControlFlow(cur_range, block, cur);
1241 }
1242 iterator.Advance();
1243 }
1244 }
1245}
1246
1247
1248void LAllocator::BuildLiveRanges() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001249 HPhase phase("L_Build live ranges", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001250 InitializeLivenessAnalysis();
1251 // Process the blocks in reverse order.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001252 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001253 for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) {
1254 HBasicBlock* block = blocks->at(block_id);
1255 BitVector* live = ComputeLiveOut(block);
1256 // Initially consider all live_out values live for the entire block. We
1257 // will shorten these intervals if necessary.
1258 AddInitialIntervals(block, live);
1259
1260 // Process the instructions in reverse order, generating and killing
1261 // live values.
1262 ProcessInstructions(block, live);
1263 // All phi output operands are killed by this block.
1264 const ZoneList<HPhi*>* phis = block->phis();
1265 for (int i = 0; i < phis->length(); ++i) {
1266 // The live range interval already ends at the first instruction of the
1267 // block.
1268 HPhi* phi = phis->at(i);
1269 live->Remove(phi->id());
1270
1271 LOperand* hint = NULL;
1272 LOperand* phi_operand = NULL;
1273 LGap* gap = GetLastGap(phi->block()->predecessors()->at(0));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001274 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001275 for (int j = 0; j < move->move_operands()->length(); ++j) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001276 LOperand* to = move->move_operands()->at(j).destination();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001277 if (to->IsUnallocated() &&
1278 LUnallocated::cast(to)->virtual_register() == phi->id()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001279 hint = move->move_operands()->at(j).source();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001280 phi_operand = to;
1281 break;
1282 }
1283 }
1284 ASSERT(hint != NULL);
1285
1286 LifetimePosition block_start = LifetimePosition::FromInstructionIndex(
1287 block->first_instruction_index());
1288 Define(block_start, phi_operand, hint);
1289 }
1290
1291 // Now live is live_in for this block except not including values live
1292 // out on backward successor edges.
1293 live_in_sets_[block_id] = live;
1294
1295 // If this block is a loop header go back and patch up the necessary
1296 // predecessor blocks.
1297 if (block->IsLoopHeader()) {
1298 // TODO(kmillikin): Need to be able to get the last block of the loop
1299 // in the loop information. Add a live range stretching from the first
1300 // loop instruction to the last for each value live on entry to the
1301 // header.
1302 HBasicBlock* back_edge = block->loop_information()->GetLastBackEdge();
1303 BitVector::Iterator iterator(live);
1304 LifetimePosition start = LifetimePosition::FromInstructionIndex(
1305 block->first_instruction_index());
1306 LifetimePosition end = LifetimePosition::FromInstructionIndex(
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001307 back_edge->last_instruction_index()).NextInstruction();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001308 while (!iterator.Done()) {
1309 int operand_index = iterator.Current();
1310 LiveRange* range = LiveRangeFor(operand_index);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001311 range->EnsureInterval(start, end, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001312 iterator.Advance();
1313 }
1314
1315 for (int i = block->block_id() + 1; i <= back_edge->block_id(); ++i) {
1316 live_in_sets_[i]->Union(*live);
1317 }
1318 }
1319
1320#ifdef DEBUG
1321 if (block_id == 0) {
1322 BitVector::Iterator iterator(live);
1323 bool found = false;
1324 while (!iterator.Done()) {
1325 found = true;
1326 int operand_index = iterator.Current();
1327 PrintF("Function: %s\n",
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001328 *chunk_->info()->function()->debug_name()->ToCString());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001329 PrintF("Value %d used before first definition!\n", operand_index);
1330 LiveRange* range = LiveRangeFor(operand_index);
1331 PrintF("First use is at %d\n", range->first_pos()->pos().Value());
1332 iterator.Advance();
1333 }
1334 ASSERT(!found);
1335 }
1336#endif
1337 }
1338}
1339
1340
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001341bool LAllocator::SafePointsAreInOrder() const {
1342 const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps();
1343 int safe_point = 0;
1344 for (int i = 0; i < pointer_maps->length(); ++i) {
1345 LPointerMap* map = pointer_maps->at(i);
1346 if (safe_point > map->lithium_position()) return false;
1347 safe_point = map->lithium_position();
1348 }
1349 return true;
1350}
1351
1352
1353void LAllocator::PopulatePointerMaps() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001354 HPhase phase("L_Populate pointer maps", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001355 const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps();
1356
1357 ASSERT(SafePointsAreInOrder());
1358
1359 // Iterate over all safe point positions and record a pointer
1360 // for all spilled live ranges at this point.
1361 int first_safe_point_index = 0;
1362 int last_range_start = 0;
1363 for (int range_idx = 0; range_idx < live_ranges()->length(); ++range_idx) {
1364 LiveRange* range = live_ranges()->at(range_idx);
1365 if (range == NULL) continue;
1366 // Iterate over the first parts of multi-part live ranges.
1367 if (range->parent() != NULL) continue;
1368 // Skip non-pointer values.
1369 if (!HasTaggedValue(range->id())) continue;
1370 // Skip empty live ranges.
1371 if (range->IsEmpty()) continue;
1372
1373 // Find the extent of the range and its children.
1374 int start = range->Start().InstructionIndex();
1375 int end = 0;
1376 for (LiveRange* cur = range; cur != NULL; cur = cur->next()) {
1377 LifetimePosition this_end = cur->End();
1378 if (this_end.InstructionIndex() > end) end = this_end.InstructionIndex();
1379 ASSERT(cur->Start().InstructionIndex() >= start);
1380 }
1381
1382 // Most of the ranges are in order, but not all. Keep an eye on when
1383 // they step backwards and reset the first_safe_point_index so we don't
1384 // miss any safe points.
1385 if (start < last_range_start) {
1386 first_safe_point_index = 0;
1387 }
1388 last_range_start = start;
1389
1390 // Step across all the safe points that are before the start of this range,
1391 // recording how far we step in order to save doing this for the next range.
1392 while (first_safe_point_index < pointer_maps->length()) {
1393 LPointerMap* map = pointer_maps->at(first_safe_point_index);
1394 int safe_point = map->lithium_position();
1395 if (safe_point >= start) break;
1396 first_safe_point_index++;
1397 }
1398
1399 // Step through the safe points to see whether they are in the range.
1400 for (int safe_point_index = first_safe_point_index;
1401 safe_point_index < pointer_maps->length();
1402 ++safe_point_index) {
1403 LPointerMap* map = pointer_maps->at(safe_point_index);
1404 int safe_point = map->lithium_position();
1405
1406 // The safe points are sorted so we can stop searching here.
1407 if (safe_point - 1 > end) break;
1408
1409 // Advance to the next active range that covers the current
1410 // safe point position.
1411 LifetimePosition safe_point_pos =
1412 LifetimePosition::FromInstructionIndex(safe_point);
1413 LiveRange* cur = range;
1414 while (cur != NULL && !cur->Covers(safe_point_pos.PrevInstruction())) {
1415 cur = cur->next();
1416 }
1417 if (cur == NULL) continue;
1418
1419 // Check if the live range is spilled and the safe point is after
1420 // the spill position.
1421 if (range->HasAllocatedSpillOperand() &&
1422 safe_point >= range->spill_start_index()) {
1423 TraceAlloc("Pointer for range %d (spilled at %d) at safe point %d\n",
1424 range->id(), range->spill_start_index(), safe_point);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001425 map->RecordPointer(range->GetSpillOperand(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001426 }
1427
1428 if (!cur->IsSpilled()) {
1429 TraceAlloc("Pointer in register for range %d (start at %d) "
1430 "at safe point %d\n",
1431 cur->id(), cur->Start().Value(), safe_point);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001432 LOperand* operand = cur->CreateAssignedOperand(zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001433 ASSERT(!operand->IsStackSlot());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001434 map->RecordPointer(operand, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001435 }
1436 }
1437 }
1438}
1439
1440
1441void LAllocator::ProcessOsrEntry() {
1442 const ZoneList<LInstruction*>* instrs = chunk_->instructions();
1443
1444 // Linear search for the OSR entry instruction in the chunk.
1445 int index = -1;
1446 while (++index < instrs->length() &&
1447 !instrs->at(index)->IsOsrEntry()) {
1448 }
1449 ASSERT(index < instrs->length());
1450 LOsrEntry* instruction = LOsrEntry::cast(instrs->at(index));
1451
1452 LifetimePosition position = LifetimePosition::FromInstructionIndex(index);
1453 for (int i = 0; i < live_ranges()->length(); ++i) {
1454 LiveRange* range = live_ranges()->at(i);
1455 if (range != NULL) {
1456 if (range->Covers(position) &&
1457 range->HasRegisterAssigned() &&
1458 range->TopLevel()->HasAllocatedSpillOperand()) {
1459 int reg_index = range->assigned_register();
1460 LOperand* spill_operand = range->TopLevel()->GetSpillOperand();
1461 if (range->IsDouble()) {
1462 instruction->MarkSpilledDoubleRegister(reg_index, spill_operand);
1463 } else {
1464 instruction->MarkSpilledRegister(reg_index, spill_operand);
1465 }
1466 }
1467 }
1468 }
1469}
1470
1471
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001472void LAllocator::AllocateGeneralRegisters() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001473 HPhase phase("L_Allocate general registers", this);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001474 num_registers_ = Register::kNumAllocatableRegisters;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001475 AllocateRegisters();
1476}
1477
1478
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001479void LAllocator::AllocateDoubleRegisters() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001480 HPhase phase("L_Allocate double registers", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001481 num_registers_ = DoubleRegister::kNumAllocatableRegisters;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001482 mode_ = DOUBLE_REGISTERS;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001483 AllocateRegisters();
1484}
1485
1486
1487void LAllocator::AllocateRegisters() {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001488 ASSERT(unhandled_live_ranges_.is_empty());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001489
1490 for (int i = 0; i < live_ranges_.length(); ++i) {
1491 if (live_ranges_[i] != NULL) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001492 if (RequiredRegisterKind(live_ranges_[i]->id()) == mode_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001493 AddToUnhandledUnsorted(live_ranges_[i]);
1494 }
1495 }
1496 }
1497 SortUnhandled();
1498 ASSERT(UnhandledIsSorted());
1499
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001500 ASSERT(reusable_slots_.is_empty());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001501 ASSERT(active_live_ranges_.is_empty());
1502 ASSERT(inactive_live_ranges_.is_empty());
1503
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001504 if (mode_ == DOUBLE_REGISTERS) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001505 for (int i = 0; i < fixed_double_live_ranges_.length(); ++i) {
1506 LiveRange* current = fixed_double_live_ranges_.at(i);
1507 if (current != NULL) {
1508 AddToInactive(current);
1509 }
1510 }
1511 } else {
1512 for (int i = 0; i < fixed_live_ranges_.length(); ++i) {
1513 LiveRange* current = fixed_live_ranges_.at(i);
1514 if (current != NULL) {
1515 AddToInactive(current);
1516 }
1517 }
1518 }
1519
1520 while (!unhandled_live_ranges_.is_empty()) {
1521 ASSERT(UnhandledIsSorted());
1522 LiveRange* current = unhandled_live_ranges_.RemoveLast();
1523 ASSERT(UnhandledIsSorted());
1524 LifetimePosition position = current->Start();
1525 TraceAlloc("Processing interval %d start=%d\n",
1526 current->id(),
1527 position.Value());
1528
1529 if (current->HasAllocatedSpillOperand()) {
1530 TraceAlloc("Live range %d already has a spill operand\n", current->id());
1531 LifetimePosition next_pos = position;
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001532 if (IsGapAt(next_pos.InstructionIndex())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001533 next_pos = next_pos.NextInstruction();
1534 }
1535 UsePosition* pos = current->NextUsePositionRegisterIsBeneficial(next_pos);
1536 // If the range already has a spill operand and it doesn't need a
1537 // register immediately, split it and spill the first part of the range.
1538 if (pos == NULL) {
1539 Spill(current);
1540 continue;
1541 } else if (pos->pos().Value() >
1542 current->Start().NextInstruction().Value()) {
1543 // Do not spill live range eagerly if use position that can benefit from
1544 // the register is too close to the start of live range.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001545 SpillBetween(current, current->Start(), pos->pos());
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001546 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001547 ASSERT(UnhandledIsSorted());
1548 continue;
1549 }
1550 }
1551
1552 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1553 LiveRange* cur_active = active_live_ranges_.at(i);
1554 if (cur_active->End().Value() <= position.Value()) {
1555 ActiveToHandled(cur_active);
1556 --i; // The live range was removed from the list of active live ranges.
1557 } else if (!cur_active->Covers(position)) {
1558 ActiveToInactive(cur_active);
1559 --i; // The live range was removed from the list of active live ranges.
1560 }
1561 }
1562
1563 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1564 LiveRange* cur_inactive = inactive_live_ranges_.at(i);
1565 if (cur_inactive->End().Value() <= position.Value()) {
1566 InactiveToHandled(cur_inactive);
1567 --i; // Live range was removed from the list of inactive live ranges.
1568 } else if (cur_inactive->Covers(position)) {
1569 InactiveToActive(cur_inactive);
1570 --i; // Live range was removed from the list of inactive live ranges.
1571 }
1572 }
1573
1574 ASSERT(!current->HasRegisterAssigned() && !current->IsSpilled());
1575
1576 bool result = TryAllocateFreeReg(current);
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001577 if (!AllocationOk()) return;
1578
1579 if (!result) AllocateBlockedReg(current);
1580 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001581
1582 if (current->HasRegisterAssigned()) {
1583 AddToActive(current);
1584 }
1585 }
1586
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001587 reusable_slots_.Rewind(0);
1588 active_live_ranges_.Rewind(0);
1589 inactive_live_ranges_.Rewind(0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001590}
1591
1592
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001593const char* LAllocator::RegisterName(int allocation_index) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001594 if (mode_ == GENERAL_REGISTERS) {
1595 return Register::AllocationIndexToString(allocation_index);
1596 } else {
1597 return DoubleRegister::AllocationIndexToString(allocation_index);
1598 }
1599}
1600
1601
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001602void LAllocator::TraceAlloc(const char* msg, ...) {
1603 if (FLAG_trace_alloc) {
1604 va_list arguments;
1605 va_start(arguments, msg);
1606 OS::VPrint(msg, arguments);
1607 va_end(arguments);
1608 }
1609}
1610
1611
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001612bool LAllocator::HasTaggedValue(int virtual_register) const {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001613 HValue* value = graph_->LookupValue(virtual_register);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001614 if (value == NULL) return false;
1615 return value->representation().IsTagged();
1616}
1617
1618
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001619RegisterKind LAllocator::RequiredRegisterKind(int virtual_register) const {
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001620 if (virtual_register < first_artificial_register_) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001621 HValue* value = graph_->LookupValue(virtual_register);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001622 if (value != NULL && value->representation().IsDouble()) {
1623 return DOUBLE_REGISTERS;
1624 }
1625 } else if (double_artificial_registers_.Contains(
1626 virtual_register - first_artificial_register_)) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001627 return DOUBLE_REGISTERS;
1628 }
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001629
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001630 return GENERAL_REGISTERS;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001631}
1632
1633
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001634void LAllocator::AddToActive(LiveRange* range) {
1635 TraceAlloc("Add live range %d to active\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001636 active_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001637}
1638
1639
1640void LAllocator::AddToInactive(LiveRange* range) {
1641 TraceAlloc("Add live range %d to inactive\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001642 inactive_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001643}
1644
1645
1646void LAllocator::AddToUnhandledSorted(LiveRange* range) {
1647 if (range == NULL || range->IsEmpty()) return;
1648 ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled());
1649 for (int i = unhandled_live_ranges_.length() - 1; i >= 0; --i) {
1650 LiveRange* cur_range = unhandled_live_ranges_.at(i);
1651 if (range->ShouldBeAllocatedBefore(cur_range)) {
1652 TraceAlloc("Add live range %d to unhandled at %d\n", range->id(), i + 1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001653 unhandled_live_ranges_.InsertAt(i + 1, range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001654 ASSERT(UnhandledIsSorted());
1655 return;
1656 }
1657 }
1658 TraceAlloc("Add live range %d to unhandled at start\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001659 unhandled_live_ranges_.InsertAt(0, range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001660 ASSERT(UnhandledIsSorted());
1661}
1662
1663
1664void LAllocator::AddToUnhandledUnsorted(LiveRange* range) {
1665 if (range == NULL || range->IsEmpty()) return;
1666 ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled());
1667 TraceAlloc("Add live range %d to unhandled unsorted at end\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001668 unhandled_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001669}
1670
1671
1672static int UnhandledSortHelper(LiveRange* const* a, LiveRange* const* b) {
1673 ASSERT(!(*a)->ShouldBeAllocatedBefore(*b) ||
1674 !(*b)->ShouldBeAllocatedBefore(*a));
1675 if ((*a)->ShouldBeAllocatedBefore(*b)) return 1;
1676 if ((*b)->ShouldBeAllocatedBefore(*a)) return -1;
1677 return (*a)->id() - (*b)->id();
1678}
1679
1680
1681// Sort the unhandled live ranges so that the ranges to be processed first are
1682// at the end of the array list. This is convenient for the register allocation
1683// algorithm because it is efficient to remove elements from the end.
1684void LAllocator::SortUnhandled() {
1685 TraceAlloc("Sort unhandled\n");
1686 unhandled_live_ranges_.Sort(&UnhandledSortHelper);
1687}
1688
1689
1690bool LAllocator::UnhandledIsSorted() {
1691 int len = unhandled_live_ranges_.length();
1692 for (int i = 1; i < len; i++) {
1693 LiveRange* a = unhandled_live_ranges_.at(i - 1);
1694 LiveRange* b = unhandled_live_ranges_.at(i);
1695 if (a->Start().Value() < b->Start().Value()) return false;
1696 }
1697 return true;
1698}
1699
1700
1701void LAllocator::FreeSpillSlot(LiveRange* range) {
1702 // Check that we are the last range.
1703 if (range->next() != NULL) return;
1704
1705 if (!range->TopLevel()->HasAllocatedSpillOperand()) return;
1706
1707 int index = range->TopLevel()->GetSpillOperand()->index();
1708 if (index >= 0) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001709 reusable_slots_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001710 }
1711}
1712
1713
1714LOperand* LAllocator::TryReuseSpillSlot(LiveRange* range) {
1715 if (reusable_slots_.is_empty()) return NULL;
1716 if (reusable_slots_.first()->End().Value() >
1717 range->TopLevel()->Start().Value()) {
1718 return NULL;
1719 }
1720 LOperand* result = reusable_slots_.first()->TopLevel()->GetSpillOperand();
1721 reusable_slots_.Remove(0);
1722 return result;
1723}
1724
1725
1726void LAllocator::ActiveToHandled(LiveRange* range) {
1727 ASSERT(active_live_ranges_.Contains(range));
1728 active_live_ranges_.RemoveElement(range);
1729 TraceAlloc("Moving live range %d from active to handled\n", range->id());
1730 FreeSpillSlot(range);
1731}
1732
1733
1734void LAllocator::ActiveToInactive(LiveRange* range) {
1735 ASSERT(active_live_ranges_.Contains(range));
1736 active_live_ranges_.RemoveElement(range);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001737 inactive_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001738 TraceAlloc("Moving live range %d from active to inactive\n", range->id());
1739}
1740
1741
1742void LAllocator::InactiveToHandled(LiveRange* range) {
1743 ASSERT(inactive_live_ranges_.Contains(range));
1744 inactive_live_ranges_.RemoveElement(range);
1745 TraceAlloc("Moving live range %d from inactive to handled\n", range->id());
1746 FreeSpillSlot(range);
1747}
1748
1749
1750void LAllocator::InactiveToActive(LiveRange* range) {
1751 ASSERT(inactive_live_ranges_.Contains(range));
1752 inactive_live_ranges_.RemoveElement(range);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001753 active_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001754 TraceAlloc("Moving live range %d from inactive to active\n", range->id());
1755}
1756
1757
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001758// TryAllocateFreeReg and AllocateBlockedReg assume this
1759// when allocating local arrays.
1760STATIC_ASSERT(DoubleRegister::kNumAllocatableRegisters >=
1761 Register::kNumAllocatableRegisters);
1762
1763
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001764bool LAllocator::TryAllocateFreeReg(LiveRange* current) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001765 LifetimePosition free_until_pos[DoubleRegister::kNumAllocatableRegisters];
1766
1767 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
1768 free_until_pos[i] = LifetimePosition::MaxPosition();
1769 }
1770
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001771 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1772 LiveRange* cur_active = active_live_ranges_.at(i);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001773 free_until_pos[cur_active->assigned_register()] =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001774 LifetimePosition::FromInstructionIndex(0);
1775 }
1776
1777 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1778 LiveRange* cur_inactive = inactive_live_ranges_.at(i);
1779 ASSERT(cur_inactive->End().Value() > current->Start().Value());
1780 LifetimePosition next_intersection =
1781 cur_inactive->FirstIntersection(current);
1782 if (!next_intersection.IsValid()) continue;
1783 int cur_reg = cur_inactive->assigned_register();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001784 free_until_pos[cur_reg] = Min(free_until_pos[cur_reg], next_intersection);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001785 }
1786
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001787 UsePosition* hinted_use = current->FirstPosWithHint();
1788 if (hinted_use != NULL) {
1789 LOperand* hint = hinted_use->hint();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001790 if (hint->IsRegister() || hint->IsDoubleRegister()) {
1791 int register_index = hint->index();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001792 TraceAlloc(
1793 "Found reg hint %s (free until [%d) for live range %d (end %d[).\n",
1794 RegisterName(register_index),
1795 free_until_pos[register_index].Value(),
1796 current->id(),
1797 current->End().Value());
1798
1799 // The desired register is free until the end of the current live range.
1800 if (free_until_pos[register_index].Value() >= current->End().Value()) {
1801 TraceAlloc("Assigning preferred reg %s to live range %d\n",
1802 RegisterName(register_index),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001803 current->id());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001804 current->set_assigned_register(register_index, mode_, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001805 return true;
1806 }
1807 }
1808 }
1809
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001810 // Find the register which stays free for the longest time.
1811 int reg = 0;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001812 for (int i = 1; i < RegisterCount(); ++i) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001813 if (free_until_pos[i].Value() > free_until_pos[reg].Value()) {
1814 reg = i;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001815 }
1816 }
1817
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001818 LifetimePosition pos = free_until_pos[reg];
1819
1820 if (pos.Value() <= current->Start().Value()) {
1821 // All registers are blocked.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001822 return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001823 }
1824
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001825 if (pos.Value() < current->End().Value()) {
1826 // Register reg is available at the range start but becomes blocked before
1827 // the range end. Split current at position where it becomes blocked.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001828 LiveRange* tail = SplitRangeAt(current, pos);
1829 if (!AllocationOk()) return false;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001830 AddToUnhandledSorted(tail);
1831 }
1832
1833
1834 // Register reg is available at the range start and is free until
1835 // the range end.
1836 ASSERT(pos.Value() >= current->End().Value());
1837 TraceAlloc("Assigning free reg %s to live range %d\n",
1838 RegisterName(reg),
1839 current->id());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001840 current->set_assigned_register(reg, mode_, zone_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001841
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001842 return true;
1843}
1844
1845
1846void LAllocator::AllocateBlockedReg(LiveRange* current) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001847 UsePosition* register_use = current->NextRegisterPosition(current->Start());
1848 if (register_use == NULL) {
1849 // There is no use in the current live range that requires a register.
1850 // We can just spill it.
1851 Spill(current);
1852 return;
1853 }
1854
1855
1856 LifetimePosition use_pos[DoubleRegister::kNumAllocatableRegisters];
1857 LifetimePosition block_pos[DoubleRegister::kNumAllocatableRegisters];
1858
1859 for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; i++) {
1860 use_pos[i] = block_pos[i] = LifetimePosition::MaxPosition();
1861 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001862
1863 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1864 LiveRange* range = active_live_ranges_[i];
1865 int cur_reg = range->assigned_register();
1866 if (range->IsFixed() || !range->CanBeSpilled(current->Start())) {
1867 block_pos[cur_reg] = use_pos[cur_reg] =
1868 LifetimePosition::FromInstructionIndex(0);
1869 } else {
1870 UsePosition* next_use = range->NextUsePositionRegisterIsBeneficial(
1871 current->Start());
1872 if (next_use == NULL) {
1873 use_pos[cur_reg] = range->End();
1874 } else {
1875 use_pos[cur_reg] = next_use->pos();
1876 }
1877 }
1878 }
1879
1880 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1881 LiveRange* range = inactive_live_ranges_.at(i);
1882 ASSERT(range->End().Value() > current->Start().Value());
1883 LifetimePosition next_intersection = range->FirstIntersection(current);
1884 if (!next_intersection.IsValid()) continue;
1885 int cur_reg = range->assigned_register();
1886 if (range->IsFixed()) {
1887 block_pos[cur_reg] = Min(block_pos[cur_reg], next_intersection);
1888 use_pos[cur_reg] = Min(block_pos[cur_reg], use_pos[cur_reg]);
1889 } else {
1890 use_pos[cur_reg] = Min(use_pos[cur_reg], next_intersection);
1891 }
1892 }
1893
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001894 int reg = 0;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001895 for (int i = 1; i < RegisterCount(); ++i) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001896 if (use_pos[i].Value() > use_pos[reg].Value()) {
1897 reg = i;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001898 }
1899 }
1900
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001901 LifetimePosition pos = use_pos[reg];
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001902
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001903 if (pos.Value() < register_use->pos().Value()) {
1904 // All registers are blocked before the first use that requires a register.
1905 // Spill starting part of live range up to that use.
1906 //
1907 // Corner case: the first use position is equal to the start of the range.
1908 // In this case we have nothing to spill and SpillBetween will just return
1909 // this range to the list of unhandled ones. This will lead to the infinite
1910 // loop.
1911 ASSERT(current->Start().Value() < register_use->pos().Value());
1912 SpillBetween(current, current->Start(), register_use->pos());
1913 return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001914 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001915
1916 if (block_pos[reg].Value() < current->End().Value()) {
1917 // Register becomes blocked before the current range end. Split before that
1918 // position.
1919 LiveRange* tail = SplitBetween(current,
1920 current->Start(),
1921 block_pos[reg].InstructionStart());
1922 AddToUnhandledSorted(tail);
1923 }
1924
1925 // Register reg is not blocked for the whole range.
1926 ASSERT(block_pos[reg].Value() >= current->End().Value());
1927 TraceAlloc("Assigning blocked reg %s to live range %d\n",
1928 RegisterName(reg),
1929 current->id());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001930 current->set_assigned_register(reg, mode_, zone_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001931
1932 // This register was not free. Thus we need to find and spill
1933 // parts of active and inactive live regions that use the same register
1934 // at the same lifetime positions as current.
1935 SplitAndSpillIntersecting(current);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001936}
1937
1938
1939void LAllocator::SplitAndSpillIntersecting(LiveRange* current) {
1940 ASSERT(current->HasRegisterAssigned());
1941 int reg = current->assigned_register();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001942 LifetimePosition split_pos = current->Start();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001943 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1944 LiveRange* range = active_live_ranges_[i];
1945 if (range->assigned_register() == reg) {
1946 UsePosition* next_pos = range->NextRegisterPosition(current->Start());
1947 if (next_pos == NULL) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001948 SpillAfter(range, split_pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001949 } else {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001950 SpillBetween(range, split_pos, next_pos->pos());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001951 }
1952 ActiveToHandled(range);
1953 --i;
1954 }
1955 }
1956
1957 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1958 LiveRange* range = inactive_live_ranges_[i];
1959 ASSERT(range->End().Value() > current->Start().Value());
1960 if (range->assigned_register() == reg && !range->IsFixed()) {
1961 LifetimePosition next_intersection = range->FirstIntersection(current);
1962 if (next_intersection.IsValid()) {
1963 UsePosition* next_pos = range->NextRegisterPosition(current->Start());
1964 if (next_pos == NULL) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001965 SpillAfter(range, split_pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001966 } else {
1967 next_intersection = Min(next_intersection, next_pos->pos());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001968 SpillBetween(range, split_pos, next_intersection);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001969 }
1970 InactiveToHandled(range);
1971 --i;
1972 }
1973 }
1974 }
1975}
1976
1977
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001978bool LAllocator::IsBlockBoundary(LifetimePosition pos) {
1979 return pos.IsInstructionStart() &&
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001980 InstructionAt(pos.InstructionIndex())->IsLabel();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001981}
1982
1983
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001984LiveRange* LAllocator::SplitRangeAt(LiveRange* range, LifetimePosition pos) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001985 ASSERT(!range->IsFixed());
1986 TraceAlloc("Splitting live range %d at %d\n", range->id(), pos.Value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001987
1988 if (pos.Value() <= range->Start().Value()) return range;
1989
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001990 // We can't properly connect liveranges if split occured at the end
1991 // of control instruction.
1992 ASSERT(pos.IsInstructionStart() ||
1993 !chunk_->instructions()->at(pos.InstructionIndex())->IsControl());
1994
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001995 LiveRange* result = LiveRangeFor(GetVirtualRegister());
1996 if (!AllocationOk()) return NULL;
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001997 range->SplitAt(pos, result, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001998 return result;
1999}
2000
2001
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002002LiveRange* LAllocator::SplitBetween(LiveRange* range,
2003 LifetimePosition start,
2004 LifetimePosition end) {
2005 ASSERT(!range->IsFixed());
2006 TraceAlloc("Splitting live range %d in position between [%d, %d]\n",
2007 range->id(),
2008 start.Value(),
2009 end.Value());
2010
2011 LifetimePosition split_pos = FindOptimalSplitPos(start, end);
2012 ASSERT(split_pos.Value() >= start.Value());
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002013 return SplitRangeAt(range, split_pos);
kmillikin@chromium.orgfacd82d2010-12-13 18:00:51 +00002014}
2015
2016
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002017LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start,
2018 LifetimePosition end) {
2019 int start_instr = start.InstructionIndex();
2020 int end_instr = end.InstructionIndex();
2021 ASSERT(start_instr <= end_instr);
2022
2023 // We have no choice
2024 if (start_instr == end_instr) return end;
2025
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002026 HBasicBlock* start_block = GetBlock(start);
2027 HBasicBlock* end_block = GetBlock(end);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002028
2029 if (end_block == start_block) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002030 // The interval is split in the same basic block. Split at the latest
2031 // possible position.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002032 return end;
2033 }
2034
2035 HBasicBlock* block = end_block;
2036 // Find header of outermost loop.
2037 while (block->parent_loop_header() != NULL &&
2038 block->parent_loop_header()->block_id() > start_block->block_id()) {
2039 block = block->parent_loop_header();
2040 }
2041
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002042 // We did not find any suitable outer loop. Split at the latest possible
2043 // position unless end_block is a loop header itself.
2044 if (block == end_block && !end_block->IsLoopHeader()) return end;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002045
2046 return LifetimePosition::FromInstructionIndex(
2047 block->first_instruction_index());
2048}
2049
2050
2051void LAllocator::SpillAfter(LiveRange* range, LifetimePosition pos) {
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002052 LiveRange* second_part = SplitRangeAt(range, pos);
2053 if (!AllocationOk()) return;
lrn@chromium.org8541d772010-12-15 12:05:09 +00002054 Spill(second_part);
2055}
2056
2057
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002058void LAllocator::SpillBetween(LiveRange* range,
2059 LifetimePosition start,
2060 LifetimePosition end) {
2061 ASSERT(start.Value() < end.Value());
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002062 LiveRange* second_part = SplitRangeAt(range, start);
2063 if (!AllocationOk()) return;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002064
2065 if (second_part->Start().Value() < end.Value()) {
2066 // The split result intersects with [start, end[.
2067 // Split it at position between ]start+1, end[, spill the middle part
2068 // and put the rest to unhandled.
2069 LiveRange* third_part = SplitBetween(
2070 second_part,
2071 second_part->Start().InstructionEnd(),
2072 end.PrevInstruction().InstructionEnd());
2073
2074 ASSERT(third_part != second_part);
2075
2076 Spill(second_part);
2077 AddToUnhandledSorted(third_part);
2078 } else {
2079 // The split result does not intersect with [start, end[.
2080 // Nothing to spill. Just put it to unhandled as whole.
2081 AddToUnhandledSorted(second_part);
2082 }
2083}
2084
2085
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002086void LAllocator::Spill(LiveRange* range) {
2087 ASSERT(!range->IsSpilled());
2088 TraceAlloc("Spilling live range %d\n", range->id());
2089 LiveRange* first = range->TopLevel();
2090
2091 if (!first->HasAllocatedSpillOperand()) {
2092 LOperand* op = TryReuseSpillSlot(range);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002093 if (op == NULL) op = chunk_->GetNextSpillSlot(mode_ == DOUBLE_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002094 first->SetSpillOperand(op);
2095 }
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00002096 range->MakeSpilled(zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002097}
2098
2099
2100int LAllocator::RegisterCount() const {
2101 return num_registers_;
2102}
2103
2104
2105#ifdef DEBUG
2106
2107
2108void LAllocator::Verify() const {
2109 for (int i = 0; i < live_ranges()->length(); ++i) {
2110 LiveRange* current = live_ranges()->at(i);
2111 if (current != NULL) current->Verify();
2112 }
2113}
2114
2115
2116#endif
2117
2118
2119} } // namespace v8::internal