| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "register_allocator.h" |
| 18 | |
| Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 19 | #include <sstream> |
| 20 | |
| Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 21 | #include "base/bit_vector-inl.h" |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 22 | #include "code_generator.h" |
| 23 | #include "ssa_liveness_analysis.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | static constexpr size_t kMaxLifetimePosition = -1; |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 28 | static constexpr size_t kDefaultNumberOfSpillSlots = 4; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 29 | |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 30 | // For simplicity, we implement register pairs as (reg, reg + 1). |
| 31 | // Note that this is a requirement for double registers on ARM, since we |
| 32 | // allocate SRegister. |
| 33 | static int GetHighForLowRegister(int reg) { return reg + 1; } |
| 34 | static bool IsLowRegister(int reg) { return (reg & 1) == 0; } |
| 35 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 36 | RegisterAllocator::RegisterAllocator(ArenaAllocator* allocator, |
| 37 | CodeGenerator* codegen, |
| 38 | const SsaLivenessAnalysis& liveness) |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 39 | : allocator_(allocator), |
| 40 | codegen_(codegen), |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 41 | liveness_(liveness), |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 42 | unhandled_core_intervals_(allocator, 0), |
| 43 | unhandled_fp_intervals_(allocator, 0), |
| 44 | unhandled_(nullptr), |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 45 | handled_(allocator, 0), |
| 46 | active_(allocator, 0), |
| 47 | inactive_(allocator, 0), |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 48 | physical_core_register_intervals_(allocator, codegen->GetNumberOfCoreRegisters()), |
| 49 | physical_fp_register_intervals_(allocator, codegen->GetNumberOfFloatingPointRegisters()), |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 50 | temp_intervals_(allocator, 4), |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 51 | int_spill_slots_(allocator, kDefaultNumberOfSpillSlots), |
| 52 | long_spill_slots_(allocator, kDefaultNumberOfSpillSlots), |
| 53 | float_spill_slots_(allocator, kDefaultNumberOfSpillSlots), |
| 54 | double_spill_slots_(allocator, kDefaultNumberOfSpillSlots), |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 55 | safepoints_(allocator, 0), |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 56 | processing_core_registers_(false), |
| 57 | number_of_registers_(-1), |
| 58 | registers_array_(nullptr), |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 59 | blocked_core_registers_(codegen->GetBlockedCoreRegisters()), |
| 60 | blocked_fp_registers_(codegen->GetBlockedFloatingPointRegisters()), |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 61 | reserved_out_slots_(0), |
| Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 62 | maximum_number_of_live_core_registers_(0), |
| 63 | maximum_number_of_live_fp_registers_(0) { |
| Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 64 | static constexpr bool kIsBaseline = false; |
| 65 | codegen->SetupBlockedRegisters(kIsBaseline); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 66 | physical_core_register_intervals_.SetSize(codegen->GetNumberOfCoreRegisters()); |
| 67 | physical_fp_register_intervals_.SetSize(codegen->GetNumberOfFloatingPointRegisters()); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 68 | // Always reserve for the current method and the graph's max out registers. |
| 69 | // TODO: compute it instead. |
| 70 | reserved_out_slots_ = 1 + codegen->GetGraph()->GetMaximumNumberOfOutVRegs(); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 73 | bool RegisterAllocator::CanAllocateRegistersFor(const HGraph& graph, |
| 74 | InstructionSet instruction_set) { |
| 75 | if (!Supports(instruction_set)) { |
| 76 | return false; |
| 77 | } |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 78 | if (instruction_set == kArm64 |
| 79 | || instruction_set == kX86_64 |
| 80 | || instruction_set == kArm |
| 81 | || instruction_set == kThumb2) { |
| Alexandre Rames | 3e69f16 | 2014-12-10 10:36:50 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 84 | for (size_t i = 0, e = graph.GetBlocks().Size(); i < e; ++i) { |
| 85 | for (HInstructionIterator it(graph.GetBlocks().Get(i)->GetInstructions()); |
| 86 | !it.Done(); |
| 87 | it.Advance()) { |
| 88 | HInstruction* current = it.Current(); |
| Mark Mendell | 7c8d009 | 2015-01-26 11:21:33 -0500 | [diff] [blame] | 89 | if (instruction_set == kX86 && current->GetType() == Primitive::kPrimLong) { |
| 90 | return false; |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 91 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static bool ShouldProcess(bool processing_core_registers, LiveInterval* interval) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 98 | if (interval == nullptr) return false; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 99 | bool is_core_register = (interval->GetType() != Primitive::kPrimDouble) |
| 100 | && (interval->GetType() != Primitive::kPrimFloat); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 101 | return processing_core_registers == is_core_register; |
| 102 | } |
| 103 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 104 | void RegisterAllocator::AllocateRegisters() { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 105 | AllocateRegistersInternal(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 106 | Resolve(); |
| 107 | |
| 108 | if (kIsDebugBuild) { |
| 109 | processing_core_registers_ = true; |
| 110 | ValidateInternal(true); |
| 111 | processing_core_registers_ = false; |
| 112 | ValidateInternal(true); |
| Nicolas Geoffray | 5976857 | 2014-12-01 09:50:04 +0000 | [diff] [blame] | 113 | // Check that the linear order is still correct with regards to lifetime positions. |
| 114 | // Since only parallel moves have been inserted during the register allocation, |
| 115 | // these checks are mostly for making sure these moves have been added correctly. |
| 116 | size_t current_liveness = 0; |
| 117 | for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) { |
| 118 | HBasicBlock* block = it.Current(); |
| 119 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 120 | HInstruction* instruction = inst_it.Current(); |
| 121 | DCHECK_LE(current_liveness, instruction->GetLifetimePosition()); |
| 122 | current_liveness = instruction->GetLifetimePosition(); |
| 123 | } |
| 124 | for (HInstructionIterator inst_it(block->GetInstructions()); |
| 125 | !inst_it.Done(); |
| 126 | inst_it.Advance()) { |
| 127 | HInstruction* instruction = inst_it.Current(); |
| 128 | DCHECK_LE(current_liveness, instruction->GetLifetimePosition()) << instruction->DebugName(); |
| 129 | current_liveness = instruction->GetLifetimePosition(); |
| 130 | } |
| 131 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | void RegisterAllocator::BlockRegister(Location location, |
| 136 | size_t start, |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 137 | size_t end) { |
| Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 138 | int reg = location.reg(); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 139 | DCHECK(location.IsRegister() || location.IsFpuRegister()); |
| 140 | LiveInterval* interval = location.IsRegister() |
| 141 | ? physical_core_register_intervals_.Get(reg) |
| 142 | : physical_fp_register_intervals_.Get(reg); |
| 143 | Primitive::Type type = location.IsRegister() |
| 144 | ? Primitive::kPrimInt |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 145 | : Primitive::kPrimFloat; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 146 | if (interval == nullptr) { |
| 147 | interval = LiveInterval::MakeFixedInterval(allocator_, reg, type); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 148 | if (location.IsRegister()) { |
| 149 | physical_core_register_intervals_.Put(reg, interval); |
| 150 | } else { |
| 151 | physical_fp_register_intervals_.Put(reg, interval); |
| 152 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 153 | } |
| 154 | DCHECK(interval->GetRegister() == reg); |
| 155 | interval->AddRange(start, end); |
| 156 | } |
| 157 | |
| 158 | void RegisterAllocator::AllocateRegistersInternal() { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 159 | // Iterate post-order, to ensure the list is sorted, and the last added interval |
| 160 | // is the one with the lowest start position. |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 161 | for (HLinearPostOrderIterator it(liveness_); !it.Done(); it.Advance()) { |
| 162 | HBasicBlock* block = it.Current(); |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 163 | for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done(); |
| 164 | back_it.Advance()) { |
| 165 | ProcessInstruction(back_it.Current()); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 166 | } |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 167 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 168 | ProcessInstruction(inst_it.Current()); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 172 | number_of_registers_ = codegen_->GetNumberOfCoreRegisters(); |
| 173 | registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_); |
| 174 | processing_core_registers_ = true; |
| 175 | unhandled_ = &unhandled_core_intervals_; |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 176 | for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) { |
| 177 | LiveInterval* fixed = physical_core_register_intervals_.Get(i); |
| 178 | if (fixed != nullptr) { |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 179 | // Fixed interval is added to inactive_ instead of unhandled_. |
| 180 | // It's also the only type of inactive interval whose start position |
| 181 | // can be after the current interval during linear scan. |
| 182 | // Fixed interval is never split and never moves to unhandled_. |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 183 | inactive_.Add(fixed); |
| 184 | } |
| 185 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 186 | LinearScan(); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 187 | |
| 188 | inactive_.Reset(); |
| 189 | active_.Reset(); |
| 190 | handled_.Reset(); |
| 191 | |
| 192 | number_of_registers_ = codegen_->GetNumberOfFloatingPointRegisters(); |
| 193 | registers_array_ = allocator_->AllocArray<size_t>(number_of_registers_); |
| 194 | processing_core_registers_ = false; |
| 195 | unhandled_ = &unhandled_fp_intervals_; |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 196 | for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) { |
| 197 | LiveInterval* fixed = physical_fp_register_intervals_.Get(i); |
| 198 | if (fixed != nullptr) { |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 199 | // Fixed interval is added to inactive_ instead of unhandled_. |
| 200 | // It's also the only type of inactive interval whose start position |
| 201 | // can be after the current interval during linear scan. |
| 202 | // Fixed interval is never split and never moves to unhandled_. |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 203 | inactive_.Add(fixed); |
| 204 | } |
| 205 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 206 | LinearScan(); |
| 207 | } |
| 208 | |
| 209 | void RegisterAllocator::ProcessInstruction(HInstruction* instruction) { |
| 210 | LocationSummary* locations = instruction->GetLocations(); |
| 211 | size_t position = instruction->GetLifetimePosition(); |
| 212 | |
| 213 | if (locations == nullptr) return; |
| 214 | |
| 215 | // Create synthesized intervals for temporaries. |
| 216 | for (size_t i = 0; i < locations->GetTempCount(); ++i) { |
| 217 | Location temp = locations->GetTemp(i); |
| Nicolas Geoffray | 52839d1 | 2014-11-07 17:47:25 +0000 | [diff] [blame] | 218 | if (temp.IsRegister() || temp.IsFpuRegister()) { |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 219 | BlockRegister(temp, position, position + 1); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 220 | } else { |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 221 | DCHECK(temp.IsUnallocated()); |
| Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 222 | switch (temp.GetPolicy()) { |
| 223 | case Location::kRequiresRegister: { |
| 224 | LiveInterval* interval = |
| 225 | LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimInt); |
| 226 | temp_intervals_.Add(interval); |
| 227 | interval->AddRange(position, position + 1); |
| 228 | unhandled_core_intervals_.Add(interval); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | case Location::kRequiresFpuRegister: { |
| 233 | LiveInterval* interval = |
| 234 | LiveInterval::MakeTempInterval(allocator_, Primitive::kPrimDouble); |
| 235 | temp_intervals_.Add(interval); |
| 236 | interval->AddRange(position, position + 1); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 237 | if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) { |
| 238 | interval->AddHighInterval(true); |
| 239 | LiveInterval* high = interval->GetHighInterval(); |
| 240 | temp_intervals_.Add(high); |
| 241 | unhandled_fp_intervals_.Add(high); |
| 242 | } |
| Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 243 | unhandled_fp_intervals_.Add(interval); |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | default: |
| 248 | LOG(FATAL) << "Unexpected policy for temporary location " |
| 249 | << temp.GetPolicy(); |
| 250 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 254 | bool core_register = (instruction->GetType() != Primitive::kPrimDouble) |
| 255 | && (instruction->GetType() != Primitive::kPrimFloat); |
| 256 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 257 | if (locations->CanCall()) { |
| Nicolas Geoffray | c0572a4 | 2015-02-06 14:35:25 +0000 | [diff] [blame] | 258 | if (codegen_->IsLeafMethod()) { |
| 259 | // TODO: We do this here because we do not want the suspend check to artificially |
| 260 | // create live registers. We should find another place, but this is currently the |
| 261 | // simplest. |
| 262 | DCHECK(instruction->IsSuspendCheckEntry()); |
| 263 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 264 | return; |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 265 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 266 | safepoints_.Add(instruction); |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 267 | if (locations->OnlyCallsOnSlowPath()) { |
| 268 | // We add a synthesized range at this position to record the live registers |
| 269 | // at this position. Ideally, we could just update the safepoints when locations |
| 270 | // are updated, but we currently need to know the full stack size before updating |
| 271 | // locations (because of parameters and the fact that we don't have a frame pointer). |
| 272 | // And knowing the full stack size requires to know the maximum number of live |
| 273 | // registers at calls in slow paths. |
| 274 | // By adding the following interval in the algorithm, we can compute this |
| 275 | // maximum before updating locations. |
| 276 | LiveInterval* interval = LiveInterval::MakeSlowPathInterval(allocator_, instruction); |
| Nicolas Geoffray | acd0339 | 2014-11-26 15:46:52 +0000 | [diff] [blame] | 277 | interval->AddRange(position, position + 1); |
| Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 278 | AddSorted(&unhandled_core_intervals_, interval); |
| 279 | AddSorted(&unhandled_fp_intervals_, interval); |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | if (locations->WillCall()) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 284 | // Block all registers. |
| 285 | for (size_t i = 0; i < codegen_->GetNumberOfCoreRegisters(); ++i) { |
| Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 286 | if (!codegen_->IsCoreCalleeSaveRegister(i)) { |
| 287 | BlockRegister(Location::RegisterLocation(i), |
| 288 | position, |
| 289 | position + 1); |
| 290 | } |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 291 | } |
| 292 | for (size_t i = 0; i < codegen_->GetNumberOfFloatingPointRegisters(); ++i) { |
| Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 293 | if (!codegen_->IsFloatingPointCalleeSaveRegister(i)) { |
| 294 | BlockRegister(Location::FpuRegisterLocation(i), |
| 295 | position, |
| 296 | position + 1); |
| 297 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | for (size_t i = 0; i < instruction->InputCount(); ++i) { |
| 302 | Location input = locations->InAt(i); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 303 | if (input.IsRegister() || input.IsFpuRegister()) { |
| 304 | BlockRegister(input, position, position + 1); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 305 | } else if (input.IsPair()) { |
| 306 | BlockRegister(input.ToLow(), position, position + 1); |
| 307 | BlockRegister(input.ToHigh(), position, position + 1); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 311 | LiveInterval* current = instruction->GetLiveInterval(); |
| 312 | if (current == nullptr) return; |
| 313 | |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 314 | GrowableArray<LiveInterval*>& unhandled = core_register |
| 315 | ? unhandled_core_intervals_ |
| 316 | : unhandled_fp_intervals_; |
| 317 | |
| Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 318 | DCHECK(unhandled.IsEmpty() || current->StartsBeforeOrAt(unhandled.Peek())); |
| Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 319 | |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 320 | if (codegen_->NeedsTwoRegisters(current->GetType())) { |
| 321 | current->AddHighInterval(); |
| 322 | } |
| 323 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 324 | // Some instructions define their output in fixed register/stack slot. We need |
| 325 | // to ensure we know these locations before doing register allocation. For a |
| 326 | // given register, we create an interval that covers these locations. The register |
| 327 | // will be unavailable at these locations when trying to allocate one for an |
| 328 | // interval. |
| 329 | // |
| 330 | // The backwards walking ensures the ranges are ordered on increasing start positions. |
| 331 | Location output = locations->Out(); |
| Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 332 | if (output.IsUnallocated() && output.GetPolicy() == Location::kSameAsFirstInput) { |
| 333 | Location first = locations->InAt(0); |
| 334 | if (first.IsRegister() || first.IsFpuRegister()) { |
| 335 | current->SetFrom(position + 1); |
| 336 | current->SetRegister(first.reg()); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 337 | } else if (first.IsPair()) { |
| 338 | current->SetFrom(position + 1); |
| 339 | current->SetRegister(first.low()); |
| 340 | LiveInterval* high = current->GetHighInterval(); |
| 341 | high->SetRegister(first.high()); |
| 342 | high->SetFrom(position + 1); |
| Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 343 | } |
| 344 | } else if (output.IsRegister() || output.IsFpuRegister()) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 345 | // Shift the interval's start by one to account for the blocked register. |
| 346 | current->SetFrom(position + 1); |
| Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 347 | current->SetRegister(output.reg()); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 348 | BlockRegister(output, position, position + 1); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 349 | } else if (output.IsPair()) { |
| 350 | current->SetFrom(position + 1); |
| 351 | current->SetRegister(output.low()); |
| 352 | LiveInterval* high = current->GetHighInterval(); |
| 353 | high->SetRegister(output.high()); |
| 354 | high->SetFrom(position + 1); |
| 355 | BlockRegister(output.ToLow(), position, position + 1); |
| 356 | BlockRegister(output.ToHigh(), position, position + 1); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 357 | } else if (output.IsStackSlot() || output.IsDoubleStackSlot()) { |
| 358 | current->SetSpillSlot(output.GetStackIndex()); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 359 | } else { |
| 360 | DCHECK(output.IsUnallocated() || output.IsConstant()); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // If needed, add interval to the list of unhandled intervals. |
| 364 | if (current->HasSpillSlot() || instruction->IsConstant()) { |
| Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 365 | // Split just before first register use. |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 366 | size_t first_register_use = current->FirstRegisterUse(); |
| 367 | if (first_register_use != kNoLifetime) { |
| Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 368 | LiveInterval* split = Split(current, first_register_use - 1); |
| Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 369 | // Don't add directly to `unhandled`, it needs to be sorted and the start |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 370 | // of this new interval might be after intervals already in the list. |
| 371 | AddSorted(&unhandled, split); |
| 372 | } else { |
| 373 | // Nothing to do, we won't allocate a register for this value. |
| 374 | } |
| 375 | } else { |
| Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 376 | // Don't add directly to `unhandled`, temp or safepoint intervals |
| 377 | // for this instruction may have been added, and those can be |
| 378 | // processed first. |
| 379 | AddSorted(&unhandled, current); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 380 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 383 | class AllRangesIterator : public ValueObject { |
| 384 | public: |
| 385 | explicit AllRangesIterator(LiveInterval* interval) |
| 386 | : current_interval_(interval), |
| 387 | current_range_(interval->GetFirstRange()) {} |
| 388 | |
| 389 | bool Done() const { return current_interval_ == nullptr; } |
| 390 | LiveRange* CurrentRange() const { return current_range_; } |
| 391 | LiveInterval* CurrentInterval() const { return current_interval_; } |
| 392 | |
| 393 | void Advance() { |
| 394 | current_range_ = current_range_->GetNext(); |
| 395 | if (current_range_ == nullptr) { |
| 396 | current_interval_ = current_interval_->GetNextSibling(); |
| 397 | if (current_interval_ != nullptr) { |
| 398 | current_range_ = current_interval_->GetFirstRange(); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | private: |
| 404 | LiveInterval* current_interval_; |
| 405 | LiveRange* current_range_; |
| 406 | |
| 407 | DISALLOW_COPY_AND_ASSIGN(AllRangesIterator); |
| 408 | }; |
| 409 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 410 | bool RegisterAllocator::ValidateInternal(bool log_fatal_on_failure) const { |
| 411 | // To simplify unit testing, we eagerly create the array of intervals, and |
| 412 | // call the helper method. |
| 413 | GrowableArray<LiveInterval*> intervals(allocator_, 0); |
| 414 | for (size_t i = 0; i < liveness_.GetNumberOfSsaValues(); ++i) { |
| 415 | HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); |
| 416 | if (ShouldProcess(processing_core_registers_, instruction->GetLiveInterval())) { |
| 417 | intervals.Add(instruction->GetLiveInterval()); |
| 418 | } |
| 419 | } |
| 420 | |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 421 | if (processing_core_registers_) { |
| 422 | for (size_t i = 0, e = physical_core_register_intervals_.Size(); i < e; ++i) { |
| 423 | LiveInterval* fixed = physical_core_register_intervals_.Get(i); |
| 424 | if (fixed != nullptr) { |
| 425 | intervals.Add(fixed); |
| 426 | } |
| 427 | } |
| 428 | } else { |
| 429 | for (size_t i = 0, e = physical_fp_register_intervals_.Size(); i < e; ++i) { |
| 430 | LiveInterval* fixed = physical_fp_register_intervals_.Get(i); |
| 431 | if (fixed != nullptr) { |
| 432 | intervals.Add(fixed); |
| 433 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 437 | for (size_t i = 0, e = temp_intervals_.Size(); i < e; ++i) { |
| 438 | LiveInterval* temp = temp_intervals_.Get(i); |
| 439 | if (ShouldProcess(processing_core_registers_, temp)) { |
| 440 | intervals.Add(temp); |
| 441 | } |
| 442 | } |
| 443 | |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 444 | return ValidateIntervals(intervals, GetNumberOfSpillSlots(), reserved_out_slots_, *codegen_, |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 445 | allocator_, processing_core_registers_, log_fatal_on_failure); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 448 | bool RegisterAllocator::ValidateIntervals(const GrowableArray<LiveInterval*>& intervals, |
| 449 | size_t number_of_spill_slots, |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 450 | size_t number_of_out_slots, |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 451 | const CodeGenerator& codegen, |
| 452 | ArenaAllocator* allocator, |
| 453 | bool processing_core_registers, |
| 454 | bool log_fatal_on_failure) { |
| 455 | size_t number_of_registers = processing_core_registers |
| 456 | ? codegen.GetNumberOfCoreRegisters() |
| 457 | : codegen.GetNumberOfFloatingPointRegisters(); |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 458 | GrowableArray<ArenaBitVector*> liveness_of_values( |
| 459 | allocator, number_of_registers + number_of_spill_slots); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 460 | |
| 461 | // Allocate a bit vector per register. A live interval that has a register |
| 462 | // allocated will populate the associated bit vector based on its live ranges. |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 463 | for (size_t i = 0; i < number_of_registers + number_of_spill_slots; ++i) { |
| 464 | liveness_of_values.Add(new (allocator) ArenaBitVector(allocator, 0, true)); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 467 | for (size_t i = 0, e = intervals.Size(); i < e; ++i) { |
| 468 | for (AllRangesIterator it(intervals.Get(i)); !it.Done(); it.Advance()) { |
| 469 | LiveInterval* current = it.CurrentInterval(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 470 | HInstruction* defined_by = current->GetParent()->GetDefinedBy(); |
| 471 | if (current->GetParent()->HasSpillSlot() |
| 472 | // Parameters have their own stack slot. |
| 473 | && !(defined_by != nullptr && defined_by->IsParameterValue())) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 474 | BitVector* liveness_of_spill_slot = liveness_of_values.Get(number_of_registers |
| 475 | + current->GetParent()->GetSpillSlot() / kVRegSize |
| 476 | - number_of_out_slots); |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 477 | for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) { |
| 478 | if (liveness_of_spill_slot->IsBitSet(j)) { |
| 479 | if (log_fatal_on_failure) { |
| 480 | std::ostringstream message; |
| 481 | message << "Spill slot conflict at " << j; |
| 482 | LOG(FATAL) << message.str(); |
| 483 | } else { |
| 484 | return false; |
| 485 | } |
| 486 | } else { |
| 487 | liveness_of_spill_slot->SetBit(j); |
| 488 | } |
| 489 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 490 | } |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 491 | |
| 492 | if (current->HasRegister()) { |
| 493 | BitVector* liveness_of_register = liveness_of_values.Get(current->GetRegister()); |
| 494 | for (size_t j = it.CurrentRange()->GetStart(); j < it.CurrentRange()->GetEnd(); ++j) { |
| 495 | if (liveness_of_register->IsBitSet(j)) { |
| Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 496 | if (current->IsUsingInputRegister() && current->CanUseInputRegister()) { |
| 497 | continue; |
| 498 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 499 | if (log_fatal_on_failure) { |
| 500 | std::ostringstream message; |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 501 | message << "Register conflict at " << j << " "; |
| 502 | if (defined_by != nullptr) { |
| 503 | message << "(" << defined_by->DebugName() << ")"; |
| 504 | } |
| 505 | message << "for "; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 506 | if (processing_core_registers) { |
| 507 | codegen.DumpCoreRegister(message, current->GetRegister()); |
| 508 | } else { |
| 509 | codegen.DumpFloatingPointRegister(message, current->GetRegister()); |
| 510 | } |
| 511 | LOG(FATAL) << message.str(); |
| 512 | } else { |
| 513 | return false; |
| 514 | } |
| 515 | } else { |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 516 | liveness_of_register->SetBit(j); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 517 | } |
| 518 | } |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 519 | } |
| 520 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 521 | } |
| 522 | return true; |
| 523 | } |
| 524 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 525 | void RegisterAllocator::DumpInterval(std::ostream& stream, LiveInterval* interval) const { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 526 | interval->Dump(stream); |
| 527 | stream << ": "; |
| 528 | if (interval->HasRegister()) { |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 529 | if (interval->IsFloatingPoint()) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 530 | codegen_->DumpFloatingPointRegister(stream, interval->GetRegister()); |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 531 | } else { |
| 532 | codegen_->DumpCoreRegister(stream, interval->GetRegister()); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 533 | } |
| 534 | } else { |
| 535 | stream << "spilled"; |
| 536 | } |
| 537 | stream << std::endl; |
| 538 | } |
| 539 | |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 540 | void RegisterAllocator::DumpAllIntervals(std::ostream& stream) const { |
| 541 | stream << "inactive: " << std::endl; |
| 542 | for (size_t i = 0; i < inactive_.Size(); i ++) { |
| 543 | DumpInterval(stream, inactive_.Get(i)); |
| 544 | } |
| 545 | stream << "active: " << std::endl; |
| 546 | for (size_t i = 0; i < active_.Size(); i ++) { |
| 547 | DumpInterval(stream, active_.Get(i)); |
| 548 | } |
| 549 | stream << "unhandled: " << std::endl; |
| 550 | auto unhandled = (unhandled_ != nullptr) ? |
| 551 | unhandled_ : &unhandled_core_intervals_; |
| 552 | for (size_t i = 0; i < unhandled->Size(); i ++) { |
| 553 | DumpInterval(stream, unhandled->Get(i)); |
| 554 | } |
| 555 | stream << "handled: " << std::endl; |
| 556 | for (size_t i = 0; i < handled_.Size(); i ++) { |
| 557 | DumpInterval(stream, handled_.Get(i)); |
| 558 | } |
| 559 | } |
| 560 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 561 | // By the book implementation of a linear scan register allocator. |
| 562 | void RegisterAllocator::LinearScan() { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 563 | while (!unhandled_->IsEmpty()) { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 564 | // (1) Remove interval with the lowest start position from unhandled. |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 565 | LiveInterval* current = unhandled_->Pop(); |
| 566 | DCHECK(!current->IsFixed() && !current->HasSpillSlot()); |
| Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 567 | DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() >= current->GetStart()); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 568 | DCHECK(!current->IsLowInterval() || unhandled_->Peek()->IsHighInterval()); |
| Nicolas Geoffray | 87d0376 | 2014-11-19 15:17:56 +0000 | [diff] [blame] | 569 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 570 | size_t position = current->GetStart(); |
| 571 | |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 572 | // Remember the inactive_ size here since the ones moved to inactive_ from |
| 573 | // active_ below shouldn't need to be re-checked. |
| 574 | size_t inactive_intervals_to_handle = inactive_.Size(); |
| 575 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 576 | // (2) Remove currently active intervals that are dead at this position. |
| 577 | // Move active intervals that have a lifetime hole at this position |
| 578 | // to inactive. |
| 579 | for (size_t i = 0; i < active_.Size(); ++i) { |
| 580 | LiveInterval* interval = active_.Get(i); |
| 581 | if (interval->IsDeadAt(position)) { |
| 582 | active_.Delete(interval); |
| 583 | --i; |
| 584 | handled_.Add(interval); |
| 585 | } else if (!interval->Covers(position)) { |
| 586 | active_.Delete(interval); |
| 587 | --i; |
| 588 | inactive_.Add(interval); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // (3) Remove currently inactive intervals that are dead at this position. |
| 593 | // Move inactive intervals that cover this position to active. |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 594 | for (size_t i = 0; i < inactive_intervals_to_handle; ++i) { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 595 | LiveInterval* interval = inactive_.Get(i); |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 596 | DCHECK(interval->GetStart() < position || interval->IsFixed()); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 597 | if (interval->IsDeadAt(position)) { |
| 598 | inactive_.Delete(interval); |
| 599 | --i; |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 600 | --inactive_intervals_to_handle; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 601 | handled_.Add(interval); |
| 602 | } else if (interval->Covers(position)) { |
| 603 | inactive_.Delete(interval); |
| 604 | --i; |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 605 | --inactive_intervals_to_handle; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 606 | active_.Add(interval); |
| 607 | } |
| 608 | } |
| 609 | |
| Nicolas Geoffray | acd0339 | 2014-11-26 15:46:52 +0000 | [diff] [blame] | 610 | if (current->IsSlowPathSafepoint()) { |
| 611 | // Synthesized interval to record the maximum number of live registers |
| 612 | // at safepoints. No need to allocate a register for it. |
| Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 613 | if (processing_core_registers_) { |
| 614 | maximum_number_of_live_core_registers_ = |
| 615 | std::max(maximum_number_of_live_core_registers_, active_.Size()); |
| 616 | } else { |
| 617 | maximum_number_of_live_fp_registers_ = |
| 618 | std::max(maximum_number_of_live_fp_registers_, active_.Size()); |
| 619 | } |
| Nicolas Geoffray | acd0339 | 2014-11-26 15:46:52 +0000 | [diff] [blame] | 620 | DCHECK(unhandled_->IsEmpty() || unhandled_->Peek()->GetStart() > current->GetStart()); |
| 621 | continue; |
| 622 | } |
| 623 | |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 624 | if (current->IsHighInterval() && !current->GetLowInterval()->HasRegister()) { |
| 625 | DCHECK(!current->HasRegister()); |
| 626 | // Allocating the low part was unsucessful. The splitted interval for the high part |
| 627 | // will be handled next (it is in the `unhandled_` list). |
| 628 | continue; |
| 629 | } |
| 630 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 631 | // (4) Try to find an available register. |
| 632 | bool success = TryAllocateFreeReg(current); |
| 633 | |
| 634 | // (5) If no register could be found, we need to spill. |
| 635 | if (!success) { |
| 636 | success = AllocateBlockedReg(current); |
| 637 | } |
| 638 | |
| 639 | // (6) If the interval had a register allocated, add it to the list of active |
| 640 | // intervals. |
| 641 | if (success) { |
| Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 642 | codegen_->AddAllocatedRegister(processing_core_registers_ |
| 643 | ? Location::RegisterLocation(current->GetRegister()) |
| 644 | : Location::FpuRegisterLocation(current->GetRegister())); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 645 | active_.Add(current); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 646 | if (current->HasHighInterval() && !current->GetHighInterval()->HasRegister()) { |
| 647 | current->GetHighInterval()->SetRegister(GetHighForLowRegister(current->GetRegister())); |
| 648 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 653 | static void FreeIfNotCoverAt(LiveInterval* interval, size_t position, size_t* free_until) { |
| 654 | DCHECK(!interval->IsHighInterval()); |
| 655 | // Note that the same instruction may occur multiple times in the input list, |
| 656 | // so `free_until` may have changed already. |
| 657 | if (interval->IsDeadAt(position)) { |
| 658 | // Set the register to be free. Note that inactive intervals might later |
| 659 | // update this. |
| 660 | free_until[interval->GetRegister()] = kMaxLifetimePosition; |
| 661 | if (interval->HasHighInterval()) { |
| 662 | DCHECK(interval->GetHighInterval()->IsDeadAt(position)); |
| 663 | free_until[interval->GetHighInterval()->GetRegister()] = kMaxLifetimePosition; |
| 664 | } |
| 665 | } else if (!interval->Covers(position)) { |
| 666 | // The interval becomes inactive at `defined_by`. We make its register |
| 667 | // available only until the next use strictly after `defined_by`. |
| 668 | free_until[interval->GetRegister()] = interval->FirstUseAfter(position); |
| 669 | if (interval->HasHighInterval()) { |
| 670 | DCHECK(!interval->GetHighInterval()->Covers(position)); |
| 671 | free_until[interval->GetHighInterval()->GetRegister()] = free_until[interval->GetRegister()]; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 676 | // Find a free register. If multiple are found, pick the register that |
| 677 | // is free the longest. |
| 678 | bool RegisterAllocator::TryAllocateFreeReg(LiveInterval* current) { |
| 679 | size_t* free_until = registers_array_; |
| 680 | |
| 681 | // First set all registers to be free. |
| 682 | for (size_t i = 0; i < number_of_registers_; ++i) { |
| 683 | free_until[i] = kMaxLifetimePosition; |
| 684 | } |
| 685 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 686 | // For each active interval, set its register to not free. |
| 687 | for (size_t i = 0, e = active_.Size(); i < e; ++i) { |
| 688 | LiveInterval* interval = active_.Get(i); |
| 689 | DCHECK(interval->HasRegister()); |
| 690 | free_until[interval->GetRegister()] = 0; |
| 691 | } |
| 692 | |
| Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 693 | // An interval that starts an instruction (that is, it is not split), may |
| 694 | // re-use the registers used by the inputs of that instruciton, based on the |
| 695 | // location summary. |
| 696 | HInstruction* defined_by = current->GetDefinedBy(); |
| 697 | if (defined_by != nullptr && !current->IsSplit()) { |
| 698 | LocationSummary* locations = defined_by->GetLocations(); |
| 699 | if (!locations->OutputCanOverlapWithInputs() && locations->Out().IsUnallocated()) { |
| 700 | for (HInputIterator it(defined_by); !it.Done(); it.Advance()) { |
| 701 | // Take the last interval of the input. It is the location of that interval |
| 702 | // that will be used at `defined_by`. |
| 703 | LiveInterval* interval = it.Current()->GetLiveInterval()->GetLastSibling(); |
| 704 | // Note that interval may have not been processed yet. |
| 705 | // TODO: Handle non-split intervals last in the work list. |
| 706 | if (interval->HasRegister() && interval->SameRegisterKind(*current)) { |
| 707 | // The input must be live until the end of `defined_by`, to comply to |
| 708 | // the linear scan algorithm. So we use `defined_by`'s end lifetime |
| 709 | // position to check whether the input is dead or is inactive after |
| 710 | // `defined_by`. |
| 711 | DCHECK(interval->Covers(defined_by->GetLifetimePosition())); |
| 712 | size_t position = defined_by->GetLifetimePosition() + 1; |
| 713 | FreeIfNotCoverAt(interval, position, free_until); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 719 | // For each inactive interval, set its register to be free until |
| 720 | // the next intersection with `current`. |
| 721 | for (size_t i = 0, e = inactive_.Size(); i < e; ++i) { |
| 722 | LiveInterval* inactive = inactive_.Get(i); |
| 723 | // Temp/Slow-path-safepoint interval has no holes. |
| 724 | DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint()); |
| 725 | if (!current->IsSplit() && !inactive->IsFixed()) { |
| 726 | // Neither current nor inactive are fixed. |
| 727 | // Thanks to SSA, a non-split interval starting in a hole of an |
| 728 | // inactive interval should never intersect with that inactive interval. |
| 729 | // Only if it's not fixed though, because fixed intervals don't come from SSA. |
| 730 | DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | DCHECK(inactive->HasRegister()); |
| 735 | if (free_until[inactive->GetRegister()] == 0) { |
| 736 | // Already used by some active interval. No need to intersect. |
| 737 | continue; |
| 738 | } |
| 739 | size_t next_intersection = inactive->FirstIntersectionWith(current); |
| 740 | if (next_intersection != kNoLifetime) { |
| 741 | free_until[inactive->GetRegister()] = |
| 742 | std::min(free_until[inactive->GetRegister()], next_intersection); |
| 743 | } |
| 744 | } |
| 745 | |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 746 | int reg = kNoRegister; |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 747 | if (current->HasRegister()) { |
| 748 | // Some instructions have a fixed register output. |
| 749 | reg = current->GetRegister(); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 750 | if (free_until[reg] == 0) { |
| 751 | DCHECK(current->IsHighInterval()); |
| 752 | // AllocateBlockedReg will spill the holder of the register. |
| 753 | return false; |
| 754 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 755 | } else { |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 756 | DCHECK(!current->IsHighInterval()); |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 757 | int hint = current->FindFirstRegisterHint(free_until); |
| 758 | if (hint != kNoRegister) { |
| 759 | DCHECK(!IsBlocked(hint)); |
| 760 | reg = hint; |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 761 | } else if (current->IsLowInterval()) { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 762 | reg = FindAvailableRegisterPair(free_until, current->GetStart()); |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 763 | } else { |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 764 | reg = FindAvailableRegister(free_until); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 768 | DCHECK_NE(reg, kNoRegister); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 769 | // If we could not find a register, we need to spill. |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 770 | if (free_until[reg] == 0) { |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | if (current->IsLowInterval() && free_until[GetHighForLowRegister(reg)] == 0) { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | |
| 778 | current->SetRegister(reg); |
| 779 | if (!current->IsDeadAt(free_until[reg])) { |
| 780 | // If the register is only available for a subset of live ranges |
| 781 | // covered by `current`, split `current` at the position where |
| 782 | // the register is not available anymore. |
| 783 | LiveInterval* split = Split(current, free_until[reg]); |
| 784 | DCHECK(split != nullptr); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 785 | AddSorted(unhandled_, split); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | bool RegisterAllocator::IsBlocked(int reg) const { |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 791 | return processing_core_registers_ |
| 792 | ? blocked_core_registers_[reg] |
| 793 | : blocked_fp_registers_[reg]; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 794 | } |
| 795 | |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 796 | int RegisterAllocator::FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const { |
| 797 | int reg = kNoRegister; |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 798 | // Pick the register pair that is used the last. |
| 799 | for (size_t i = 0; i < number_of_registers_; ++i) { |
| 800 | if (IsBlocked(i)) continue; |
| 801 | if (!IsLowRegister(i)) continue; |
| 802 | int high_register = GetHighForLowRegister(i); |
| 803 | if (IsBlocked(high_register)) continue; |
| 804 | int existing_high_register = GetHighForLowRegister(reg); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 805 | if ((reg == kNoRegister) || (next_use[i] >= next_use[reg] |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 806 | && next_use[high_register] >= next_use[existing_high_register])) { |
| 807 | reg = i; |
| 808 | if (next_use[i] == kMaxLifetimePosition |
| 809 | && next_use[high_register] == kMaxLifetimePosition) { |
| 810 | break; |
| 811 | } |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 812 | } else if (next_use[reg] <= starting_at || next_use[existing_high_register] <= starting_at) { |
| 813 | // If one of the current register is known to be unavailable, just unconditionally |
| 814 | // try a new one. |
| 815 | reg = i; |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | return reg; |
| 819 | } |
| 820 | |
| 821 | int RegisterAllocator::FindAvailableRegister(size_t* next_use) const { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 822 | int reg = kNoRegister; |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 823 | // Pick the register that is used the last. |
| 824 | for (size_t i = 0; i < number_of_registers_; ++i) { |
| 825 | if (IsBlocked(i)) continue; |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 826 | if (reg == kNoRegister || next_use[i] > next_use[reg]) { |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 827 | reg = i; |
| 828 | if (next_use[i] == kMaxLifetimePosition) break; |
| 829 | } |
| 830 | } |
| 831 | return reg; |
| 832 | } |
| 833 | |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 834 | bool RegisterAllocator::TrySplitNonPairIntervalAt(size_t position, |
| 835 | size_t first_register_use, |
| 836 | size_t* next_use) { |
| 837 | for (size_t i = 0, e = active_.Size(); i < e; ++i) { |
| 838 | LiveInterval* active = active_.Get(i); |
| 839 | DCHECK(active->HasRegister()); |
| 840 | // Split the first interval found. |
| 841 | if (first_register_use <= next_use[active->GetRegister()] |
| 842 | && !active->IsLowInterval() |
| 843 | && !active->IsHighInterval()) { |
| 844 | LiveInterval* split = Split(active, position); |
| 845 | active_.DeleteAt(i); |
| 846 | if (split != active) { |
| 847 | handled_.Add(active); |
| 848 | } |
| 849 | AddSorted(unhandled_, split); |
| 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | return false; |
| 854 | } |
| 855 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 856 | // Find the register that is used the last, and spill the interval |
| 857 | // that holds it. If the first use of `current` is after that register |
| 858 | // we spill `current` instead. |
| 859 | bool RegisterAllocator::AllocateBlockedReg(LiveInterval* current) { |
| 860 | size_t first_register_use = current->FirstRegisterUse(); |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 861 | if (first_register_use == kNoLifetime) { |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 862 | AllocateSpillSlotFor(current); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 863 | return false; |
| 864 | } |
| 865 | |
| 866 | // First set all registers as not being used. |
| 867 | size_t* next_use = registers_array_; |
| 868 | for (size_t i = 0; i < number_of_registers_; ++i) { |
| 869 | next_use[i] = kMaxLifetimePosition; |
| 870 | } |
| 871 | |
| 872 | // For each active interval, find the next use of its register after the |
| 873 | // start of current. |
| 874 | for (size_t i = 0, e = active_.Size(); i < e; ++i) { |
| 875 | LiveInterval* active = active_.Get(i); |
| 876 | DCHECK(active->HasRegister()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 877 | if (active->IsFixed()) { |
| 878 | next_use[active->GetRegister()] = current->GetStart(); |
| 879 | } else { |
| 880 | size_t use = active->FirstRegisterUseAfter(current->GetStart()); |
| 881 | if (use != kNoLifetime) { |
| 882 | next_use[active->GetRegister()] = use; |
| 883 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
| 887 | // For each inactive interval, find the next use of its register after the |
| 888 | // start of current. |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 889 | for (size_t i = 0, e = inactive_.Size(); i < e; ++i) { |
| 890 | LiveInterval* inactive = inactive_.Get(i); |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 891 | // Temp/Slow-path-safepoint interval has no holes. |
| 892 | DCHECK(!inactive->IsTemp() && !inactive->IsSlowPathSafepoint()); |
| 893 | if (!current->IsSplit() && !inactive->IsFixed()) { |
| 894 | // Neither current nor inactive are fixed. |
| 895 | // Thanks to SSA, a non-split interval starting in a hole of an |
| 896 | // inactive interval should never intersect with that inactive interval. |
| 897 | // Only if it's not fixed though, because fixed intervals don't come from SSA. |
| 898 | DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); |
| 899 | continue; |
| 900 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 901 | DCHECK(inactive->HasRegister()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 902 | size_t next_intersection = inactive->FirstIntersectionWith(current); |
| 903 | if (next_intersection != kNoLifetime) { |
| 904 | if (inactive->IsFixed()) { |
| 905 | next_use[inactive->GetRegister()] = |
| 906 | std::min(next_intersection, next_use[inactive->GetRegister()]); |
| 907 | } else { |
| 908 | size_t use = inactive->FirstRegisterUseAfter(current->GetStart()); |
| 909 | if (use != kNoLifetime) { |
| 910 | next_use[inactive->GetRegister()] = std::min(use, next_use[inactive->GetRegister()]); |
| 911 | } |
| 912 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 916 | int reg = kNoRegister; |
| 917 | bool should_spill = false; |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 918 | if (current->HasRegister()) { |
| 919 | DCHECK(current->IsHighInterval()); |
| 920 | reg = current->GetRegister(); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 921 | // When allocating the low part, we made sure the high register was available. |
| 922 | DCHECK_LT(first_register_use, next_use[reg]); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 923 | } else if (current->IsLowInterval()) { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 924 | reg = FindAvailableRegisterPair(next_use, current->GetStart()); |
| 925 | // We should spill if both registers are not available. |
| 926 | should_spill = (first_register_use >= next_use[reg]) |
| 927 | || (first_register_use >= next_use[GetHighForLowRegister(reg)]); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 928 | } else { |
| 929 | DCHECK(!current->IsHighInterval()); |
| 930 | reg = FindAvailableRegister(next_use); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 931 | should_spill = (first_register_use >= next_use[reg]); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 934 | DCHECK_NE(reg, kNoRegister); |
| 935 | if (should_spill) { |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 936 | DCHECK(!current->IsHighInterval()); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 937 | bool is_allocation_at_use_site = (current->GetStart() == (first_register_use - 1)); |
| 938 | if (current->IsLowInterval() |
| 939 | && is_allocation_at_use_site |
| 940 | && TrySplitNonPairIntervalAt(current->GetStart(), first_register_use, next_use)) { |
| 941 | // If we're allocating a register for `current` because the instruction at |
| 942 | // that position requires it, but we think we should spill, then there are |
| 943 | // non-pair intervals blocking the allocation. We split the first |
| 944 | // interval found, and put ourselves first in the `unhandled_` list. |
| 945 | LiveInterval* existing = unhandled_->Peek(); |
| 946 | DCHECK(existing->IsHighInterval()); |
| 947 | DCHECK_EQ(existing->GetLowInterval(), current); |
| 948 | unhandled_->Add(current); |
| 949 | } else { |
| 950 | // If the first use of that instruction is after the last use of the found |
| 951 | // register, we split this interval just before its first register use. |
| 952 | AllocateSpillSlotFor(current); |
| 953 | LiveInterval* split = Split(current, first_register_use - 1); |
| 954 | DCHECK_NE(current, split) << "There is not enough registers available for " |
| 955 | << split->GetParent()->GetDefinedBy()->DebugName() << " " |
| 956 | << split->GetParent()->GetDefinedBy()->GetId() |
| 957 | << " at " << first_register_use - 1; |
| 958 | AddSorted(unhandled_, split); |
| 959 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 960 | return false; |
| 961 | } else { |
| 962 | // Use this register and spill the active and inactives interval that |
| 963 | // have that register. |
| 964 | current->SetRegister(reg); |
| 965 | |
| 966 | for (size_t i = 0, e = active_.Size(); i < e; ++i) { |
| 967 | LiveInterval* active = active_.Get(i); |
| 968 | if (active->GetRegister() == reg) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 969 | DCHECK(!active->IsFixed()); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 970 | LiveInterval* split = Split(active, current->GetStart()); |
| 971 | active_.DeleteAt(i); |
| Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 972 | if (split != active) { |
| 973 | handled_.Add(active); |
| 974 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 975 | AddSorted(unhandled_, split); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 976 | |
| 977 | if (active->IsLowInterval() || active->IsHighInterval()) { |
| 978 | LiveInterval* other_half = active->IsLowInterval() |
| 979 | ? active->GetHighInterval() |
| 980 | : active->GetLowInterval(); |
| 981 | // We also need to remove the other half from the list of actives. |
| 982 | bool found = false; |
| 983 | for (size_t j = 0; j < active_.Size(); ++j) { |
| 984 | if (active_.Get(j) == other_half) { |
| 985 | found = true; |
| 986 | active_.DeleteAt(j); |
| 987 | handled_.Add(other_half); |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | DCHECK(found); |
| 992 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 993 | break; |
| 994 | } |
| 995 | } |
| 996 | |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 997 | for (size_t i = 0, e = inactive_.Size(); i < e; ++i) { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 998 | LiveInterval* inactive = inactive_.Get(i); |
| 999 | if (inactive->GetRegister() == reg) { |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 1000 | if (!current->IsSplit() && !inactive->IsFixed()) { |
| 1001 | // Neither current nor inactive are fixed. |
| 1002 | // Thanks to SSA, a non-split interval starting in a hole of an |
| 1003 | // inactive interval should never intersect with that inactive interval. |
| 1004 | // Only if it's not fixed though, because fixed intervals don't come from SSA. |
| 1005 | DCHECK_EQ(inactive->FirstIntersectionWith(current), kNoLifetime); |
| 1006 | continue; |
| 1007 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1008 | size_t next_intersection = inactive->FirstIntersectionWith(current); |
| 1009 | if (next_intersection != kNoLifetime) { |
| 1010 | if (inactive->IsFixed()) { |
| 1011 | LiveInterval* split = Split(current, next_intersection); |
| Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 1012 | DCHECK_NE(split, current); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1013 | AddSorted(unhandled_, split); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1014 | } else { |
| Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 1015 | // Split at the start of `current`, which will lead to splitting |
| 1016 | // at the end of the lifetime hole of `inactive`. |
| 1017 | LiveInterval* split = Split(inactive, current->GetStart()); |
| 1018 | // If it's inactive, it must start before the current interval. |
| 1019 | DCHECK_NE(split, inactive); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1020 | inactive_.DeleteAt(i); |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 1021 | --i; |
| 1022 | --e; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1023 | handled_.Add(inactive); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1024 | AddSorted(unhandled_, split); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (inactive->IsLowInterval() || inactive->IsHighInterval()) { |
| 1027 | LiveInterval* other_half = inactive->IsLowInterval() |
| 1028 | ? inactive->GetHighInterval() |
| 1029 | : inactive->GetLowInterval(); |
| 1030 | |
| 1031 | // We also need to remove the other half from the list of inactives. |
| 1032 | bool found = false; |
| 1033 | for (size_t j = 0; j < inactive_.Size(); ++j) { |
| 1034 | if (inactive_.Get(j) == other_half) { |
| 1035 | found = true; |
| 1036 | inactive_.DeleteAt(j); |
| 1037 | --e; |
| 1038 | handled_.Add(other_half); |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | DCHECK(found); |
| 1043 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1044 | } |
| 1045 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return true; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1053 | void RegisterAllocator::AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval) { |
| Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 1054 | DCHECK(!interval->IsFixed() && !interval->HasSpillSlot()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1055 | size_t insert_at = 0; |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1056 | for (size_t i = array->Size(); i > 0; --i) { |
| 1057 | LiveInterval* current = array->Get(i - 1); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1058 | // High intervals must be processed right after their low equivalent. |
| 1059 | if (current->StartsAfter(interval) && !current->IsHighInterval()) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1060 | insert_at = i; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1061 | break; |
| Nicolas Geoffray | acd0339 | 2014-11-26 15:46:52 +0000 | [diff] [blame] | 1062 | } else if ((current->GetStart() == interval->GetStart()) && current->IsSlowPathSafepoint()) { |
| 1063 | // Ensure the slow path interval is the last to be processed at its location: we want the |
| 1064 | // interval to know all live registers at this location. |
| 1065 | DCHECK(i == 1 || array->Get(i - 2)->StartsAfter(current)); |
| 1066 | insert_at = i; |
| 1067 | break; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1068 | } |
| 1069 | } |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1070 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1071 | array->InsertAt(insert_at, interval); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1072 | // Insert the high interval before the low, to ensure the low is processed before. |
| 1073 | if (interval->HasHighInterval()) { |
| 1074 | array->InsertAt(insert_at, interval->GetHighInterval()); |
| 1075 | } else if (interval->HasLowInterval()) { |
| 1076 | array->InsertAt(insert_at + 1, interval->GetLowInterval()); |
| 1077 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | LiveInterval* RegisterAllocator::Split(LiveInterval* interval, size_t position) { |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1081 | DCHECK_GE(position, interval->GetStart()); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1082 | DCHECK(!interval->IsDeadAt(position)); |
| 1083 | if (position == interval->GetStart()) { |
| 1084 | // Spill slot will be allocated when handling `interval` again. |
| 1085 | interval->ClearRegister(); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1086 | if (interval->HasHighInterval()) { |
| 1087 | interval->GetHighInterval()->ClearRegister(); |
| 1088 | } else if (interval->HasLowInterval()) { |
| 1089 | interval->GetLowInterval()->ClearRegister(); |
| 1090 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1091 | return interval; |
| 1092 | } else { |
| 1093 | LiveInterval* new_interval = interval->SplitAt(position); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1094 | if (interval->HasHighInterval()) { |
| 1095 | LiveInterval* high = interval->GetHighInterval()->SplitAt(position); |
| 1096 | new_interval->SetHighInterval(high); |
| 1097 | high->SetLowInterval(new_interval); |
| 1098 | } else if (interval->HasLowInterval()) { |
| 1099 | LiveInterval* low = interval->GetLowInterval()->SplitAt(position); |
| 1100 | new_interval->SetLowInterval(low); |
| 1101 | low->SetHighInterval(new_interval); |
| 1102 | } |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1103 | return new_interval; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1107 | void RegisterAllocator::AllocateSpillSlotFor(LiveInterval* interval) { |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1108 | if (interval->IsHighInterval()) { |
| 1109 | // The low interval will contain the spill slot. |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1113 | LiveInterval* parent = interval->GetParent(); |
| 1114 | |
| 1115 | // An instruction gets a spill slot for its entire lifetime. If the parent |
| 1116 | // of this interval already has a spill slot, there is nothing to do. |
| 1117 | if (parent->HasSpillSlot()) { |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1121 | HInstruction* defined_by = parent->GetDefinedBy(); |
| 1122 | if (defined_by->IsParameterValue()) { |
| 1123 | // Parameters have their own stack slot. |
| 1124 | parent->SetSpillSlot(codegen_->GetStackSlotOfParameter(defined_by->AsParameterValue())); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 1128 | if (defined_by->IsConstant()) { |
| 1129 | // Constants don't need a spill slot. |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1133 | LiveInterval* last_sibling = interval; |
| 1134 | while (last_sibling->GetNextSibling() != nullptr) { |
| 1135 | last_sibling = last_sibling->GetNextSibling(); |
| 1136 | } |
| 1137 | size_t end = last_sibling->GetEnd(); |
| 1138 | |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1139 | GrowableArray<size_t>* spill_slots = nullptr; |
| 1140 | switch (interval->GetType()) { |
| 1141 | case Primitive::kPrimDouble: |
| 1142 | spill_slots = &double_spill_slots_; |
| 1143 | break; |
| 1144 | case Primitive::kPrimLong: |
| 1145 | spill_slots = &long_spill_slots_; |
| 1146 | break; |
| 1147 | case Primitive::kPrimFloat: |
| 1148 | spill_slots = &float_spill_slots_; |
| 1149 | break; |
| 1150 | case Primitive::kPrimNot: |
| 1151 | case Primitive::kPrimInt: |
| 1152 | case Primitive::kPrimChar: |
| 1153 | case Primitive::kPrimByte: |
| 1154 | case Primitive::kPrimBoolean: |
| 1155 | case Primitive::kPrimShort: |
| 1156 | spill_slots = &int_spill_slots_; |
| 1157 | break; |
| 1158 | case Primitive::kPrimVoid: |
| 1159 | LOG(FATAL) << "Unexpected type for interval " << interval->GetType(); |
| 1160 | } |
| 1161 | |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1162 | // Find an available spill slot. |
| 1163 | size_t slot = 0; |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1164 | for (size_t e = spill_slots->Size(); slot < e; ++slot) { |
| 1165 | if (spill_slots->Get(slot) <= parent->GetStart() |
| 1166 | && (slot == (e - 1) || spill_slots->Get(slot + 1) <= parent->GetStart())) { |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1171 | if (parent->NeedsTwoSpillSlots()) { |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1172 | if (slot == spill_slots->Size()) { |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1173 | // We need a new spill slot. |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1174 | spill_slots->Add(end); |
| 1175 | spill_slots->Add(end); |
| 1176 | } else if (slot == spill_slots->Size() - 1) { |
| 1177 | spill_slots->Put(slot, end); |
| 1178 | spill_slots->Add(end); |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1179 | } else { |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1180 | spill_slots->Put(slot, end); |
| 1181 | spill_slots->Put(slot + 1, end); |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1182 | } |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1183 | } else { |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1184 | if (slot == spill_slots->Size()) { |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1185 | // We need a new spill slot. |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1186 | spill_slots->Add(end); |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1187 | } else { |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1188 | spill_slots->Put(slot, end); |
| Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 1189 | } |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1192 | // Note that the exact spill slot location will be computed when we resolve, |
| 1193 | // that is when we know the number of spill slots for each type. |
| 1194 | parent->SetSpillSlot(slot); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
| Nicolas Geoffray | 2a877f3 | 2014-09-10 10:49:34 +0100 | [diff] [blame] | 1197 | static bool IsValidDestination(Location destination) { |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1198 | return destination.IsRegister() |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1199 | || destination.IsRegisterPair() |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1200 | || destination.IsFpuRegister() |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1201 | || destination.IsFpuRegisterPair() |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1202 | || destination.IsStackSlot() |
| 1203 | || destination.IsDoubleStackSlot(); |
| Nicolas Geoffray | 2a877f3 | 2014-09-10 10:49:34 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1206 | void RegisterAllocator::AddInputMoveFor(HInstruction* user, |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1207 | Location source, |
| 1208 | Location destination) const { |
| 1209 | if (source.Equals(destination)) return; |
| 1210 | |
| Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 1211 | DCHECK(!user->IsPhi()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1212 | |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1213 | HInstruction* previous = user->GetPrevious(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1214 | HParallelMove* move = nullptr; |
| 1215 | if (previous == nullptr |
| Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 1216 | || !previous->IsParallelMove() |
| Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 1217 | || previous->GetLifetimePosition() < user->GetLifetimePosition()) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1218 | move = new (allocator_) HParallelMove(allocator_); |
| Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 1219 | move->SetLifetimePosition(user->GetLifetimePosition()); |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1220 | user->GetBlock()->InsertInstructionBefore(move, user); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1221 | } else { |
| 1222 | move = previous->AsParallelMove(); |
| 1223 | } |
| Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 1224 | DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition()); |
| Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 1225 | move->AddMove(source, destination, nullptr); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
| Nicolas Geoffray | 46fbaab | 2014-11-26 18:30:23 +0000 | [diff] [blame] | 1228 | static bool IsInstructionStart(size_t position) { |
| 1229 | return (position & 1) == 0; |
| 1230 | } |
| 1231 | |
| 1232 | static bool IsInstructionEnd(size_t position) { |
| 1233 | return (position & 1) == 1; |
| 1234 | } |
| 1235 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1236 | void RegisterAllocator::InsertParallelMoveAt(size_t position, |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1237 | HInstruction* instruction, |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1238 | Location source, |
| 1239 | Location destination) const { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1240 | DCHECK(IsValidDestination(destination)) << destination; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1241 | if (source.Equals(destination)) return; |
| 1242 | |
| 1243 | HInstruction* at = liveness_.GetInstructionFromPosition(position / 2); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1244 | HParallelMove* move; |
| Nicolas Geoffray | 46fbaab | 2014-11-26 18:30:23 +0000 | [diff] [blame] | 1245 | if (at == nullptr) { |
| 1246 | if (IsInstructionStart(position)) { |
| 1247 | // Block boundary, don't do anything the connection of split siblings will handle it. |
| 1248 | return; |
| 1249 | } else { |
| 1250 | // Move must happen before the first instruction of the block. |
| 1251 | at = liveness_.GetInstructionFromPosition((position + 1) / 2); |
| Nicolas Geoffray | 5976857 | 2014-12-01 09:50:04 +0000 | [diff] [blame] | 1252 | // Note that parallel moves may have already been inserted, so we explicitly |
| 1253 | // ask for the first instruction of the block: `GetInstructionFromPosition` does |
| 1254 | // not contain the moves. |
| 1255 | at = at->GetBlock()->GetFirstInstruction(); |
| 1256 | if (at->GetLifetimePosition() != position) { |
| 1257 | DCHECK_GT(at->GetLifetimePosition(), position); |
| Nicolas Geoffray | 46fbaab | 2014-11-26 18:30:23 +0000 | [diff] [blame] | 1258 | move = new (allocator_) HParallelMove(allocator_); |
| 1259 | move->SetLifetimePosition(position); |
| 1260 | at->GetBlock()->InsertInstructionBefore(move, at); |
| Nicolas Geoffray | 5976857 | 2014-12-01 09:50:04 +0000 | [diff] [blame] | 1261 | } else { |
| 1262 | DCHECK(at->IsParallelMove()); |
| 1263 | move = at->AsParallelMove(); |
| Nicolas Geoffray | 46fbaab | 2014-11-26 18:30:23 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | } else if (IsInstructionEnd(position)) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1267 | // Move must happen after the instruction. |
| 1268 | DCHECK(!at->IsControlFlow()); |
| 1269 | move = at->GetNext()->AsParallelMove(); |
| Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1270 | // This is a parallel move for connecting siblings in a same block. We need to |
| 1271 | // differentiate it with moves for connecting blocks, and input moves. |
| Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 1272 | if (move == nullptr || move->GetLifetimePosition() > position) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1273 | move = new (allocator_) HParallelMove(allocator_); |
| 1274 | move->SetLifetimePosition(position); |
| 1275 | at->GetBlock()->InsertInstructionBefore(move, at->GetNext()); |
| 1276 | } |
| 1277 | } else { |
| 1278 | // Move must happen before the instruction. |
| 1279 | HInstruction* previous = at->GetPrevious(); |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1280 | if (previous == nullptr |
| 1281 | || !previous->IsParallelMove() |
| 1282 | || previous->GetLifetimePosition() != position) { |
| 1283 | // If the previous is a parallel move, then its position must be lower |
| 1284 | // than the given `position`: it was added just after the non-parallel |
| 1285 | // move instruction that precedes `instruction`. |
| 1286 | DCHECK(previous == nullptr |
| 1287 | || !previous->IsParallelMove() |
| 1288 | || previous->GetLifetimePosition() < position); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1289 | move = new (allocator_) HParallelMove(allocator_); |
| 1290 | move->SetLifetimePosition(position); |
| 1291 | at->GetBlock()->InsertInstructionBefore(move, at); |
| 1292 | } else { |
| 1293 | move = previous->AsParallelMove(); |
| 1294 | } |
| 1295 | } |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1296 | DCHECK_EQ(move->GetLifetimePosition(), position); |
| Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 1297 | move->AddMove(source, destination, instruction); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | void RegisterAllocator::InsertParallelMoveAtExitOf(HBasicBlock* block, |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1301 | HInstruction* instruction, |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1302 | Location source, |
| 1303 | Location destination) const { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1304 | DCHECK(IsValidDestination(destination)) << destination; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1305 | if (source.Equals(destination)) return; |
| 1306 | |
| 1307 | DCHECK_EQ(block->GetSuccessors().Size(), 1u); |
| 1308 | HInstruction* last = block->GetLastInstruction(); |
| Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 1309 | // We insert moves at exit for phi predecessors and connecting blocks. |
| 1310 | // A block ending with an if cannot branch to a block with phis because |
| 1311 | // we do not allow critical edges. It can also not connect |
| 1312 | // a split interval between two blocks: the move has to happen in the successor. |
| 1313 | DCHECK(!last->IsIf()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1314 | HInstruction* previous = last->GetPrevious(); |
| 1315 | HParallelMove* move; |
| Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1316 | // This is a parallel move for connecting blocks. We need to differentiate |
| 1317 | // it with moves for connecting siblings in a same block, and output moves. |
| Nicolas Geoffray | 5976857 | 2014-12-01 09:50:04 +0000 | [diff] [blame] | 1318 | size_t position = last->GetLifetimePosition(); |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1319 | if (previous == nullptr || !previous->IsParallelMove() |
| Nicolas Geoffray | 5976857 | 2014-12-01 09:50:04 +0000 | [diff] [blame] | 1320 | || previous->AsParallelMove()->GetLifetimePosition() != position) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1321 | move = new (allocator_) HParallelMove(allocator_); |
| Nicolas Geoffray | 5976857 | 2014-12-01 09:50:04 +0000 | [diff] [blame] | 1322 | move->SetLifetimePosition(position); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1323 | block->InsertInstructionBefore(move, last); |
| 1324 | } else { |
| 1325 | move = previous->AsParallelMove(); |
| 1326 | } |
| Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 1327 | move->AddMove(source, destination, instruction); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | void RegisterAllocator::InsertParallelMoveAtEntryOf(HBasicBlock* block, |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1331 | HInstruction* instruction, |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1332 | Location source, |
| 1333 | Location destination) const { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1334 | DCHECK(IsValidDestination(destination)) << destination; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1335 | if (source.Equals(destination)) return; |
| 1336 | |
| 1337 | HInstruction* first = block->GetFirstInstruction(); |
| 1338 | HParallelMove* move = first->AsParallelMove(); |
| Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1339 | // This is a parallel move for connecting blocks. We need to differentiate |
| 1340 | // it with moves for connecting siblings in a same block, and input moves. |
| 1341 | if (move == nullptr || move->GetLifetimePosition() != block->GetLifetimeStart()) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1342 | move = new (allocator_) HParallelMove(allocator_); |
| 1343 | move->SetLifetimePosition(block->GetLifetimeStart()); |
| 1344 | block->InsertInstructionBefore(move, first); |
| 1345 | } |
| Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 1346 | move->AddMove(source, destination, instruction); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | void RegisterAllocator::InsertMoveAfter(HInstruction* instruction, |
| 1350 | Location source, |
| 1351 | Location destination) const { |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1352 | DCHECK(IsValidDestination(destination)) << destination; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1353 | if (source.Equals(destination)) return; |
| 1354 | |
| Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 1355 | if (instruction->IsPhi()) { |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1356 | InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1357 | return; |
| 1358 | } |
| 1359 | |
| Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1360 | size_t position = instruction->GetLifetimePosition() + 1; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1361 | HParallelMove* move = instruction->GetNext()->AsParallelMove(); |
| Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1362 | // This is a parallel move for moving the output of an instruction. We need |
| 1363 | // to differentiate with input moves, moves for connecting siblings in a |
| 1364 | // and moves for connecting blocks. |
| 1365 | if (move == nullptr || move->GetLifetimePosition() != position) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1366 | move = new (allocator_) HParallelMove(allocator_); |
| Nicolas Geoffray | e27f31a | 2014-06-12 17:53:14 +0100 | [diff] [blame] | 1367 | move->SetLifetimePosition(position); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1368 | instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext()); |
| 1369 | } |
| Nicolas Geoffray | 42d1f5f | 2015-01-16 09:14:18 +0000 | [diff] [blame] | 1370 | move->AddMove(source, destination, instruction); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | void RegisterAllocator::ConnectSiblings(LiveInterval* interval) { |
| 1374 | LiveInterval* current = interval; |
| 1375 | if (current->HasSpillSlot() && current->HasRegister()) { |
| 1376 | // We spill eagerly, so move must be at definition. |
| 1377 | InsertMoveAfter(interval->GetDefinedBy(), |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1378 | interval->ToLocation(), |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1379 | interval->NeedsTwoSpillSlots() |
| Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1380 | ? Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot()) |
| 1381 | : Location::StackSlot(interval->GetParent()->GetSpillSlot())); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1382 | } |
| 1383 | UsePosition* use = current->GetFirstUse(); |
| 1384 | |
| 1385 | // Walk over all siblings, updating locations of use positions, and |
| 1386 | // connecting them when they are adjacent. |
| 1387 | do { |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1388 | Location source = current->ToLocation(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1389 | |
| 1390 | // Walk over all uses covered by this interval, and update the location |
| 1391 | // information. |
| 1392 | while (use != nullptr && use->GetPosition() <= current->GetEnd()) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1393 | LocationSummary* locations = use->GetUser()->GetLocations(); |
| 1394 | if (use->GetIsEnvironment()) { |
| 1395 | locations->SetEnvironmentAt(use->GetInputIndex(), source); |
| 1396 | } else { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1397 | Location expected_location = locations->InAt(use->GetInputIndex()); |
| Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1398 | // The expected (actual) location may be invalid in case the input is unused. Currently |
| 1399 | // this only happens for intrinsics. |
| 1400 | if (expected_location.IsValid()) { |
| 1401 | if (expected_location.IsUnallocated()) { |
| 1402 | locations->SetInAt(use->GetInputIndex(), source); |
| 1403 | } else if (!expected_location.IsConstant()) { |
| 1404 | AddInputMoveFor(use->GetUser(), source, expected_location); |
| 1405 | } |
| 1406 | } else { |
| 1407 | DCHECK(use->GetUser()->IsInvoke()); |
| 1408 | DCHECK(use->GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | use = use->GetNext(); |
| 1412 | } |
| 1413 | |
| 1414 | // If the next interval starts just after this one, and has a register, |
| 1415 | // insert a move. |
| 1416 | LiveInterval* next_sibling = current->GetNextSibling(); |
| 1417 | if (next_sibling != nullptr |
| 1418 | && next_sibling->HasRegister() |
| 1419 | && current->GetEnd() == next_sibling->GetStart()) { |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1420 | Location destination = next_sibling->ToLocation(); |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1421 | InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1422 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1423 | |
| 1424 | // At each safepoint, we record stack and register information. |
| 1425 | for (size_t i = 0, e = safepoints_.Size(); i < e; ++i) { |
| 1426 | HInstruction* safepoint = safepoints_.Get(i); |
| 1427 | size_t position = safepoint->GetLifetimePosition(); |
| 1428 | LocationSummary* locations = safepoint->GetLocations(); |
| Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1429 | if (!current->Covers(position)) { |
| 1430 | continue; |
| 1431 | } |
| 1432 | if (interval->GetStart() == position) { |
| 1433 | // The safepoint is for this instruction, so the location of the instruction |
| 1434 | // does not need to be saved. |
| 1435 | continue; |
| 1436 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1437 | |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 1438 | if ((current->GetType() == Primitive::kPrimNot) && current->GetParent()->HasSpillSlot()) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1439 | locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize); |
| 1440 | } |
| 1441 | |
| 1442 | switch (source.GetKind()) { |
| 1443 | case Location::kRegister: { |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 1444 | locations->AddLiveRegister(source); |
| Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 1445 | if (kIsDebugBuild && locations->OnlyCallsOnSlowPath()) { |
| 1446 | DCHECK_LE(locations->GetNumberOfLiveRegisters(), |
| 1447 | maximum_number_of_live_core_registers_ + |
| 1448 | maximum_number_of_live_fp_registers_); |
| 1449 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1450 | if (current->GetType() == Primitive::kPrimNot) { |
| Nicolas Geoffray | 56b9ee6 | 2014-10-09 11:47:51 +0100 | [diff] [blame] | 1451 | locations->SetRegisterBit(source.reg()); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1452 | } |
| 1453 | break; |
| 1454 | } |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 1455 | case Location::kFpuRegister: { |
| 1456 | locations->AddLiveRegister(source); |
| 1457 | break; |
| 1458 | } |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 1459 | |
| 1460 | case Location::kRegisterPair: |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1461 | case Location::kFpuRegisterPair: { |
| 1462 | locations->AddLiveRegister(source.ToLow()); |
| 1463 | locations->AddLiveRegister(source.ToHigh()); |
| 1464 | break; |
| 1465 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1466 | case Location::kStackSlot: // Fall-through |
| 1467 | case Location::kDoubleStackSlot: // Fall-through |
| 1468 | case Location::kConstant: { |
| 1469 | // Nothing to do. |
| 1470 | break; |
| 1471 | } |
| 1472 | default: { |
| 1473 | LOG(FATAL) << "Unexpected location for object"; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1477 | current = next_sibling; |
| 1478 | } while (current != nullptr); |
| 1479 | DCHECK(use == nullptr); |
| 1480 | } |
| 1481 | |
| 1482 | void RegisterAllocator::ConnectSplitSiblings(LiveInterval* interval, |
| 1483 | HBasicBlock* from, |
| 1484 | HBasicBlock* to) const { |
| 1485 | if (interval->GetNextSibling() == nullptr) { |
| 1486 | // Nothing to connect. The whole range was allocated to the same location. |
| 1487 | return; |
| 1488 | } |
| 1489 | |
| Nicolas Geoffray | 46fbaab | 2014-11-26 18:30:23 +0000 | [diff] [blame] | 1490 | // Intervals end at the lifetime end of a block. The decrement by one |
| 1491 | // ensures the `Cover` call will return true. |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1492 | size_t from_position = from->GetLifetimeEnd() - 1; |
| Nicolas Geoffray | 46fbaab | 2014-11-26 18:30:23 +0000 | [diff] [blame] | 1493 | size_t to_position = to->GetLifetimeStart(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1494 | |
| 1495 | LiveInterval* destination = nullptr; |
| 1496 | LiveInterval* source = nullptr; |
| 1497 | |
| 1498 | LiveInterval* current = interval; |
| 1499 | |
| 1500 | // Check the intervals that cover `from` and `to`. |
| 1501 | while ((current != nullptr) && (source == nullptr || destination == nullptr)) { |
| 1502 | if (current->Covers(from_position)) { |
| 1503 | DCHECK(source == nullptr); |
| 1504 | source = current; |
| 1505 | } |
| 1506 | if (current->Covers(to_position)) { |
| 1507 | DCHECK(destination == nullptr); |
| 1508 | destination = current; |
| 1509 | } |
| 1510 | |
| 1511 | current = current->GetNextSibling(); |
| 1512 | } |
| 1513 | |
| 1514 | if (destination == source) { |
| 1515 | // Interval was not split. |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 1519 | DCHECK(destination != nullptr && source != nullptr); |
| 1520 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1521 | if (!destination->HasRegister()) { |
| 1522 | // Values are eagerly spilled. Spill slot already contains appropriate value. |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | // If `from` has only one successor, we can put the moves at the exit of it. Otherwise |
| 1527 | // we need to put the moves at the entry of `to`. |
| 1528 | if (from->GetSuccessors().Size() == 1) { |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1529 | InsertParallelMoveAtExitOf(from, |
| 1530 | interval->GetParent()->GetDefinedBy(), |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1531 | source->ToLocation(), |
| 1532 | destination->ToLocation()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1533 | } else { |
| 1534 | DCHECK_EQ(to->GetPredecessors().Size(), 1u); |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1535 | InsertParallelMoveAtEntryOf(to, |
| 1536 | interval->GetParent()->GetDefinedBy(), |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1537 | source->ToLocation(), |
| 1538 | destination->ToLocation()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1542 | void RegisterAllocator::Resolve() { |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1543 | codegen_->InitializeCodeGeneration(GetNumberOfSpillSlots(), |
| Nicolas Geoffray | 4c204ba | 2015-02-03 15:12:35 +0000 | [diff] [blame] | 1544 | maximum_number_of_live_core_registers_, |
| 1545 | maximum_number_of_live_fp_registers_, |
| 1546 | reserved_out_slots_, |
| 1547 | liveness_.GetLinearOrder()); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1548 | |
| 1549 | // Adjust the Out Location of instructions. |
| 1550 | // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration. |
| 1551 | for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { |
| 1552 | HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); |
| 1553 | LiveInterval* current = instruction->GetLiveInterval(); |
| 1554 | LocationSummary* locations = instruction->GetLocations(); |
| 1555 | Location location = locations->Out(); |
| Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 1556 | if (instruction->IsParameterValue()) { |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1557 | // Now that we know the frame size, adjust the parameter's location. |
| 1558 | if (location.IsStackSlot()) { |
| 1559 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 1560 | current->SetSpillSlot(location.GetStackIndex()); |
| Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 1561 | locations->UpdateOut(location); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1562 | } else if (location.IsDoubleStackSlot()) { |
| 1563 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 1564 | current->SetSpillSlot(location.GetStackIndex()); |
| Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 1565 | locations->UpdateOut(location); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1566 | } else if (current->HasSpillSlot()) { |
| 1567 | current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize()); |
| 1568 | } |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 1569 | } else if (current->HasSpillSlot()) { |
| 1570 | // Adjust the stack slot, now that we know the number of them for each type. |
| 1571 | // The way this implementation lays out the stack is the following: |
| 1572 | // [parameter slots ] |
| 1573 | // [double spill slots ] |
| 1574 | // [long spill slots ] |
| 1575 | // [float spill slots ] |
| 1576 | // [int/ref values ] |
| 1577 | // [maximum out values ] (number of arguments for calls) |
| 1578 | // [art method ]. |
| 1579 | uint32_t slot = current->GetSpillSlot(); |
| 1580 | switch (current->GetType()) { |
| 1581 | case Primitive::kPrimDouble: |
| 1582 | slot += long_spill_slots_.Size(); |
| 1583 | FALLTHROUGH_INTENDED; |
| 1584 | case Primitive::kPrimLong: |
| 1585 | slot += float_spill_slots_.Size(); |
| 1586 | FALLTHROUGH_INTENDED; |
| 1587 | case Primitive::kPrimFloat: |
| 1588 | slot += int_spill_slots_.Size(); |
| 1589 | FALLTHROUGH_INTENDED; |
| 1590 | case Primitive::kPrimNot: |
| 1591 | case Primitive::kPrimInt: |
| 1592 | case Primitive::kPrimChar: |
| 1593 | case Primitive::kPrimByte: |
| 1594 | case Primitive::kPrimBoolean: |
| 1595 | case Primitive::kPrimShort: |
| 1596 | slot += reserved_out_slots_; |
| 1597 | break; |
| 1598 | case Primitive::kPrimVoid: |
| 1599 | LOG(FATAL) << "Unexpected type for interval " << current->GetType(); |
| 1600 | } |
| 1601 | current->SetSpillSlot(slot * kVRegSize); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1602 | } |
| 1603 | |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1604 | Location source = current->ToLocation(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1605 | |
| 1606 | if (location.IsUnallocated()) { |
| 1607 | if (location.GetPolicy() == Location::kSameAsFirstInput) { |
| Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 1608 | if (locations->InAt(0).IsUnallocated()) { |
| 1609 | locations->SetInAt(0, source); |
| 1610 | } else { |
| 1611 | DCHECK(locations->InAt(0).Equals(source)); |
| 1612 | } |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1613 | } |
| Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 1614 | locations->UpdateOut(source); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1615 | } else { |
| 1616 | DCHECK(source.Equals(location)); |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | // Connect siblings. |
| 1621 | for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { |
| 1622 | HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); |
| 1623 | ConnectSiblings(instruction->GetLiveInterval()); |
| 1624 | } |
| 1625 | |
| 1626 | // Resolve non-linear control flow across branches. Order does not matter. |
| 1627 | for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) { |
| 1628 | HBasicBlock* block = it.Current(); |
| 1629 | BitVector* live = liveness_.GetLiveInSet(*block); |
| 1630 | for (uint32_t idx : live->Indexes()) { |
| 1631 | HInstruction* current = liveness_.GetInstructionFromSsaIndex(idx); |
| 1632 | LiveInterval* interval = current->GetLiveInterval(); |
| 1633 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 1634 | ConnectSplitSiblings(interval, block->GetPredecessors().Get(i), block); |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | // Resolve phi inputs. Order does not matter. |
| 1640 | for (HLinearOrderIterator it(liveness_); !it.Done(); it.Advance()) { |
| 1641 | HBasicBlock* current = it.Current(); |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1642 | for (HInstructionIterator inst_it(current->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| 1643 | HInstruction* phi = inst_it.Current(); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1644 | for (size_t i = 0, e = current->GetPredecessors().Size(); i < e; ++i) { |
| 1645 | HBasicBlock* predecessor = current->GetPredecessors().Get(i); |
| 1646 | DCHECK_EQ(predecessor->GetSuccessors().Size(), 1u); |
| 1647 | HInstruction* input = phi->InputAt(i); |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1648 | Location source = input->GetLiveInterval()->GetLocationAt( |
| 1649 | predecessor->GetLifetimeEnd() - 1); |
| 1650 | Location destination = phi->GetLiveInterval()->ToLocation(); |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 1651 | InsertParallelMoveAtExitOf(predecessor, nullptr, source, destination); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1655 | |
| 1656 | // Assign temp locations. |
| 1657 | HInstruction* current = nullptr; |
| 1658 | size_t temp_index = 0; |
| 1659 | for (size_t i = 0; i < temp_intervals_.Size(); ++i) { |
| 1660 | LiveInterval* temp = temp_intervals_.Get(i); |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1661 | if (temp->IsHighInterval()) { |
| 1662 | // High intervals can be skipped, they are already handled by the low interval. |
| 1663 | continue; |
| 1664 | } |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1665 | HInstruction* at = liveness_.GetTempUser(temp); |
| 1666 | if (at != current) { |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1667 | temp_index = 0; |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1668 | current = at; |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1669 | } |
| Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1670 | LocationSummary* locations = at->GetLocations(); |
| Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 1671 | switch (temp->GetType()) { |
| 1672 | case Primitive::kPrimInt: |
| 1673 | locations->SetTempAt( |
| 1674 | temp_index++, Location::RegisterLocation(temp->GetRegister())); |
| 1675 | break; |
| 1676 | |
| 1677 | case Primitive::kPrimDouble: |
| Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1678 | if (codegen_->NeedsTwoRegisters(Primitive::kPrimDouble)) { |
| 1679 | Location location = Location::FpuRegisterPairLocation( |
| 1680 | temp->GetRegister(), temp->GetHighInterval()->GetRegister()); |
| 1681 | locations->SetTempAt(temp_index++, location); |
| 1682 | } else { |
| 1683 | locations->SetTempAt( |
| 1684 | temp_index++, Location::FpuRegisterLocation(temp->GetRegister())); |
| 1685 | } |
| Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 1686 | break; |
| 1687 | |
| 1688 | default: |
| 1689 | LOG(FATAL) << "Unexpected type for temporary location " |
| 1690 | << temp->GetType(); |
| 1691 | } |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1692 | } |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1695 | } // namespace art |