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