| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_allocation_resolver.h" |
| 18 | |
| 19 | #include "code_generator.h" |
| Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 20 | #include "linear_order.h" |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 21 | #include "ssa_liveness_analysis.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 25 | RegisterAllocationResolver::RegisterAllocationResolver(CodeGenerator* codegen, |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 26 | const SsaLivenessAnalysis& liveness) |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 27 | : allocator_(codegen->GetGraph()->GetAllocator()), |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 28 | codegen_(codegen), |
| 29 | liveness_(liveness) {} |
| 30 | |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 31 | void RegisterAllocationResolver::Resolve(ArrayRef<HInstruction* const> safepoints, |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 32 | size_t reserved_out_slots, |
| 33 | size_t int_spill_slots, |
| 34 | size_t long_spill_slots, |
| 35 | size_t float_spill_slots, |
| 36 | size_t double_spill_slots, |
| 37 | size_t catch_phi_spill_slots, |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 38 | ArrayRef<LiveInterval* const> temp_intervals) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 39 | size_t spill_slots = int_spill_slots |
| 40 | + long_spill_slots |
| 41 | + float_spill_slots |
| 42 | + double_spill_slots |
| 43 | + catch_phi_spill_slots; |
| 44 | |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 45 | // Update safepoints and calculate the size of the spills. |
| 46 | UpdateSafepointLiveRegisters(); |
| 47 | size_t maximum_safepoint_spill_size = CalculateMaximumSafepointSpillSize(safepoints); |
| 48 | |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 49 | // Computes frame size and spill mask. |
| 50 | codegen_->InitializeCodeGeneration(spill_slots, |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 51 | maximum_safepoint_spill_size, |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 52 | reserved_out_slots, // Includes slot(s) for the art method. |
| 53 | codegen_->GetGraph()->GetLinearOrder()); |
| 54 | |
| 55 | // Resolve outputs, including stack locations. |
| 56 | // TODO: Use pointers of Location inside LiveInterval to avoid doing another iteration. |
| 57 | for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { |
| 58 | HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); |
| 59 | LiveInterval* current = instruction->GetLiveInterval(); |
| 60 | LocationSummary* locations = instruction->GetLocations(); |
| 61 | Location location = locations->Out(); |
| 62 | if (instruction->IsParameterValue()) { |
| 63 | // Now that we know the frame size, adjust the parameter's location. |
| 64 | if (location.IsStackSlot()) { |
| 65 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 66 | current->SetSpillSlot(location.GetStackIndex()); |
| 67 | locations->UpdateOut(location); |
| 68 | } else if (location.IsDoubleStackSlot()) { |
| 69 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 70 | current->SetSpillSlot(location.GetStackIndex()); |
| 71 | locations->UpdateOut(location); |
| 72 | } else if (current->HasSpillSlot()) { |
| 73 | current->SetSpillSlot(current->GetSpillSlot() + codegen_->GetFrameSize()); |
| 74 | } |
| 75 | } else if (instruction->IsCurrentMethod()) { |
| 76 | // The current method is always at offset 0. |
| 77 | DCHECK(!current->HasSpillSlot() || (current->GetSpillSlot() == 0)); |
| 78 | } else if (instruction->IsPhi() && instruction->AsPhi()->IsCatchPhi()) { |
| 79 | DCHECK(current->HasSpillSlot()); |
| 80 | size_t slot = current->GetSpillSlot() |
| 81 | + spill_slots |
| 82 | + reserved_out_slots |
| 83 | - catch_phi_spill_slots; |
| 84 | current->SetSpillSlot(slot * kVRegSize); |
| 85 | } else if (current->HasSpillSlot()) { |
| 86 | // Adjust the stack slot, now that we know the number of them for each type. |
| 87 | // The way this implementation lays out the stack is the following: |
| 88 | // [parameter slots ] |
| Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 89 | // [art method (caller) ] |
| 90 | // [entry spill (core) ] |
| 91 | // [entry spill (float) ] |
| 92 | // [should_deoptimize flag] (this is optional) |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 93 | // [catch phi spill slots ] |
| 94 | // [double spill slots ] |
| 95 | // [long spill slots ] |
| 96 | // [float spill slots ] |
| 97 | // [int/ref values ] |
| 98 | // [maximum out values ] (number of arguments for calls) |
| 99 | // [art method ]. |
| 100 | size_t slot = current->GetSpillSlot(); |
| 101 | switch (current->GetType()) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 102 | case DataType::Type::kFloat64: |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 103 | slot += long_spill_slots; |
| 104 | FALLTHROUGH_INTENDED; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 105 | case DataType::Type::kInt64: |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 106 | slot += float_spill_slots; |
| 107 | FALLTHROUGH_INTENDED; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 108 | case DataType::Type::kFloat32: |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 109 | slot += int_spill_slots; |
| 110 | FALLTHROUGH_INTENDED; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 111 | case DataType::Type::kReference: |
| 112 | case DataType::Type::kInt32: |
| 113 | case DataType::Type::kUint16: |
| Vladimir Marko | d5d2f2c | 2017-09-26 12:37:26 +0100 | [diff] [blame] | 114 | case DataType::Type::kUint8: |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 115 | case DataType::Type::kInt8: |
| 116 | case DataType::Type::kBool: |
| 117 | case DataType::Type::kInt16: |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 118 | slot += reserved_out_slots; |
| 119 | break; |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 120 | case DataType::Type::kVoid: |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 121 | LOG(FATAL) << "Unexpected type for interval " << current->GetType(); |
| 122 | } |
| 123 | current->SetSpillSlot(slot * kVRegSize); |
| 124 | } |
| 125 | |
| 126 | Location source = current->ToLocation(); |
| 127 | |
| 128 | if (location.IsUnallocated()) { |
| 129 | if (location.GetPolicy() == Location::kSameAsFirstInput) { |
| 130 | if (locations->InAt(0).IsUnallocated()) { |
| 131 | locations->SetInAt(0, source); |
| 132 | } else { |
| 133 | DCHECK(locations->InAt(0).Equals(source)); |
| 134 | } |
| 135 | } |
| 136 | locations->UpdateOut(source); |
| 137 | } else { |
| 138 | DCHECK(source.Equals(location)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Connect siblings and resolve inputs. |
| 143 | for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { |
| 144 | HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 145 | ConnectSiblings(instruction->GetLiveInterval()); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Resolve non-linear control flow across branches. Order does not matter. |
| Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 149 | for (HBasicBlock* block : codegen_->GetGraph()->GetLinearOrder()) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 150 | if (block->IsCatchBlock() || |
| 151 | (block->IsLoopHeader() && block->GetLoopInformation()->IsIrreducible())) { |
| 152 | // Instructions live at the top of catch blocks or irreducible loop header |
| 153 | // were forced to spill. |
| 154 | if (kIsDebugBuild) { |
| 155 | BitVector* live = liveness_.GetLiveInSet(*block); |
| 156 | for (uint32_t idx : live->Indexes()) { |
| 157 | LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval(); |
| 158 | LiveInterval* sibling = interval->GetSiblingAt(block->GetLifetimeStart()); |
| 159 | // `GetSiblingAt` returns the sibling that contains a position, but there could be |
| 160 | // a lifetime hole in it. `CoversSlow` returns whether the interval is live at that |
| 161 | // position. |
| 162 | if ((sibling != nullptr) && sibling->CoversSlow(block->GetLifetimeStart())) { |
| 163 | DCHECK(!sibling->HasRegister()); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } else { |
| 168 | BitVector* live = liveness_.GetLiveInSet(*block); |
| 169 | for (uint32_t idx : live->Indexes()) { |
| 170 | LiveInterval* interval = liveness_.GetInstructionFromSsaIndex(idx)->GetLiveInterval(); |
| 171 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
| 172 | ConnectSplitSiblings(interval, predecessor, block); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Resolve phi inputs. Order does not matter. |
| Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 179 | for (HBasicBlock* block : codegen_->GetGraph()->GetLinearOrder()) { |
| 180 | if (block->IsCatchBlock()) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 181 | // Catch phi values are set at runtime by the exception delivery mechanism. |
| 182 | } else { |
| Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 183 | for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 184 | HInstruction* phi = inst_it.Current(); |
| Aart Bik | 9620230 | 2016-10-04 17:33:56 -0700 | [diff] [blame] | 185 | for (size_t i = 0, e = block->GetPredecessors().size(); i < e; ++i) { |
| 186 | HBasicBlock* predecessor = block->GetPredecessors()[i]; |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 187 | DCHECK_EQ(predecessor->GetNormalSuccessors().size(), 1u); |
| 188 | HInstruction* input = phi->InputAt(i); |
| 189 | Location source = input->GetLiveInterval()->GetLocationAt( |
| 190 | predecessor->GetLifetimeEnd() - 1); |
| 191 | Location destination = phi->GetLiveInterval()->ToLocation(); |
| 192 | InsertParallelMoveAtExitOf(predecessor, phi, source, destination); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Resolve temp locations. |
| 199 | for (LiveInterval* temp : temp_intervals) { |
| 200 | if (temp->IsHighInterval()) { |
| 201 | // High intervals can be skipped, they are already handled by the low interval. |
| 202 | continue; |
| 203 | } |
| 204 | HInstruction* at = liveness_.GetTempUser(temp); |
| 205 | size_t temp_index = liveness_.GetTempIndex(temp); |
| 206 | LocationSummary* locations = at->GetLocations(); |
| 207 | switch (temp->GetType()) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 208 | case DataType::Type::kInt32: |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 209 | locations->SetTempAt(temp_index, Location::RegisterLocation(temp->GetRegister())); |
| 210 | break; |
| 211 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 212 | case DataType::Type::kFloat64: |
| 213 | if (codegen_->NeedsTwoRegisters(DataType::Type::kFloat64)) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 214 | Location location = Location::FpuRegisterPairLocation( |
| 215 | temp->GetRegister(), temp->GetHighInterval()->GetRegister()); |
| 216 | locations->SetTempAt(temp_index, location); |
| 217 | } else { |
| 218 | locations->SetTempAt(temp_index, Location::FpuRegisterLocation(temp->GetRegister())); |
| 219 | } |
| 220 | break; |
| 221 | |
| 222 | default: |
| 223 | LOG(FATAL) << "Unexpected type for temporary location " |
| 224 | << temp->GetType(); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 229 | void RegisterAllocationResolver::UpdateSafepointLiveRegisters() { |
| 230 | for (size_t i = 0, e = liveness_.GetNumberOfSsaValues(); i < e; ++i) { |
| 231 | HInstruction* instruction = liveness_.GetInstructionFromSsaIndex(i); |
| 232 | for (LiveInterval* current = instruction->GetLiveInterval(); |
| 233 | current != nullptr; |
| 234 | current = current->GetNextSibling()) { |
| 235 | if (!current->HasRegister()) { |
| 236 | continue; |
| 237 | } |
| 238 | Location source = current->ToLocation(); |
| 239 | for (SafepointPosition* safepoint_position = current->GetFirstSafepoint(); |
| 240 | safepoint_position != nullptr; |
| 241 | safepoint_position = safepoint_position->GetNext()) { |
| 242 | DCHECK(current->CoversSlow(safepoint_position->GetPosition())); |
| 243 | LocationSummary* locations = safepoint_position->GetLocations(); |
| 244 | switch (source.GetKind()) { |
| 245 | case Location::kRegister: |
| 246 | case Location::kFpuRegister: { |
| 247 | locations->AddLiveRegister(source); |
| 248 | break; |
| 249 | } |
| 250 | case Location::kRegisterPair: |
| 251 | case Location::kFpuRegisterPair: { |
| 252 | locations->AddLiveRegister(source.ToLow()); |
| 253 | locations->AddLiveRegister(source.ToHigh()); |
| 254 | break; |
| 255 | } |
| 256 | case Location::kStackSlot: // Fall-through |
| 257 | case Location::kDoubleStackSlot: // Fall-through |
| 258 | case Location::kConstant: { |
| 259 | // Nothing to do. |
| 260 | break; |
| 261 | } |
| 262 | default: { |
| 263 | LOG(FATAL) << "Unexpected location for object"; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | size_t RegisterAllocationResolver::CalculateMaximumSafepointSpillSize( |
| 272 | ArrayRef<HInstruction* const> safepoints) { |
| 273 | size_t core_register_spill_size = codegen_->GetWordSize(); |
| 274 | size_t fp_register_spill_size = codegen_->GetFloatingPointSpillSlotSize(); |
| 275 | size_t maximum_safepoint_spill_size = 0u; |
| 276 | for (HInstruction* instruction : safepoints) { |
| 277 | LocationSummary* locations = instruction->GetLocations(); |
| 278 | if (locations->OnlyCallsOnSlowPath()) { |
| 279 | size_t core_spills = |
| 280 | codegen_->GetNumberOfSlowPathSpills(locations, /* core_registers */ true); |
| 281 | size_t fp_spills = |
| 282 | codegen_->GetNumberOfSlowPathSpills(locations, /* core_registers */ false); |
| 283 | size_t spill_size = |
| 284 | core_register_spill_size * core_spills + fp_register_spill_size * fp_spills; |
| 285 | maximum_safepoint_spill_size = std::max(maximum_safepoint_spill_size, spill_size); |
| 286 | } else if (locations->CallsOnMainAndSlowPath()) { |
| 287 | // Nothing to spill on the slow path if the main path already clobbers caller-saves. |
| 288 | DCHECK_EQ(0u, codegen_->GetNumberOfSlowPathSpills(locations, /* core_registers */ true)); |
| 289 | DCHECK_EQ(0u, codegen_->GetNumberOfSlowPathSpills(locations, /* core_registers */ false)); |
| 290 | } |
| 291 | } |
| 292 | return maximum_safepoint_spill_size; |
| 293 | } |
| 294 | |
| 295 | void RegisterAllocationResolver::ConnectSiblings(LiveInterval* interval) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 296 | LiveInterval* current = interval; |
| 297 | if (current->HasSpillSlot() |
| 298 | && current->HasRegister() |
| 299 | // Currently, we spill unconditionnally the current method in the code generators. |
| 300 | && !interval->GetDefinedBy()->IsCurrentMethod()) { |
| 301 | // We spill eagerly, so move must be at definition. |
| Aart Bik | cc89525 | 2017-03-21 10:55:15 -0700 | [diff] [blame] | 302 | Location loc; |
| 303 | switch (interval->NumberOfSpillSlotsNeeded()) { |
| 304 | case 1: loc = Location::StackSlot(interval->GetParent()->GetSpillSlot()); break; |
| 305 | case 2: loc = Location::DoubleStackSlot(interval->GetParent()->GetSpillSlot()); break; |
| Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 306 | case 4: loc = Location::SIMDStackSlot(interval->GetParent()->GetSpillSlot()); break; |
| Aart Bik | cc89525 | 2017-03-21 10:55:15 -0700 | [diff] [blame] | 307 | default: LOG(FATAL) << "Unexpected number of spill slots"; UNREACHABLE(); |
| 308 | } |
| 309 | InsertMoveAfter(interval->GetDefinedBy(), interval->ToLocation(), loc); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 310 | } |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 311 | UsePositionList::const_iterator use_it = current->GetUses().begin(); |
| 312 | const UsePositionList::const_iterator use_end = current->GetUses().end(); |
| 313 | EnvUsePositionList::const_iterator env_use_it = current->GetEnvironmentUses().begin(); |
| 314 | const EnvUsePositionList::const_iterator env_use_end = current->GetEnvironmentUses().end(); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 315 | |
| 316 | // Walk over all siblings, updating locations of use positions, and |
| 317 | // connecting them when they are adjacent. |
| 318 | do { |
| 319 | Location source = current->ToLocation(); |
| 320 | |
| 321 | // Walk over all uses covered by this interval, and update the location |
| 322 | // information. |
| 323 | |
| 324 | LiveRange* range = current->GetFirstRange(); |
| 325 | while (range != nullptr) { |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 326 | // Process uses in the closed interval [range->GetStart(), range->GetEnd()]. |
| 327 | // FindMatchingUseRange() expects a half-open interval, so pass `range->GetEnd() + 1u`. |
| 328 | size_t range_begin = range->GetStart(); |
| 329 | size_t range_end = range->GetEnd() + 1u; |
| 330 | auto matching_use_range = |
| 331 | FindMatchingUseRange(use_it, use_end, range_begin, range_end); |
| 332 | DCHECK(std::all_of(use_it, |
| 333 | matching_use_range.begin(), |
| 334 | [](const UsePosition& pos) { return pos.IsSynthesized(); })); |
| 335 | for (const UsePosition& use : matching_use_range) { |
| 336 | DCHECK(current->CoversSlow(use.GetPosition()) || (use.GetPosition() == range->GetEnd())); |
| 337 | if (!use.IsSynthesized()) { |
| 338 | LocationSummary* locations = use.GetUser()->GetLocations(); |
| 339 | Location expected_location = locations->InAt(use.GetInputIndex()); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 340 | // The expected (actual) location may be invalid in case the input is unused. Currently |
| 341 | // this only happens for intrinsics. |
| 342 | if (expected_location.IsValid()) { |
| 343 | if (expected_location.IsUnallocated()) { |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 344 | locations->SetInAt(use.GetInputIndex(), source); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 345 | } else if (!expected_location.IsConstant()) { |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 346 | AddInputMoveFor( |
| 347 | interval->GetDefinedBy(), use.GetUser(), source, expected_location); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 348 | } |
| 349 | } else { |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 350 | DCHECK(use.GetUser()->IsInvoke()); |
| 351 | DCHECK(use.GetUser()->AsInvoke()->GetIntrinsic() != Intrinsics::kNone); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 354 | } |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 355 | use_it = matching_use_range.end(); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 356 | |
| 357 | // Walk over the environment uses, and update their locations. |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 358 | auto matching_env_use_range = |
| 359 | FindMatchingUseRange(env_use_it, env_use_end, range_begin, range_end); |
| 360 | for (const EnvUsePosition& env_use : matching_env_use_range) { |
| 361 | DCHECK(current->CoversSlow(env_use.GetPosition()) |
| 362 | || (env_use.GetPosition() == range->GetEnd())); |
| 363 | HEnvironment* environment = env_use.GetEnvironment(); |
| 364 | environment->SetLocationAt(env_use.GetInputIndex(), source); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 365 | } |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 366 | env_use_it = matching_env_use_range.end(); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 367 | |
| 368 | range = range->GetNext(); |
| 369 | } |
| 370 | |
| 371 | // If the next interval starts just after this one, and has a register, |
| 372 | // insert a move. |
| 373 | LiveInterval* next_sibling = current->GetNextSibling(); |
| 374 | if (next_sibling != nullptr |
| 375 | && next_sibling->HasRegister() |
| 376 | && current->GetEnd() == next_sibling->GetStart()) { |
| 377 | Location destination = next_sibling->ToLocation(); |
| 378 | InsertParallelMoveAt(current->GetEnd(), interval->GetDefinedBy(), source, destination); |
| 379 | } |
| 380 | |
| 381 | for (SafepointPosition* safepoint_position = current->GetFirstSafepoint(); |
| 382 | safepoint_position != nullptr; |
| 383 | safepoint_position = safepoint_position->GetNext()) { |
| 384 | DCHECK(current->CoversSlow(safepoint_position->GetPosition())); |
| 385 | |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 386 | if (current->GetType() == DataType::Type::kReference) { |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 387 | DCHECK(interval->GetDefinedBy()->IsActualObject()) |
| 388 | << interval->GetDefinedBy()->DebugName() |
| Roland Levillain | 19c5419 | 2016-11-04 13:44:09 +0000 | [diff] [blame] | 389 | << '(' << interval->GetDefinedBy()->GetId() << ')' |
| 390 | << "@" << safepoint_position->GetInstruction()->DebugName() |
| 391 | << '(' << safepoint_position->GetInstruction()->GetId() << ')'; |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 392 | LocationSummary* locations = safepoint_position->GetLocations(); |
| 393 | if (current->GetParent()->HasSpillSlot()) { |
| 394 | locations->SetStackBit(current->GetParent()->GetSpillSlot() / kVRegSize); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 395 | } |
| Vladimir Marko | 70e9746 | 2016-08-09 11:04:26 +0100 | [diff] [blame] | 396 | if (source.GetKind() == Location::kRegister) { |
| 397 | locations->SetRegisterBit(source.reg()); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | current = next_sibling; |
| 402 | } while (current != nullptr); |
| 403 | |
| Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 404 | // Following uses can only be synthesized uses. |
| 405 | DCHECK(std::all_of(use_it, use_end, [](const UsePosition& pos) { return pos.IsSynthesized(); })); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static bool IsMaterializableEntryBlockInstructionOfGraphWithIrreducibleLoop( |
| 409 | HInstruction* instruction) { |
| 410 | return instruction->GetBlock()->GetGraph()->HasIrreducibleLoops() && |
| 411 | (instruction->IsConstant() || instruction->IsCurrentMethod()); |
| 412 | } |
| 413 | |
| 414 | void RegisterAllocationResolver::ConnectSplitSiblings(LiveInterval* interval, |
| 415 | HBasicBlock* from, |
| 416 | HBasicBlock* to) const { |
| 417 | if (interval->GetNextSibling() == nullptr) { |
| 418 | // Nothing to connect. The whole range was allocated to the same location. |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | // Find the intervals that cover `from` and `to`. |
| 423 | size_t destination_position = to->GetLifetimeStart(); |
| 424 | size_t source_position = from->GetLifetimeEnd() - 1; |
| 425 | LiveInterval* destination = interval->GetSiblingAt(destination_position); |
| 426 | LiveInterval* source = interval->GetSiblingAt(source_position); |
| 427 | |
| 428 | if (destination == source) { |
| 429 | // Interval was not split. |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | LiveInterval* parent = interval->GetParent(); |
| 434 | HInstruction* defined_by = parent->GetDefinedBy(); |
| 435 | if (codegen_->GetGraph()->HasIrreducibleLoops() && |
| 436 | (destination == nullptr || !destination->CoversSlow(destination_position))) { |
| 437 | // Our live_in fixed point calculation has found that the instruction is live |
| 438 | // in the `to` block because it will eventually enter an irreducible loop. Our |
| 439 | // live interval computation however does not compute a fixed point, and |
| 440 | // therefore will not have a location for that instruction for `to`. |
| 441 | // Because the instruction is a constant or the ArtMethod, we don't need to |
| 442 | // do anything: it will be materialized in the irreducible loop. |
| 443 | DCHECK(IsMaterializableEntryBlockInstructionOfGraphWithIrreducibleLoop(defined_by)) |
| 444 | << defined_by->DebugName() << ":" << defined_by->GetId() |
| 445 | << " " << from->GetBlockId() << " -> " << to->GetBlockId(); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | if (!destination->HasRegister()) { |
| 450 | // Values are eagerly spilled. Spill slot already contains appropriate value. |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | Location location_source; |
| 455 | // `GetSiblingAt` returns the interval whose start and end cover `position`, |
| 456 | // but does not check whether the interval is inactive at that position. |
| 457 | // The only situation where the interval is inactive at that position is in the |
| 458 | // presence of irreducible loops for constants and ArtMethod. |
| 459 | if (codegen_->GetGraph()->HasIrreducibleLoops() && |
| 460 | (source == nullptr || !source->CoversSlow(source_position))) { |
| 461 | DCHECK(IsMaterializableEntryBlockInstructionOfGraphWithIrreducibleLoop(defined_by)); |
| 462 | if (defined_by->IsConstant()) { |
| 463 | location_source = defined_by->GetLocations()->Out(); |
| 464 | } else { |
| 465 | DCHECK(defined_by->IsCurrentMethod()); |
| Aart Bik | cc89525 | 2017-03-21 10:55:15 -0700 | [diff] [blame] | 466 | switch (parent->NumberOfSpillSlotsNeeded()) { |
| 467 | case 1: location_source = Location::StackSlot(parent->GetSpillSlot()); break; |
| 468 | case 2: location_source = Location::DoubleStackSlot(parent->GetSpillSlot()); break; |
| Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 469 | case 4: location_source = Location::SIMDStackSlot(parent->GetSpillSlot()); break; |
| Aart Bik | cc89525 | 2017-03-21 10:55:15 -0700 | [diff] [blame] | 470 | default: LOG(FATAL) << "Unexpected number of spill slots"; UNREACHABLE(); |
| 471 | } |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 472 | } |
| 473 | } else { |
| 474 | DCHECK(source != nullptr); |
| 475 | DCHECK(source->CoversSlow(source_position)); |
| 476 | DCHECK(destination->CoversSlow(destination_position)); |
| 477 | location_source = source->ToLocation(); |
| 478 | } |
| 479 | |
| 480 | // If `from` has only one successor, we can put the moves at the exit of it. Otherwise |
| 481 | // we need to put the moves at the entry of `to`. |
| 482 | if (from->GetNormalSuccessors().size() == 1) { |
| 483 | InsertParallelMoveAtExitOf(from, |
| 484 | defined_by, |
| 485 | location_source, |
| 486 | destination->ToLocation()); |
| 487 | } else { |
| 488 | DCHECK_EQ(to->GetPredecessors().size(), 1u); |
| 489 | InsertParallelMoveAtEntryOf(to, |
| 490 | defined_by, |
| 491 | location_source, |
| 492 | destination->ToLocation()); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static bool IsValidDestination(Location destination) { |
| 497 | return destination.IsRegister() |
| 498 | || destination.IsRegisterPair() |
| 499 | || destination.IsFpuRegister() |
| 500 | || destination.IsFpuRegisterPair() |
| 501 | || destination.IsStackSlot() |
| Aart Bik | 5576f37 | 2017-03-23 16:17:37 -0700 | [diff] [blame] | 502 | || destination.IsDoubleStackSlot() |
| 503 | || destination.IsSIMDStackSlot(); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void RegisterAllocationResolver::AddMove(HParallelMove* move, |
| 507 | Location source, |
| 508 | Location destination, |
| 509 | HInstruction* instruction, |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 510 | DataType::Type type) const { |
| 511 | if (type == DataType::Type::kInt64 |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 512 | && codegen_->ShouldSplitLongMoves() |
| 513 | // The parallel move resolver knows how to deal with long constants. |
| 514 | && !source.IsConstant()) { |
| Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 515 | move->AddMove(source.ToLow(), destination.ToLow(), DataType::Type::kInt32, instruction); |
| 516 | move->AddMove(source.ToHigh(), destination.ToHigh(), DataType::Type::kInt32, nullptr); |
| Matthew Gharrity | 5d6e27d | 2016-07-18 13:38:44 -0700 | [diff] [blame] | 517 | } else { |
| 518 | move->AddMove(source, destination, type, instruction); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void RegisterAllocationResolver::AddInputMoveFor(HInstruction* input, |
| 523 | HInstruction* user, |
| 524 | Location source, |
| 525 | Location destination) const { |
| 526 | if (source.Equals(destination)) return; |
| 527 | |
| 528 | DCHECK(!user->IsPhi()); |
| 529 | |
| 530 | HInstruction* previous = user->GetPrevious(); |
| 531 | HParallelMove* move = nullptr; |
| 532 | if (previous == nullptr |
| 533 | || !previous->IsParallelMove() |
| 534 | || previous->GetLifetimePosition() < user->GetLifetimePosition()) { |
| 535 | move = new (allocator_) HParallelMove(allocator_); |
| 536 | move->SetLifetimePosition(user->GetLifetimePosition()); |
| 537 | user->GetBlock()->InsertInstructionBefore(move, user); |
| 538 | } else { |
| 539 | move = previous->AsParallelMove(); |
| 540 | } |
| 541 | DCHECK_EQ(move->GetLifetimePosition(), user->GetLifetimePosition()); |
| 542 | AddMove(move, source, destination, nullptr, input->GetType()); |
| 543 | } |
| 544 | |
| 545 | static bool IsInstructionStart(size_t position) { |
| 546 | return (position & 1) == 0; |
| 547 | } |
| 548 | |
| 549 | static bool IsInstructionEnd(size_t position) { |
| 550 | return (position & 1) == 1; |
| 551 | } |
| 552 | |
| 553 | void RegisterAllocationResolver::InsertParallelMoveAt(size_t position, |
| 554 | HInstruction* instruction, |
| 555 | Location source, |
| 556 | Location destination) const { |
| 557 | DCHECK(IsValidDestination(destination)) << destination; |
| 558 | if (source.Equals(destination)) return; |
| 559 | |
| 560 | HInstruction* at = liveness_.GetInstructionFromPosition(position / 2); |
| 561 | HParallelMove* move; |
| 562 | if (at == nullptr) { |
| 563 | if (IsInstructionStart(position)) { |
| 564 | // Block boundary, don't do anything the connection of split siblings will handle it. |
| 565 | return; |
| 566 | } else { |
| 567 | // Move must happen before the first instruction of the block. |
| 568 | at = liveness_.GetInstructionFromPosition((position + 1) / 2); |
| 569 | // Note that parallel moves may have already been inserted, so we explicitly |
| 570 | // ask for the first instruction of the block: `GetInstructionFromPosition` does |
| 571 | // not contain the `HParallelMove` instructions. |
| 572 | at = at->GetBlock()->GetFirstInstruction(); |
| 573 | |
| 574 | if (at->GetLifetimePosition() < position) { |
| 575 | // We may insert moves for split siblings and phi spills at the beginning of the block. |
| 576 | // Since this is a different lifetime position, we need to go to the next instruction. |
| 577 | DCHECK(at->IsParallelMove()); |
| 578 | at = at->GetNext(); |
| 579 | } |
| 580 | |
| 581 | if (at->GetLifetimePosition() != position) { |
| 582 | DCHECK_GT(at->GetLifetimePosition(), position); |
| 583 | move = new (allocator_) HParallelMove(allocator_); |
| 584 | move->SetLifetimePosition(position); |
| 585 | at->GetBlock()->InsertInstructionBefore(move, at); |
| 586 | } else { |
| 587 | DCHECK(at->IsParallelMove()); |
| 588 | move = at->AsParallelMove(); |
| 589 | } |
| 590 | } |
| 591 | } else if (IsInstructionEnd(position)) { |
| 592 | // Move must happen after the instruction. |
| 593 | DCHECK(!at->IsControlFlow()); |
| 594 | move = at->GetNext()->AsParallelMove(); |
| 595 | // This is a parallel move for connecting siblings in a same block. We need to |
| 596 | // differentiate it with moves for connecting blocks, and input moves. |
| 597 | if (move == nullptr || move->GetLifetimePosition() > position) { |
| 598 | move = new (allocator_) HParallelMove(allocator_); |
| 599 | move->SetLifetimePosition(position); |
| 600 | at->GetBlock()->InsertInstructionBefore(move, at->GetNext()); |
| 601 | } |
| 602 | } else { |
| 603 | // Move must happen before the instruction. |
| 604 | HInstruction* previous = at->GetPrevious(); |
| 605 | if (previous == nullptr |
| 606 | || !previous->IsParallelMove() |
| 607 | || previous->GetLifetimePosition() != position) { |
| 608 | // If the previous is a parallel move, then its position must be lower |
| 609 | // than the given `position`: it was added just after the non-parallel |
| 610 | // move instruction that precedes `instruction`. |
| 611 | DCHECK(previous == nullptr |
| 612 | || !previous->IsParallelMove() |
| 613 | || previous->GetLifetimePosition() < position); |
| 614 | move = new (allocator_) HParallelMove(allocator_); |
| 615 | move->SetLifetimePosition(position); |
| 616 | at->GetBlock()->InsertInstructionBefore(move, at); |
| 617 | } else { |
| 618 | move = previous->AsParallelMove(); |
| 619 | } |
| 620 | } |
| 621 | DCHECK_EQ(move->GetLifetimePosition(), position); |
| 622 | AddMove(move, source, destination, instruction, instruction->GetType()); |
| 623 | } |
| 624 | |
| 625 | void RegisterAllocationResolver::InsertParallelMoveAtExitOf(HBasicBlock* block, |
| 626 | HInstruction* instruction, |
| 627 | Location source, |
| 628 | Location destination) const { |
| 629 | DCHECK(IsValidDestination(destination)) << destination; |
| 630 | if (source.Equals(destination)) return; |
| 631 | |
| 632 | DCHECK_EQ(block->GetNormalSuccessors().size(), 1u); |
| 633 | HInstruction* last = block->GetLastInstruction(); |
| 634 | // We insert moves at exit for phi predecessors and connecting blocks. |
| 635 | // A block ending with an if or a packed switch cannot branch to a block |
| 636 | // with phis because we do not allow critical edges. It can also not connect |
| 637 | // a split interval between two blocks: the move has to happen in the successor. |
| 638 | DCHECK(!last->IsIf() && !last->IsPackedSwitch()); |
| 639 | HInstruction* previous = last->GetPrevious(); |
| 640 | HParallelMove* move; |
| 641 | // This is a parallel move for connecting blocks. We need to differentiate |
| 642 | // it with moves for connecting siblings in a same block, and output moves. |
| 643 | size_t position = last->GetLifetimePosition(); |
| 644 | if (previous == nullptr || !previous->IsParallelMove() |
| 645 | || previous->AsParallelMove()->GetLifetimePosition() != position) { |
| 646 | move = new (allocator_) HParallelMove(allocator_); |
| 647 | move->SetLifetimePosition(position); |
| 648 | block->InsertInstructionBefore(move, last); |
| 649 | } else { |
| 650 | move = previous->AsParallelMove(); |
| 651 | } |
| 652 | AddMove(move, source, destination, instruction, instruction->GetType()); |
| 653 | } |
| 654 | |
| 655 | void RegisterAllocationResolver::InsertParallelMoveAtEntryOf(HBasicBlock* block, |
| 656 | HInstruction* instruction, |
| 657 | Location source, |
| 658 | Location destination) const { |
| 659 | DCHECK(IsValidDestination(destination)) << destination; |
| 660 | if (source.Equals(destination)) return; |
| 661 | |
| 662 | HInstruction* first = block->GetFirstInstruction(); |
| 663 | HParallelMove* move = first->AsParallelMove(); |
| 664 | size_t position = block->GetLifetimeStart(); |
| 665 | // This is a parallel move for connecting blocks. We need to differentiate |
| 666 | // it with moves for connecting siblings in a same block, and input moves. |
| 667 | if (move == nullptr || move->GetLifetimePosition() != position) { |
| 668 | move = new (allocator_) HParallelMove(allocator_); |
| 669 | move->SetLifetimePosition(position); |
| 670 | block->InsertInstructionBefore(move, first); |
| 671 | } |
| 672 | AddMove(move, source, destination, instruction, instruction->GetType()); |
| 673 | } |
| 674 | |
| 675 | void RegisterAllocationResolver::InsertMoveAfter(HInstruction* instruction, |
| 676 | Location source, |
| 677 | Location destination) const { |
| 678 | DCHECK(IsValidDestination(destination)) << destination; |
| 679 | if (source.Equals(destination)) return; |
| 680 | |
| 681 | if (instruction->IsPhi()) { |
| 682 | InsertParallelMoveAtEntryOf(instruction->GetBlock(), instruction, source, destination); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | size_t position = instruction->GetLifetimePosition() + 1; |
| 687 | HParallelMove* move = instruction->GetNext()->AsParallelMove(); |
| 688 | // This is a parallel move for moving the output of an instruction. We need |
| 689 | // to differentiate with input moves, moves for connecting siblings in a |
| 690 | // and moves for connecting blocks. |
| 691 | if (move == nullptr || move->GetLifetimePosition() != position) { |
| 692 | move = new (allocator_) HParallelMove(allocator_); |
| 693 | move->SetLifetimePosition(position); |
| 694 | instruction->GetBlock()->InsertInstructionBefore(move, instruction->GetNext()); |
| 695 | } |
| 696 | AddMove(move, source, destination, instruction, instruction->GetType()); |
| 697 | } |
| 698 | |
| 699 | } // namespace art |