blob: e0304643e6c50aa9f9412e991cf90892b32e78c8 [file] [log] [blame]
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001/*
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 Marko80afd022015-05-19 18:08:00 +010020#include "arch/instruction_set.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010021#include "base/macros.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010023#include "utils/growable_array.h"
24
25namespace art {
26
27class CodeGenerator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010028class HBasicBlock;
29class HGraph;
30class HInstruction;
31class HParallelMove;
David Brazdil77a48ae2015-09-15 12:34:04 +000032class HPhi;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010033class LiveInterval;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010034class Location;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010035class SsaLivenessAnalysis;
36
37/**
38 * An implementation of a linear scan register allocator on an `HGraph` with SSA form.
39 */
40class RegisterAllocator {
41 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010042 RegisterAllocator(ArenaAllocator* allocator,
43 CodeGenerator* codegen,
44 const SsaLivenessAnalysis& analysis);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010045
46 // Main entry point for the register allocator. Given the liveness analysis,
47 // allocates registers to live intervals.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010048 void AllocateRegisters();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010049
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 Geoffray86dbb9a2014-06-04 11:12:39 +010052 bool Validate(bool log_fatal_on_failure) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053 processing_core_registers_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010054 if (!ValidateInternal(log_fatal_on_failure)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 return false;
56 }
57 processing_core_registers_ = false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010058 return ValidateInternal(log_fatal_on_failure);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 }
60
61 // Helper method for validation. Used by unit testing.
62 static bool ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010063 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010065 const CodeGenerator& codegen,
66 ArenaAllocator* allocator,
67 bool processing_core_registers,
68 bool log_fatal_on_failure);
69
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010070 static bool CanAllocateRegistersFor(const HGraph& graph, InstructionSet instruction_set);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010071
72 size_t GetNumberOfSpillSlots() const {
Nicolas Geoffray776b3182015-02-23 14:14:57 +000073 return int_spill_slots_.Size()
74 + long_spill_slots_.Size()
75 + float_spill_slots_.Size()
David Brazdil77a48ae2015-09-15 12:34:04 +000076 + double_spill_slots_.Size()
77 + catch_phi_spill_slots_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078 }
79
Andreas Gampe7c3952f2015-02-19 18:21:24 -080080 static constexpr const char* kRegisterAllocatorPassName = "register";
81
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010082 private:
83 // Main methods of the allocator.
84 void LinearScan();
85 bool TryAllocateFreeReg(LiveInterval* interval);
86 bool AllocateBlockedReg(LiveInterval* interval);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010087 void Resolve();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010088
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 // Add `interval` in the given sorted list.
90 static void AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010091
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +010092 // 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 Geoffraya7062e02014-05-22 12:50:17 +010098
99 // Returns whether `reg` is blocked by the code generator.
100 bool IsBlocked(int reg) const;
101
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100102 // Update the interval for the register in `location` to cover [start, end).
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100103 void BlockRegister(Location location, size_t start, size_t end);
David Brazdil77a48ae2015-09-15 12:34:04 +0000104 void BlockRegisters(size_t start, size_t end, bool caller_save_only = false);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100105
David Brazdil77a48ae2015-09-15 12:34:04 +0000106 // Allocate a spill slot for the given interval. Should be called in linear
107 // order of interval starting positions.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100108 void AllocateSpillSlotFor(LiveInterval* interval);
109
David Brazdil77a48ae2015-09-15 12:34:04 +0000110 // 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 Geoffray86dbb9a2014-06-04 11:12:39 +0100115 // 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 Geoffray740475d2014-09-29 10:33:25 +0100122 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 Geoffray86dbb9a2014-06-04 11:12:39 +0100130 void InsertMoveAfter(HInstruction* instruction, Location source, Location destination) const;
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000131 void AddInputMoveFor(HInstruction* input,
132 HInstruction* user,
133 Location source,
134 Location destination) const;
Nicolas Geoffray740475d2014-09-29 10:33:25 +0100135 void InsertParallelMoveAt(size_t position,
136 HInstruction* instruction,
137 Location source,
138 Location destination) const;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100139
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000140 void AddMove(HParallelMove* move,
141 Location source,
142 Location destination,
143 HInstruction* instruction,
144 Primitive::Type type) const;
145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 // Helper methods.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100147 void AllocateRegistersInternal();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100148 void ProcessInstruction(HInstruction* instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100149 bool ValidateInternal(bool log_fatal_on_failure) const;
150 void DumpInterval(std::ostream& stream, LiveInterval* interval) const;
Mingyao Yang296bd602014-10-06 16:47:28 -0700151 void DumpAllIntervals(std::ostream& stream) const;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000152 int FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const;
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100153 int FindAvailableRegister(size_t* next_use, LiveInterval* current) const;
154 bool IsCallerSaveRegister(int reg) const;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100155
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000156 // Try splitting an active non-pair or unaligned pair interval at the given `position`.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000157 // Returns whether it was successful at finding such an interval.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000158 bool TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
159 size_t first_register_use,
160 size_t* next_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000161
Nicolas Geoffray5b168de2015-03-27 10:27:22 +0000162 // 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 Geoffraya7062e02014-05-22 12:50:17 +0100169 ArenaAllocator* const allocator_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100170 CodeGenerator* const codegen_;
171 const SsaLivenessAnalysis& liveness_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100172
Nicolas Geoffray39468442014-09-02 15:17:15 +0100173 // 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 Geoffraya7062e02014-05-22 12:50:17 +0100184
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 Geoffray39468442014-09-02 15:17:15 +0100196 // Fixed intervals for physical registers. Such intervals cover the positions
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100197 // where an instruction requires a specific register.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100198 GrowableArray<LiveInterval*> physical_core_register_intervals_;
199 GrowableArray<LiveInterval*> physical_fp_register_intervals_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100200
Nicolas Geoffray39468442014-09-02 15:17:15 +0100201 // Intervals for temporaries. Such intervals cover the positions
202 // where an instruction requires a temporary.
203 GrowableArray<LiveInterval*> temp_intervals_;
204
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000205 // 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 Geoffray31d76b42014-06-09 15:02:22 +0100213
David Brazdil77a48ae2015-09-15 12:34:04 +0000214 // 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 Geoffray39468442014-09-02 15:17:15 +0100219 // Instructions that need a safepoint.
220 GrowableArray<HInstruction*> safepoints_;
221
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100222 // 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 Geoffray102cbed2014-10-15 18:31:05 +0100233 bool* const blocked_core_registers_;
234 bool* const blocked_fp_registers_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100235
Nicolas Geoffray39468442014-09-02 15:17:15 +0100236 // Slots reserved for out arguments.
237 size_t reserved_out_slots_;
238
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500239 // 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 Geoffray3bca0df2014-09-19 11:01:00 +0100244
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700245 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000246 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100247
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100248 DISALLOW_COPY_AND_ASSIGN(RegisterAllocator);
249};
250
251} // namespace art
252
253#endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_