blob: 58600b789b939f9e64bf0f6a587d3203766ebb86 [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"
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010021#include "base/arena_containers.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010022#include "base/macros.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "primitive.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010024
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.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010062 static bool ValidateIntervals(const ArenaVector<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 {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010073 return int_spill_slots_.size()
74 + long_spill_slots_.size()
75 + float_spill_slots_.size()
76 + double_spill_slots_.size()
David Brazdil77a48ae2015-09-15 12:34:04 +000077 + 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.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010090 static void AddSorted(ArenaVector<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 Geoffraya7062e02014-05-22 12:50:17 +0100162 ArenaAllocator* const allocator_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100163 CodeGenerator* const codegen_;
164 const SsaLivenessAnalysis& liveness_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100165
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 // List of intervals for core registers that must be processed, ordered by start
167 // position. Last entry is the interval that has the lowest start position.
168 // This list is initially populated before doing the linear scan.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100169 ArenaVector<LiveInterval*> unhandled_core_intervals_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100170
171 // List of intervals for floating-point registers. Same comments as above.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100172 ArenaVector<LiveInterval*> unhandled_fp_intervals_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100173
174 // Currently processed list of unhandled intervals. Either `unhandled_core_intervals_`
175 // or `unhandled_fp_intervals_`.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100176 ArenaVector<LiveInterval*>* unhandled_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100177
178 // List of intervals that have been processed.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100179 ArenaVector<LiveInterval*> handled_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100180
181 // List of intervals that are currently active when processing a new live interval.
182 // That is, they have a live range that spans the start of the new interval.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100183 ArenaVector<LiveInterval*> active_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100184
185 // List of intervals that are currently inactive when processing a new live interval.
186 // That is, they have a lifetime hole that spans the start of the new interval.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100187 ArenaVector<LiveInterval*> inactive_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100188
Nicolas Geoffray39468442014-09-02 15:17:15 +0100189 // Fixed intervals for physical registers. Such intervals cover the positions
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100190 // where an instruction requires a specific register.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100191 ArenaVector<LiveInterval*> physical_core_register_intervals_;
192 ArenaVector<LiveInterval*> physical_fp_register_intervals_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100193
Nicolas Geoffray39468442014-09-02 15:17:15 +0100194 // Intervals for temporaries. Such intervals cover the positions
195 // where an instruction requires a temporary.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100196 ArenaVector<LiveInterval*> temp_intervals_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100197
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000198 // The spill slots allocated for live intervals. We ensure spill slots
199 // are typed to avoid (1) doing moves and swaps between two different kinds
200 // of registers, and (2) swapping between a single stack slot and a double
201 // stack slot. This simplifies the parallel move resolver.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100202 ArenaVector<size_t> int_spill_slots_;
203 ArenaVector<size_t> long_spill_slots_;
204 ArenaVector<size_t> float_spill_slots_;
205 ArenaVector<size_t> double_spill_slots_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100206
David Brazdil77a48ae2015-09-15 12:34:04 +0000207 // Spill slots allocated to catch phis. This category is special-cased because
208 // (1) slots are allocated prior to linear scan and in reverse linear order,
209 // (2) equivalent phis need to share slots despite having different types.
210 size_t catch_phi_spill_slots_;
211
Nicolas Geoffray39468442014-09-02 15:17:15 +0100212 // Instructions that need a safepoint.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100213 ArenaVector<HInstruction*> safepoints_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100214
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100215 // True if processing core registers. False if processing floating
216 // point registers.
217 bool processing_core_registers_;
218
219 // Number of registers for the current register kind (core or floating point).
220 size_t number_of_registers_;
221
222 // Temporary array, allocated ahead of time for simplicity.
223 size_t* registers_array_;
224
225 // Blocked registers, as decided by the code generator.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100226 bool* const blocked_core_registers_;
227 bool* const blocked_fp_registers_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100228
Nicolas Geoffray39468442014-09-02 15:17:15 +0100229 // Slots reserved for out arguments.
230 size_t reserved_out_slots_;
231
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500232 // The maximum live core registers at safepoints.
233 size_t maximum_number_of_live_core_registers_;
234
235 // The maximum live FP registers at safepoints.
236 size_t maximum_number_of_live_fp_registers_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100237
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700238 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000239 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100240
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100241 DISALLOW_COPY_AND_ASSIGN(RegisterAllocator);
242};
243
244} // namespace art
245
246#endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_