| 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 | #ifndef ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_ |
| 19 | |
| Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 20 | #include "arch/instruction_set.h" |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 21 | #include "base/macros.h" |
| Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 22 | #include "primitive.h" |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 23 | #include "utils/growable_array.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class CodeGenerator; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 28 | class HBasicBlock; |
| 29 | class HGraph; |
| 30 | class HInstruction; |
| 31 | class HParallelMove; |
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame^] | 32 | class HPhi; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 33 | class LiveInterval; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 34 | class Location; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 35 | class SsaLivenessAnalysis; |
| 36 | |
| 37 | /** |
| 38 | * An implementation of a linear scan register allocator on an `HGraph` with SSA form. |
| 39 | */ |
| 40 | class RegisterAllocator { |
| 41 | public: |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 42 | RegisterAllocator(ArenaAllocator* allocator, |
| 43 | CodeGenerator* codegen, |
| 44 | const SsaLivenessAnalysis& analysis); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 45 | |
| 46 | // Main entry point for the register allocator. Given the liveness analysis, |
| 47 | // allocates registers to live intervals. |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 48 | void AllocateRegisters(); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 49 | |
| 50 | // Validate that the register allocator did not allocate the same register to |
| 51 | // intervals that intersect each other. Returns false if it did not. |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 52 | bool Validate(bool log_fatal_on_failure) { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 53 | processing_core_registers_ = true; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 54 | if (!ValidateInternal(log_fatal_on_failure)) { |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | processing_core_registers_ = false; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 58 | return ValidateInternal(log_fatal_on_failure); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Helper method for validation. Used by unit testing. |
| 62 | static bool ValidateIntervals(const GrowableArray<LiveInterval*>& intervals, |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 63 | size_t number_of_spill_slots, |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 64 | size_t number_of_out_slots, |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 65 | const CodeGenerator& codegen, |
| 66 | ArenaAllocator* allocator, |
| 67 | bool processing_core_registers, |
| 68 | bool log_fatal_on_failure); |
| 69 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 70 | static bool CanAllocateRegistersFor(const HGraph& graph, InstructionSet instruction_set); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 71 | |
| 72 | size_t GetNumberOfSpillSlots() const { |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 73 | return int_spill_slots_.Size() |
| 74 | + long_spill_slots_.Size() |
| 75 | + float_spill_slots_.Size() |
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame^] | 76 | + double_spill_slots_.Size() |
| 77 | + catch_phi_spill_slots_; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 80 | static constexpr const char* kRegisterAllocatorPassName = "register"; |
| 81 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 82 | private: |
| 83 | // Main methods of the allocator. |
| 84 | void LinearScan(); |
| 85 | bool TryAllocateFreeReg(LiveInterval* interval); |
| 86 | bool AllocateBlockedReg(LiveInterval* interval); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 87 | void Resolve(); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 88 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 89 | // Add `interval` in the given sorted list. |
| 90 | static void AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 91 | |
| Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 92 | // Split `interval` at the position `position`. The new interval starts at `position`. |
| 93 | LiveInterval* Split(LiveInterval* interval, size_t position); |
| 94 | |
| 95 | // Split `interval` at a position between `from` and `to`. The method will try |
| 96 | // to find an optimal split position. |
| 97 | LiveInterval* SplitBetween(LiveInterval* interval, size_t from, size_t to); |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 98 | |
| 99 | // Returns whether `reg` is blocked by the code generator. |
| 100 | bool IsBlocked(int reg) const; |
| 101 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 102 | // Update the interval for the register in `location` to cover [start, end). |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 103 | void BlockRegister(Location location, size_t start, size_t end); |
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame^] | 104 | void BlockRegisters(size_t start, size_t end, bool caller_save_only = false); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 105 | |
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame^] | 106 | // Allocate a spill slot for the given interval. Should be called in linear |
| 107 | // order of interval starting positions. |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 108 | void AllocateSpillSlotFor(LiveInterval* interval); |
| 109 | |
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame^] | 110 | // Allocate a spill slot for the given catch phi. Will allocate the same slot |
| 111 | // for phis which share the same vreg. Must be called in reverse linear order |
| 112 | // of lifetime positions and ascending vreg numbers for correctness. |
| 113 | void AllocateSpillSlotForCatchPhi(HPhi* phi); |
| 114 | |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 115 | // Connect adjacent siblings within blocks. |
| 116 | void ConnectSiblings(LiveInterval* interval); |
| 117 | |
| 118 | // Connect siblings between block entries and exits. |
| 119 | void ConnectSplitSiblings(LiveInterval* interval, HBasicBlock* from, HBasicBlock* to) const; |
| 120 | |
| 121 | // Helper methods to insert parallel moves in the graph. |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 122 | void InsertParallelMoveAtExitOf(HBasicBlock* block, |
| 123 | HInstruction* instruction, |
| 124 | Location source, |
| 125 | Location destination) const; |
| 126 | void InsertParallelMoveAtEntryOf(HBasicBlock* block, |
| 127 | HInstruction* instruction, |
| 128 | Location source, |
| 129 | Location destination) const; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 130 | void InsertMoveAfter(HInstruction* instruction, Location source, Location destination) const; |
| Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 131 | void AddInputMoveFor(HInstruction* input, |
| 132 | HInstruction* user, |
| 133 | Location source, |
| 134 | Location destination) const; |
| Nicolas Geoffray | 740475d | 2014-09-29 10:33:25 +0100 | [diff] [blame] | 135 | void InsertParallelMoveAt(size_t position, |
| 136 | HInstruction* instruction, |
| 137 | Location source, |
| 138 | Location destination) const; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 139 | |
| Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 140 | void AddMove(HParallelMove* move, |
| 141 | Location source, |
| 142 | Location destination, |
| 143 | HInstruction* instruction, |
| 144 | Primitive::Type type) const; |
| 145 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 146 | // Helper methods. |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 147 | void AllocateRegistersInternal(); |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 148 | void ProcessInstruction(HInstruction* instruction); |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 149 | bool ValidateInternal(bool log_fatal_on_failure) const; |
| 150 | void DumpInterval(std::ostream& stream, LiveInterval* interval) const; |
| Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 151 | void DumpAllIntervals(std::ostream& stream) const; |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 152 | int FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const; |
| Nicolas Geoffray | 8826f67 | 2015-04-17 09:15:11 +0100 | [diff] [blame] | 153 | int FindAvailableRegister(size_t* next_use, LiveInterval* current) const; |
| 154 | bool IsCallerSaveRegister(int reg) const; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 155 | |
| Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 156 | // Try splitting an active non-pair or unaligned pair interval at the given `position`. |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 157 | // Returns whether it was successful at finding such an interval. |
| Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 158 | bool TrySplitNonPairOrUnalignedPairIntervalAt(size_t position, |
| 159 | size_t first_register_use, |
| 160 | size_t* next_use); |
| Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 161 | |
| Nicolas Geoffray | 5b168de | 2015-03-27 10:27:22 +0000 | [diff] [blame] | 162 | // If `interval` has another half, remove it from the list of `intervals`. |
| 163 | // `index` holds the index at which `interval` is in `intervals`. |
| 164 | // Returns whether there is another half. |
| 165 | bool PotentiallyRemoveOtherHalf(LiveInterval* interval, |
| 166 | GrowableArray<LiveInterval*>* intervals, |
| 167 | size_t index); |
| 168 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 169 | ArenaAllocator* const allocator_; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 170 | CodeGenerator* const codegen_; |
| 171 | const SsaLivenessAnalysis& liveness_; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 172 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 173 | // List of intervals for core registers that must be processed, ordered by start |
| 174 | // position. Last entry is the interval that has the lowest start position. |
| 175 | // This list is initially populated before doing the linear scan. |
| 176 | GrowableArray<LiveInterval*> unhandled_core_intervals_; |
| 177 | |
| 178 | // List of intervals for floating-point registers. Same comments as above. |
| 179 | GrowableArray<LiveInterval*> unhandled_fp_intervals_; |
| 180 | |
| 181 | // Currently processed list of unhandled intervals. Either `unhandled_core_intervals_` |
| 182 | // or `unhandled_fp_intervals_`. |
| 183 | GrowableArray<LiveInterval*>* unhandled_; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 184 | |
| 185 | // List of intervals that have been processed. |
| 186 | GrowableArray<LiveInterval*> handled_; |
| 187 | |
| 188 | // List of intervals that are currently active when processing a new live interval. |
| 189 | // That is, they have a live range that spans the start of the new interval. |
| 190 | GrowableArray<LiveInterval*> active_; |
| 191 | |
| 192 | // List of intervals that are currently inactive when processing a new live interval. |
| 193 | // That is, they have a lifetime hole that spans the start of the new interval. |
| 194 | GrowableArray<LiveInterval*> inactive_; |
| 195 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 196 | // Fixed intervals for physical registers. Such intervals cover the positions |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 197 | // where an instruction requires a specific register. |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 198 | GrowableArray<LiveInterval*> physical_core_register_intervals_; |
| 199 | GrowableArray<LiveInterval*> physical_fp_register_intervals_; |
| Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 200 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 201 | // Intervals for temporaries. Such intervals cover the positions |
| 202 | // where an instruction requires a temporary. |
| 203 | GrowableArray<LiveInterval*> temp_intervals_; |
| 204 | |
| Nicolas Geoffray | 776b318 | 2015-02-23 14:14:57 +0000 | [diff] [blame] | 205 | // The spill slots allocated for live intervals. We ensure spill slots |
| 206 | // are typed to avoid (1) doing moves and swaps between two different kinds |
| 207 | // of registers, and (2) swapping between a single stack slot and a double |
| 208 | // stack slot. This simplifies the parallel move resolver. |
| 209 | GrowableArray<size_t> int_spill_slots_; |
| 210 | GrowableArray<size_t> long_spill_slots_; |
| 211 | GrowableArray<size_t> float_spill_slots_; |
| 212 | GrowableArray<size_t> double_spill_slots_; |
| Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 213 | |
| David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame^] | 214 | // Spill slots allocated to catch phis. This category is special-cased because |
| 215 | // (1) slots are allocated prior to linear scan and in reverse linear order, |
| 216 | // (2) equivalent phis need to share slots despite having different types. |
| 217 | size_t catch_phi_spill_slots_; |
| 218 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 219 | // Instructions that need a safepoint. |
| 220 | GrowableArray<HInstruction*> safepoints_; |
| 221 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 222 | // True if processing core registers. False if processing floating |
| 223 | // point registers. |
| 224 | bool processing_core_registers_; |
| 225 | |
| 226 | // Number of registers for the current register kind (core or floating point). |
| 227 | size_t number_of_registers_; |
| 228 | |
| 229 | // Temporary array, allocated ahead of time for simplicity. |
| 230 | size_t* registers_array_; |
| 231 | |
| 232 | // Blocked registers, as decided by the code generator. |
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 233 | bool* const blocked_core_registers_; |
| 234 | bool* const blocked_fp_registers_; |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 235 | |
| Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 236 | // Slots reserved for out arguments. |
| 237 | size_t reserved_out_slots_; |
| 238 | |
| Mark Mendell | f85a9ca | 2015-01-13 09:20:58 -0500 | [diff] [blame] | 239 | // The maximum live core registers at safepoints. |
| 240 | size_t maximum_number_of_live_core_registers_; |
| 241 | |
| 242 | // The maximum live FP registers at safepoints. |
| 243 | size_t maximum_number_of_live_fp_registers_; |
| Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 244 | |
| Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 245 | ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil); |
| Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 246 | ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive); |
| Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 247 | |
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 248 | DISALLOW_COPY_AND_ASSIGN(RegisterAllocator); |
| 249 | }; |
| 250 | |
| 251 | } // namespace art |
| 252 | |
| 253 | #endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_ |