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