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