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