blob: c29fe75921919098c382262961d3a2371de946ed [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;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010032class LiveInterval;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010033class Location;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010034class SsaLivenessAnalysis;
35
36/**
37 * An implementation of a linear scan register allocator on an `HGraph` with SSA form.
38 */
39class RegisterAllocator {
40 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010041 RegisterAllocator(ArenaAllocator* allocator,
42 CodeGenerator* codegen,
43 const SsaLivenessAnalysis& analysis);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010044
45 // Main entry point for the register allocator. Given the liveness analysis,
46 // allocates registers to live intervals.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010047 void AllocateRegisters();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010048
49 // Validate that the register allocator did not allocate the same register to
50 // intervals that intersect each other. Returns false if it did not.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010051 bool Validate(bool log_fatal_on_failure) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010052 processing_core_registers_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010053 if (!ValidateInternal(log_fatal_on_failure)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010054 return false;
55 }
56 processing_core_registers_ = false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010057 return ValidateInternal(log_fatal_on_failure);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058 }
59
60 // Helper method for validation. Used by unit testing.
61 static bool ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010062 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +010063 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010064 const CodeGenerator& codegen,
65 ArenaAllocator* allocator,
66 bool processing_core_registers,
67 bool log_fatal_on_failure);
68
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010069 static bool CanAllocateRegistersFor(const HGraph& graph, InstructionSet instruction_set);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010070
71 size_t GetNumberOfSpillSlots() const {
Nicolas Geoffray776b3182015-02-23 14:14:57 +000072 return int_spill_slots_.Size()
73 + long_spill_slots_.Size()
74 + float_spill_slots_.Size()
David Brazdil659562a2015-09-14 21:26:33 +000075 + double_spill_slots_.Size();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010076 }
77
Andreas Gampe7c3952f2015-02-19 18:21:24 -080078 static constexpr const char* kRegisterAllocatorPassName = "register";
79
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 private:
81 // Main methods of the allocator.
82 void LinearScan();
83 bool TryAllocateFreeReg(LiveInterval* interval);
84 bool AllocateBlockedReg(LiveInterval* interval);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010085 void Resolve();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010086
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 // Add `interval` in the given sorted list.
88 static void AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010089
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +010090 // Split `interval` at the position `position`. The new interval starts at `position`.
91 LiveInterval* Split(LiveInterval* interval, size_t position);
92
93 // Split `interval` at a position between `from` and `to`. The method will try
94 // to find an optimal split position.
95 LiveInterval* SplitBetween(LiveInterval* interval, size_t from, size_t to);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010096
97 // Returns whether `reg` is blocked by the code generator.
98 bool IsBlocked(int reg) const;
99
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100100 // Update the interval for the register in `location` to cover [start, end).
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100101 void BlockRegister(Location location, size_t start, size_t end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100102
David Brazdil659562a2015-09-14 21:26:33 +0000103 // Allocate a spill slot for the given interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100104 void AllocateSpillSlotFor(LiveInterval* interval);
105
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100106 // Connect adjacent siblings within blocks.
107 void ConnectSiblings(LiveInterval* interval);
108
109 // Connect siblings between block entries and exits.
110 void ConnectSplitSiblings(LiveInterval* interval, HBasicBlock* from, HBasicBlock* to) const;
111
112 // Helper methods to insert parallel moves in the graph.
Nicolas Geoffray740475d2014-09-29 10:33:25 +0100113 void InsertParallelMoveAtExitOf(HBasicBlock* block,
114 HInstruction* instruction,
115 Location source,
116 Location destination) const;
117 void InsertParallelMoveAtEntryOf(HBasicBlock* block,
118 HInstruction* instruction,
119 Location source,
120 Location destination) const;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100121 void InsertMoveAfter(HInstruction* instruction, Location source, Location destination) const;
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000122 void AddInputMoveFor(HInstruction* input,
123 HInstruction* user,
124 Location source,
125 Location destination) const;
Nicolas Geoffray740475d2014-09-29 10:33:25 +0100126 void InsertParallelMoveAt(size_t position,
127 HInstruction* instruction,
128 Location source,
129 Location destination) const;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100130
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000131 void AddMove(HParallelMove* move,
132 Location source,
133 Location destination,
134 HInstruction* instruction,
135 Primitive::Type type) const;
136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137 // Helper methods.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100138 void AllocateRegistersInternal();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100139 void ProcessInstruction(HInstruction* instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100140 bool ValidateInternal(bool log_fatal_on_failure) const;
141 void DumpInterval(std::ostream& stream, LiveInterval* interval) const;
Mingyao Yang296bd602014-10-06 16:47:28 -0700142 void DumpAllIntervals(std::ostream& stream) const;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000143 int FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const;
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100144 int FindAvailableRegister(size_t* next_use, LiveInterval* current) const;
145 bool IsCallerSaveRegister(int reg) const;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000147 // Try splitting an active non-pair or unaligned pair interval at the given `position`.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000148 // Returns whether it was successful at finding such an interval.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000149 bool TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
150 size_t first_register_use,
151 size_t* next_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000152
Nicolas Geoffray5b168de2015-03-27 10:27:22 +0000153 // If `interval` has another half, remove it from the list of `intervals`.
154 // `index` holds the index at which `interval` is in `intervals`.
155 // Returns whether there is another half.
156 bool PotentiallyRemoveOtherHalf(LiveInterval* interval,
157 GrowableArray<LiveInterval*>* intervals,
158 size_t index);
159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100160 ArenaAllocator* const allocator_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100161 CodeGenerator* const codegen_;
162 const SsaLivenessAnalysis& liveness_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100163
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 // List of intervals for core registers that must be processed, ordered by start
165 // position. Last entry is the interval that has the lowest start position.
166 // This list is initially populated before doing the linear scan.
167 GrowableArray<LiveInterval*> unhandled_core_intervals_;
168
169 // List of intervals for floating-point registers. Same comments as above.
170 GrowableArray<LiveInterval*> unhandled_fp_intervals_;
171
172 // Currently processed list of unhandled intervals. Either `unhandled_core_intervals_`
173 // or `unhandled_fp_intervals_`.
174 GrowableArray<LiveInterval*>* unhandled_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100175
176 // List of intervals that have been processed.
177 GrowableArray<LiveInterval*> handled_;
178
179 // List of intervals that are currently active when processing a new live interval.
180 // That is, they have a live range that spans the start of the new interval.
181 GrowableArray<LiveInterval*> active_;
182
183 // List of intervals that are currently inactive when processing a new live interval.
184 // That is, they have a lifetime hole that spans the start of the new interval.
185 GrowableArray<LiveInterval*> inactive_;
186
Nicolas Geoffray39468442014-09-02 15:17:15 +0100187 // Fixed intervals for physical registers. Such intervals cover the positions
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100188 // where an instruction requires a specific register.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100189 GrowableArray<LiveInterval*> physical_core_register_intervals_;
190 GrowableArray<LiveInterval*> physical_fp_register_intervals_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100191
Nicolas Geoffray39468442014-09-02 15:17:15 +0100192 // Intervals for temporaries. Such intervals cover the positions
193 // where an instruction requires a temporary.
194 GrowableArray<LiveInterval*> temp_intervals_;
195
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000196 // The spill slots allocated for live intervals. We ensure spill slots
197 // are typed to avoid (1) doing moves and swaps between two different kinds
198 // of registers, and (2) swapping between a single stack slot and a double
199 // stack slot. This simplifies the parallel move resolver.
200 GrowableArray<size_t> int_spill_slots_;
201 GrowableArray<size_t> long_spill_slots_;
202 GrowableArray<size_t> float_spill_slots_;
203 GrowableArray<size_t> double_spill_slots_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100204
Nicolas Geoffray39468442014-09-02 15:17:15 +0100205 // Instructions that need a safepoint.
206 GrowableArray<HInstruction*> safepoints_;
207
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100208 // True if processing core registers. False if processing floating
209 // point registers.
210 bool processing_core_registers_;
211
212 // Number of registers for the current register kind (core or floating point).
213 size_t number_of_registers_;
214
215 // Temporary array, allocated ahead of time for simplicity.
216 size_t* registers_array_;
217
218 // Blocked registers, as decided by the code generator.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100219 bool* const blocked_core_registers_;
220 bool* const blocked_fp_registers_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100221
Nicolas Geoffray39468442014-09-02 15:17:15 +0100222 // Slots reserved for out arguments.
223 size_t reserved_out_slots_;
224
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500225 // The maximum live core registers at safepoints.
226 size_t maximum_number_of_live_core_registers_;
227
228 // The maximum live FP registers at safepoints.
229 size_t maximum_number_of_live_fp_registers_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100230
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700231 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000232 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100234 DISALLOW_COPY_AND_ASSIGN(RegisterAllocator);
235};
236
237} // namespace art
238
239#endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_