blob: 9a1e0d7f10d299d01151bb44107301bb99f9dc62 [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
Matthew Gharritye9288852016-07-14 14:08:16 -070017#ifndef ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_LINEAR_SCAN_H_
18#define ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_LINEAR_SCAN_H_
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010019
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"
Santiago Aboy Solanes88b5c1d2021-09-14 15:58:22 +010022#include "base/scoped_arena_containers.h"
Matthew Gharrity8f49d4b2016-07-14 13:24:00 -070023#include "register_allocator.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010024
Vladimir Marko0a516052019-10-14 13:00:44 +000025namespace art {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010026
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 */
Matthew Gharrity8f49d4b2016-07-14 13:24:00 -070040class RegisterAllocatorLinearScan : public RegisterAllocator {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010041 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010042 RegisterAllocatorLinearScan(ScopedArenaAllocator* allocator,
Matthew Gharrity8f49d4b2016-07-14 13:24:00 -070043 CodeGenerator* codegen,
44 const SsaLivenessAnalysis& analysis);
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010045 ~RegisterAllocatorLinearScan() override;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010047 void AllocateRegisters() override;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010049 bool Validate(bool log_fatal_on_failure) override {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010050 processing_core_registers_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010051 if (!ValidateInternal(log_fatal_on_failure)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010052 return false;
53 }
54 processing_core_registers_ = false;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010055 return ValidateInternal(log_fatal_on_failure);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010056 }
57
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010058 size_t GetNumberOfSpillSlots() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010059 return int_spill_slots_.size()
60 + long_spill_slots_.size()
61 + float_spill_slots_.size()
62 + double_spill_slots_.size()
David Brazdil77a48ae2015-09-15 12:34:04 +000063 + catch_phi_spill_slots_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010064 }
65
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010066 private:
67 // Main methods of the allocator.
68 void LinearScan();
69 bool TryAllocateFreeReg(LiveInterval* interval);
70 bool AllocateBlockedReg(LiveInterval* interval);
71
Nicolas Geoffray39468442014-09-02 15:17:15 +010072 // Add `interval` in the given sorted list.
Vladimir Markoe764d2e2017-10-05 14:35:55 +010073 static void AddSorted(ScopedArenaVector<LiveInterval*>* array, LiveInterval* interval);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010074
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010075 // Returns whether `reg` is blocked by the code generator.
76 bool IsBlocked(int reg) const;
77
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078 // Update the interval for the register in `location` to cover [start, end).
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010079 void BlockRegister(Location location, size_t start, size_t end);
David Brazdil77a48ae2015-09-15 12:34:04 +000080 void BlockRegisters(size_t start, size_t end, bool caller_save_only = false);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010081
David Brazdil77a48ae2015-09-15 12:34:04 +000082 // Allocate a spill slot for the given interval. Should be called in linear
83 // order of interval starting positions.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010084 void AllocateSpillSlotFor(LiveInterval* interval);
85
David Brazdil77a48ae2015-09-15 12:34:04 +000086 // Allocate a spill slot for the given catch phi. Will allocate the same slot
87 // for phis which share the same vreg. Must be called in reverse linear order
88 // of lifetime positions and ascending vreg numbers for correctness.
89 void AllocateSpillSlotForCatchPhi(HPhi* phi);
90
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010091 // Helper methods.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010092 void AllocateRegistersInternal();
Nicolas Geoffray39468442014-09-02 15:17:15 +010093 void ProcessInstruction(HInstruction* instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010094 bool ValidateInternal(bool log_fatal_on_failure) const;
95 void DumpInterval(std::ostream& stream, LiveInterval* interval) const;
Mingyao Yang296bd602014-10-06 16:47:28 -070096 void DumpAllIntervals(std::ostream& stream) const;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000097 int FindAvailableRegisterPair(size_t* next_use, size_t starting_at) const;
Nicolas Geoffray8826f672015-04-17 09:15:11 +010098 int FindAvailableRegister(size_t* next_use, LiveInterval* current) const;
99 bool IsCallerSaveRegister(int reg) const;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100
Santiago Aboy Solanes88b5c1d2021-09-14 15:58:22 +0100101 // If any inputs require specific registers, block those registers
102 // at the position of this instruction.
103 void CheckForFixedInputs(HInstruction* instruction);
104
105 // If the output of an instruction requires a specific register, split
106 // the interval and assign the register to the first part.
107 void CheckForFixedOutput(HInstruction* instruction);
108
109 // Add all applicable safepoints to a live interval.
110 // Currently depends on instruction processing order.
111 void AddSafepointsFor(HInstruction* instruction);
112
113 // Collect all live intervals associated with the temporary locations
114 // needed by an instruction.
115 void CheckForTempLiveIntervals(HInstruction* instruction);
116
117 // If a safe point is needed, add a synthesized interval to later record
118 // the number of live registers at this point.
119 void CheckForSafepoint(HInstruction* instruction);
120
121 // Try to remove the SuspendCheck at function entry. Returns true if it was successful.
122 bool TryRemoveSuspendCheckEntry(HInstruction* instruction);
123
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000124 // Try splitting an active non-pair or unaligned pair interval at the given `position`.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000125 // Returns whether it was successful at finding such an interval.
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000126 bool TrySplitNonPairOrUnalignedPairIntervalAt(size_t position,
127 size_t first_register_use,
128 size_t* next_use);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000129
Nicolas Geoffray39468442014-09-02 15:17:15 +0100130 // List of intervals for core registers that must be processed, ordered by start
131 // position. Last entry is the interval that has the lowest start position.
132 // This list is initially populated before doing the linear scan.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100133 ScopedArenaVector<LiveInterval*> unhandled_core_intervals_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100134
135 // List of intervals for floating-point registers. Same comments as above.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100136 ScopedArenaVector<LiveInterval*> unhandled_fp_intervals_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100137
138 // Currently processed list of unhandled intervals. Either `unhandled_core_intervals_`
139 // or `unhandled_fp_intervals_`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100140 ScopedArenaVector<LiveInterval*>* unhandled_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141
142 // List of intervals that have been processed.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100143 ScopedArenaVector<LiveInterval*> handled_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100144
145 // List of intervals that are currently active when processing a new live interval.
146 // That is, they have a live range that spans the start of the new interval.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100147 ScopedArenaVector<LiveInterval*> active_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148
149 // List of intervals that are currently inactive when processing a new live interval.
150 // That is, they have a lifetime hole that spans the start of the new interval.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100151 ScopedArenaVector<LiveInterval*> inactive_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100152
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 // Fixed intervals for physical registers. Such intervals cover the positions
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100154 // where an instruction requires a specific register.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100155 ScopedArenaVector<LiveInterval*> physical_core_register_intervals_;
156 ScopedArenaVector<LiveInterval*> physical_fp_register_intervals_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100157
Nicolas Geoffray39468442014-09-02 15:17:15 +0100158 // Intervals for temporaries. Such intervals cover the positions
159 // where an instruction requires a temporary.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100160 ScopedArenaVector<LiveInterval*> temp_intervals_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161
Nicolas Geoffray776b3182015-02-23 14:14:57 +0000162 // The spill slots allocated for live intervals. We ensure spill slots
163 // are typed to avoid (1) doing moves and swaps between two different kinds
164 // of registers, and (2) swapping between a single stack slot and a double
165 // stack slot. This simplifies the parallel move resolver.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100166 ScopedArenaVector<size_t> int_spill_slots_;
167 ScopedArenaVector<size_t> long_spill_slots_;
168 ScopedArenaVector<size_t> float_spill_slots_;
169 ScopedArenaVector<size_t> double_spill_slots_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100170
David Brazdil77a48ae2015-09-15 12:34:04 +0000171 // Spill slots allocated to catch phis. This category is special-cased because
172 // (1) slots are allocated prior to linear scan and in reverse linear order,
173 // (2) equivalent phis need to share slots despite having different types.
174 size_t catch_phi_spill_slots_;
175
Nicolas Geoffray39468442014-09-02 15:17:15 +0100176 // Instructions that need a safepoint.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100177 ScopedArenaVector<HInstruction*> safepoints_;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100178
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100179 // True if processing core registers. False if processing floating
180 // point registers.
181 bool processing_core_registers_;
182
183 // Number of registers for the current register kind (core or floating point).
184 size_t number_of_registers_;
185
186 // Temporary array, allocated ahead of time for simplicity.
187 size_t* registers_array_;
188
189 // Blocked registers, as decided by the code generator.
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100190 bool* const blocked_core_registers_;
191 bool* const blocked_fp_registers_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100192
Nicolas Geoffray39468442014-09-02 15:17:15 +0100193 // Slots reserved for out arguments.
194 size_t reserved_out_slots_;
195
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700196 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000197 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffrayaac0f392014-09-16 14:11:14 +0100198
Matthew Gharrity8f49d4b2016-07-14 13:24:00 -0700199 DISALLOW_COPY_AND_ASSIGN(RegisterAllocatorLinearScan);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100200};
201
202} // namespace art
203
Matthew Gharritye9288852016-07-14 14:08:16 -0700204#endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_LINEAR_SCAN_H_