kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2 | // 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 28 | #include "v8.h" |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 29 | #include "lithium-allocator-inl.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 30 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 31 | #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.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 40 | #elif V8_TARGET_ARCH_MIPS |
| 41 | #include "mips/lithium-mips.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 42 | #else |
| 43 | #error "Unknown architecture." |
| 44 | #endif |
| 45 | |
| 46 | namespace v8 { |
| 47 | namespace internal { |
| 48 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 49 | static inline LifetimePosition Min(LifetimePosition a, LifetimePosition b) { |
| 50 | return a.Value() < b.Value() ? a : b; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | static inline LifetimePosition Max(LifetimePosition a, LifetimePosition b) { |
| 55 | return a.Value() > b.Value() ? a : b; |
| 56 | } |
| 57 | |
| 58 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 59 | UsePosition::UsePosition(LifetimePosition pos, LOperand* operand) |
| 60 | : operand_(operand), |
| 61 | hint_(NULL), |
| 62 | pos_(pos), |
| 63 | next_(NULL), |
| 64 | requires_reg_(false), |
| 65 | register_beneficial_(true) { |
| 66 | if (operand_ != NULL && operand_->IsUnallocated()) { |
| 67 | LUnallocated* unalloc = LUnallocated::cast(operand_); |
| 68 | requires_reg_ = unalloc->HasRegisterPolicy(); |
| 69 | register_beneficial_ = !unalloc->HasAnyPolicy(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 70 | } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 71 | ASSERT(pos_.IsValid()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 72 | } |
| 73 | |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 74 | |
| 75 | bool UsePosition::HasHint() const { |
| 76 | return hint_ != NULL && !hint_->IsUnallocated(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | bool UsePosition::RequiresRegister() const { |
| 81 | return requires_reg_; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | bool UsePosition::RegisterIsBeneficial() const { |
| 86 | return register_beneficial_; |
| 87 | } |
| 88 | |
| 89 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 90 | void UseInterval::SplitAt(LifetimePosition pos, Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 91 | ASSERT(Contains(pos) && pos.Value() != start().Value()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 92 | UseInterval* after = new(zone) UseInterval(pos, end_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 93 | after->next_ = next_; |
| 94 | next_ = after; |
| 95 | end_ = pos; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | #ifdef DEBUG |
| 100 | |
| 101 | |
| 102 | void LiveRange::Verify() const { |
| 103 | UsePosition* cur = first_pos_; |
| 104 | while (cur != NULL) { |
| 105 | ASSERT(Start().Value() <= cur->pos().Value() && |
| 106 | cur->pos().Value() <= End().Value()); |
| 107 | cur = cur->next(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | bool LiveRange::HasOverlap(UseInterval* target) const { |
| 113 | UseInterval* current_interval = first_interval_; |
| 114 | while (current_interval != NULL) { |
| 115 | // Intervals overlap if the start of one is contained in the other. |
| 116 | if (current_interval->Contains(target->start()) || |
| 117 | target->Contains(current_interval->start())) { |
| 118 | return true; |
| 119 | } |
| 120 | current_interval = current_interval->next(); |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | #endif |
| 127 | |
| 128 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 129 | LiveRange::LiveRange(int id, Zone* zone) |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 130 | : id_(id), |
| 131 | spilled_(false), |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 132 | is_double_(false), |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 133 | assigned_register_(kInvalidAssignment), |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 134 | last_interval_(NULL), |
| 135 | first_interval_(NULL), |
| 136 | first_pos_(NULL), |
| 137 | parent_(NULL), |
| 138 | next_(NULL), |
| 139 | current_interval_(NULL), |
| 140 | last_processed_use_(NULL), |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 141 | spill_operand_(new(zone) LOperand()), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 142 | spill_start_index_(kMaxInt) { } |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 143 | |
| 144 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 145 | void LiveRange::set_assigned_register(int reg, |
| 146 | RegisterKind register_kind, |
| 147 | Zone* zone) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 148 | ASSERT(!HasRegisterAssigned() && !IsSpilled()); |
| 149 | assigned_register_ = reg; |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 150 | is_double_ = (register_kind == DOUBLE_REGISTERS); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 151 | ConvertOperands(zone); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 155 | void LiveRange::MakeSpilled(Zone* zone) { |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 156 | ASSERT(!IsSpilled()); |
| 157 | ASSERT(TopLevel()->HasAllocatedSpillOperand()); |
| 158 | spilled_ = true; |
| 159 | assigned_register_ = kInvalidAssignment; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 160 | ConvertOperands(zone); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | bool LiveRange::HasAllocatedSpillOperand() const { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 165 | ASSERT(spill_operand_ != NULL); |
| 166 | return !spill_operand_->IsIgnored(); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | void LiveRange::SetSpillOperand(LOperand* operand) { |
| 171 | ASSERT(!operand->IsUnallocated()); |
| 172 | ASSERT(spill_operand_ != NULL); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 173 | ASSERT(spill_operand_->IsIgnored()); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 174 | spill_operand_->ConvertTo(operand->kind(), operand->index()); |
| 175 | } |
| 176 | |
| 177 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 178 | UsePosition* LiveRange::NextUsePosition(LifetimePosition start) { |
| 179 | UsePosition* use_pos = last_processed_use_; |
| 180 | if (use_pos == NULL) use_pos = first_pos(); |
| 181 | while (use_pos != NULL && use_pos->pos().Value() < start.Value()) { |
| 182 | use_pos = use_pos->next(); |
| 183 | } |
| 184 | last_processed_use_ = use_pos; |
| 185 | return use_pos; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | UsePosition* LiveRange::NextUsePositionRegisterIsBeneficial( |
| 190 | LifetimePosition start) { |
| 191 | UsePosition* pos = NextUsePosition(start); |
| 192 | while (pos != NULL && !pos->RegisterIsBeneficial()) { |
| 193 | pos = pos->next(); |
| 194 | } |
| 195 | return pos; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | UsePosition* LiveRange::NextRegisterPosition(LifetimePosition start) { |
| 200 | UsePosition* pos = NextUsePosition(start); |
| 201 | while (pos != NULL && !pos->RequiresRegister()) { |
| 202 | pos = pos->next(); |
| 203 | } |
| 204 | return pos; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | bool LiveRange::CanBeSpilled(LifetimePosition pos) { |
| 209 | // TODO(kmillikin): Comment. Now. |
| 210 | if (pos.Value() <= Start().Value() && HasRegisterAssigned()) return false; |
| 211 | |
| 212 | // We cannot spill a live range that has a use requiring a register |
| 213 | // at the current or the immediate next position. |
| 214 | UsePosition* use_pos = NextRegisterPosition(pos); |
| 215 | if (use_pos == NULL) return true; |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 216 | return |
| 217 | use_pos->pos().Value() > pos.NextInstruction().InstructionEnd().Value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | UsePosition* LiveRange::FirstPosWithHint() const { |
| 222 | UsePosition* pos = first_pos_; |
| 223 | while (pos != NULL && !pos->HasHint()) pos = pos->next(); |
| 224 | return pos; |
| 225 | } |
| 226 | |
| 227 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 228 | LOperand* LiveRange::CreateAssignedOperand(Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 229 | LOperand* op = NULL; |
| 230 | if (HasRegisterAssigned()) { |
| 231 | ASSERT(!IsSpilled()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 232 | if (IsDouble()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 233 | op = LDoubleRegister::Create(assigned_register(), zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 234 | } else { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 235 | op = LRegister::Create(assigned_register(), zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 236 | } |
| 237 | } else if (IsSpilled()) { |
| 238 | ASSERT(!HasRegisterAssigned()); |
| 239 | op = TopLevel()->GetSpillOperand(); |
| 240 | ASSERT(!op->IsUnallocated()); |
| 241 | } else { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 242 | LUnallocated* unalloc = new(zone) LUnallocated(LUnallocated::NONE); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 243 | unalloc->set_virtual_register(id_); |
| 244 | op = unalloc; |
| 245 | } |
| 246 | return op; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | UseInterval* LiveRange::FirstSearchIntervalForPosition( |
| 251 | LifetimePosition position) const { |
| 252 | if (current_interval_ == NULL) return first_interval_; |
| 253 | if (current_interval_->start().Value() > position.Value()) { |
| 254 | current_interval_ = NULL; |
| 255 | return first_interval_; |
| 256 | } |
| 257 | return current_interval_; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | void LiveRange::AdvanceLastProcessedMarker( |
| 262 | UseInterval* to_start_of, LifetimePosition but_not_past) const { |
| 263 | if (to_start_of == NULL) return; |
| 264 | if (to_start_of->start().Value() > but_not_past.Value()) return; |
| 265 | LifetimePosition start = |
| 266 | current_interval_ == NULL ? LifetimePosition::Invalid() |
| 267 | : current_interval_->start(); |
| 268 | if (to_start_of->start().Value() > start.Value()) { |
| 269 | current_interval_ = to_start_of; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 274 | void LiveRange::SplitAt(LifetimePosition position, |
| 275 | LiveRange* result, |
| 276 | Zone* zone) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 277 | ASSERT(Start().Value() < position.Value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 278 | ASSERT(result->IsEmpty()); |
| 279 | // Find the last interval that ends before the position. If the |
| 280 | // position is contained in one of the intervals in the chain, we |
| 281 | // split that interval and use the first part. |
| 282 | UseInterval* current = FirstSearchIntervalForPosition(position); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 283 | |
| 284 | // If the split position coincides with the beginning of a use interval |
| 285 | // we need to split use positons in a special way. |
| 286 | bool split_at_start = false; |
| 287 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 288 | if (current->start().Value() == position.Value()) { |
| 289 | // When splitting at start we need to locate the previous use interval. |
| 290 | current = first_interval_; |
| 291 | } |
| 292 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 293 | while (current != NULL) { |
| 294 | if (current->Contains(position)) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 295 | current->SplitAt(position, zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | UseInterval* next = current->next(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 299 | if (next->start().Value() >= position.Value()) { |
| 300 | split_at_start = (next->start().Value() == position.Value()); |
| 301 | break; |
| 302 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 303 | current = next; |
| 304 | } |
| 305 | |
| 306 | // Partition original use intervals to the two live ranges. |
| 307 | UseInterval* before = current; |
| 308 | UseInterval* after = before->next(); |
| 309 | result->last_interval_ = (last_interval_ == before) |
| 310 | ? after // Only interval in the range after split. |
| 311 | : last_interval_; // Last interval of the original range. |
| 312 | result->first_interval_ = after; |
| 313 | last_interval_ = before; |
| 314 | |
| 315 | // Find the last use position before the split and the first use |
| 316 | // position after it. |
| 317 | UsePosition* use_after = first_pos_; |
| 318 | UsePosition* use_before = NULL; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 319 | if (split_at_start) { |
| 320 | // The split position coincides with the beginning of a use interval (the |
| 321 | // end of a lifetime hole). Use at this position should be attributed to |
| 322 | // the split child because split child owns use interval covering it. |
| 323 | while (use_after != NULL && use_after->pos().Value() < position.Value()) { |
| 324 | use_before = use_after; |
| 325 | use_after = use_after->next(); |
| 326 | } |
| 327 | } else { |
| 328 | while (use_after != NULL && use_after->pos().Value() <= position.Value()) { |
| 329 | use_before = use_after; |
| 330 | use_after = use_after->next(); |
| 331 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Partition original use positions to the two live ranges. |
| 335 | if (use_before != NULL) { |
| 336 | use_before->next_ = NULL; |
| 337 | } else { |
| 338 | first_pos_ = NULL; |
| 339 | } |
| 340 | result->first_pos_ = use_after; |
| 341 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 342 | // Discard cached iteration state. It might be pointing |
| 343 | // to the use that no longer belongs to this live range. |
| 344 | last_processed_use_ = NULL; |
| 345 | current_interval_ = NULL; |
| 346 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 347 | // Link the new live range in the chain before any of the other |
| 348 | // ranges linked from the range before the split. |
| 349 | result->parent_ = (parent_ == NULL) ? this : parent_; |
| 350 | result->next_ = next_; |
| 351 | next_ = result; |
| 352 | |
| 353 | #ifdef DEBUG |
| 354 | Verify(); |
| 355 | result->Verify(); |
| 356 | #endif |
| 357 | } |
| 358 | |
| 359 | |
| 360 | // This implements an ordering on live ranges so that they are ordered by their |
| 361 | // start positions. This is needed for the correctness of the register |
| 362 | // allocation algorithm. If two live ranges start at the same offset then there |
| 363 | // is a tie breaker based on where the value is first used. This part of the |
| 364 | // ordering is merely a heuristic. |
| 365 | bool LiveRange::ShouldBeAllocatedBefore(const LiveRange* other) const { |
| 366 | LifetimePosition start = Start(); |
| 367 | LifetimePosition other_start = other->Start(); |
| 368 | if (start.Value() == other_start.Value()) { |
| 369 | UsePosition* pos = FirstPosWithHint(); |
| 370 | if (pos == NULL) return false; |
| 371 | UsePosition* other_pos = other->first_pos(); |
| 372 | if (other_pos == NULL) return true; |
| 373 | return pos->pos().Value() < other_pos->pos().Value(); |
| 374 | } |
| 375 | return start.Value() < other_start.Value(); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | void LiveRange::ShortenTo(LifetimePosition start) { |
| 380 | LAllocator::TraceAlloc("Shorten live range %d to [%d\n", id_, start.Value()); |
| 381 | ASSERT(first_interval_ != NULL); |
| 382 | ASSERT(first_interval_->start().Value() <= start.Value()); |
| 383 | ASSERT(start.Value() < first_interval_->end().Value()); |
| 384 | first_interval_->set_start(start); |
| 385 | } |
| 386 | |
| 387 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 388 | void LiveRange::EnsureInterval(LifetimePosition start, |
| 389 | LifetimePosition end, |
| 390 | Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 391 | LAllocator::TraceAlloc("Ensure live range %d in interval [%d %d[\n", |
| 392 | id_, |
| 393 | start.Value(), |
| 394 | end.Value()); |
| 395 | LifetimePosition new_end = end; |
| 396 | while (first_interval_ != NULL && |
| 397 | first_interval_->start().Value() <= end.Value()) { |
| 398 | if (first_interval_->end().Value() > end.Value()) { |
| 399 | new_end = first_interval_->end(); |
| 400 | } |
| 401 | first_interval_ = first_interval_->next(); |
| 402 | } |
| 403 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 404 | UseInterval* new_interval = new(zone) UseInterval(start, new_end); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 405 | new_interval->next_ = first_interval_; |
| 406 | first_interval_ = new_interval; |
| 407 | if (new_interval->next() == NULL) { |
| 408 | last_interval_ = new_interval; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 413 | void LiveRange::AddUseInterval(LifetimePosition start, |
| 414 | LifetimePosition end, |
| 415 | Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 416 | LAllocator::TraceAlloc("Add to live range %d interval [%d %d[\n", |
| 417 | id_, |
| 418 | start.Value(), |
| 419 | end.Value()); |
| 420 | if (first_interval_ == NULL) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 421 | UseInterval* interval = new(zone) UseInterval(start, end); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 422 | first_interval_ = interval; |
| 423 | last_interval_ = interval; |
| 424 | } else { |
| 425 | if (end.Value() == first_interval_->start().Value()) { |
| 426 | first_interval_->set_start(start); |
| 427 | } else if (end.Value() < first_interval_->start().Value()) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 428 | UseInterval* interval = new(zone) UseInterval(start, end); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 429 | interval->set_next(first_interval_); |
| 430 | first_interval_ = interval; |
| 431 | } else { |
| 432 | // Order of instruction's processing (see ProcessInstructions) guarantees |
| 433 | // that each new use interval either precedes or intersects with |
| 434 | // last added interval. |
| 435 | ASSERT(start.Value() < first_interval_->end().Value()); |
| 436 | first_interval_->start_ = Min(start, first_interval_->start_); |
| 437 | first_interval_->end_ = Max(end, first_interval_->end_); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
| 443 | UsePosition* LiveRange::AddUsePosition(LifetimePosition pos, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 444 | LOperand* operand, |
| 445 | Zone* zone) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 446 | LAllocator::TraceAlloc("Add to live range %d use position %d\n", |
| 447 | id_, |
| 448 | pos.Value()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 449 | UsePosition* use_pos = new(zone) UsePosition(pos, operand); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 450 | UsePosition* prev = NULL; |
| 451 | UsePosition* current = first_pos_; |
| 452 | while (current != NULL && current->pos().Value() < pos.Value()) { |
| 453 | prev = current; |
| 454 | current = current->next(); |
| 455 | } |
| 456 | |
| 457 | if (prev == NULL) { |
| 458 | use_pos->set_next(first_pos_); |
| 459 | first_pos_ = use_pos; |
| 460 | } else { |
| 461 | use_pos->next_ = prev->next_; |
| 462 | prev->next_ = use_pos; |
| 463 | } |
| 464 | |
| 465 | return use_pos; |
| 466 | } |
| 467 | |
| 468 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 469 | void LiveRange::ConvertOperands(Zone* zone) { |
| 470 | LOperand* op = CreateAssignedOperand(zone); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 471 | UsePosition* use_pos = first_pos(); |
| 472 | while (use_pos != NULL) { |
| 473 | ASSERT(Start().Value() <= use_pos->pos().Value() && |
| 474 | use_pos->pos().Value() <= End().Value()); |
| 475 | |
| 476 | if (use_pos->HasOperand()) { |
| 477 | ASSERT(op->IsRegister() || op->IsDoubleRegister() || |
| 478 | !use_pos->RequiresRegister()); |
| 479 | use_pos->operand()->ConvertTo(op->kind(), op->index()); |
| 480 | } |
| 481 | use_pos = use_pos->next(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 486 | bool LiveRange::CanCover(LifetimePosition position) const { |
| 487 | if (IsEmpty()) return false; |
| 488 | return Start().Value() <= position.Value() && |
| 489 | position.Value() < End().Value(); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | bool LiveRange::Covers(LifetimePosition position) { |
| 494 | if (!CanCover(position)) return false; |
| 495 | UseInterval* start_search = FirstSearchIntervalForPosition(position); |
| 496 | for (UseInterval* interval = start_search; |
| 497 | interval != NULL; |
| 498 | interval = interval->next()) { |
| 499 | ASSERT(interval->next() == NULL || |
| 500 | interval->next()->start().Value() >= interval->start().Value()); |
| 501 | AdvanceLastProcessedMarker(interval, position); |
| 502 | if (interval->Contains(position)) return true; |
| 503 | if (interval->start().Value() > position.Value()) return false; |
| 504 | } |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | LifetimePosition LiveRange::FirstIntersection(LiveRange* other) { |
| 510 | UseInterval* b = other->first_interval(); |
| 511 | if (b == NULL) return LifetimePosition::Invalid(); |
| 512 | LifetimePosition advance_last_processed_up_to = b->start(); |
| 513 | UseInterval* a = FirstSearchIntervalForPosition(b->start()); |
| 514 | while (a != NULL && b != NULL) { |
| 515 | if (a->start().Value() > other->End().Value()) break; |
| 516 | if (b->start().Value() > End().Value()) break; |
| 517 | LifetimePosition cur_intersection = a->Intersect(b); |
| 518 | if (cur_intersection.IsValid()) { |
| 519 | return cur_intersection; |
| 520 | } |
| 521 | if (a->start().Value() < b->start().Value()) { |
| 522 | a = a->next(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 523 | if (a == NULL || a->start().Value() > other->End().Value()) break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 524 | AdvanceLastProcessedMarker(a, advance_last_processed_up_to); |
| 525 | } else { |
| 526 | b = b->next(); |
| 527 | } |
| 528 | } |
| 529 | return LifetimePosition::Invalid(); |
| 530 | } |
| 531 | |
| 532 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 533 | LAllocator::LAllocator(int num_values, HGraph* graph) |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 534 | : zone_(graph->zone()), |
| 535 | chunk_(NULL), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 536 | live_in_sets_(graph->blocks()->length(), zone_), |
| 537 | live_ranges_(num_values * 2, zone_), |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 538 | fixed_live_ranges_(NULL), |
| 539 | fixed_double_live_ranges_(NULL), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 540 | unhandled_live_ranges_(num_values * 2, zone_), |
| 541 | active_live_ranges_(8, zone_), |
| 542 | inactive_live_ranges_(8, zone_), |
| 543 | reusable_slots_(8, zone_), |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 544 | next_virtual_register_(num_values), |
| 545 | first_artificial_register_(num_values), |
danno@chromium.org | 2c45679 | 2011-11-11 12:00:53 +0000 | [diff] [blame] | 546 | mode_(GENERAL_REGISTERS), |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 547 | num_registers_(-1), |
| 548 | graph_(graph), |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 549 | has_osr_entry_(false), |
| 550 | allocation_ok_(true) { } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 551 | |
| 552 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 553 | void LAllocator::InitializeLivenessAnalysis() { |
| 554 | // Initialize the live_in sets for each block to NULL. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 555 | int block_count = graph_->blocks()->length(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 556 | live_in_sets_.Initialize(block_count, zone()); |
| 557 | live_in_sets_.AddBlock(NULL, block_count, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | BitVector* LAllocator::ComputeLiveOut(HBasicBlock* block) { |
| 562 | // Compute live out for the given block, except not including backward |
| 563 | // successor edges. |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 564 | BitVector* live_out = new(zone_) BitVector(next_virtual_register_, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 565 | |
| 566 | // Process all successor blocks. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 567 | for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 568 | // Add values live on entry to the successor. Note the successor's |
| 569 | // live_in will not be computed yet for backwards edges. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 570 | HBasicBlock* successor = it.Current(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 571 | BitVector* live_in = live_in_sets_[successor->block_id()]; |
| 572 | if (live_in != NULL) live_out->Union(*live_in); |
| 573 | |
| 574 | // All phi input operands corresponding to this successor edge are live |
| 575 | // out from this block. |
| 576 | int index = successor->PredecessorIndexOf(block); |
| 577 | const ZoneList<HPhi*>* phis = successor->phis(); |
| 578 | for (int i = 0; i < phis->length(); ++i) { |
| 579 | HPhi* phi = phis->at(i); |
| 580 | if (!phi->OperandAt(index)->IsConstant()) { |
| 581 | live_out->Add(phi->OperandAt(index)->id()); |
| 582 | } |
| 583 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | return live_out; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | void LAllocator::AddInitialIntervals(HBasicBlock* block, |
| 591 | BitVector* live_out) { |
| 592 | // Add an interval that includes the entire block to the live range for |
| 593 | // each live_out value. |
| 594 | LifetimePosition start = LifetimePosition::FromInstructionIndex( |
| 595 | block->first_instruction_index()); |
| 596 | LifetimePosition end = LifetimePosition::FromInstructionIndex( |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 597 | block->last_instruction_index()).NextInstruction(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 598 | BitVector::Iterator iterator(live_out); |
| 599 | while (!iterator.Done()) { |
| 600 | int operand_index = iterator.Current(); |
| 601 | LiveRange* range = LiveRangeFor(operand_index); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 602 | range->AddUseInterval(start, end, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 603 | iterator.Advance(); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | |
| 608 | int LAllocator::FixedDoubleLiveRangeID(int index) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 609 | return -index - 1 - Register::kMaxNumAllocatableRegisters; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | LOperand* LAllocator::AllocateFixed(LUnallocated* operand, |
| 614 | int pos, |
| 615 | bool is_tagged) { |
| 616 | TraceAlloc("Allocating fixed reg for op %d\n", operand->virtual_register()); |
| 617 | ASSERT(operand->HasFixedPolicy()); |
| 618 | if (operand->policy() == LUnallocated::FIXED_SLOT) { |
| 619 | operand->ConvertTo(LOperand::STACK_SLOT, operand->fixed_index()); |
| 620 | } else if (operand->policy() == LUnallocated::FIXED_REGISTER) { |
| 621 | int reg_index = operand->fixed_index(); |
| 622 | operand->ConvertTo(LOperand::REGISTER, reg_index); |
| 623 | } else if (operand->policy() == LUnallocated::FIXED_DOUBLE_REGISTER) { |
| 624 | int reg_index = operand->fixed_index(); |
| 625 | operand->ConvertTo(LOperand::DOUBLE_REGISTER, reg_index); |
| 626 | } else { |
| 627 | UNREACHABLE(); |
| 628 | } |
| 629 | if (is_tagged) { |
| 630 | TraceAlloc("Fixed reg is tagged at %d\n", pos); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 631 | LInstruction* instr = InstructionAt(pos); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 632 | if (instr->HasPointerMap()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 633 | instr->pointer_map()->RecordPointer(operand, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | return operand; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | LiveRange* LAllocator::FixedLiveRangeFor(int index) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 641 | ASSERT(index < Register::kMaxNumAllocatableRegisters); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 642 | LiveRange* result = fixed_live_ranges_[index]; |
| 643 | if (result == NULL) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 644 | result = new(zone_) LiveRange(FixedLiveRangeID(index), zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 645 | ASSERT(result->IsFixed()); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 646 | SetLiveRangeAssignedRegister(result, index, GENERAL_REGISTERS, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 647 | fixed_live_ranges_[index] = result; |
| 648 | } |
| 649 | return result; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | LiveRange* LAllocator::FixedDoubleLiveRangeFor(int index) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 654 | ASSERT(index < DoubleRegister::NumAllocatableRegisters()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 655 | LiveRange* result = fixed_double_live_ranges_[index]; |
| 656 | if (result == NULL) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 657 | result = new(zone_) LiveRange(FixedDoubleLiveRangeID(index), zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 658 | ASSERT(result->IsFixed()); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 659 | SetLiveRangeAssignedRegister(result, index, DOUBLE_REGISTERS, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 660 | fixed_double_live_ranges_[index] = result; |
| 661 | } |
| 662 | return result; |
| 663 | } |
| 664 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 665 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 666 | LiveRange* LAllocator::LiveRangeFor(int index) { |
| 667 | if (index >= live_ranges_.length()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 668 | live_ranges_.AddBlock(NULL, index - live_ranges_.length() + 1, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 669 | } |
| 670 | LiveRange* result = live_ranges_[index]; |
| 671 | if (result == NULL) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 672 | result = new(zone_) LiveRange(index, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 673 | live_ranges_[index] = result; |
| 674 | } |
| 675 | return result; |
| 676 | } |
| 677 | |
| 678 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 679 | LGap* LAllocator::GetLastGap(HBasicBlock* block) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 680 | int last_instruction = block->last_instruction_index(); |
| 681 | int index = chunk_->NearestGapPos(last_instruction); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 682 | return GapAt(index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
| 686 | HPhi* LAllocator::LookupPhi(LOperand* operand) const { |
| 687 | if (!operand->IsUnallocated()) return NULL; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 688 | int index = LUnallocated::cast(operand)->virtual_register(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 689 | HValue* instr = graph_->LookupValue(index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 690 | if (instr != NULL && instr->IsPhi()) { |
| 691 | return HPhi::cast(instr); |
| 692 | } |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | LiveRange* LAllocator::LiveRangeFor(LOperand* operand) { |
| 698 | if (operand->IsUnallocated()) { |
| 699 | return LiveRangeFor(LUnallocated::cast(operand)->virtual_register()); |
| 700 | } else if (operand->IsRegister()) { |
| 701 | return FixedLiveRangeFor(operand->index()); |
| 702 | } else if (operand->IsDoubleRegister()) { |
| 703 | return FixedDoubleLiveRangeFor(operand->index()); |
| 704 | } else { |
| 705 | return NULL; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | |
| 710 | void LAllocator::Define(LifetimePosition position, |
| 711 | LOperand* operand, |
| 712 | LOperand* hint) { |
| 713 | LiveRange* range = LiveRangeFor(operand); |
| 714 | if (range == NULL) return; |
| 715 | |
| 716 | if (range->IsEmpty() || range->Start().Value() > position.Value()) { |
| 717 | // Can happen if there is a definition without use. |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 718 | range->AddUseInterval(position, position.NextInstruction(), zone_); |
| 719 | range->AddUsePosition(position.NextInstruction(), NULL, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 720 | } else { |
| 721 | range->ShortenTo(position); |
| 722 | } |
| 723 | |
| 724 | if (operand->IsUnallocated()) { |
| 725 | LUnallocated* unalloc_operand = LUnallocated::cast(operand); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 726 | range->AddUsePosition(position, unalloc_operand, zone_)->set_hint(hint); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | |
| 731 | void LAllocator::Use(LifetimePosition block_start, |
| 732 | LifetimePosition position, |
| 733 | LOperand* operand, |
| 734 | LOperand* hint) { |
| 735 | LiveRange* range = LiveRangeFor(operand); |
| 736 | if (range == NULL) return; |
| 737 | if (operand->IsUnallocated()) { |
| 738 | LUnallocated* unalloc_operand = LUnallocated::cast(operand); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 739 | range->AddUsePosition(position, unalloc_operand, zone_)->set_hint(hint); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 740 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 741 | range->AddUseInterval(block_start, position, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
| 745 | void LAllocator::AddConstraintsGapMove(int index, |
| 746 | LOperand* from, |
| 747 | LOperand* to) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 748 | LGap* gap = GapAt(index); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 749 | LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 750 | if (from->IsUnallocated()) { |
| 751 | const ZoneList<LMoveOperands>* move_operands = move->move_operands(); |
| 752 | for (int i = 0; i < move_operands->length(); ++i) { |
| 753 | LMoveOperands cur = move_operands->at(i); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 754 | LOperand* cur_to = cur.destination(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 755 | if (cur_to->IsUnallocated()) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 756 | if (LUnallocated::cast(cur_to)->virtual_register() == |
| 757 | LUnallocated::cast(from)->virtual_register()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 758 | move->AddMove(cur.source(), to, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 759 | return; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 764 | move->AddMove(from, to, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | |
| 768 | void LAllocator::MeetRegisterConstraints(HBasicBlock* block) { |
| 769 | int start = block->first_instruction_index(); |
| 770 | int end = block->last_instruction_index(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 771 | if (start == -1) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 772 | for (int i = start; i <= end; ++i) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 773 | if (IsGapAt(i)) { |
| 774 | LInstruction* instr = NULL; |
| 775 | LInstruction* prev_instr = NULL; |
| 776 | if (i < end) instr = InstructionAt(i + 1); |
| 777 | if (i > start) prev_instr = InstructionAt(i - 1); |
| 778 | MeetConstraintsBetween(prev_instr, instr, i); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 779 | if (!AllocationOk()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 785 | void LAllocator::MeetConstraintsBetween(LInstruction* first, |
| 786 | LInstruction* second, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 787 | int gap_index) { |
| 788 | // Handle fixed temporaries. |
| 789 | if (first != NULL) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 790 | for (TempIterator it(first); !it.Done(); it.Advance()) { |
| 791 | LUnallocated* temp = LUnallocated::cast(it.Current()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 792 | if (temp->HasFixedPolicy()) { |
| 793 | AllocateFixed(temp, gap_index - 1, false); |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // Handle fixed output operand. |
| 799 | if (first != NULL && first->Output() != NULL) { |
| 800 | LUnallocated* first_output = LUnallocated::cast(first->Output()); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 801 | LiveRange* range = LiveRangeFor(first_output->virtual_register()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 802 | bool assigned = false; |
| 803 | if (first_output->HasFixedPolicy()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 804 | LUnallocated* output_copy = first_output->CopyUnconstrained(zone()); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 805 | bool is_tagged = HasTaggedValue(first_output->virtual_register()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 806 | AllocateFixed(first_output, gap_index, is_tagged); |
| 807 | |
| 808 | // This value is produced on the stack, we never need to spill it. |
| 809 | if (first_output->IsStackSlot()) { |
| 810 | range->SetSpillOperand(first_output); |
| 811 | range->SetSpillStartIndex(gap_index - 1); |
| 812 | assigned = true; |
| 813 | } |
| 814 | chunk_->AddGapMove(gap_index, first_output, output_copy); |
| 815 | } |
| 816 | |
| 817 | if (!assigned) { |
| 818 | range->SetSpillStartIndex(gap_index); |
| 819 | |
| 820 | // This move to spill operand is not a real use. Liveness analysis |
| 821 | // and splitting of live ranges do not account for it. |
| 822 | // Thus it should be inserted to a lifetime position corresponding to |
| 823 | // the instruction end. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 824 | LGap* gap = GapAt(gap_index); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 825 | LParallelMove* move = gap->GetOrCreateParallelMove(LGap::BEFORE, zone()); |
| 826 | move->AddMove(first_output, range->GetSpillOperand(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
| 830 | // Handle fixed input operands of second instruction. |
| 831 | if (second != NULL) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 832 | for (UseIterator it(second); !it.Done(); it.Advance()) { |
| 833 | LUnallocated* cur_input = LUnallocated::cast(it.Current()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 834 | if (cur_input->HasFixedPolicy()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 835 | LUnallocated* input_copy = cur_input->CopyUnconstrained(zone()); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 836 | bool is_tagged = HasTaggedValue(cur_input->virtual_register()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 837 | AllocateFixed(cur_input, gap_index + 1, is_tagged); |
| 838 | AddConstraintsGapMove(gap_index, input_copy, cur_input); |
| 839 | } else if (cur_input->policy() == LUnallocated::WRITABLE_REGISTER) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 840 | // The live range of writable input registers always goes until the end |
| 841 | // of the instruction. |
| 842 | ASSERT(!cur_input->IsUsedAtStart()); |
| 843 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 844 | LUnallocated* input_copy = cur_input->CopyUnconstrained(zone()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 845 | cur_input->set_virtual_register(GetVirtualRegister()); |
| 846 | if (!AllocationOk()) return; |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 847 | |
| 848 | if (RequiredRegisterKind(input_copy->virtual_register()) == |
| 849 | DOUBLE_REGISTERS) { |
| 850 | double_artificial_registers_.Add( |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 851 | cur_input->virtual_register() - first_artificial_register_, |
| 852 | zone_); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 853 | } |
| 854 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 855 | AddConstraintsGapMove(gap_index, input_copy, cur_input); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | // Handle "output same as input" for second instruction. |
| 861 | if (second != NULL && second->Output() != NULL) { |
| 862 | LUnallocated* second_output = LUnallocated::cast(second->Output()); |
| 863 | if (second_output->HasSameAsInputPolicy()) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 864 | LUnallocated* cur_input = LUnallocated::cast(second->FirstInput()); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 865 | int output_vreg = second_output->virtual_register(); |
| 866 | int input_vreg = cur_input->virtual_register(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 867 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 868 | LUnallocated* input_copy = cur_input->CopyUnconstrained(zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 869 | cur_input->set_virtual_register(second_output->virtual_register()); |
| 870 | AddConstraintsGapMove(gap_index, input_copy, cur_input); |
| 871 | |
| 872 | if (HasTaggedValue(input_vreg) && !HasTaggedValue(output_vreg)) { |
| 873 | int index = gap_index + 1; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 874 | LInstruction* instr = InstructionAt(index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 875 | if (instr->HasPointerMap()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 876 | instr->pointer_map()->RecordPointer(input_copy, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 877 | } |
| 878 | } else if (!HasTaggedValue(input_vreg) && HasTaggedValue(output_vreg)) { |
| 879 | // The input is assumed to immediately have a tagged representation, |
| 880 | // before the pointer map can be used. I.e. the pointer map at the |
| 881 | // instruction will include the output operand (whose value at the |
| 882 | // beginning of the instruction is equal to the input operand). If |
| 883 | // this is not desired, then the pointer map at this instruction needs |
| 884 | // to be adjusted manually. |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | |
| 891 | void LAllocator::ProcessInstructions(HBasicBlock* block, BitVector* live) { |
| 892 | int block_start = block->first_instruction_index(); |
| 893 | int index = block->last_instruction_index(); |
| 894 | |
| 895 | LifetimePosition block_start_position = |
| 896 | LifetimePosition::FromInstructionIndex(block_start); |
| 897 | |
| 898 | while (index >= block_start) { |
| 899 | LifetimePosition curr_position = |
| 900 | LifetimePosition::FromInstructionIndex(index); |
| 901 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 902 | if (IsGapAt(index)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 903 | // We have a gap at this position. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 904 | LGap* gap = GapAt(index); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 905 | LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 906 | const ZoneList<LMoveOperands>* move_operands = move->move_operands(); |
| 907 | for (int i = 0; i < move_operands->length(); ++i) { |
| 908 | LMoveOperands* cur = &move_operands->at(i); |
| 909 | if (cur->IsIgnored()) continue; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 910 | LOperand* from = cur->source(); |
| 911 | LOperand* to = cur->destination(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 912 | HPhi* phi = LookupPhi(to); |
| 913 | LOperand* hint = to; |
| 914 | if (phi != NULL) { |
| 915 | // This is a phi resolving move. |
| 916 | if (!phi->block()->IsLoopHeader()) { |
| 917 | hint = LiveRangeFor(phi->id())->FirstHint(); |
| 918 | } |
| 919 | } else { |
| 920 | if (to->IsUnallocated()) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 921 | if (live->Contains(LUnallocated::cast(to)->virtual_register())) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 922 | Define(curr_position, to, from); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 923 | live->Remove(LUnallocated::cast(to)->virtual_register()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 924 | } else { |
| 925 | cur->Eliminate(); |
| 926 | continue; |
| 927 | } |
| 928 | } else { |
| 929 | Define(curr_position, to, from); |
| 930 | } |
| 931 | } |
| 932 | Use(block_start_position, curr_position, from, hint); |
| 933 | if (from->IsUnallocated()) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 934 | live->Add(LUnallocated::cast(from)->virtual_register()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | } else { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 938 | ASSERT(!IsGapAt(index)); |
| 939 | LInstruction* instr = InstructionAt(index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 940 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 941 | if (instr != NULL) { |
| 942 | LOperand* output = instr->Output(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 943 | if (output != NULL) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 944 | if (output->IsUnallocated()) { |
| 945 | live->Remove(LUnallocated::cast(output)->virtual_register()); |
| 946 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 947 | Define(curr_position, output, NULL); |
| 948 | } |
| 949 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 950 | if (instr->ClobbersRegisters()) { |
| 951 | for (int i = 0; i < Register::kMaxNumAllocatableRegisters; ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 952 | if (output == NULL || !output->IsRegister() || |
| 953 | output->index() != i) { |
| 954 | LiveRange* range = FixedLiveRangeFor(i); |
| 955 | range->AddUseInterval(curr_position, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 956 | curr_position.InstructionEnd(), |
| 957 | zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 960 | } |
| 961 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 962 | if (instr->ClobbersDoubleRegisters()) { |
| 963 | for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 964 | if (output == NULL || !output->IsDoubleRegister() || |
| 965 | output->index() != i) { |
| 966 | LiveRange* range = FixedDoubleLiveRangeFor(i); |
| 967 | range->AddUseInterval(curr_position, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 968 | curr_position.InstructionEnd(), |
| 969 | zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 974 | for (UseIterator it(instr); !it.Done(); it.Advance()) { |
| 975 | LOperand* input = it.Current(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 976 | |
| 977 | LifetimePosition use_pos; |
| 978 | if (input->IsUnallocated() && |
| 979 | LUnallocated::cast(input)->IsUsedAtStart()) { |
| 980 | use_pos = curr_position; |
| 981 | } else { |
| 982 | use_pos = curr_position.InstructionEnd(); |
| 983 | } |
| 984 | |
| 985 | Use(block_start_position, use_pos, input, NULL); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 986 | if (input->IsUnallocated()) { |
| 987 | live->Add(LUnallocated::cast(input)->virtual_register()); |
| 988 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 989 | } |
| 990 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 991 | for (TempIterator it(instr); !it.Done(); it.Advance()) { |
| 992 | LOperand* temp = it.Current(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 993 | if (instr->ClobbersTemps()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 994 | if (temp->IsRegister()) continue; |
| 995 | if (temp->IsUnallocated()) { |
| 996 | LUnallocated* temp_unalloc = LUnallocated::cast(temp); |
| 997 | if (temp_unalloc->HasFixedPolicy()) { |
| 998 | continue; |
| 999 | } |
| 1000 | } |
| 1001 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1002 | Use(block_start_position, curr_position.InstructionEnd(), temp, NULL); |
| 1003 | Define(curr_position, temp, NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | index = index - 1; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | void LAllocator::ResolvePhis(HBasicBlock* block) { |
| 1014 | const ZoneList<HPhi*>* phis = block->phis(); |
| 1015 | for (int i = 0; i < phis->length(); ++i) { |
| 1016 | HPhi* phi = phis->at(i); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1017 | LUnallocated* phi_operand = new(zone_) LUnallocated(LUnallocated::NONE); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1018 | phi_operand->set_virtual_register(phi->id()); |
| 1019 | for (int j = 0; j < phi->OperandCount(); ++j) { |
| 1020 | HValue* op = phi->OperandAt(j); |
| 1021 | LOperand* operand = NULL; |
| 1022 | if (op->IsConstant() && op->EmitAtUses()) { |
| 1023 | HConstant* constant = HConstant::cast(op); |
| 1024 | operand = chunk_->DefineConstantOperand(constant); |
| 1025 | } else { |
| 1026 | ASSERT(!op->EmitAtUses()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1027 | LUnallocated* unalloc = new(zone_) LUnallocated(LUnallocated::ANY); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1028 | unalloc->set_virtual_register(op->id()); |
| 1029 | operand = unalloc; |
| 1030 | } |
| 1031 | HBasicBlock* cur_block = block->predecessors()->at(j); |
| 1032 | // The gap move must be added without any special processing as in |
| 1033 | // the AddConstraintsGapMove. |
| 1034 | chunk_->AddGapMove(cur_block->last_instruction_index() - 1, |
| 1035 | operand, |
| 1036 | phi_operand); |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 1037 | |
| 1038 | // We are going to insert a move before the branch instruction. |
| 1039 | // Some branch instructions (e.g. loops' back edges) |
| 1040 | // can potentially cause a GC so they have a pointer map. |
| 1041 | // By inserting a move we essentially create a copy of a |
| 1042 | // value which is invisible to PopulatePointerMaps(), because we store |
| 1043 | // it into a location different from the operand of a live range |
| 1044 | // covering a branch instruction. |
| 1045 | // Thus we need to manually record a pointer. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1046 | LInstruction* branch = |
| 1047 | InstructionAt(cur_block->last_instruction_index()); |
| 1048 | if (branch->HasPointerMap()) { |
| 1049 | if (phi->representation().IsTagged()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1050 | branch->pointer_map()->RecordPointer(phi_operand, zone()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1051 | } else if (!phi->representation().IsDouble()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1052 | branch->pointer_map()->RecordUntagged(phi_operand, zone()); |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | LiveRange* live_range = LiveRangeFor(phi->id()); |
| 1058 | LLabel* label = chunk_->GetLabel(phi->block()->block_id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1059 | label->GetOrCreateParallelMove(LGap::START, zone())-> |
| 1060 | AddMove(phi_operand, live_range->GetSpillOperand(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1061 | live_range->SetSpillStartIndex(phi->block()->first_instruction_index()); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1066 | bool LAllocator::Allocate(LChunk* chunk) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1067 | ASSERT(chunk_ == NULL); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1068 | chunk_ = static_cast<LPlatformChunk*>(chunk); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 1069 | assigned_registers_ = |
| 1070 | new(zone()) BitVector(Register::NumAllocatableRegisters(), zone()); |
| 1071 | assigned_registers_->Clear(); |
| 1072 | assigned_double_registers_ = |
| 1073 | new(zone()) BitVector(DoubleRegister::NumAllocatableRegisters(), |
| 1074 | zone()); |
| 1075 | assigned_double_registers_->Clear(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1076 | MeetRegisterConstraints(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1077 | if (!AllocationOk()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1078 | ResolvePhis(); |
| 1079 | BuildLiveRanges(); |
| 1080 | AllocateGeneralRegisters(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1081 | if (!AllocationOk()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1082 | AllocateDoubleRegisters(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1083 | if (!AllocationOk()) return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1084 | PopulatePointerMaps(); |
| 1085 | if (has_osr_entry_) ProcessOsrEntry(); |
| 1086 | ConnectRanges(); |
| 1087 | ResolveControlFlow(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1088 | return true; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | void LAllocator::MeetRegisterConstraints() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1093 | HPhase phase("L_Register constraints", chunk_); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1094 | first_artificial_register_ = next_virtual_register_; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1095 | const ZoneList<HBasicBlock*>* blocks = graph_->blocks(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1096 | for (int i = 0; i < blocks->length(); ++i) { |
| 1097 | HBasicBlock* block = blocks->at(i); |
| 1098 | MeetRegisterConstraints(block); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1099 | if (!AllocationOk()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | void LAllocator::ResolvePhis() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1105 | HPhase phase("L_Resolve phis", chunk_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Process the blocks in reverse order. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1108 | const ZoneList<HBasicBlock*>* blocks = graph_->blocks(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1109 | for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) { |
| 1110 | HBasicBlock* block = blocks->at(block_id); |
| 1111 | ResolvePhis(block); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | void LAllocator::ResolveControlFlow(LiveRange* range, |
| 1117 | HBasicBlock* block, |
| 1118 | HBasicBlock* pred) { |
| 1119 | LifetimePosition pred_end = |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1120 | LifetimePosition::FromInstructionIndex(pred->last_instruction_index()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1121 | LifetimePosition cur_start = |
| 1122 | LifetimePosition::FromInstructionIndex(block->first_instruction_index()); |
| 1123 | LiveRange* pred_cover = NULL; |
| 1124 | LiveRange* cur_cover = NULL; |
| 1125 | LiveRange* cur_range = range; |
| 1126 | while (cur_range != NULL && (cur_cover == NULL || pred_cover == NULL)) { |
| 1127 | if (cur_range->CanCover(cur_start)) { |
| 1128 | ASSERT(cur_cover == NULL); |
| 1129 | cur_cover = cur_range; |
| 1130 | } |
| 1131 | if (cur_range->CanCover(pred_end)) { |
| 1132 | ASSERT(pred_cover == NULL); |
| 1133 | pred_cover = cur_range; |
| 1134 | } |
| 1135 | cur_range = cur_range->next(); |
| 1136 | } |
| 1137 | |
| 1138 | if (cur_cover->IsSpilled()) return; |
| 1139 | ASSERT(pred_cover != NULL && cur_cover != NULL); |
| 1140 | if (pred_cover != cur_cover) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1141 | LOperand* pred_op = pred_cover->CreateAssignedOperand(zone_); |
| 1142 | LOperand* cur_op = cur_cover->CreateAssignedOperand(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1143 | if (!pred_op->Equals(cur_op)) { |
| 1144 | LGap* gap = NULL; |
| 1145 | if (block->predecessors()->length() == 1) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1146 | gap = GapAt(block->first_instruction_index()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1147 | } else { |
| 1148 | ASSERT(pred->end()->SecondSuccessor() == NULL); |
| 1149 | gap = GetLastGap(pred); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1150 | |
| 1151 | // We are going to insert a move before the branch instruction. |
| 1152 | // Some branch instructions (e.g. loops' back edges) |
| 1153 | // can potentially cause a GC so they have a pointer map. |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 1154 | // By inserting a move we essentially create a copy of a |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1155 | // value which is invisible to PopulatePointerMaps(), because we store |
| 1156 | // it into a location different from the operand of a live range |
| 1157 | // covering a branch instruction. |
| 1158 | // Thus we need to manually record a pointer. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1159 | LInstruction* branch = InstructionAt(pred->last_instruction_index()); |
| 1160 | if (branch->HasPointerMap()) { |
| 1161 | if (HasTaggedValue(range->id())) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1162 | branch->pointer_map()->RecordPointer(cur_op, zone()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1163 | } else if (!cur_op->IsDoubleStackSlot() && |
| 1164 | !cur_op->IsDoubleRegister()) { |
| 1165 | branch->pointer_map()->RemovePointer(cur_op); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1168 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1169 | gap->GetOrCreateParallelMove( |
| 1170 | LGap::START, zone())->AddMove(pred_op, cur_op, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | LParallelMove* LAllocator::GetConnectingParallelMove(LifetimePosition pos) { |
| 1177 | int index = pos.InstructionIndex(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1178 | if (IsGapAt(index)) { |
| 1179 | LGap* gap = GapAt(index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1180 | return gap->GetOrCreateParallelMove( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1181 | pos.IsInstructionStart() ? LGap::START : LGap::END, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1182 | } |
| 1183 | int gap_pos = pos.IsInstructionStart() ? (index - 1) : (index + 1); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1184 | return GapAt(gap_pos)->GetOrCreateParallelMove( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1185 | (gap_pos < index) ? LGap::AFTER : LGap::BEFORE, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
| 1189 | HBasicBlock* LAllocator::GetBlock(LifetimePosition pos) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1190 | LGap* gap = GapAt(chunk_->NearestGapPos(pos.InstructionIndex())); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1191 | return gap->block(); |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | void LAllocator::ConnectRanges() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1196 | HPhase phase("L_Connect ranges", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1197 | for (int i = 0; i < live_ranges()->length(); ++i) { |
| 1198 | LiveRange* first_range = live_ranges()->at(i); |
| 1199 | if (first_range == NULL || first_range->parent() != NULL) continue; |
| 1200 | |
| 1201 | LiveRange* second_range = first_range->next(); |
| 1202 | while (second_range != NULL) { |
| 1203 | LifetimePosition pos = second_range->Start(); |
| 1204 | |
| 1205 | if (!second_range->IsSpilled()) { |
| 1206 | // Add gap move if the two live ranges touch and there is no block |
| 1207 | // boundary. |
| 1208 | if (first_range->End().Value() == pos.Value()) { |
| 1209 | bool should_insert = true; |
| 1210 | if (IsBlockBoundary(pos)) { |
| 1211 | should_insert = CanEagerlyResolveControlFlow(GetBlock(pos)); |
| 1212 | } |
| 1213 | if (should_insert) { |
| 1214 | LParallelMove* move = GetConnectingParallelMove(pos); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1215 | LOperand* prev_operand = first_range->CreateAssignedOperand(zone_); |
| 1216 | LOperand* cur_operand = second_range->CreateAssignedOperand(zone_); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1217 | move->AddMove(prev_operand, cur_operand, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | first_range = second_range; |
| 1223 | second_range = second_range->next(); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | bool LAllocator::CanEagerlyResolveControlFlow(HBasicBlock* block) const { |
| 1230 | if (block->predecessors()->length() != 1) return false; |
| 1231 | return block->predecessors()->first()->block_id() == block->block_id() - 1; |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | void LAllocator::ResolveControlFlow() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1236 | HPhase phase("L_Resolve control flow", this); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1237 | const ZoneList<HBasicBlock*>* blocks = graph_->blocks(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1238 | for (int block_id = 1; block_id < blocks->length(); ++block_id) { |
| 1239 | HBasicBlock* block = blocks->at(block_id); |
| 1240 | if (CanEagerlyResolveControlFlow(block)) continue; |
| 1241 | BitVector* live = live_in_sets_[block->block_id()]; |
| 1242 | BitVector::Iterator iterator(live); |
| 1243 | while (!iterator.Done()) { |
| 1244 | int operand_index = iterator.Current(); |
| 1245 | for (int i = 0; i < block->predecessors()->length(); ++i) { |
| 1246 | HBasicBlock* cur = block->predecessors()->at(i); |
| 1247 | LiveRange* cur_range = LiveRangeFor(operand_index); |
| 1248 | ResolveControlFlow(cur_range, block, cur); |
| 1249 | } |
| 1250 | iterator.Advance(); |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | void LAllocator::BuildLiveRanges() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1257 | HPhase phase("L_Build live ranges", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1258 | InitializeLivenessAnalysis(); |
| 1259 | // Process the blocks in reverse order. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1260 | const ZoneList<HBasicBlock*>* blocks = graph_->blocks(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1261 | for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) { |
| 1262 | HBasicBlock* block = blocks->at(block_id); |
| 1263 | BitVector* live = ComputeLiveOut(block); |
| 1264 | // Initially consider all live_out values live for the entire block. We |
| 1265 | // will shorten these intervals if necessary. |
| 1266 | AddInitialIntervals(block, live); |
| 1267 | |
| 1268 | // Process the instructions in reverse order, generating and killing |
| 1269 | // live values. |
| 1270 | ProcessInstructions(block, live); |
| 1271 | // All phi output operands are killed by this block. |
| 1272 | const ZoneList<HPhi*>* phis = block->phis(); |
| 1273 | for (int i = 0; i < phis->length(); ++i) { |
| 1274 | // The live range interval already ends at the first instruction of the |
| 1275 | // block. |
| 1276 | HPhi* phi = phis->at(i); |
| 1277 | live->Remove(phi->id()); |
| 1278 | |
| 1279 | LOperand* hint = NULL; |
| 1280 | LOperand* phi_operand = NULL; |
| 1281 | LGap* gap = GetLastGap(phi->block()->predecessors()->at(0)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1282 | LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1283 | for (int j = 0; j < move->move_operands()->length(); ++j) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1284 | LOperand* to = move->move_operands()->at(j).destination(); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1285 | if (to->IsUnallocated() && |
| 1286 | LUnallocated::cast(to)->virtual_register() == phi->id()) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 1287 | hint = move->move_operands()->at(j).source(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1288 | phi_operand = to; |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | ASSERT(hint != NULL); |
| 1293 | |
| 1294 | LifetimePosition block_start = LifetimePosition::FromInstructionIndex( |
| 1295 | block->first_instruction_index()); |
| 1296 | Define(block_start, phi_operand, hint); |
| 1297 | } |
| 1298 | |
| 1299 | // Now live is live_in for this block except not including values live |
| 1300 | // out on backward successor edges. |
| 1301 | live_in_sets_[block_id] = live; |
| 1302 | |
| 1303 | // If this block is a loop header go back and patch up the necessary |
| 1304 | // predecessor blocks. |
| 1305 | if (block->IsLoopHeader()) { |
| 1306 | // TODO(kmillikin): Need to be able to get the last block of the loop |
| 1307 | // in the loop information. Add a live range stretching from the first |
| 1308 | // loop instruction to the last for each value live on entry to the |
| 1309 | // header. |
| 1310 | HBasicBlock* back_edge = block->loop_information()->GetLastBackEdge(); |
| 1311 | BitVector::Iterator iterator(live); |
| 1312 | LifetimePosition start = LifetimePosition::FromInstructionIndex( |
| 1313 | block->first_instruction_index()); |
| 1314 | LifetimePosition end = LifetimePosition::FromInstructionIndex( |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1315 | back_edge->last_instruction_index()).NextInstruction(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1316 | while (!iterator.Done()) { |
| 1317 | int operand_index = iterator.Current(); |
| 1318 | LiveRange* range = LiveRangeFor(operand_index); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1319 | range->EnsureInterval(start, end, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1320 | iterator.Advance(); |
| 1321 | } |
| 1322 | |
| 1323 | for (int i = block->block_id() + 1; i <= back_edge->block_id(); ++i) { |
| 1324 | live_in_sets_[i]->Union(*live); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | #ifdef DEBUG |
| 1329 | if (block_id == 0) { |
| 1330 | BitVector::Iterator iterator(live); |
| 1331 | bool found = false; |
| 1332 | while (!iterator.Done()) { |
| 1333 | found = true; |
| 1334 | int operand_index = iterator.Current(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1335 | if (chunk_->info()->IsStub()) { |
| 1336 | CodeStub::Major major_key = chunk_->info()->code_stub()->MajorKey(); |
| 1337 | PrintF("Function: %s\n", CodeStub::MajorName(major_key, false)); |
| 1338 | } else { |
| 1339 | ASSERT(chunk_->info()->IsOptimizing()); |
| 1340 | PrintF("Function: %s\n", |
| 1341 | *chunk_->info()->function()->debug_name()->ToCString()); |
| 1342 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1343 | PrintF("Value %d used before first definition!\n", operand_index); |
| 1344 | LiveRange* range = LiveRangeFor(operand_index); |
| 1345 | PrintF("First use is at %d\n", range->first_pos()->pos().Value()); |
| 1346 | iterator.Advance(); |
| 1347 | } |
| 1348 | ASSERT(!found); |
| 1349 | } |
| 1350 | #endif |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1355 | bool LAllocator::SafePointsAreInOrder() const { |
| 1356 | const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps(); |
| 1357 | int safe_point = 0; |
| 1358 | for (int i = 0; i < pointer_maps->length(); ++i) { |
| 1359 | LPointerMap* map = pointer_maps->at(i); |
| 1360 | if (safe_point > map->lithium_position()) return false; |
| 1361 | safe_point = map->lithium_position(); |
| 1362 | } |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | |
| 1367 | void LAllocator::PopulatePointerMaps() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1368 | HPhase phase("L_Populate pointer maps", this); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1369 | const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps(); |
| 1370 | |
| 1371 | ASSERT(SafePointsAreInOrder()); |
| 1372 | |
| 1373 | // Iterate over all safe point positions and record a pointer |
| 1374 | // for all spilled live ranges at this point. |
| 1375 | int first_safe_point_index = 0; |
| 1376 | int last_range_start = 0; |
| 1377 | for (int range_idx = 0; range_idx < live_ranges()->length(); ++range_idx) { |
| 1378 | LiveRange* range = live_ranges()->at(range_idx); |
| 1379 | if (range == NULL) continue; |
| 1380 | // Iterate over the first parts of multi-part live ranges. |
| 1381 | if (range->parent() != NULL) continue; |
| 1382 | // Skip non-pointer values. |
| 1383 | if (!HasTaggedValue(range->id())) continue; |
| 1384 | // Skip empty live ranges. |
| 1385 | if (range->IsEmpty()) continue; |
| 1386 | |
| 1387 | // Find the extent of the range and its children. |
| 1388 | int start = range->Start().InstructionIndex(); |
| 1389 | int end = 0; |
| 1390 | for (LiveRange* cur = range; cur != NULL; cur = cur->next()) { |
| 1391 | LifetimePosition this_end = cur->End(); |
| 1392 | if (this_end.InstructionIndex() > end) end = this_end.InstructionIndex(); |
| 1393 | ASSERT(cur->Start().InstructionIndex() >= start); |
| 1394 | } |
| 1395 | |
| 1396 | // Most of the ranges are in order, but not all. Keep an eye on when |
| 1397 | // they step backwards and reset the first_safe_point_index so we don't |
| 1398 | // miss any safe points. |
| 1399 | if (start < last_range_start) { |
| 1400 | first_safe_point_index = 0; |
| 1401 | } |
| 1402 | last_range_start = start; |
| 1403 | |
| 1404 | // Step across all the safe points that are before the start of this range, |
| 1405 | // recording how far we step in order to save doing this for the next range. |
| 1406 | while (first_safe_point_index < pointer_maps->length()) { |
| 1407 | LPointerMap* map = pointer_maps->at(first_safe_point_index); |
| 1408 | int safe_point = map->lithium_position(); |
| 1409 | if (safe_point >= start) break; |
| 1410 | first_safe_point_index++; |
| 1411 | } |
| 1412 | |
| 1413 | // Step through the safe points to see whether they are in the range. |
| 1414 | for (int safe_point_index = first_safe_point_index; |
| 1415 | safe_point_index < pointer_maps->length(); |
| 1416 | ++safe_point_index) { |
| 1417 | LPointerMap* map = pointer_maps->at(safe_point_index); |
| 1418 | int safe_point = map->lithium_position(); |
| 1419 | |
| 1420 | // The safe points are sorted so we can stop searching here. |
| 1421 | if (safe_point - 1 > end) break; |
| 1422 | |
| 1423 | // Advance to the next active range that covers the current |
| 1424 | // safe point position. |
| 1425 | LifetimePosition safe_point_pos = |
| 1426 | LifetimePosition::FromInstructionIndex(safe_point); |
| 1427 | LiveRange* cur = range; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 1428 | while (cur != NULL && !cur->Covers(safe_point_pos)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1429 | cur = cur->next(); |
| 1430 | } |
| 1431 | if (cur == NULL) continue; |
| 1432 | |
| 1433 | // Check if the live range is spilled and the safe point is after |
| 1434 | // the spill position. |
| 1435 | if (range->HasAllocatedSpillOperand() && |
| 1436 | safe_point >= range->spill_start_index()) { |
| 1437 | TraceAlloc("Pointer for range %d (spilled at %d) at safe point %d\n", |
| 1438 | range->id(), range->spill_start_index(), safe_point); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1439 | map->RecordPointer(range->GetSpillOperand(), zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | if (!cur->IsSpilled()) { |
| 1443 | TraceAlloc("Pointer in register for range %d (start at %d) " |
| 1444 | "at safe point %d\n", |
| 1445 | cur->id(), cur->Start().Value(), safe_point); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1446 | LOperand* operand = cur->CreateAssignedOperand(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1447 | ASSERT(!operand->IsStackSlot()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1448 | map->RecordPointer(operand, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | void LAllocator::ProcessOsrEntry() { |
| 1456 | const ZoneList<LInstruction*>* instrs = chunk_->instructions(); |
| 1457 | |
| 1458 | // Linear search for the OSR entry instruction in the chunk. |
| 1459 | int index = -1; |
| 1460 | while (++index < instrs->length() && |
| 1461 | !instrs->at(index)->IsOsrEntry()) { |
| 1462 | } |
| 1463 | ASSERT(index < instrs->length()); |
| 1464 | LOsrEntry* instruction = LOsrEntry::cast(instrs->at(index)); |
| 1465 | |
| 1466 | LifetimePosition position = LifetimePosition::FromInstructionIndex(index); |
| 1467 | for (int i = 0; i < live_ranges()->length(); ++i) { |
| 1468 | LiveRange* range = live_ranges()->at(i); |
| 1469 | if (range != NULL) { |
| 1470 | if (range->Covers(position) && |
| 1471 | range->HasRegisterAssigned() && |
| 1472 | range->TopLevel()->HasAllocatedSpillOperand()) { |
| 1473 | int reg_index = range->assigned_register(); |
| 1474 | LOperand* spill_operand = range->TopLevel()->GetSpillOperand(); |
| 1475 | if (range->IsDouble()) { |
| 1476 | instruction->MarkSpilledDoubleRegister(reg_index, spill_operand); |
| 1477 | } else { |
| 1478 | instruction->MarkSpilledRegister(reg_index, spill_operand); |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1486 | void LAllocator::AllocateGeneralRegisters() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1487 | HPhase phase("L_Allocate general registers", this); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1488 | num_registers_ = Register::NumAllocatableRegisters(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1489 | AllocateRegisters(); |
| 1490 | } |
| 1491 | |
| 1492 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1493 | void LAllocator::AllocateDoubleRegisters() { |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 1494 | HPhase phase("L_Allocate double registers", this); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1495 | num_registers_ = DoubleRegister::NumAllocatableRegisters(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1496 | mode_ = DOUBLE_REGISTERS; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1497 | AllocateRegisters(); |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | void LAllocator::AllocateRegisters() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1502 | ASSERT(unhandled_live_ranges_.is_empty()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1503 | |
| 1504 | for (int i = 0; i < live_ranges_.length(); ++i) { |
| 1505 | if (live_ranges_[i] != NULL) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1506 | if (RequiredRegisterKind(live_ranges_[i]->id()) == mode_) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1507 | AddToUnhandledUnsorted(live_ranges_[i]); |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | SortUnhandled(); |
| 1512 | ASSERT(UnhandledIsSorted()); |
| 1513 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1514 | ASSERT(reusable_slots_.is_empty()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1515 | ASSERT(active_live_ranges_.is_empty()); |
| 1516 | ASSERT(inactive_live_ranges_.is_empty()); |
| 1517 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1518 | if (mode_ == DOUBLE_REGISTERS) { |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 1519 | for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1520 | LiveRange* current = fixed_double_live_ranges_.at(i); |
| 1521 | if (current != NULL) { |
| 1522 | AddToInactive(current); |
| 1523 | } |
| 1524 | } |
| 1525 | } else { |
| 1526 | for (int i = 0; i < fixed_live_ranges_.length(); ++i) { |
| 1527 | LiveRange* current = fixed_live_ranges_.at(i); |
| 1528 | if (current != NULL) { |
| 1529 | AddToInactive(current); |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | while (!unhandled_live_ranges_.is_empty()) { |
| 1535 | ASSERT(UnhandledIsSorted()); |
| 1536 | LiveRange* current = unhandled_live_ranges_.RemoveLast(); |
| 1537 | ASSERT(UnhandledIsSorted()); |
| 1538 | LifetimePosition position = current->Start(); |
| 1539 | TraceAlloc("Processing interval %d start=%d\n", |
| 1540 | current->id(), |
| 1541 | position.Value()); |
| 1542 | |
| 1543 | if (current->HasAllocatedSpillOperand()) { |
| 1544 | TraceAlloc("Live range %d already has a spill operand\n", current->id()); |
| 1545 | LifetimePosition next_pos = position; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1546 | if (IsGapAt(next_pos.InstructionIndex())) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1547 | next_pos = next_pos.NextInstruction(); |
| 1548 | } |
| 1549 | UsePosition* pos = current->NextUsePositionRegisterIsBeneficial(next_pos); |
| 1550 | // If the range already has a spill operand and it doesn't need a |
| 1551 | // register immediately, split it and spill the first part of the range. |
| 1552 | if (pos == NULL) { |
| 1553 | Spill(current); |
| 1554 | continue; |
| 1555 | } else if (pos->pos().Value() > |
| 1556 | current->Start().NextInstruction().Value()) { |
| 1557 | // Do not spill live range eagerly if use position that can benefit from |
| 1558 | // the register is too close to the start of live range. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1559 | SpillBetween(current, current->Start(), pos->pos()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1560 | if (!AllocationOk()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1561 | ASSERT(UnhandledIsSorted()); |
| 1562 | continue; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | for (int i = 0; i < active_live_ranges_.length(); ++i) { |
| 1567 | LiveRange* cur_active = active_live_ranges_.at(i); |
| 1568 | if (cur_active->End().Value() <= position.Value()) { |
| 1569 | ActiveToHandled(cur_active); |
| 1570 | --i; // The live range was removed from the list of active live ranges. |
| 1571 | } else if (!cur_active->Covers(position)) { |
| 1572 | ActiveToInactive(cur_active); |
| 1573 | --i; // The live range was removed from the list of active live ranges. |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | for (int i = 0; i < inactive_live_ranges_.length(); ++i) { |
| 1578 | LiveRange* cur_inactive = inactive_live_ranges_.at(i); |
| 1579 | if (cur_inactive->End().Value() <= position.Value()) { |
| 1580 | InactiveToHandled(cur_inactive); |
| 1581 | --i; // Live range was removed from the list of inactive live ranges. |
| 1582 | } else if (cur_inactive->Covers(position)) { |
| 1583 | InactiveToActive(cur_inactive); |
| 1584 | --i; // Live range was removed from the list of inactive live ranges. |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | ASSERT(!current->HasRegisterAssigned() && !current->IsSpilled()); |
| 1589 | |
| 1590 | bool result = TryAllocateFreeReg(current); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1591 | if (!AllocationOk()) return; |
| 1592 | |
| 1593 | if (!result) AllocateBlockedReg(current); |
| 1594 | if (!AllocationOk()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1595 | |
| 1596 | if (current->HasRegisterAssigned()) { |
| 1597 | AddToActive(current); |
| 1598 | } |
| 1599 | } |
| 1600 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1601 | reusable_slots_.Rewind(0); |
| 1602 | active_live_ranges_.Rewind(0); |
| 1603 | inactive_live_ranges_.Rewind(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1607 | const char* LAllocator::RegisterName(int allocation_index) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1608 | if (mode_ == GENERAL_REGISTERS) { |
| 1609 | return Register::AllocationIndexToString(allocation_index); |
| 1610 | } else { |
| 1611 | return DoubleRegister::AllocationIndexToString(allocation_index); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1616 | void LAllocator::TraceAlloc(const char* msg, ...) { |
| 1617 | if (FLAG_trace_alloc) { |
| 1618 | va_list arguments; |
| 1619 | va_start(arguments, msg); |
| 1620 | OS::VPrint(msg, arguments); |
| 1621 | va_end(arguments); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1626 | bool LAllocator::HasTaggedValue(int virtual_register) const { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1627 | HValue* value = graph_->LookupValue(virtual_register); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1628 | if (value == NULL) return false; |
| 1629 | return value->representation().IsTagged(); |
| 1630 | } |
| 1631 | |
| 1632 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1633 | RegisterKind LAllocator::RequiredRegisterKind(int virtual_register) const { |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1634 | if (virtual_register < first_artificial_register_) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1635 | HValue* value = graph_->LookupValue(virtual_register); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1636 | if (value != NULL && value->representation().IsDouble()) { |
| 1637 | return DOUBLE_REGISTERS; |
| 1638 | } |
| 1639 | } else if (double_artificial_registers_.Contains( |
| 1640 | virtual_register - first_artificial_register_)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1641 | return DOUBLE_REGISTERS; |
| 1642 | } |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 1643 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1644 | return GENERAL_REGISTERS; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1648 | void LAllocator::AddToActive(LiveRange* range) { |
| 1649 | TraceAlloc("Add live range %d to active\n", range->id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1650 | active_live_ranges_.Add(range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | void LAllocator::AddToInactive(LiveRange* range) { |
| 1655 | TraceAlloc("Add live range %d to inactive\n", range->id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1656 | inactive_live_ranges_.Add(range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | |
| 1660 | void LAllocator::AddToUnhandledSorted(LiveRange* range) { |
| 1661 | if (range == NULL || range->IsEmpty()) return; |
| 1662 | ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled()); |
| 1663 | for (int i = unhandled_live_ranges_.length() - 1; i >= 0; --i) { |
| 1664 | LiveRange* cur_range = unhandled_live_ranges_.at(i); |
| 1665 | if (range->ShouldBeAllocatedBefore(cur_range)) { |
| 1666 | TraceAlloc("Add live range %d to unhandled at %d\n", range->id(), i + 1); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1667 | unhandled_live_ranges_.InsertAt(i + 1, range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1668 | ASSERT(UnhandledIsSorted()); |
| 1669 | return; |
| 1670 | } |
| 1671 | } |
| 1672 | TraceAlloc("Add live range %d to unhandled at start\n", range->id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1673 | unhandled_live_ranges_.InsertAt(0, range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1674 | ASSERT(UnhandledIsSorted()); |
| 1675 | } |
| 1676 | |
| 1677 | |
| 1678 | void LAllocator::AddToUnhandledUnsorted(LiveRange* range) { |
| 1679 | if (range == NULL || range->IsEmpty()) return; |
| 1680 | ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled()); |
| 1681 | TraceAlloc("Add live range %d to unhandled unsorted at end\n", range->id()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1682 | unhandled_live_ranges_.Add(range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | static int UnhandledSortHelper(LiveRange* const* a, LiveRange* const* b) { |
| 1687 | ASSERT(!(*a)->ShouldBeAllocatedBefore(*b) || |
| 1688 | !(*b)->ShouldBeAllocatedBefore(*a)); |
| 1689 | if ((*a)->ShouldBeAllocatedBefore(*b)) return 1; |
| 1690 | if ((*b)->ShouldBeAllocatedBefore(*a)) return -1; |
| 1691 | return (*a)->id() - (*b)->id(); |
| 1692 | } |
| 1693 | |
| 1694 | |
| 1695 | // Sort the unhandled live ranges so that the ranges to be processed first are |
| 1696 | // at the end of the array list. This is convenient for the register allocation |
| 1697 | // algorithm because it is efficient to remove elements from the end. |
| 1698 | void LAllocator::SortUnhandled() { |
| 1699 | TraceAlloc("Sort unhandled\n"); |
| 1700 | unhandled_live_ranges_.Sort(&UnhandledSortHelper); |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | bool LAllocator::UnhandledIsSorted() { |
| 1705 | int len = unhandled_live_ranges_.length(); |
| 1706 | for (int i = 1; i < len; i++) { |
| 1707 | LiveRange* a = unhandled_live_ranges_.at(i - 1); |
| 1708 | LiveRange* b = unhandled_live_ranges_.at(i); |
| 1709 | if (a->Start().Value() < b->Start().Value()) return false; |
| 1710 | } |
| 1711 | return true; |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | void LAllocator::FreeSpillSlot(LiveRange* range) { |
| 1716 | // Check that we are the last range. |
| 1717 | if (range->next() != NULL) return; |
| 1718 | |
| 1719 | if (!range->TopLevel()->HasAllocatedSpillOperand()) return; |
| 1720 | |
| 1721 | int index = range->TopLevel()->GetSpillOperand()->index(); |
| 1722 | if (index >= 0) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1723 | reusable_slots_.Add(range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | |
| 1728 | LOperand* LAllocator::TryReuseSpillSlot(LiveRange* range) { |
| 1729 | if (reusable_slots_.is_empty()) return NULL; |
| 1730 | if (reusable_slots_.first()->End().Value() > |
| 1731 | range->TopLevel()->Start().Value()) { |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | LOperand* result = reusable_slots_.first()->TopLevel()->GetSpillOperand(); |
| 1735 | reusable_slots_.Remove(0); |
| 1736 | return result; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | void LAllocator::ActiveToHandled(LiveRange* range) { |
| 1741 | ASSERT(active_live_ranges_.Contains(range)); |
| 1742 | active_live_ranges_.RemoveElement(range); |
| 1743 | TraceAlloc("Moving live range %d from active to handled\n", range->id()); |
| 1744 | FreeSpillSlot(range); |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | void LAllocator::ActiveToInactive(LiveRange* range) { |
| 1749 | ASSERT(active_live_ranges_.Contains(range)); |
| 1750 | active_live_ranges_.RemoveElement(range); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1751 | inactive_live_ranges_.Add(range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1752 | TraceAlloc("Moving live range %d from active to inactive\n", range->id()); |
| 1753 | } |
| 1754 | |
| 1755 | |
| 1756 | void LAllocator::InactiveToHandled(LiveRange* range) { |
| 1757 | ASSERT(inactive_live_ranges_.Contains(range)); |
| 1758 | inactive_live_ranges_.RemoveElement(range); |
| 1759 | TraceAlloc("Moving live range %d from inactive to handled\n", range->id()); |
| 1760 | FreeSpillSlot(range); |
| 1761 | } |
| 1762 | |
| 1763 | |
| 1764 | void LAllocator::InactiveToActive(LiveRange* range) { |
| 1765 | ASSERT(inactive_live_ranges_.Contains(range)); |
| 1766 | inactive_live_ranges_.RemoveElement(range); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1767 | active_live_ranges_.Add(range, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1768 | TraceAlloc("Moving live range %d from inactive to active\n", range->id()); |
| 1769 | } |
| 1770 | |
| 1771 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1772 | // TryAllocateFreeReg and AllocateBlockedReg assume this |
| 1773 | // when allocating local arrays. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1774 | STATIC_ASSERT(DoubleRegister::kMaxNumAllocatableRegisters >= |
| 1775 | Register::kMaxNumAllocatableRegisters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1776 | |
| 1777 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1778 | bool LAllocator::TryAllocateFreeReg(LiveRange* current) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1779 | LifetimePosition free_until_pos[DoubleRegister::kMaxNumAllocatableRegisters]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1780 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1781 | for (int i = 0; i < DoubleRegister::kMaxNumAllocatableRegisters; i++) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1782 | free_until_pos[i] = LifetimePosition::MaxPosition(); |
| 1783 | } |
| 1784 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1785 | for (int i = 0; i < active_live_ranges_.length(); ++i) { |
| 1786 | LiveRange* cur_active = active_live_ranges_.at(i); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1787 | free_until_pos[cur_active->assigned_register()] = |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1788 | LifetimePosition::FromInstructionIndex(0); |
| 1789 | } |
| 1790 | |
| 1791 | for (int i = 0; i < inactive_live_ranges_.length(); ++i) { |
| 1792 | LiveRange* cur_inactive = inactive_live_ranges_.at(i); |
| 1793 | ASSERT(cur_inactive->End().Value() > current->Start().Value()); |
| 1794 | LifetimePosition next_intersection = |
| 1795 | cur_inactive->FirstIntersection(current); |
| 1796 | if (!next_intersection.IsValid()) continue; |
| 1797 | int cur_reg = cur_inactive->assigned_register(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1798 | free_until_pos[cur_reg] = Min(free_until_pos[cur_reg], next_intersection); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1801 | UsePosition* hinted_use = current->FirstPosWithHint(); |
| 1802 | if (hinted_use != NULL) { |
| 1803 | LOperand* hint = hinted_use->hint(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1804 | if (hint->IsRegister() || hint->IsDoubleRegister()) { |
| 1805 | int register_index = hint->index(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1806 | TraceAlloc( |
| 1807 | "Found reg hint %s (free until [%d) for live range %d (end %d[).\n", |
| 1808 | RegisterName(register_index), |
| 1809 | free_until_pos[register_index].Value(), |
| 1810 | current->id(), |
| 1811 | current->End().Value()); |
| 1812 | |
| 1813 | // The desired register is free until the end of the current live range. |
| 1814 | if (free_until_pos[register_index].Value() >= current->End().Value()) { |
| 1815 | TraceAlloc("Assigning preferred reg %s to live range %d\n", |
| 1816 | RegisterName(register_index), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1817 | current->id()); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 1818 | SetLiveRangeAssignedRegister(current, register_index, mode_, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1819 | return true; |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1824 | // Find the register which stays free for the longest time. |
| 1825 | int reg = 0; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1826 | for (int i = 1; i < RegisterCount(); ++i) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1827 | if (free_until_pos[i].Value() > free_until_pos[reg].Value()) { |
| 1828 | reg = i; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1832 | LifetimePosition pos = free_until_pos[reg]; |
| 1833 | |
| 1834 | if (pos.Value() <= current->Start().Value()) { |
| 1835 | // All registers are blocked. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1836 | return false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1839 | if (pos.Value() < current->End().Value()) { |
| 1840 | // Register reg is available at the range start but becomes blocked before |
| 1841 | // the range end. Split current at position where it becomes blocked. |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1842 | LiveRange* tail = SplitRangeAt(current, pos); |
| 1843 | if (!AllocationOk()) return false; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1844 | AddToUnhandledSorted(tail); |
| 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | // Register reg is available at the range start and is free until |
| 1849 | // the range end. |
| 1850 | ASSERT(pos.Value() >= current->End().Value()); |
| 1851 | TraceAlloc("Assigning free reg %s to live range %d\n", |
| 1852 | RegisterName(reg), |
| 1853 | current->id()); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 1854 | SetLiveRangeAssignedRegister(current, reg, mode_, zone_); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1855 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1856 | return true; |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | void LAllocator::AllocateBlockedReg(LiveRange* current) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1861 | UsePosition* register_use = current->NextRegisterPosition(current->Start()); |
| 1862 | if (register_use == NULL) { |
| 1863 | // There is no use in the current live range that requires a register. |
| 1864 | // We can just spill it. |
| 1865 | Spill(current); |
| 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1870 | LifetimePosition use_pos[DoubleRegister::kMaxNumAllocatableRegisters]; |
| 1871 | LifetimePosition block_pos[DoubleRegister::kMaxNumAllocatableRegisters]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1872 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1873 | for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); i++) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1874 | use_pos[i] = block_pos[i] = LifetimePosition::MaxPosition(); |
| 1875 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1876 | |
| 1877 | for (int i = 0; i < active_live_ranges_.length(); ++i) { |
| 1878 | LiveRange* range = active_live_ranges_[i]; |
| 1879 | int cur_reg = range->assigned_register(); |
| 1880 | if (range->IsFixed() || !range->CanBeSpilled(current->Start())) { |
| 1881 | block_pos[cur_reg] = use_pos[cur_reg] = |
| 1882 | LifetimePosition::FromInstructionIndex(0); |
| 1883 | } else { |
| 1884 | UsePosition* next_use = range->NextUsePositionRegisterIsBeneficial( |
| 1885 | current->Start()); |
| 1886 | if (next_use == NULL) { |
| 1887 | use_pos[cur_reg] = range->End(); |
| 1888 | } else { |
| 1889 | use_pos[cur_reg] = next_use->pos(); |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | for (int i = 0; i < inactive_live_ranges_.length(); ++i) { |
| 1895 | LiveRange* range = inactive_live_ranges_.at(i); |
| 1896 | ASSERT(range->End().Value() > current->Start().Value()); |
| 1897 | LifetimePosition next_intersection = range->FirstIntersection(current); |
| 1898 | if (!next_intersection.IsValid()) continue; |
| 1899 | int cur_reg = range->assigned_register(); |
| 1900 | if (range->IsFixed()) { |
| 1901 | block_pos[cur_reg] = Min(block_pos[cur_reg], next_intersection); |
| 1902 | use_pos[cur_reg] = Min(block_pos[cur_reg], use_pos[cur_reg]); |
| 1903 | } else { |
| 1904 | use_pos[cur_reg] = Min(use_pos[cur_reg], next_intersection); |
| 1905 | } |
| 1906 | } |
| 1907 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1908 | int reg = 0; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1909 | for (int i = 1; i < RegisterCount(); ++i) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1910 | if (use_pos[i].Value() > use_pos[reg].Value()) { |
| 1911 | reg = i; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1915 | LifetimePosition pos = use_pos[reg]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1916 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1917 | if (pos.Value() < register_use->pos().Value()) { |
| 1918 | // All registers are blocked before the first use that requires a register. |
| 1919 | // Spill starting part of live range up to that use. |
| 1920 | // |
| 1921 | // Corner case: the first use position is equal to the start of the range. |
| 1922 | // In this case we have nothing to spill and SpillBetween will just return |
| 1923 | // this range to the list of unhandled ones. This will lead to the infinite |
| 1924 | // loop. |
| 1925 | ASSERT(current->Start().Value() < register_use->pos().Value()); |
| 1926 | SpillBetween(current, current->Start(), register_use->pos()); |
| 1927 | return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1928 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1929 | |
| 1930 | if (block_pos[reg].Value() < current->End().Value()) { |
| 1931 | // Register becomes blocked before the current range end. Split before that |
| 1932 | // position. |
| 1933 | LiveRange* tail = SplitBetween(current, |
| 1934 | current->Start(), |
| 1935 | block_pos[reg].InstructionStart()); |
| 1936 | AddToUnhandledSorted(tail); |
| 1937 | } |
| 1938 | |
| 1939 | // Register reg is not blocked for the whole range. |
| 1940 | ASSERT(block_pos[reg].Value() >= current->End().Value()); |
| 1941 | TraceAlloc("Assigning blocked reg %s to live range %d\n", |
| 1942 | RegisterName(reg), |
| 1943 | current->id()); |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 1944 | SetLiveRangeAssignedRegister(current, reg, mode_, zone_); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1945 | |
| 1946 | // This register was not free. Thus we need to find and spill |
| 1947 | // parts of active and inactive live regions that use the same register |
| 1948 | // at the same lifetime positions as current. |
| 1949 | SplitAndSpillIntersecting(current); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | |
| 1953 | void LAllocator::SplitAndSpillIntersecting(LiveRange* current) { |
| 1954 | ASSERT(current->HasRegisterAssigned()); |
| 1955 | int reg = current->assigned_register(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1956 | LifetimePosition split_pos = current->Start(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1957 | for (int i = 0; i < active_live_ranges_.length(); ++i) { |
| 1958 | LiveRange* range = active_live_ranges_[i]; |
| 1959 | if (range->assigned_register() == reg) { |
| 1960 | UsePosition* next_pos = range->NextRegisterPosition(current->Start()); |
| 1961 | if (next_pos == NULL) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1962 | SpillAfter(range, split_pos); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1963 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1964 | SpillBetween(range, split_pos, next_pos->pos()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1965 | } |
| 1966 | ActiveToHandled(range); |
| 1967 | --i; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | for (int i = 0; i < inactive_live_ranges_.length(); ++i) { |
| 1972 | LiveRange* range = inactive_live_ranges_[i]; |
| 1973 | ASSERT(range->End().Value() > current->Start().Value()); |
| 1974 | if (range->assigned_register() == reg && !range->IsFixed()) { |
| 1975 | LifetimePosition next_intersection = range->FirstIntersection(current); |
| 1976 | if (next_intersection.IsValid()) { |
| 1977 | UsePosition* next_pos = range->NextRegisterPosition(current->Start()); |
| 1978 | if (next_pos == NULL) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1979 | SpillAfter(range, split_pos); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1980 | } else { |
| 1981 | next_intersection = Min(next_intersection, next_pos->pos()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1982 | SpillBetween(range, split_pos, next_intersection); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1983 | } |
| 1984 | InactiveToHandled(range); |
| 1985 | --i; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1992 | bool LAllocator::IsBlockBoundary(LifetimePosition pos) { |
| 1993 | return pos.IsInstructionStart() && |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1994 | InstructionAt(pos.InstructionIndex())->IsLabel(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1998 | LiveRange* LAllocator::SplitRangeAt(LiveRange* range, LifetimePosition pos) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1999 | ASSERT(!range->IsFixed()); |
| 2000 | TraceAlloc("Splitting live range %d at %d\n", range->id(), pos.Value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2001 | |
| 2002 | if (pos.Value() <= range->Start().Value()) return range; |
| 2003 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2004 | // We can't properly connect liveranges if split occured at the end |
| 2005 | // of control instruction. |
| 2006 | ASSERT(pos.IsInstructionStart() || |
| 2007 | !chunk_->instructions()->at(pos.InstructionIndex())->IsControl()); |
| 2008 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2009 | LiveRange* result = LiveRangeFor(GetVirtualRegister()); |
| 2010 | if (!AllocationOk()) return NULL; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2011 | range->SplitAt(pos, result, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2012 | return result; |
| 2013 | } |
| 2014 | |
| 2015 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2016 | LiveRange* LAllocator::SplitBetween(LiveRange* range, |
| 2017 | LifetimePosition start, |
| 2018 | LifetimePosition end) { |
| 2019 | ASSERT(!range->IsFixed()); |
| 2020 | TraceAlloc("Splitting live range %d in position between [%d, %d]\n", |
| 2021 | range->id(), |
| 2022 | start.Value(), |
| 2023 | end.Value()); |
| 2024 | |
| 2025 | LifetimePosition split_pos = FindOptimalSplitPos(start, end); |
| 2026 | ASSERT(split_pos.Value() >= start.Value()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2027 | return SplitRangeAt(range, split_pos); |
kmillikin@chromium.org | facd82d | 2010-12-13 18:00:51 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2031 | LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start, |
| 2032 | LifetimePosition end) { |
| 2033 | int start_instr = start.InstructionIndex(); |
| 2034 | int end_instr = end.InstructionIndex(); |
| 2035 | ASSERT(start_instr <= end_instr); |
| 2036 | |
| 2037 | // We have no choice |
| 2038 | if (start_instr == end_instr) return end; |
| 2039 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2040 | HBasicBlock* start_block = GetBlock(start); |
| 2041 | HBasicBlock* end_block = GetBlock(end); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2042 | |
| 2043 | if (end_block == start_block) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2044 | // The interval is split in the same basic block. Split at the latest |
| 2045 | // possible position. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2046 | return end; |
| 2047 | } |
| 2048 | |
| 2049 | HBasicBlock* block = end_block; |
| 2050 | // Find header of outermost loop. |
| 2051 | while (block->parent_loop_header() != NULL && |
| 2052 | block->parent_loop_header()->block_id() > start_block->block_id()) { |
| 2053 | block = block->parent_loop_header(); |
| 2054 | } |
| 2055 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 2056 | // We did not find any suitable outer loop. Split at the latest possible |
| 2057 | // position unless end_block is a loop header itself. |
| 2058 | if (block == end_block && !end_block->IsLoopHeader()) return end; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2059 | |
| 2060 | return LifetimePosition::FromInstructionIndex( |
| 2061 | block->first_instruction_index()); |
| 2062 | } |
| 2063 | |
| 2064 | |
| 2065 | void LAllocator::SpillAfter(LiveRange* range, LifetimePosition pos) { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2066 | LiveRange* second_part = SplitRangeAt(range, pos); |
| 2067 | if (!AllocationOk()) return; |
lrn@chromium.org | 8541d77 | 2010-12-15 12:05:09 +0000 | [diff] [blame] | 2068 | Spill(second_part); |
| 2069 | } |
| 2070 | |
| 2071 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2072 | void LAllocator::SpillBetween(LiveRange* range, |
| 2073 | LifetimePosition start, |
| 2074 | LifetimePosition end) { |
| 2075 | ASSERT(start.Value() < end.Value()); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2076 | LiveRange* second_part = SplitRangeAt(range, start); |
| 2077 | if (!AllocationOk()) return; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2078 | |
| 2079 | if (second_part->Start().Value() < end.Value()) { |
| 2080 | // The split result intersects with [start, end[. |
| 2081 | // Split it at position between ]start+1, end[, spill the middle part |
| 2082 | // and put the rest to unhandled. |
| 2083 | LiveRange* third_part = SplitBetween( |
| 2084 | second_part, |
| 2085 | second_part->Start().InstructionEnd(), |
| 2086 | end.PrevInstruction().InstructionEnd()); |
| 2087 | |
| 2088 | ASSERT(third_part != second_part); |
| 2089 | |
| 2090 | Spill(second_part); |
| 2091 | AddToUnhandledSorted(third_part); |
| 2092 | } else { |
| 2093 | // The split result does not intersect with [start, end[. |
| 2094 | // Nothing to spill. Just put it to unhandled as whole. |
| 2095 | AddToUnhandledSorted(second_part); |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2100 | void LAllocator::Spill(LiveRange* range) { |
| 2101 | ASSERT(!range->IsSpilled()); |
| 2102 | TraceAlloc("Spilling live range %d\n", range->id()); |
| 2103 | LiveRange* first = range->TopLevel(); |
| 2104 | |
| 2105 | if (!first->HasAllocatedSpillOperand()) { |
| 2106 | LOperand* op = TryReuseSpillSlot(range); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2107 | if (op == NULL) op = chunk_->GetNextSpillSlot(mode_ == DOUBLE_REGISTERS); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2108 | first->SetSpillOperand(op); |
| 2109 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2110 | range->MakeSpilled(zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | int LAllocator::RegisterCount() const { |
| 2115 | return num_registers_; |
| 2116 | } |
| 2117 | |
| 2118 | |
| 2119 | #ifdef DEBUG |
| 2120 | |
| 2121 | |
| 2122 | void LAllocator::Verify() const { |
| 2123 | for (int i = 0; i < live_ranges()->length(); ++i) { |
| 2124 | LiveRange* current = live_ranges()->at(i); |
| 2125 | if (current != NULL) current->Verify(); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | |
| 2130 | #endif |
| 2131 | |
| 2132 | |
| 2133 | } } // namespace v8::internal |