blob: b8f70bdc1824b9849e69bb8110885234cf7b2f84 [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
20#include "base/macros.h"
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "primitive.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010022#include "utils/growable_array.h"
23
24namespace art {
25
26class CodeGenerator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010027class HBasicBlock;
28class HGraph;
29class HInstruction;
30class HParallelMove;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010031class LiveInterval;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010032class Location;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010033class SsaLivenessAnalysis;
34
35/**
36 * An implementation of a linear scan register allocator on an `HGraph` with SSA form.
37 */
38class RegisterAllocator {
39 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010040 RegisterAllocator(ArenaAllocator* allocator,
41 CodeGenerator* codegen,
42 const SsaLivenessAnalysis& analysis);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010043
44 // Main entry point for the register allocator. Given the liveness analysis,
45 // allocates registers to live intervals.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010046 void AllocateRegisters();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010047
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 Geoffray86dbb9a2014-06-04 11:12:39 +010050 bool Validate(bool log_fatal_on_failure) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010051 processing_core_registers_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010052 if (!ValidateInternal(log_fatal_on_failure)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010053 return false;
54 }
55 processing_core_registers_ = false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010056 return ValidateInternal(log_fatal_on_failure);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057 }
58
59 // Helper method for validation. Used by unit testing.
60 static bool ValidateIntervals(const GrowableArray<LiveInterval*>& intervals,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010061 size_t number_of_spill_slots,
Nicolas Geoffray39468442014-09-02 15:17:15 +010062 size_t number_of_out_slots,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010063 const CodeGenerator& codegen,
64 ArenaAllocator* allocator,
65 bool processing_core_registers,
66 bool log_fatal_on_failure);
67
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010068 static bool CanAllocateRegistersFor(const HGraph& graph, InstructionSet instruction_set);
69 static bool Supports(InstructionSet instruction_set) {
Alexandre Rames3e69f162014-12-10 10:36:50 +000070 return instruction_set == kArm
71 || instruction_set == kArm64
72 || instruction_set == kThumb2
73 || instruction_set == kX86
74 || instruction_set == kX86_64;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010075 }
76
77 size_t GetNumberOfSpillSlots() const {
78 return spill_slots_.Size();
79 }
80
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010081 private:
82 // Main methods of the allocator.
83 void LinearScan();
84 bool TryAllocateFreeReg(LiveInterval* interval);
85 bool AllocateBlockedReg(LiveInterval* interval);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010086 void Resolve();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010087
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 // Add `interval` in the given sorted list.
89 static void AddSorted(GrowableArray<LiveInterval*>* array, LiveInterval* interval);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010090
91 // Split `interval` at the position `at`. The new interval starts at `at`.
92 LiveInterval* Split(LiveInterval* interval, size_t at);
93
94 // Returns whether `reg` is blocked by the code generator.
95 bool IsBlocked(int reg) const;
96
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010097 // Update the interval for the register in `location` to cover [start, end).
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010098 void BlockRegister(Location location, size_t start, size_t end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010099
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100100 // Allocate a spill slot for the given interval.
101 void AllocateSpillSlotFor(LiveInterval* interval);
102
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100103 // Connect adjacent siblings within blocks.
104 void ConnectSiblings(LiveInterval* interval);
105
106 // Connect siblings between block entries and exits.
107 void ConnectSplitSiblings(LiveInterval* interval, HBasicBlock* from, HBasicBlock* to) const;
108
109 // Helper methods to insert parallel moves in the graph.
Nicolas Geoffray740475d2014-09-29 10:33:25 +0100110 void InsertParallelMoveAtExitOf(HBasicBlock* block,
111 HInstruction* instruction,
112 Location source,
113 Location destination) const;
114 void InsertParallelMoveAtEntryOf(HBasicBlock* block,
115 HInstruction* instruction,
116 Location source,
117 Location destination) const;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100118 void InsertMoveAfter(HInstruction* instruction, Location source, Location destination) const;
Nicolas Geoffray740475d2014-09-29 10:33:25 +0100119 void AddInputMoveFor(HInstruction* user, Location source, Location destination) const;
120 void InsertParallelMoveAt(size_t position,
121 HInstruction* instruction,
122 Location source,
123 Location destination) const;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125 // Helper methods.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100126 void AllocateRegistersInternal();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100127 void ProcessInstruction(HInstruction* instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100128 bool ValidateInternal(bool log_fatal_on_failure) const;
129 void DumpInterval(std::ostream& stream, LiveInterval* interval) const;
Mingyao Yang296bd602014-10-06 16:47:28 -0700130 void DumpAllIntervals(std::ostream& stream) const;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000131 int FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000132 int FindAvailableRegister(size_t* next_use) const;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000134 // Try splitting an active non-pair interval at the given `position`.
135 // Returns whether it was successful at finding such an interval.
136 bool TrySplitNonPairIntervalAt(size_t position, size_t first_register_use, size_t* next_use);
137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 ArenaAllocator* const allocator_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100139 CodeGenerator* const codegen_;
140 const SsaLivenessAnalysis& liveness_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141
Nicolas Geoffray39468442014-09-02 15:17:15 +0100142 // List of intervals for core registers that must be processed, ordered by start
143 // position. Last entry is the interval that has the lowest start position.
144 // This list is initially populated before doing the linear scan.
145 GrowableArray<LiveInterval*> unhandled_core_intervals_;
146
147 // List of intervals for floating-point registers. Same comments as above.
148 GrowableArray<LiveInterval*> unhandled_fp_intervals_;
149
150 // Currently processed list of unhandled intervals. Either `unhandled_core_intervals_`
151 // or `unhandled_fp_intervals_`.
152 GrowableArray<LiveInterval*>* unhandled_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100153
154 // List of intervals that have been processed.
155 GrowableArray<LiveInterval*> handled_;
156
157 // List of intervals that are currently active when processing a new live interval.
158 // That is, they have a live range that spans the start of the new interval.
159 GrowableArray<LiveInterval*> active_;
160
161 // List of intervals that are currently inactive when processing a new live interval.
162 // That is, they have a lifetime hole that spans the start of the new interval.
163 GrowableArray<LiveInterval*> inactive_;
164
Nicolas Geoffray39468442014-09-02 15:17:15 +0100165 // Fixed intervals for physical registers. Such intervals cover the positions
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100166 // where an instruction requires a specific register.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100167 GrowableArray<LiveInterval*> physical_core_register_intervals_;
168 GrowableArray<LiveInterval*> physical_fp_register_intervals_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100169
Nicolas Geoffray39468442014-09-02 15:17:15 +0100170 // Intervals for temporaries. Such intervals cover the positions
171 // where an instruction requires a temporary.
172 GrowableArray<LiveInterval*> temp_intervals_;
173
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100174 // The spill slots allocated for live intervals.
175 GrowableArray<size_t> spill_slots_;
176
Nicolas Geoffray39468442014-09-02 15:17:15 +0100177 // Instructions that need a safepoint.
178 GrowableArray<HInstruction*> safepoints_;
179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100180 // True if processing core registers. False if processing floating
181 // point registers.
182 bool processing_core_registers_;
183
184 // Number of registers for the current register kind (core or floating point).
185 size_t number_of_registers_;
186
187 // Temporary array, allocated ahead of time for simplicity.
188 size_t* registers_array_;
189
190 // Blocked registers, as decided by the code generator.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100191 bool* const blocked_core_registers_;
192 bool* const blocked_fp_registers_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100193
Nicolas Geoffray39468442014-09-02 15:17:15 +0100194 // Slots reserved for out arguments.
195 size_t reserved_out_slots_;
196
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500197 // The maximum live core registers at safepoints.
198 size_t maximum_number_of_live_core_registers_;
199
200 // The maximum live FP registers at safepoints.
201 size_t maximum_number_of_live_fp_registers_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100202
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700203 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000204 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100205
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100206 DISALLOW_COPY_AND_ASSIGN(RegisterAllocator);
207};
208
209} // namespace art
210
211#endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_