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