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