blob: 74132b3b76a6138fa530aa46f72fce9883f1585e [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
danno@chromium.orgf005df62013-04-30 16:36:45 +000059UsePosition::UsePosition(LifetimePosition pos,
60 LOperand* operand,
61 LOperand* hint)
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000062 : operand_(operand),
danno@chromium.orgf005df62013-04-30 16:36:45 +000063 hint_(hint),
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000064 pos_(pos),
65 next_(NULL),
66 requires_reg_(false),
67 register_beneficial_(true) {
68 if (operand_ != NULL && operand_->IsUnallocated()) {
69 LUnallocated* unalloc = LUnallocated::cast(operand_);
70 requires_reg_ = unalloc->HasRegisterPolicy();
71 register_beneficial_ = !unalloc->HasAnyPolicy();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000072 }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000073 ASSERT(pos_.IsValid());
kasperl@chromium.orga5551262010-12-07 12:49:48 +000074}
75
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000076
77bool UsePosition::HasHint() const {
78 return hint_ != NULL && !hint_->IsUnallocated();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000079}
80
81
82bool UsePosition::RequiresRegister() const {
83 return requires_reg_;
84}
85
86
87bool UsePosition::RegisterIsBeneficial() const {
88 return register_beneficial_;
89}
90
91
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +000092void UseInterval::SplitAt(LifetimePosition pos, Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000093 ASSERT(Contains(pos) && pos.Value() != start().Value());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +000094 UseInterval* after = new(zone) UseInterval(pos, end_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000095 after->next_ = next_;
96 next_ = after;
97 end_ = pos;
98}
99
100
101#ifdef DEBUG
102
103
104void LiveRange::Verify() const {
105 UsePosition* cur = first_pos_;
106 while (cur != NULL) {
107 ASSERT(Start().Value() <= cur->pos().Value() &&
108 cur->pos().Value() <= End().Value());
109 cur = cur->next();
110 }
111}
112
113
114bool LiveRange::HasOverlap(UseInterval* target) const {
115 UseInterval* current_interval = first_interval_;
116 while (current_interval != NULL) {
117 // Intervals overlap if the start of one is contained in the other.
118 if (current_interval->Contains(target->start()) ||
119 target->Contains(current_interval->start())) {
120 return true;
121 }
122 current_interval = current_interval->next();
123 }
124 return false;
125}
126
127
128#endif
129
130
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000131LiveRange::LiveRange(int id, Zone* zone)
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000132 : id_(id),
133 spilled_(false),
danno@chromium.org2c456792011-11-11 12:00:53 +0000134 is_double_(false),
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000135 assigned_register_(kInvalidAssignment),
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000136 last_interval_(NULL),
137 first_interval_(NULL),
138 first_pos_(NULL),
139 parent_(NULL),
140 next_(NULL),
141 current_interval_(NULL),
142 last_processed_use_(NULL),
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000143 current_hint_operand_(NULL),
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000144 spill_operand_(new(zone) LOperand()),
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000145 spill_start_index_(kMaxInt) { }
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000146
147
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000148void LiveRange::set_assigned_register(int reg,
149 RegisterKind register_kind,
150 Zone* zone) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000151 ASSERT(!HasRegisterAssigned() && !IsSpilled());
152 assigned_register_ = reg;
danno@chromium.org2c456792011-11-11 12:00:53 +0000153 is_double_ = (register_kind == DOUBLE_REGISTERS);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000154 ConvertOperands(zone);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000155}
156
157
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000158void LiveRange::MakeSpilled(Zone* zone) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000159 ASSERT(!IsSpilled());
160 ASSERT(TopLevel()->HasAllocatedSpillOperand());
161 spilled_ = true;
162 assigned_register_ = kInvalidAssignment;
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000163 ConvertOperands(zone);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000164}
165
166
167bool LiveRange::HasAllocatedSpillOperand() const {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000168 ASSERT(spill_operand_ != NULL);
169 return !spill_operand_->IsIgnored();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000170}
171
172
173void LiveRange::SetSpillOperand(LOperand* operand) {
174 ASSERT(!operand->IsUnallocated());
175 ASSERT(spill_operand_ != NULL);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000176 ASSERT(spill_operand_->IsIgnored());
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000177 spill_operand_->ConvertTo(operand->kind(), operand->index());
178}
179
180
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000181UsePosition* LiveRange::NextUsePosition(LifetimePosition start) {
182 UsePosition* use_pos = last_processed_use_;
183 if (use_pos == NULL) use_pos = first_pos();
184 while (use_pos != NULL && use_pos->pos().Value() < start.Value()) {
185 use_pos = use_pos->next();
186 }
187 last_processed_use_ = use_pos;
188 return use_pos;
189}
190
191
192UsePosition* LiveRange::NextUsePositionRegisterIsBeneficial(
193 LifetimePosition start) {
194 UsePosition* pos = NextUsePosition(start);
195 while (pos != NULL && !pos->RegisterIsBeneficial()) {
196 pos = pos->next();
197 }
198 return pos;
199}
200
201
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000202UsePosition* LiveRange::PreviousUsePositionRegisterIsBeneficial(
203 LifetimePosition start) {
204 UsePosition* pos = first_pos();
205 UsePosition* prev = NULL;
206 while (pos != NULL && pos->pos().Value() < start.Value()) {
207 if (pos->RegisterIsBeneficial()) prev = pos;
208 pos = pos->next();
209 }
210 return prev;
211}
212
213
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000214UsePosition* LiveRange::NextRegisterPosition(LifetimePosition start) {
215 UsePosition* pos = NextUsePosition(start);
216 while (pos != NULL && !pos->RequiresRegister()) {
217 pos = pos->next();
218 }
219 return pos;
220}
221
222
223bool LiveRange::CanBeSpilled(LifetimePosition pos) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000224 // We cannot spill a live range that has a use requiring a register
225 // at the current or the immediate next position.
226 UsePosition* use_pos = NextRegisterPosition(pos);
227 if (use_pos == NULL) return true;
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +0000228 return
229 use_pos->pos().Value() > pos.NextInstruction().InstructionEnd().Value();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000230}
231
232
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000233LOperand* LiveRange::CreateAssignedOperand(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000234 LOperand* op = NULL;
235 if (HasRegisterAssigned()) {
236 ASSERT(!IsSpilled());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000237 if (IsDouble()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000238 op = LDoubleRegister::Create(assigned_register(), zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000239 } else {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000240 op = LRegister::Create(assigned_register(), zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000241 }
242 } else if (IsSpilled()) {
243 ASSERT(!HasRegisterAssigned());
244 op = TopLevel()->GetSpillOperand();
245 ASSERT(!op->IsUnallocated());
246 } else {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000247 LUnallocated* unalloc = new(zone) LUnallocated(LUnallocated::NONE);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000248 unalloc->set_virtual_register(id_);
249 op = unalloc;
250 }
251 return op;
252}
253
254
255UseInterval* LiveRange::FirstSearchIntervalForPosition(
256 LifetimePosition position) const {
257 if (current_interval_ == NULL) return first_interval_;
258 if (current_interval_->start().Value() > position.Value()) {
259 current_interval_ = NULL;
260 return first_interval_;
261 }
262 return current_interval_;
263}
264
265
266void LiveRange::AdvanceLastProcessedMarker(
267 UseInterval* to_start_of, LifetimePosition but_not_past) const {
268 if (to_start_of == NULL) return;
269 if (to_start_of->start().Value() > but_not_past.Value()) return;
270 LifetimePosition start =
271 current_interval_ == NULL ? LifetimePosition::Invalid()
272 : current_interval_->start();
273 if (to_start_of->start().Value() > start.Value()) {
274 current_interval_ = to_start_of;
275 }
276}
277
278
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000279void LiveRange::SplitAt(LifetimePosition position,
280 LiveRange* result,
281 Zone* zone) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000282 ASSERT(Start().Value() < position.Value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000283 ASSERT(result->IsEmpty());
284 // Find the last interval that ends before the position. If the
285 // position is contained in one of the intervals in the chain, we
286 // split that interval and use the first part.
287 UseInterval* current = FirstSearchIntervalForPosition(position);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000288
289 // If the split position coincides with the beginning of a use interval
290 // we need to split use positons in a special way.
291 bool split_at_start = false;
292
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000293 if (current->start().Value() == position.Value()) {
294 // When splitting at start we need to locate the previous use interval.
295 current = first_interval_;
296 }
297
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000298 while (current != NULL) {
299 if (current->Contains(position)) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000300 current->SplitAt(position, zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000301 break;
302 }
303 UseInterval* next = current->next();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000304 if (next->start().Value() >= position.Value()) {
305 split_at_start = (next->start().Value() == position.Value());
306 break;
307 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000308 current = next;
309 }
310
311 // Partition original use intervals to the two live ranges.
312 UseInterval* before = current;
313 UseInterval* after = before->next();
314 result->last_interval_ = (last_interval_ == before)
315 ? after // Only interval in the range after split.
316 : last_interval_; // Last interval of the original range.
317 result->first_interval_ = after;
318 last_interval_ = before;
319
320 // Find the last use position before the split and the first use
321 // position after it.
322 UsePosition* use_after = first_pos_;
323 UsePosition* use_before = NULL;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000324 if (split_at_start) {
325 // The split position coincides with the beginning of a use interval (the
326 // end of a lifetime hole). Use at this position should be attributed to
327 // the split child because split child owns use interval covering it.
328 while (use_after != NULL && use_after->pos().Value() < position.Value()) {
329 use_before = use_after;
330 use_after = use_after->next();
331 }
332 } else {
333 while (use_after != NULL && use_after->pos().Value() <= position.Value()) {
334 use_before = use_after;
335 use_after = use_after->next();
336 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000337 }
338
339 // Partition original use positions to the two live ranges.
340 if (use_before != NULL) {
341 use_before->next_ = NULL;
342 } else {
343 first_pos_ = NULL;
344 }
345 result->first_pos_ = use_after;
346
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000347 // Discard cached iteration state. It might be pointing
348 // to the use that no longer belongs to this live range.
349 last_processed_use_ = NULL;
350 current_interval_ = NULL;
351
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000352 // Link the new live range in the chain before any of the other
353 // ranges linked from the range before the split.
354 result->parent_ = (parent_ == NULL) ? this : parent_;
355 result->next_ = next_;
356 next_ = result;
357
358#ifdef DEBUG
359 Verify();
360 result->Verify();
361#endif
362}
363
364
365// This implements an ordering on live ranges so that they are ordered by their
366// start positions. This is needed for the correctness of the register
367// allocation algorithm. If two live ranges start at the same offset then there
368// is a tie breaker based on where the value is first used. This part of the
369// ordering is merely a heuristic.
370bool LiveRange::ShouldBeAllocatedBefore(const LiveRange* other) const {
371 LifetimePosition start = Start();
372 LifetimePosition other_start = other->Start();
373 if (start.Value() == other_start.Value()) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000374 UsePosition* pos = first_pos();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000375 if (pos == NULL) return false;
376 UsePosition* other_pos = other->first_pos();
377 if (other_pos == NULL) return true;
378 return pos->pos().Value() < other_pos->pos().Value();
379 }
380 return start.Value() < other_start.Value();
381}
382
383
384void LiveRange::ShortenTo(LifetimePosition start) {
385 LAllocator::TraceAlloc("Shorten live range %d to [%d\n", id_, start.Value());
386 ASSERT(first_interval_ != NULL);
387 ASSERT(first_interval_->start().Value() <= start.Value());
388 ASSERT(start.Value() < first_interval_->end().Value());
389 first_interval_->set_start(start);
390}
391
392
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000393void LiveRange::EnsureInterval(LifetimePosition start,
394 LifetimePosition end,
395 Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000396 LAllocator::TraceAlloc("Ensure live range %d in interval [%d %d[\n",
397 id_,
398 start.Value(),
399 end.Value());
400 LifetimePosition new_end = end;
401 while (first_interval_ != NULL &&
402 first_interval_->start().Value() <= end.Value()) {
403 if (first_interval_->end().Value() > end.Value()) {
404 new_end = first_interval_->end();
405 }
406 first_interval_ = first_interval_->next();
407 }
408
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000409 UseInterval* new_interval = new(zone) UseInterval(start, new_end);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000410 new_interval->next_ = first_interval_;
411 first_interval_ = new_interval;
412 if (new_interval->next() == NULL) {
413 last_interval_ = new_interval;
414 }
415}
416
417
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000418void LiveRange::AddUseInterval(LifetimePosition start,
419 LifetimePosition end,
420 Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000421 LAllocator::TraceAlloc("Add to live range %d interval [%d %d[\n",
422 id_,
423 start.Value(),
424 end.Value());
425 if (first_interval_ == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000426 UseInterval* interval = new(zone) UseInterval(start, end);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000427 first_interval_ = interval;
428 last_interval_ = interval;
429 } else {
430 if (end.Value() == first_interval_->start().Value()) {
431 first_interval_->set_start(start);
432 } else if (end.Value() < first_interval_->start().Value()) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000433 UseInterval* interval = new(zone) UseInterval(start, end);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000434 interval->set_next(first_interval_);
435 first_interval_ = interval;
436 } else {
437 // Order of instruction's processing (see ProcessInstructions) guarantees
438 // that each new use interval either precedes or intersects with
439 // last added interval.
440 ASSERT(start.Value() < first_interval_->end().Value());
441 first_interval_->start_ = Min(start, first_interval_->start_);
442 first_interval_->end_ = Max(end, first_interval_->end_);
443 }
444 }
445}
446
447
danno@chromium.orgf005df62013-04-30 16:36:45 +0000448void LiveRange::AddUsePosition(LifetimePosition pos,
449 LOperand* operand,
450 LOperand* hint,
451 Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000452 LAllocator::TraceAlloc("Add to live range %d use position %d\n",
453 id_,
454 pos.Value());
danno@chromium.orgf005df62013-04-30 16:36:45 +0000455 UsePosition* use_pos = new(zone) UsePosition(pos, operand, hint);
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000456 UsePosition* prev_hint = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000457 UsePosition* prev = NULL;
458 UsePosition* current = first_pos_;
459 while (current != NULL && current->pos().Value() < pos.Value()) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000460 prev_hint = current->HasHint() ? current : prev_hint;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000461 prev = current;
462 current = current->next();
463 }
464
465 if (prev == NULL) {
466 use_pos->set_next(first_pos_);
467 first_pos_ = use_pos;
468 } else {
469 use_pos->next_ = prev->next_;
470 prev->next_ = use_pos;
471 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000472
473 if (prev_hint == NULL && use_pos->HasHint()) {
474 current_hint_operand_ = hint;
475 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000476}
477
478
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000479void LiveRange::ConvertOperands(Zone* zone) {
480 LOperand* op = CreateAssignedOperand(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000481 UsePosition* use_pos = first_pos();
482 while (use_pos != NULL) {
483 ASSERT(Start().Value() <= use_pos->pos().Value() &&
484 use_pos->pos().Value() <= End().Value());
485
486 if (use_pos->HasOperand()) {
487 ASSERT(op->IsRegister() || op->IsDoubleRegister() ||
488 !use_pos->RequiresRegister());
489 use_pos->operand()->ConvertTo(op->kind(), op->index());
490 }
491 use_pos = use_pos->next();
492 }
493}
494
495
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000496bool LiveRange::CanCover(LifetimePosition position) const {
497 if (IsEmpty()) return false;
498 return Start().Value() <= position.Value() &&
499 position.Value() < End().Value();
500}
501
502
503bool LiveRange::Covers(LifetimePosition position) {
504 if (!CanCover(position)) return false;
505 UseInterval* start_search = FirstSearchIntervalForPosition(position);
506 for (UseInterval* interval = start_search;
507 interval != NULL;
508 interval = interval->next()) {
509 ASSERT(interval->next() == NULL ||
510 interval->next()->start().Value() >= interval->start().Value());
511 AdvanceLastProcessedMarker(interval, position);
512 if (interval->Contains(position)) return true;
513 if (interval->start().Value() > position.Value()) return false;
514 }
515 return false;
516}
517
518
519LifetimePosition LiveRange::FirstIntersection(LiveRange* other) {
520 UseInterval* b = other->first_interval();
521 if (b == NULL) return LifetimePosition::Invalid();
522 LifetimePosition advance_last_processed_up_to = b->start();
523 UseInterval* a = FirstSearchIntervalForPosition(b->start());
524 while (a != NULL && b != NULL) {
525 if (a->start().Value() > other->End().Value()) break;
526 if (b->start().Value() > End().Value()) break;
527 LifetimePosition cur_intersection = a->Intersect(b);
528 if (cur_intersection.IsValid()) {
529 return cur_intersection;
530 }
531 if (a->start().Value() < b->start().Value()) {
532 a = a->next();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000533 if (a == NULL || a->start().Value() > other->End().Value()) break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000534 AdvanceLastProcessedMarker(a, advance_last_processed_up_to);
535 } else {
536 b = b->next();
537 }
538 }
539 return LifetimePosition::Invalid();
540}
541
542
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000543LAllocator::LAllocator(int num_values, HGraph* graph)
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000544 : zone_(graph->zone()),
545 chunk_(NULL),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000546 live_in_sets_(graph->blocks()->length(), zone_),
547 live_ranges_(num_values * 2, zone_),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000548 fixed_live_ranges_(NULL),
549 fixed_double_live_ranges_(NULL),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000550 unhandled_live_ranges_(num_values * 2, zone_),
551 active_live_ranges_(8, zone_),
552 inactive_live_ranges_(8, zone_),
553 reusable_slots_(8, zone_),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000554 next_virtual_register_(num_values),
555 first_artificial_register_(num_values),
danno@chromium.org2c456792011-11-11 12:00:53 +0000556 mode_(GENERAL_REGISTERS),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000557 num_registers_(-1),
558 graph_(graph),
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000559 has_osr_entry_(false),
560 allocation_ok_(true) { }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000561
562
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000563void LAllocator::InitializeLivenessAnalysis() {
564 // Initialize the live_in sets for each block to NULL.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000565 int block_count = graph_->blocks()->length();
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000566 live_in_sets_.Initialize(block_count, zone());
567 live_in_sets_.AddBlock(NULL, block_count, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000568}
569
570
571BitVector* LAllocator::ComputeLiveOut(HBasicBlock* block) {
572 // Compute live out for the given block, except not including backward
573 // successor edges.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000574 BitVector* live_out = new(zone_) BitVector(next_virtual_register_, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000575
576 // Process all successor blocks.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000577 for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000578 // Add values live on entry to the successor. Note the successor's
579 // live_in will not be computed yet for backwards edges.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000580 HBasicBlock* successor = it.Current();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000581 BitVector* live_in = live_in_sets_[successor->block_id()];
582 if (live_in != NULL) live_out->Union(*live_in);
583
584 // All phi input operands corresponding to this successor edge are live
585 // out from this block.
586 int index = successor->PredecessorIndexOf(block);
587 const ZoneList<HPhi*>* phis = successor->phis();
588 for (int i = 0; i < phis->length(); ++i) {
589 HPhi* phi = phis->at(i);
590 if (!phi->OperandAt(index)->IsConstant()) {
591 live_out->Add(phi->OperandAt(index)->id());
592 }
593 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000594 }
595
596 return live_out;
597}
598
599
600void LAllocator::AddInitialIntervals(HBasicBlock* block,
601 BitVector* live_out) {
602 // Add an interval that includes the entire block to the live range for
603 // each live_out value.
604 LifetimePosition start = LifetimePosition::FromInstructionIndex(
605 block->first_instruction_index());
606 LifetimePosition end = LifetimePosition::FromInstructionIndex(
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000607 block->last_instruction_index()).NextInstruction();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000608 BitVector::Iterator iterator(live_out);
609 while (!iterator.Done()) {
610 int operand_index = iterator.Current();
611 LiveRange* range = LiveRangeFor(operand_index);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000612 range->AddUseInterval(start, end, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000613 iterator.Advance();
614 }
615}
616
617
618int LAllocator::FixedDoubleLiveRangeID(int index) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000619 return -index - 1 - Register::kMaxNumAllocatableRegisters;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000620}
621
622
623LOperand* LAllocator::AllocateFixed(LUnallocated* operand,
624 int pos,
625 bool is_tagged) {
626 TraceAlloc("Allocating fixed reg for op %d\n", operand->virtual_register());
627 ASSERT(operand->HasFixedPolicy());
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000628 if (operand->HasFixedSlotPolicy()) {
629 operand->ConvertTo(LOperand::STACK_SLOT, operand->fixed_slot_index());
630 } else if (operand->HasFixedRegisterPolicy()) {
631 int reg_index = operand->fixed_register_index();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000632 operand->ConvertTo(LOperand::REGISTER, reg_index);
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000633 } else if (operand->HasFixedDoubleRegisterPolicy()) {
634 int reg_index = operand->fixed_register_index();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000635 operand->ConvertTo(LOperand::DOUBLE_REGISTER, reg_index);
636 } else {
637 UNREACHABLE();
638 }
639 if (is_tagged) {
640 TraceAlloc("Fixed reg is tagged at %d\n", pos);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000641 LInstruction* instr = InstructionAt(pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000642 if (instr->HasPointerMap()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000643 instr->pointer_map()->RecordPointer(operand, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000644 }
645 }
646 return operand;
647}
648
649
650LiveRange* LAllocator::FixedLiveRangeFor(int index) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000651 ASSERT(index < Register::kMaxNumAllocatableRegisters);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000652 LiveRange* result = fixed_live_ranges_[index];
653 if (result == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000654 result = new(zone_) LiveRange(FixedLiveRangeID(index), zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000655 ASSERT(result->IsFixed());
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000656 SetLiveRangeAssignedRegister(result, index, GENERAL_REGISTERS, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000657 fixed_live_ranges_[index] = result;
658 }
659 return result;
660}
661
662
663LiveRange* LAllocator::FixedDoubleLiveRangeFor(int index) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000664 ASSERT(index < DoubleRegister::NumAllocatableRegisters());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000665 LiveRange* result = fixed_double_live_ranges_[index];
666 if (result == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000667 result = new(zone_) LiveRange(FixedDoubleLiveRangeID(index), zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000668 ASSERT(result->IsFixed());
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000669 SetLiveRangeAssignedRegister(result, index, DOUBLE_REGISTERS, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000670 fixed_double_live_ranges_[index] = result;
671 }
672 return result;
673}
674
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000675
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000676LiveRange* LAllocator::LiveRangeFor(int index) {
677 if (index >= live_ranges_.length()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000678 live_ranges_.AddBlock(NULL, index - live_ranges_.length() + 1, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000679 }
680 LiveRange* result = live_ranges_[index];
681 if (result == NULL) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000682 result = new(zone_) LiveRange(index, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000683 live_ranges_[index] = result;
684 }
685 return result;
686}
687
688
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000689LGap* LAllocator::GetLastGap(HBasicBlock* block) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000690 int last_instruction = block->last_instruction_index();
691 int index = chunk_->NearestGapPos(last_instruction);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000692 return GapAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000693}
694
695
696HPhi* LAllocator::LookupPhi(LOperand* operand) const {
697 if (!operand->IsUnallocated()) return NULL;
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000698 int index = LUnallocated::cast(operand)->virtual_register();
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000699 HValue* instr = graph_->LookupValue(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000700 if (instr != NULL && instr->IsPhi()) {
701 return HPhi::cast(instr);
702 }
703 return NULL;
704}
705
706
707LiveRange* LAllocator::LiveRangeFor(LOperand* operand) {
708 if (operand->IsUnallocated()) {
709 return LiveRangeFor(LUnallocated::cast(operand)->virtual_register());
710 } else if (operand->IsRegister()) {
711 return FixedLiveRangeFor(operand->index());
712 } else if (operand->IsDoubleRegister()) {
713 return FixedDoubleLiveRangeFor(operand->index());
714 } else {
715 return NULL;
716 }
717}
718
719
720void LAllocator::Define(LifetimePosition position,
721 LOperand* operand,
722 LOperand* hint) {
723 LiveRange* range = LiveRangeFor(operand);
724 if (range == NULL) return;
725
726 if (range->IsEmpty() || range->Start().Value() > position.Value()) {
727 // Can happen if there is a definition without use.
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000728 range->AddUseInterval(position, position.NextInstruction(), zone_);
danno@chromium.orgf005df62013-04-30 16:36:45 +0000729 range->AddUsePosition(position.NextInstruction(), NULL, NULL, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000730 } else {
731 range->ShortenTo(position);
732 }
733
734 if (operand->IsUnallocated()) {
735 LUnallocated* unalloc_operand = LUnallocated::cast(operand);
danno@chromium.orgf005df62013-04-30 16:36:45 +0000736 range->AddUsePosition(position, unalloc_operand, hint, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000737 }
738}
739
740
741void LAllocator::Use(LifetimePosition block_start,
742 LifetimePosition position,
743 LOperand* operand,
744 LOperand* hint) {
745 LiveRange* range = LiveRangeFor(operand);
746 if (range == NULL) return;
747 if (operand->IsUnallocated()) {
748 LUnallocated* unalloc_operand = LUnallocated::cast(operand);
danno@chromium.orgf005df62013-04-30 16:36:45 +0000749 range->AddUsePosition(position, unalloc_operand, hint, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000750 }
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000751 range->AddUseInterval(block_start, position, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000752}
753
754
755void LAllocator::AddConstraintsGapMove(int index,
756 LOperand* from,
757 LOperand* to) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000758 LGap* gap = GapAt(index);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000759 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000760 if (from->IsUnallocated()) {
761 const ZoneList<LMoveOperands>* move_operands = move->move_operands();
762 for (int i = 0; i < move_operands->length(); ++i) {
763 LMoveOperands cur = move_operands->at(i);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000764 LOperand* cur_to = cur.destination();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000765 if (cur_to->IsUnallocated()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000766 if (LUnallocated::cast(cur_to)->virtual_register() ==
767 LUnallocated::cast(from)->virtual_register()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000768 move->AddMove(cur.source(), to, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000769 return;
770 }
771 }
772 }
773 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000774 move->AddMove(from, to, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000775}
776
777
778void LAllocator::MeetRegisterConstraints(HBasicBlock* block) {
779 int start = block->first_instruction_index();
780 int end = block->last_instruction_index();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000781 if (start == -1) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000782 for (int i = start; i <= end; ++i) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000783 if (IsGapAt(i)) {
784 LInstruction* instr = NULL;
785 LInstruction* prev_instr = NULL;
786 if (i < end) instr = InstructionAt(i + 1);
787 if (i > start) prev_instr = InstructionAt(i - 1);
788 MeetConstraintsBetween(prev_instr, instr, i);
rossberg@chromium.org994edf62012-02-06 10:12:55 +0000789 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000790 }
791 }
792}
793
794
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000795void LAllocator::MeetConstraintsBetween(LInstruction* first,
796 LInstruction* second,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000797 int gap_index) {
798 // Handle fixed temporaries.
799 if (first != NULL) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000800 for (TempIterator it(first); !it.Done(); it.Advance()) {
801 LUnallocated* temp = LUnallocated::cast(it.Current());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000802 if (temp->HasFixedPolicy()) {
803 AllocateFixed(temp, gap_index - 1, false);
804 }
805 }
806 }
807
808 // Handle fixed output operand.
809 if (first != NULL && first->Output() != NULL) {
810 LUnallocated* first_output = LUnallocated::cast(first->Output());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000811 LiveRange* range = LiveRangeFor(first_output->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000812 bool assigned = false;
813 if (first_output->HasFixedPolicy()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000814 LUnallocated* output_copy = first_output->CopyUnconstrained(zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000815 bool is_tagged = HasTaggedValue(first_output->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000816 AllocateFixed(first_output, gap_index, is_tagged);
817
818 // This value is produced on the stack, we never need to spill it.
819 if (first_output->IsStackSlot()) {
820 range->SetSpillOperand(first_output);
821 range->SetSpillStartIndex(gap_index - 1);
822 assigned = true;
823 }
824 chunk_->AddGapMove(gap_index, first_output, output_copy);
825 }
826
827 if (!assigned) {
828 range->SetSpillStartIndex(gap_index);
829
830 // This move to spill operand is not a real use. Liveness analysis
831 // and splitting of live ranges do not account for it.
832 // Thus it should be inserted to a lifetime position corresponding to
833 // the instruction end.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000834 LGap* gap = GapAt(gap_index);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000835 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::BEFORE, zone());
836 move->AddMove(first_output, range->GetSpillOperand(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000837 }
838 }
839
840 // Handle fixed input operands of second instruction.
841 if (second != NULL) {
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000842 for (UseIterator it(second); !it.Done(); it.Advance()) {
843 LUnallocated* cur_input = LUnallocated::cast(it.Current());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000844 if (cur_input->HasFixedPolicy()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000845 LUnallocated* input_copy = cur_input->CopyUnconstrained(zone());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000846 bool is_tagged = HasTaggedValue(cur_input->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000847 AllocateFixed(cur_input, gap_index + 1, is_tagged);
848 AddConstraintsGapMove(gap_index, input_copy, cur_input);
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000849 } else if (cur_input->HasWritableRegisterPolicy()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000850 // The live range of writable input registers always goes until the end
851 // of the instruction.
852 ASSERT(!cur_input->IsUsedAtStart());
853
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000854 LUnallocated* input_copy = cur_input->CopyUnconstrained(zone());
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000855 int vreg = GetVirtualRegister();
rossberg@chromium.org994edf62012-02-06 10:12:55 +0000856 if (!AllocationOk()) return;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000857 cur_input->set_virtual_register(vreg);
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000858
859 if (RequiredRegisterKind(input_copy->virtual_register()) ==
860 DOUBLE_REGISTERS) {
861 double_artificial_registers_.Add(
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000862 cur_input->virtual_register() - first_artificial_register_,
863 zone_);
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000864 }
865
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000866 AddConstraintsGapMove(gap_index, input_copy, cur_input);
867 }
868 }
869 }
870
871 // Handle "output same as input" for second instruction.
872 if (second != NULL && second->Output() != NULL) {
873 LUnallocated* second_output = LUnallocated::cast(second->Output());
874 if (second_output->HasSameAsInputPolicy()) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000875 LUnallocated* cur_input = LUnallocated::cast(second->FirstInput());
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000876 int output_vreg = second_output->virtual_register();
877 int input_vreg = cur_input->virtual_register();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000878
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000879 LUnallocated* input_copy = cur_input->CopyUnconstrained(zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000880 cur_input->set_virtual_register(second_output->virtual_register());
881 AddConstraintsGapMove(gap_index, input_copy, cur_input);
882
883 if (HasTaggedValue(input_vreg) && !HasTaggedValue(output_vreg)) {
884 int index = gap_index + 1;
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000885 LInstruction* instr = InstructionAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000886 if (instr->HasPointerMap()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000887 instr->pointer_map()->RecordPointer(input_copy, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000888 }
889 } else if (!HasTaggedValue(input_vreg) && HasTaggedValue(output_vreg)) {
890 // The input is assumed to immediately have a tagged representation,
891 // before the pointer map can be used. I.e. the pointer map at the
892 // instruction will include the output operand (whose value at the
893 // beginning of the instruction is equal to the input operand). If
894 // this is not desired, then the pointer map at this instruction needs
895 // to be adjusted manually.
896 }
897 }
898 }
899}
900
901
902void LAllocator::ProcessInstructions(HBasicBlock* block, BitVector* live) {
903 int block_start = block->first_instruction_index();
904 int index = block->last_instruction_index();
905
906 LifetimePosition block_start_position =
907 LifetimePosition::FromInstructionIndex(block_start);
908
909 while (index >= block_start) {
910 LifetimePosition curr_position =
911 LifetimePosition::FromInstructionIndex(index);
912
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000913 if (IsGapAt(index)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000914 // We have a gap at this position.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000915 LGap* gap = GapAt(index);
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000916 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000917 const ZoneList<LMoveOperands>* move_operands = move->move_operands();
918 for (int i = 0; i < move_operands->length(); ++i) {
919 LMoveOperands* cur = &move_operands->at(i);
920 if (cur->IsIgnored()) continue;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000921 LOperand* from = cur->source();
922 LOperand* to = cur->destination();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000923 HPhi* phi = LookupPhi(to);
924 LOperand* hint = to;
925 if (phi != NULL) {
926 // This is a phi resolving move.
927 if (!phi->block()->IsLoopHeader()) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000928 hint = LiveRangeFor(phi->id())->current_hint_operand();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000929 }
930 } else {
931 if (to->IsUnallocated()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000932 if (live->Contains(LUnallocated::cast(to)->virtual_register())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000933 Define(curr_position, to, from);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000934 live->Remove(LUnallocated::cast(to)->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000935 } else {
936 cur->Eliminate();
937 continue;
938 }
939 } else {
940 Define(curr_position, to, from);
941 }
942 }
943 Use(block_start_position, curr_position, from, hint);
944 if (from->IsUnallocated()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000945 live->Add(LUnallocated::cast(from)->virtual_register());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000946 }
947 }
948 } else {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000949 ASSERT(!IsGapAt(index));
950 LInstruction* instr = InstructionAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000951
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000952 if (instr != NULL) {
953 LOperand* output = instr->Output();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000954 if (output != NULL) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000955 if (output->IsUnallocated()) {
956 live->Remove(LUnallocated::cast(output)->virtual_register());
957 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000958 Define(curr_position, output, NULL);
959 }
960
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000961 if (instr->ClobbersRegisters()) {
962 for (int i = 0; i < Register::kMaxNumAllocatableRegisters; ++i) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000963 if (output == NULL || !output->IsRegister() ||
964 output->index() != i) {
965 LiveRange* range = FixedLiveRangeFor(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 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000971 }
972
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000973 if (instr->ClobbersDoubleRegisters()) {
974 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000975 if (output == NULL || !output->IsDoubleRegister() ||
976 output->index() != i) {
977 LiveRange* range = FixedDoubleLiveRangeFor(i);
978 range->AddUseInterval(curr_position,
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000979 curr_position.InstructionEnd(),
980 zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000981 }
982 }
983 }
984
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000985 for (UseIterator it(instr); !it.Done(); it.Advance()) {
986 LOperand* input = it.Current();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000987
988 LifetimePosition use_pos;
989 if (input->IsUnallocated() &&
990 LUnallocated::cast(input)->IsUsedAtStart()) {
991 use_pos = curr_position;
992 } else {
993 use_pos = curr_position.InstructionEnd();
994 }
995
996 Use(block_start_position, use_pos, input, NULL);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000997 if (input->IsUnallocated()) {
998 live->Add(LUnallocated::cast(input)->virtual_register());
999 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001000 }
1001
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001002 for (TempIterator it(instr); !it.Done(); it.Advance()) {
1003 LOperand* temp = it.Current();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001004 if (instr->ClobbersTemps()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001005 if (temp->IsRegister()) continue;
1006 if (temp->IsUnallocated()) {
1007 LUnallocated* temp_unalloc = LUnallocated::cast(temp);
1008 if (temp_unalloc->HasFixedPolicy()) {
1009 continue;
1010 }
1011 }
1012 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001013 Use(block_start_position, curr_position.InstructionEnd(), temp, NULL);
1014 Define(curr_position, temp, NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001015 }
1016 }
1017 }
1018
1019 index = index - 1;
1020 }
1021}
1022
1023
1024void LAllocator::ResolvePhis(HBasicBlock* block) {
1025 const ZoneList<HPhi*>* phis = block->phis();
1026 for (int i = 0; i < phis->length(); ++i) {
1027 HPhi* phi = phis->at(i);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001028 LUnallocated* phi_operand = new(zone_) LUnallocated(LUnallocated::NONE);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001029 phi_operand->set_virtual_register(phi->id());
1030 for (int j = 0; j < phi->OperandCount(); ++j) {
1031 HValue* op = phi->OperandAt(j);
1032 LOperand* operand = NULL;
1033 if (op->IsConstant() && op->EmitAtUses()) {
1034 HConstant* constant = HConstant::cast(op);
1035 operand = chunk_->DefineConstantOperand(constant);
1036 } else {
1037 ASSERT(!op->EmitAtUses());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001038 LUnallocated* unalloc = new(zone_) LUnallocated(LUnallocated::ANY);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001039 unalloc->set_virtual_register(op->id());
1040 operand = unalloc;
1041 }
1042 HBasicBlock* cur_block = block->predecessors()->at(j);
1043 // The gap move must be added without any special processing as in
1044 // the AddConstraintsGapMove.
1045 chunk_->AddGapMove(cur_block->last_instruction_index() - 1,
1046 operand,
1047 phi_operand);
ricow@chromium.orgdcebac02011-04-20 09:44:50 +00001048
1049 // We are going to insert a move before the branch instruction.
1050 // Some branch instructions (e.g. loops' back edges)
1051 // can potentially cause a GC so they have a pointer map.
1052 // By inserting a move we essentially create a copy of a
1053 // value which is invisible to PopulatePointerMaps(), because we store
1054 // it into a location different from the operand of a live range
1055 // covering a branch instruction.
1056 // Thus we need to manually record a pointer.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001057 LInstruction* branch =
1058 InstructionAt(cur_block->last_instruction_index());
1059 if (branch->HasPointerMap()) {
1060 if (phi->representation().IsTagged()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001061 branch->pointer_map()->RecordPointer(phi_operand, zone());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001062 } else if (!phi->representation().IsDouble()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001063 branch->pointer_map()->RecordUntagged(phi_operand, zone());
ricow@chromium.orgdcebac02011-04-20 09:44:50 +00001064 }
1065 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001066 }
1067
1068 LiveRange* live_range = LiveRangeFor(phi->id());
1069 LLabel* label = chunk_->GetLabel(phi->block()->block_id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001070 label->GetOrCreateParallelMove(LGap::START, zone())->
1071 AddMove(phi_operand, live_range->GetSpillOperand(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001072 live_range->SetSpillStartIndex(phi->block()->first_instruction_index());
1073 }
1074}
1075
1076
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001077bool LAllocator::Allocate(LChunk* chunk) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001078 ASSERT(chunk_ == NULL);
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001079 chunk_ = static_cast<LPlatformChunk*>(chunk);
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00001080 assigned_registers_ =
1081 new(zone()) BitVector(Register::NumAllocatableRegisters(), zone());
1082 assigned_registers_->Clear();
1083 assigned_double_registers_ =
1084 new(zone()) BitVector(DoubleRegister::NumAllocatableRegisters(),
1085 zone());
1086 assigned_double_registers_->Clear();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 MeetRegisterConstraints();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001088 if (!AllocationOk()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 ResolvePhis();
1090 BuildLiveRanges();
1091 AllocateGeneralRegisters();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001092 if (!AllocationOk()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001093 AllocateDoubleRegisters();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001094 if (!AllocationOk()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001095 PopulatePointerMaps();
1096 if (has_osr_entry_) ProcessOsrEntry();
1097 ConnectRanges();
1098 ResolveControlFlow();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001099 return true;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001100}
1101
1102
1103void LAllocator::MeetRegisterConstraints() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001104 HPhase phase("L_Register constraints", chunk_);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001105 first_artificial_register_ = next_virtual_register_;
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001106 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001107 for (int i = 0; i < blocks->length(); ++i) {
1108 HBasicBlock* block = blocks->at(i);
1109 MeetRegisterConstraints(block);
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001110 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001111 }
1112}
1113
1114
1115void LAllocator::ResolvePhis() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001116 HPhase phase("L_Resolve phis", chunk_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001117
1118 // Process the blocks in reverse order.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001119 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001120 for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) {
1121 HBasicBlock* block = blocks->at(block_id);
1122 ResolvePhis(block);
1123 }
1124}
1125
1126
1127void LAllocator::ResolveControlFlow(LiveRange* range,
1128 HBasicBlock* block,
1129 HBasicBlock* pred) {
1130 LifetimePosition pred_end =
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001131 LifetimePosition::FromInstructionIndex(pred->last_instruction_index());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001132 LifetimePosition cur_start =
1133 LifetimePosition::FromInstructionIndex(block->first_instruction_index());
1134 LiveRange* pred_cover = NULL;
1135 LiveRange* cur_cover = NULL;
1136 LiveRange* cur_range = range;
1137 while (cur_range != NULL && (cur_cover == NULL || pred_cover == NULL)) {
1138 if (cur_range->CanCover(cur_start)) {
1139 ASSERT(cur_cover == NULL);
1140 cur_cover = cur_range;
1141 }
1142 if (cur_range->CanCover(pred_end)) {
1143 ASSERT(pred_cover == NULL);
1144 pred_cover = cur_range;
1145 }
1146 cur_range = cur_range->next();
1147 }
1148
1149 if (cur_cover->IsSpilled()) return;
1150 ASSERT(pred_cover != NULL && cur_cover != NULL);
1151 if (pred_cover != cur_cover) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001152 LOperand* pred_op = pred_cover->CreateAssignedOperand(zone_);
1153 LOperand* cur_op = cur_cover->CreateAssignedOperand(zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001154 if (!pred_op->Equals(cur_op)) {
1155 LGap* gap = NULL;
1156 if (block->predecessors()->length() == 1) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001157 gap = GapAt(block->first_instruction_index());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001158 } else {
1159 ASSERT(pred->end()->SecondSuccessor() == NULL);
1160 gap = GetLastGap(pred);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001161
1162 // We are going to insert a move before the branch instruction.
1163 // Some branch instructions (e.g. loops' back edges)
1164 // can potentially cause a GC so they have a pointer map.
ricow@chromium.orgdcebac02011-04-20 09:44:50 +00001165 // By inserting a move we essentially create a copy of a
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001166 // value which is invisible to PopulatePointerMaps(), because we store
1167 // it into a location different from the operand of a live range
1168 // covering a branch instruction.
1169 // Thus we need to manually record a pointer.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001170 LInstruction* branch = InstructionAt(pred->last_instruction_index());
1171 if (branch->HasPointerMap()) {
1172 if (HasTaggedValue(range->id())) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001173 branch->pointer_map()->RecordPointer(cur_op, zone());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001174 } else if (!cur_op->IsDoubleStackSlot() &&
1175 !cur_op->IsDoubleRegister()) {
1176 branch->pointer_map()->RemovePointer(cur_op);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001177 }
1178 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001179 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001180 gap->GetOrCreateParallelMove(
1181 LGap::START, zone())->AddMove(pred_op, cur_op, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001182 }
1183 }
1184}
1185
1186
1187LParallelMove* LAllocator::GetConnectingParallelMove(LifetimePosition pos) {
1188 int index = pos.InstructionIndex();
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001189 if (IsGapAt(index)) {
1190 LGap* gap = GapAt(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001191 return gap->GetOrCreateParallelMove(
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001192 pos.IsInstructionStart() ? LGap::START : LGap::END, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001193 }
1194 int gap_pos = pos.IsInstructionStart() ? (index - 1) : (index + 1);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001195 return GapAt(gap_pos)->GetOrCreateParallelMove(
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001196 (gap_pos < index) ? LGap::AFTER : LGap::BEFORE, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001197}
1198
1199
1200HBasicBlock* LAllocator::GetBlock(LifetimePosition pos) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001201 LGap* gap = GapAt(chunk_->NearestGapPos(pos.InstructionIndex()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001202 return gap->block();
1203}
1204
1205
1206void LAllocator::ConnectRanges() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001207 HPhase phase("L_Connect ranges", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001208 for (int i = 0; i < live_ranges()->length(); ++i) {
1209 LiveRange* first_range = live_ranges()->at(i);
1210 if (first_range == NULL || first_range->parent() != NULL) continue;
1211
1212 LiveRange* second_range = first_range->next();
1213 while (second_range != NULL) {
1214 LifetimePosition pos = second_range->Start();
1215
1216 if (!second_range->IsSpilled()) {
1217 // Add gap move if the two live ranges touch and there is no block
1218 // boundary.
1219 if (first_range->End().Value() == pos.Value()) {
1220 bool should_insert = true;
1221 if (IsBlockBoundary(pos)) {
1222 should_insert = CanEagerlyResolveControlFlow(GetBlock(pos));
1223 }
1224 if (should_insert) {
1225 LParallelMove* move = GetConnectingParallelMove(pos);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001226 LOperand* prev_operand = first_range->CreateAssignedOperand(zone_);
1227 LOperand* cur_operand = second_range->CreateAssignedOperand(zone_);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001228 move->AddMove(prev_operand, cur_operand, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001229 }
1230 }
1231 }
1232
1233 first_range = second_range;
1234 second_range = second_range->next();
1235 }
1236 }
1237}
1238
1239
1240bool LAllocator::CanEagerlyResolveControlFlow(HBasicBlock* block) const {
1241 if (block->predecessors()->length() != 1) return false;
1242 return block->predecessors()->first()->block_id() == block->block_id() - 1;
1243}
1244
1245
1246void LAllocator::ResolveControlFlow() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001247 HPhase phase("L_Resolve control flow", this);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001248 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001249 for (int block_id = 1; block_id < blocks->length(); ++block_id) {
1250 HBasicBlock* block = blocks->at(block_id);
1251 if (CanEagerlyResolveControlFlow(block)) continue;
1252 BitVector* live = live_in_sets_[block->block_id()];
1253 BitVector::Iterator iterator(live);
1254 while (!iterator.Done()) {
1255 int operand_index = iterator.Current();
1256 for (int i = 0; i < block->predecessors()->length(); ++i) {
1257 HBasicBlock* cur = block->predecessors()->at(i);
1258 LiveRange* cur_range = LiveRangeFor(operand_index);
1259 ResolveControlFlow(cur_range, block, cur);
1260 }
1261 iterator.Advance();
1262 }
1263 }
1264}
1265
1266
1267void LAllocator::BuildLiveRanges() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001268 HPhase phase("L_Build live ranges", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001269 InitializeLivenessAnalysis();
1270 // Process the blocks in reverse order.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001271 const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001272 for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) {
1273 HBasicBlock* block = blocks->at(block_id);
1274 BitVector* live = ComputeLiveOut(block);
1275 // Initially consider all live_out values live for the entire block. We
1276 // will shorten these intervals if necessary.
1277 AddInitialIntervals(block, live);
1278
1279 // Process the instructions in reverse order, generating and killing
1280 // live values.
1281 ProcessInstructions(block, live);
1282 // All phi output operands are killed by this block.
1283 const ZoneList<HPhi*>* phis = block->phis();
1284 for (int i = 0; i < phis->length(); ++i) {
1285 // The live range interval already ends at the first instruction of the
1286 // block.
1287 HPhi* phi = phis->at(i);
1288 live->Remove(phi->id());
1289
1290 LOperand* hint = NULL;
1291 LOperand* phi_operand = NULL;
1292 LGap* gap = GetLastGap(phi->block()->predecessors()->at(0));
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001293 LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001294 for (int j = 0; j < move->move_operands()->length(); ++j) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001295 LOperand* to = move->move_operands()->at(j).destination();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001296 if (to->IsUnallocated() &&
1297 LUnallocated::cast(to)->virtual_register() == phi->id()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001298 hint = move->move_operands()->at(j).source();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001299 phi_operand = to;
1300 break;
1301 }
1302 }
1303 ASSERT(hint != NULL);
1304
1305 LifetimePosition block_start = LifetimePosition::FromInstructionIndex(
1306 block->first_instruction_index());
1307 Define(block_start, phi_operand, hint);
1308 }
1309
1310 // Now live is live_in for this block except not including values live
1311 // out on backward successor edges.
1312 live_in_sets_[block_id] = live;
1313
1314 // If this block is a loop header go back and patch up the necessary
1315 // predecessor blocks.
1316 if (block->IsLoopHeader()) {
1317 // TODO(kmillikin): Need to be able to get the last block of the loop
1318 // in the loop information. Add a live range stretching from the first
1319 // loop instruction to the last for each value live on entry to the
1320 // header.
1321 HBasicBlock* back_edge = block->loop_information()->GetLastBackEdge();
1322 BitVector::Iterator iterator(live);
1323 LifetimePosition start = LifetimePosition::FromInstructionIndex(
1324 block->first_instruction_index());
1325 LifetimePosition end = LifetimePosition::FromInstructionIndex(
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001326 back_edge->last_instruction_index()).NextInstruction();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001327 while (!iterator.Done()) {
1328 int operand_index = iterator.Current();
1329 LiveRange* range = LiveRangeFor(operand_index);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001330 range->EnsureInterval(start, end, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001331 iterator.Advance();
1332 }
1333
1334 for (int i = block->block_id() + 1; i <= back_edge->block_id(); ++i) {
1335 live_in_sets_[i]->Union(*live);
1336 }
1337 }
1338
1339#ifdef DEBUG
1340 if (block_id == 0) {
1341 BitVector::Iterator iterator(live);
1342 bool found = false;
1343 while (!iterator.Done()) {
1344 found = true;
1345 int operand_index = iterator.Current();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001346 if (chunk_->info()->IsStub()) {
1347 CodeStub::Major major_key = chunk_->info()->code_stub()->MajorKey();
1348 PrintF("Function: %s\n", CodeStub::MajorName(major_key, false));
1349 } else {
1350 ASSERT(chunk_->info()->IsOptimizing());
1351 PrintF("Function: %s\n",
1352 *chunk_->info()->function()->debug_name()->ToCString());
1353 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001354 PrintF("Value %d used before first definition!\n", operand_index);
1355 LiveRange* range = LiveRangeFor(operand_index);
1356 PrintF("First use is at %d\n", range->first_pos()->pos().Value());
1357 iterator.Advance();
1358 }
1359 ASSERT(!found);
1360 }
1361#endif
1362 }
1363}
1364
1365
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001366bool LAllocator::SafePointsAreInOrder() const {
1367 const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps();
1368 int safe_point = 0;
1369 for (int i = 0; i < pointer_maps->length(); ++i) {
1370 LPointerMap* map = pointer_maps->at(i);
1371 if (safe_point > map->lithium_position()) return false;
1372 safe_point = map->lithium_position();
1373 }
1374 return true;
1375}
1376
1377
1378void LAllocator::PopulatePointerMaps() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001379 HPhase phase("L_Populate pointer maps", this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001380 const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps();
1381
1382 ASSERT(SafePointsAreInOrder());
1383
1384 // Iterate over all safe point positions and record a pointer
1385 // for all spilled live ranges at this point.
1386 int first_safe_point_index = 0;
1387 int last_range_start = 0;
1388 for (int range_idx = 0; range_idx < live_ranges()->length(); ++range_idx) {
1389 LiveRange* range = live_ranges()->at(range_idx);
1390 if (range == NULL) continue;
1391 // Iterate over the first parts of multi-part live ranges.
1392 if (range->parent() != NULL) continue;
1393 // Skip non-pointer values.
1394 if (!HasTaggedValue(range->id())) continue;
1395 // Skip empty live ranges.
1396 if (range->IsEmpty()) continue;
1397
1398 // Find the extent of the range and its children.
1399 int start = range->Start().InstructionIndex();
1400 int end = 0;
1401 for (LiveRange* cur = range; cur != NULL; cur = cur->next()) {
1402 LifetimePosition this_end = cur->End();
1403 if (this_end.InstructionIndex() > end) end = this_end.InstructionIndex();
1404 ASSERT(cur->Start().InstructionIndex() >= start);
1405 }
1406
1407 // Most of the ranges are in order, but not all. Keep an eye on when
1408 // they step backwards and reset the first_safe_point_index so we don't
1409 // miss any safe points.
1410 if (start < last_range_start) {
1411 first_safe_point_index = 0;
1412 }
1413 last_range_start = start;
1414
1415 // Step across all the safe points that are before the start of this range,
1416 // recording how far we step in order to save doing this for the next range.
1417 while (first_safe_point_index < pointer_maps->length()) {
1418 LPointerMap* map = pointer_maps->at(first_safe_point_index);
1419 int safe_point = map->lithium_position();
1420 if (safe_point >= start) break;
1421 first_safe_point_index++;
1422 }
1423
1424 // Step through the safe points to see whether they are in the range.
1425 for (int safe_point_index = first_safe_point_index;
1426 safe_point_index < pointer_maps->length();
1427 ++safe_point_index) {
1428 LPointerMap* map = pointer_maps->at(safe_point_index);
1429 int safe_point = map->lithium_position();
1430
1431 // The safe points are sorted so we can stop searching here.
1432 if (safe_point - 1 > end) break;
1433
1434 // Advance to the next active range that covers the current
1435 // safe point position.
1436 LifetimePosition safe_point_pos =
1437 LifetimePosition::FromInstructionIndex(safe_point);
1438 LiveRange* cur = range;
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001439 while (cur != NULL && !cur->Covers(safe_point_pos)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001440 cur = cur->next();
1441 }
1442 if (cur == NULL) continue;
1443
1444 // Check if the live range is spilled and the safe point is after
1445 // the spill position.
1446 if (range->HasAllocatedSpillOperand() &&
1447 safe_point >= range->spill_start_index()) {
1448 TraceAlloc("Pointer for range %d (spilled at %d) at safe point %d\n",
1449 range->id(), range->spill_start_index(), safe_point);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001450 map->RecordPointer(range->GetSpillOperand(), zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001451 }
1452
1453 if (!cur->IsSpilled()) {
1454 TraceAlloc("Pointer in register for range %d (start at %d) "
1455 "at safe point %d\n",
1456 cur->id(), cur->Start().Value(), safe_point);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001457 LOperand* operand = cur->CreateAssignedOperand(zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001458 ASSERT(!operand->IsStackSlot());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001459 map->RecordPointer(operand, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001460 }
1461 }
1462 }
1463}
1464
1465
1466void LAllocator::ProcessOsrEntry() {
1467 const ZoneList<LInstruction*>* instrs = chunk_->instructions();
1468
1469 // Linear search for the OSR entry instruction in the chunk.
1470 int index = -1;
1471 while (++index < instrs->length() &&
1472 !instrs->at(index)->IsOsrEntry()) {
1473 }
1474 ASSERT(index < instrs->length());
1475 LOsrEntry* instruction = LOsrEntry::cast(instrs->at(index));
1476
1477 LifetimePosition position = LifetimePosition::FromInstructionIndex(index);
1478 for (int i = 0; i < live_ranges()->length(); ++i) {
1479 LiveRange* range = live_ranges()->at(i);
1480 if (range != NULL) {
1481 if (range->Covers(position) &&
1482 range->HasRegisterAssigned() &&
1483 range->TopLevel()->HasAllocatedSpillOperand()) {
1484 int reg_index = range->assigned_register();
1485 LOperand* spill_operand = range->TopLevel()->GetSpillOperand();
1486 if (range->IsDouble()) {
1487 instruction->MarkSpilledDoubleRegister(reg_index, spill_operand);
1488 } else {
1489 instruction->MarkSpilledRegister(reg_index, spill_operand);
1490 }
1491 }
1492 }
1493 }
1494}
1495
1496
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001497void LAllocator::AllocateGeneralRegisters() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001498 HPhase phase("L_Allocate general registers", this);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001499 num_registers_ = Register::NumAllocatableRegisters();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001500 AllocateRegisters();
1501}
1502
1503
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001504void LAllocator::AllocateDoubleRegisters() {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00001505 HPhase phase("L_Allocate double registers", this);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001506 num_registers_ = DoubleRegister::NumAllocatableRegisters();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001507 mode_ = DOUBLE_REGISTERS;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001508 AllocateRegisters();
1509}
1510
1511
1512void LAllocator::AllocateRegisters() {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001513 ASSERT(unhandled_live_ranges_.is_empty());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001514
1515 for (int i = 0; i < live_ranges_.length(); ++i) {
1516 if (live_ranges_[i] != NULL) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001517 if (RequiredRegisterKind(live_ranges_[i]->id()) == mode_) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001518 AddToUnhandledUnsorted(live_ranges_[i]);
1519 }
1520 }
1521 }
1522 SortUnhandled();
1523 ASSERT(UnhandledIsSorted());
1524
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001525 ASSERT(reusable_slots_.is_empty());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001526 ASSERT(active_live_ranges_.is_empty());
1527 ASSERT(inactive_live_ranges_.is_empty());
1528
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001529 if (mode_ == DOUBLE_REGISTERS) {
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001530 for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001531 LiveRange* current = fixed_double_live_ranges_.at(i);
1532 if (current != NULL) {
1533 AddToInactive(current);
1534 }
1535 }
1536 } else {
1537 for (int i = 0; i < fixed_live_ranges_.length(); ++i) {
1538 LiveRange* current = fixed_live_ranges_.at(i);
1539 if (current != NULL) {
1540 AddToInactive(current);
1541 }
1542 }
1543 }
1544
1545 while (!unhandled_live_ranges_.is_empty()) {
1546 ASSERT(UnhandledIsSorted());
1547 LiveRange* current = unhandled_live_ranges_.RemoveLast();
1548 ASSERT(UnhandledIsSorted());
1549 LifetimePosition position = current->Start();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001550#ifdef DEBUG
1551 allocation_finger_ = position;
1552#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001553 TraceAlloc("Processing interval %d start=%d\n",
1554 current->id(),
1555 position.Value());
1556
1557 if (current->HasAllocatedSpillOperand()) {
1558 TraceAlloc("Live range %d already has a spill operand\n", current->id());
1559 LifetimePosition next_pos = position;
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001560 if (IsGapAt(next_pos.InstructionIndex())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001561 next_pos = next_pos.NextInstruction();
1562 }
1563 UsePosition* pos = current->NextUsePositionRegisterIsBeneficial(next_pos);
1564 // If the range already has a spill operand and it doesn't need a
1565 // register immediately, split it and spill the first part of the range.
1566 if (pos == NULL) {
1567 Spill(current);
1568 continue;
1569 } else if (pos->pos().Value() >
1570 current->Start().NextInstruction().Value()) {
1571 // Do not spill live range eagerly if use position that can benefit from
1572 // the register is too close to the start of live range.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001573 SpillBetween(current, current->Start(), pos->pos());
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001574 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001575 ASSERT(UnhandledIsSorted());
1576 continue;
1577 }
1578 }
1579
1580 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1581 LiveRange* cur_active = active_live_ranges_.at(i);
1582 if (cur_active->End().Value() <= position.Value()) {
1583 ActiveToHandled(cur_active);
1584 --i; // The live range was removed from the list of active live ranges.
1585 } else if (!cur_active->Covers(position)) {
1586 ActiveToInactive(cur_active);
1587 --i; // The live range was removed from the list of active live ranges.
1588 }
1589 }
1590
1591 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1592 LiveRange* cur_inactive = inactive_live_ranges_.at(i);
1593 if (cur_inactive->End().Value() <= position.Value()) {
1594 InactiveToHandled(cur_inactive);
1595 --i; // Live range was removed from the list of inactive live ranges.
1596 } else if (cur_inactive->Covers(position)) {
1597 InactiveToActive(cur_inactive);
1598 --i; // Live range was removed from the list of inactive live ranges.
1599 }
1600 }
1601
1602 ASSERT(!current->HasRegisterAssigned() && !current->IsSpilled());
1603
1604 bool result = TryAllocateFreeReg(current);
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001605 if (!AllocationOk()) return;
1606
1607 if (!result) AllocateBlockedReg(current);
1608 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001609
1610 if (current->HasRegisterAssigned()) {
1611 AddToActive(current);
1612 }
1613 }
1614
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001615 reusable_slots_.Rewind(0);
1616 active_live_ranges_.Rewind(0);
1617 inactive_live_ranges_.Rewind(0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001618}
1619
1620
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001621const char* LAllocator::RegisterName(int allocation_index) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001622 if (mode_ == GENERAL_REGISTERS) {
1623 return Register::AllocationIndexToString(allocation_index);
1624 } else {
1625 return DoubleRegister::AllocationIndexToString(allocation_index);
1626 }
1627}
1628
1629
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001630void LAllocator::TraceAlloc(const char* msg, ...) {
1631 if (FLAG_trace_alloc) {
1632 va_list arguments;
1633 va_start(arguments, msg);
1634 OS::VPrint(msg, arguments);
1635 va_end(arguments);
1636 }
1637}
1638
1639
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001640bool LAllocator::HasTaggedValue(int virtual_register) const {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001641 HValue* value = graph_->LookupValue(virtual_register);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001642 if (value == NULL) return false;
1643 return value->representation().IsTagged();
1644}
1645
1646
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001647RegisterKind LAllocator::RequiredRegisterKind(int virtual_register) const {
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001648 if (virtual_register < first_artificial_register_) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001649 HValue* value = graph_->LookupValue(virtual_register);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001650 if (value != NULL && value->representation().IsDouble()) {
1651 return DOUBLE_REGISTERS;
1652 }
1653 } else if (double_artificial_registers_.Contains(
1654 virtual_register - first_artificial_register_)) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001655 return DOUBLE_REGISTERS;
1656 }
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001657
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001658 return GENERAL_REGISTERS;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001659}
1660
1661
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001662void LAllocator::AddToActive(LiveRange* range) {
1663 TraceAlloc("Add live range %d to active\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001664 active_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001665}
1666
1667
1668void LAllocator::AddToInactive(LiveRange* range) {
1669 TraceAlloc("Add live range %d to inactive\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001670 inactive_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001671}
1672
1673
1674void LAllocator::AddToUnhandledSorted(LiveRange* range) {
1675 if (range == NULL || range->IsEmpty()) return;
1676 ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled());
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001677 ASSERT(allocation_finger_.Value() <= range->Start().Value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001678 for (int i = unhandled_live_ranges_.length() - 1; i >= 0; --i) {
1679 LiveRange* cur_range = unhandled_live_ranges_.at(i);
1680 if (range->ShouldBeAllocatedBefore(cur_range)) {
1681 TraceAlloc("Add live range %d to unhandled at %d\n", range->id(), i + 1);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001682 unhandled_live_ranges_.InsertAt(i + 1, range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001683 ASSERT(UnhandledIsSorted());
1684 return;
1685 }
1686 }
1687 TraceAlloc("Add live range %d to unhandled at start\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001688 unhandled_live_ranges_.InsertAt(0, range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001689 ASSERT(UnhandledIsSorted());
1690}
1691
1692
1693void LAllocator::AddToUnhandledUnsorted(LiveRange* range) {
1694 if (range == NULL || range->IsEmpty()) return;
1695 ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled());
1696 TraceAlloc("Add live range %d to unhandled unsorted at end\n", range->id());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001697 unhandled_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001698}
1699
1700
1701static int UnhandledSortHelper(LiveRange* const* a, LiveRange* const* b) {
1702 ASSERT(!(*a)->ShouldBeAllocatedBefore(*b) ||
1703 !(*b)->ShouldBeAllocatedBefore(*a));
1704 if ((*a)->ShouldBeAllocatedBefore(*b)) return 1;
1705 if ((*b)->ShouldBeAllocatedBefore(*a)) return -1;
1706 return (*a)->id() - (*b)->id();
1707}
1708
1709
1710// Sort the unhandled live ranges so that the ranges to be processed first are
1711// at the end of the array list. This is convenient for the register allocation
1712// algorithm because it is efficient to remove elements from the end.
1713void LAllocator::SortUnhandled() {
1714 TraceAlloc("Sort unhandled\n");
1715 unhandled_live_ranges_.Sort(&UnhandledSortHelper);
1716}
1717
1718
1719bool LAllocator::UnhandledIsSorted() {
1720 int len = unhandled_live_ranges_.length();
1721 for (int i = 1; i < len; i++) {
1722 LiveRange* a = unhandled_live_ranges_.at(i - 1);
1723 LiveRange* b = unhandled_live_ranges_.at(i);
1724 if (a->Start().Value() < b->Start().Value()) return false;
1725 }
1726 return true;
1727}
1728
1729
1730void LAllocator::FreeSpillSlot(LiveRange* range) {
1731 // Check that we are the last range.
1732 if (range->next() != NULL) return;
1733
1734 if (!range->TopLevel()->HasAllocatedSpillOperand()) return;
1735
1736 int index = range->TopLevel()->GetSpillOperand()->index();
1737 if (index >= 0) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001738 reusable_slots_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001739 }
1740}
1741
1742
1743LOperand* LAllocator::TryReuseSpillSlot(LiveRange* range) {
1744 if (reusable_slots_.is_empty()) return NULL;
1745 if (reusable_slots_.first()->End().Value() >
1746 range->TopLevel()->Start().Value()) {
1747 return NULL;
1748 }
1749 LOperand* result = reusable_slots_.first()->TopLevel()->GetSpillOperand();
1750 reusable_slots_.Remove(0);
1751 return result;
1752}
1753
1754
1755void LAllocator::ActiveToHandled(LiveRange* range) {
1756 ASSERT(active_live_ranges_.Contains(range));
1757 active_live_ranges_.RemoveElement(range);
1758 TraceAlloc("Moving live range %d from active to handled\n", range->id());
1759 FreeSpillSlot(range);
1760}
1761
1762
1763void LAllocator::ActiveToInactive(LiveRange* range) {
1764 ASSERT(active_live_ranges_.Contains(range));
1765 active_live_ranges_.RemoveElement(range);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001766 inactive_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001767 TraceAlloc("Moving live range %d from active to inactive\n", range->id());
1768}
1769
1770
1771void LAllocator::InactiveToHandled(LiveRange* range) {
1772 ASSERT(inactive_live_ranges_.Contains(range));
1773 inactive_live_ranges_.RemoveElement(range);
1774 TraceAlloc("Moving live range %d from inactive to handled\n", range->id());
1775 FreeSpillSlot(range);
1776}
1777
1778
1779void LAllocator::InactiveToActive(LiveRange* range) {
1780 ASSERT(inactive_live_ranges_.Contains(range));
1781 inactive_live_ranges_.RemoveElement(range);
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001782 active_live_ranges_.Add(range, zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001783 TraceAlloc("Moving live range %d from inactive to active\n", range->id());
1784}
1785
1786
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001787// TryAllocateFreeReg and AllocateBlockedReg assume this
1788// when allocating local arrays.
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001789STATIC_ASSERT(DoubleRegister::kMaxNumAllocatableRegisters >=
1790 Register::kMaxNumAllocatableRegisters);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001791
1792
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001793bool LAllocator::TryAllocateFreeReg(LiveRange* current) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001794 LifetimePosition free_until_pos[DoubleRegister::kMaxNumAllocatableRegisters];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001795
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001796 for (int i = 0; i < num_registers_; i++) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001797 free_until_pos[i] = LifetimePosition::MaxPosition();
1798 }
1799
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001800 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1801 LiveRange* cur_active = active_live_ranges_.at(i);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001802 free_until_pos[cur_active->assigned_register()] =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001803 LifetimePosition::FromInstructionIndex(0);
1804 }
1805
1806 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1807 LiveRange* cur_inactive = inactive_live_ranges_.at(i);
1808 ASSERT(cur_inactive->End().Value() > current->Start().Value());
1809 LifetimePosition next_intersection =
1810 cur_inactive->FirstIntersection(current);
1811 if (!next_intersection.IsValid()) continue;
1812 int cur_reg = cur_inactive->assigned_register();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001813 free_until_pos[cur_reg] = Min(free_until_pos[cur_reg], next_intersection);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001814 }
1815
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001816 LOperand* hint = current->FirstHint();
1817 if (hint != NULL && (hint->IsRegister() || hint->IsDoubleRegister())) {
1818 int register_index = hint->index();
1819 TraceAlloc(
1820 "Found reg hint %s (free until [%d) for live range %d (end %d[).\n",
1821 RegisterName(register_index),
1822 free_until_pos[register_index].Value(),
1823 current->id(),
1824 current->End().Value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001825
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001826 // The desired register is free until the end of the current live range.
1827 if (free_until_pos[register_index].Value() >= current->End().Value()) {
1828 TraceAlloc("Assigning preferred reg %s to live range %d\n",
1829 RegisterName(register_index),
1830 current->id());
1831 SetLiveRangeAssignedRegister(current, register_index, mode_, zone_);
1832 return true;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001833 }
1834 }
1835
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001836 // Find the register which stays free for the longest time.
1837 int reg = 0;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001838 for (int i = 1; i < RegisterCount(); ++i) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001839 if (free_until_pos[i].Value() > free_until_pos[reg].Value()) {
1840 reg = i;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001841 }
1842 }
1843
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001844 LifetimePosition pos = free_until_pos[reg];
1845
1846 if (pos.Value() <= current->Start().Value()) {
1847 // All registers are blocked.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001848 return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001849 }
1850
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001851 if (pos.Value() < current->End().Value()) {
1852 // Register reg is available at the range start but becomes blocked before
1853 // the range end. Split current at position where it becomes blocked.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001854 LiveRange* tail = SplitRangeAt(current, pos);
1855 if (!AllocationOk()) return false;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001856 AddToUnhandledSorted(tail);
1857 }
1858
1859
1860 // Register reg is available at the range start and is free until
1861 // the range end.
1862 ASSERT(pos.Value() >= current->End().Value());
1863 TraceAlloc("Assigning free reg %s to live range %d\n",
1864 RegisterName(reg),
1865 current->id());
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00001866 SetLiveRangeAssignedRegister(current, reg, mode_, zone_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001867
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001868 return true;
1869}
1870
1871
1872void LAllocator::AllocateBlockedReg(LiveRange* current) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001873 UsePosition* register_use = current->NextRegisterPosition(current->Start());
1874 if (register_use == NULL) {
1875 // There is no use in the current live range that requires a register.
1876 // We can just spill it.
1877 Spill(current);
1878 return;
1879 }
1880
1881
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001882 LifetimePosition use_pos[DoubleRegister::kMaxNumAllocatableRegisters];
1883 LifetimePosition block_pos[DoubleRegister::kMaxNumAllocatableRegisters];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001884
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001885 for (int i = 0; i < num_registers_; i++) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001886 use_pos[i] = block_pos[i] = LifetimePosition::MaxPosition();
1887 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001888
1889 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1890 LiveRange* range = active_live_ranges_[i];
1891 int cur_reg = range->assigned_register();
1892 if (range->IsFixed() || !range->CanBeSpilled(current->Start())) {
1893 block_pos[cur_reg] = use_pos[cur_reg] =
1894 LifetimePosition::FromInstructionIndex(0);
1895 } else {
1896 UsePosition* next_use = range->NextUsePositionRegisterIsBeneficial(
1897 current->Start());
1898 if (next_use == NULL) {
1899 use_pos[cur_reg] = range->End();
1900 } else {
1901 use_pos[cur_reg] = next_use->pos();
1902 }
1903 }
1904 }
1905
1906 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
1907 LiveRange* range = inactive_live_ranges_.at(i);
1908 ASSERT(range->End().Value() > current->Start().Value());
1909 LifetimePosition next_intersection = range->FirstIntersection(current);
1910 if (!next_intersection.IsValid()) continue;
1911 int cur_reg = range->assigned_register();
1912 if (range->IsFixed()) {
1913 block_pos[cur_reg] = Min(block_pos[cur_reg], next_intersection);
1914 use_pos[cur_reg] = Min(block_pos[cur_reg], use_pos[cur_reg]);
1915 } else {
1916 use_pos[cur_reg] = Min(use_pos[cur_reg], next_intersection);
1917 }
1918 }
1919
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001920 int reg = 0;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001921 for (int i = 1; i < RegisterCount(); ++i) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001922 if (use_pos[i].Value() > use_pos[reg].Value()) {
1923 reg = i;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001924 }
1925 }
1926
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001927 LifetimePosition pos = use_pos[reg];
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001928
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001929 if (pos.Value() < register_use->pos().Value()) {
1930 // All registers are blocked before the first use that requires a register.
1931 // Spill starting part of live range up to that use.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001932 SpillBetween(current, current->Start(), register_use->pos());
1933 return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001934 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001935
1936 if (block_pos[reg].Value() < current->End().Value()) {
1937 // Register becomes blocked before the current range end. Split before that
1938 // position.
1939 LiveRange* tail = SplitBetween(current,
1940 current->Start(),
1941 block_pos[reg].InstructionStart());
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001942 if (!AllocationOk()) return;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001943 AddToUnhandledSorted(tail);
1944 }
1945
1946 // Register reg is not blocked for the whole range.
1947 ASSERT(block_pos[reg].Value() >= current->End().Value());
1948 TraceAlloc("Assigning blocked reg %s to live range %d\n",
1949 RegisterName(reg),
1950 current->id());
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00001951 SetLiveRangeAssignedRegister(current, reg, mode_, zone_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001952
1953 // This register was not free. Thus we need to find and spill
1954 // parts of active and inactive live regions that use the same register
1955 // at the same lifetime positions as current.
1956 SplitAndSpillIntersecting(current);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001957}
1958
1959
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001960LifetimePosition LAllocator::FindOptimalSpillingPos(LiveRange* range,
1961 LifetimePosition pos) {
1962 HBasicBlock* block = GetBlock(pos.InstructionStart());
1963 HBasicBlock* loop_header =
1964 block->IsLoopHeader() ? block : block->parent_loop_header();
1965
1966 if (loop_header == NULL) return pos;
1967
1968 UsePosition* prev_use =
1969 range->PreviousUsePositionRegisterIsBeneficial(pos);
1970
1971 while (loop_header != NULL) {
1972 // We are going to spill live range inside the loop.
1973 // If possible try to move spilling position backwards to loop header.
1974 // This will reduce number of memory moves on the back edge.
1975 LifetimePosition loop_start = LifetimePosition::FromInstructionIndex(
1976 loop_header->first_instruction_index());
1977
1978 if (range->Covers(loop_start)) {
1979 if (prev_use == NULL || prev_use->pos().Value() < loop_start.Value()) {
1980 // No register beneficial use inside the loop before the pos.
1981 pos = loop_start;
1982 }
1983 }
1984
1985 // Try hoisting out to an outer loop.
1986 loop_header = loop_header->parent_loop_header();
1987 }
1988
1989 return pos;
1990}
1991
1992
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001993void LAllocator::SplitAndSpillIntersecting(LiveRange* current) {
1994 ASSERT(current->HasRegisterAssigned());
1995 int reg = current->assigned_register();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001996 LifetimePosition split_pos = current->Start();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001997 for (int i = 0; i < active_live_ranges_.length(); ++i) {
1998 LiveRange* range = active_live_ranges_[i];
1999 if (range->assigned_register() == reg) {
2000 UsePosition* next_pos = range->NextRegisterPosition(current->Start());
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002001 LifetimePosition spill_pos = FindOptimalSpillingPos(range, split_pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002002 if (next_pos == NULL) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002003 SpillAfter(range, spill_pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002004 } else {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002005 // When spilling between spill_pos and next_pos ensure that the range
2006 // remains spilled at least until the start of the current live range.
2007 // This guarantees that we will not introduce new unhandled ranges that
2008 // start before the current range as this violates allocation invariant
2009 // and will lead to an inconsistent state of active and inactive
2010 // live-ranges: ranges are allocated in order of their start positions,
2011 // ranges are retired from active/inactive when the start of the
2012 // current live-range is larger than their end.
2013 SpillBetweenUntil(range, spill_pos, current->Start(), next_pos->pos());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002014 }
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002015 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002016 ActiveToHandled(range);
2017 --i;
2018 }
2019 }
2020
2021 for (int i = 0; i < inactive_live_ranges_.length(); ++i) {
2022 LiveRange* range = inactive_live_ranges_[i];
2023 ASSERT(range->End().Value() > current->Start().Value());
2024 if (range->assigned_register() == reg && !range->IsFixed()) {
2025 LifetimePosition next_intersection = range->FirstIntersection(current);
2026 if (next_intersection.IsValid()) {
2027 UsePosition* next_pos = range->NextRegisterPosition(current->Start());
2028 if (next_pos == NULL) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002029 SpillAfter(range, split_pos);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002030 } else {
2031 next_intersection = Min(next_intersection, next_pos->pos());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002032 SpillBetween(range, split_pos, next_intersection);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002033 }
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002034 if (!AllocationOk()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002035 InactiveToHandled(range);
2036 --i;
2037 }
2038 }
2039 }
2040}
2041
2042
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002043bool LAllocator::IsBlockBoundary(LifetimePosition pos) {
2044 return pos.IsInstructionStart() &&
ricow@chromium.org83aa5492011-02-07 12:42:56 +00002045 InstructionAt(pos.InstructionIndex())->IsLabel();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002046}
2047
2048
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002049LiveRange* LAllocator::SplitRangeAt(LiveRange* range, LifetimePosition pos) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002050 ASSERT(!range->IsFixed());
2051 TraceAlloc("Splitting live range %d at %d\n", range->id(), pos.Value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002052
2053 if (pos.Value() <= range->Start().Value()) return range;
2054
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002055 // We can't properly connect liveranges if split occured at the end
2056 // of control instruction.
2057 ASSERT(pos.IsInstructionStart() ||
2058 !chunk_->instructions()->at(pos.InstructionIndex())->IsControl());
2059
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002060 int vreg = GetVirtualRegister();
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002061 if (!AllocationOk()) return NULL;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002062 LiveRange* result = LiveRangeFor(vreg);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00002063 range->SplitAt(pos, result, zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002064 return result;
2065}
2066
2067
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002068LiveRange* LAllocator::SplitBetween(LiveRange* range,
2069 LifetimePosition start,
2070 LifetimePosition end) {
2071 ASSERT(!range->IsFixed());
2072 TraceAlloc("Splitting live range %d in position between [%d, %d]\n",
2073 range->id(),
2074 start.Value(),
2075 end.Value());
2076
2077 LifetimePosition split_pos = FindOptimalSplitPos(start, end);
2078 ASSERT(split_pos.Value() >= start.Value());
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002079 return SplitRangeAt(range, split_pos);
kmillikin@chromium.orgfacd82d2010-12-13 18:00:51 +00002080}
2081
2082
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002083LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start,
2084 LifetimePosition end) {
2085 int start_instr = start.InstructionIndex();
2086 int end_instr = end.InstructionIndex();
2087 ASSERT(start_instr <= end_instr);
2088
2089 // We have no choice
2090 if (start_instr == end_instr) return end;
2091
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002092 HBasicBlock* start_block = GetBlock(start);
2093 HBasicBlock* end_block = GetBlock(end);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002094
2095 if (end_block == start_block) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002096 // The interval is split in the same basic block. Split at the latest
2097 // possible position.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002098 return end;
2099 }
2100
2101 HBasicBlock* block = end_block;
2102 // Find header of outermost loop.
2103 while (block->parent_loop_header() != NULL &&
2104 block->parent_loop_header()->block_id() > start_block->block_id()) {
2105 block = block->parent_loop_header();
2106 }
2107
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002108 // We did not find any suitable outer loop. Split at the latest possible
2109 // position unless end_block is a loop header itself.
2110 if (block == end_block && !end_block->IsLoopHeader()) return end;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002111
2112 return LifetimePosition::FromInstructionIndex(
2113 block->first_instruction_index());
2114}
2115
2116
2117void LAllocator::SpillAfter(LiveRange* range, LifetimePosition pos) {
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002118 LiveRange* second_part = SplitRangeAt(range, pos);
2119 if (!AllocationOk()) return;
lrn@chromium.org8541d772010-12-15 12:05:09 +00002120 Spill(second_part);
2121}
2122
2123
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002124void LAllocator::SpillBetween(LiveRange* range,
2125 LifetimePosition start,
2126 LifetimePosition end) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002127 SpillBetweenUntil(range, start, start, end);
2128}
2129
2130
2131void LAllocator::SpillBetweenUntil(LiveRange* range,
2132 LifetimePosition start,
2133 LifetimePosition until,
2134 LifetimePosition end) {
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00002135 CHECK(start.Value() < end.Value());
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002136 LiveRange* second_part = SplitRangeAt(range, start);
2137 if (!AllocationOk()) return;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002138
2139 if (second_part->Start().Value() < end.Value()) {
2140 // The split result intersects with [start, end[.
2141 // Split it at position between ]start+1, end[, spill the middle part
2142 // and put the rest to unhandled.
2143 LiveRange* third_part = SplitBetween(
2144 second_part,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002145 Max(second_part->Start().InstructionEnd(), until),
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002146 end.PrevInstruction().InstructionEnd());
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002147 if (!AllocationOk()) return;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002148
2149 ASSERT(third_part != second_part);
2150
2151 Spill(second_part);
2152 AddToUnhandledSorted(third_part);
2153 } else {
2154 // The split result does not intersect with [start, end[.
2155 // Nothing to spill. Just put it to unhandled as whole.
2156 AddToUnhandledSorted(second_part);
2157 }
2158}
2159
2160
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002161void LAllocator::Spill(LiveRange* range) {
2162 ASSERT(!range->IsSpilled());
2163 TraceAlloc("Spilling live range %d\n", range->id());
2164 LiveRange* first = range->TopLevel();
2165
2166 if (!first->HasAllocatedSpillOperand()) {
2167 LOperand* op = TryReuseSpillSlot(range);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002168 if (op == NULL) op = chunk_->GetNextSpillSlot(mode_ == DOUBLE_REGISTERS);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002169 first->SetSpillOperand(op);
2170 }
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00002171 range->MakeSpilled(zone_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002172}
2173
2174
2175int LAllocator::RegisterCount() const {
2176 return num_registers_;
2177}
2178
2179
2180#ifdef DEBUG
2181
2182
2183void LAllocator::Verify() const {
2184 for (int i = 0; i < live_ranges()->length(); ++i) {
2185 LiveRange* current = live_ranges()->at(i);
2186 if (current != NULL) current->Verify();
2187 }
2188}
2189
2190
2191#endif
2192
2193
2194} } // namespace v8::internal