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