blob: be1c7ec7c6fc1ed9744a4f5f405f94f2730c5d87 [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 Geoffraya7062e02014-05-22 12:50:17 +010062 const CodeGenerator& codegen,
63 ArenaAllocator* allocator,
64 bool processing_core_registers,
65 bool log_fatal_on_failure);
66
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010067 static bool CanAllocateRegistersFor(const HGraph& graph, InstructionSet instruction_set);
68 static bool Supports(InstructionSet instruction_set) {
Nicolas Geoffray93bedb72014-07-18 10:23:59 +010069 return instruction_set == kX86
70 || instruction_set == kArm
71 || instruction_set == kX86_64
72 || instruction_set == kThumb2;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010073 }
74
75 size_t GetNumberOfSpillSlots() const {
76 return spill_slots_.Size();
77 }
78
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010079 private:
80 // Main methods of the allocator.
81 void LinearScan();
82 bool TryAllocateFreeReg(LiveInterval* interval);
83 bool AllocateBlockedReg(LiveInterval* interval);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010084 void Resolve();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010085
86 // Add `interval` in the sorted list of unhandled intervals.
87 void AddToUnhandled(LiveInterval* interval);
88
89 // Split `interval` at the position `at`. The new interval starts at `at`.
90 LiveInterval* Split(LiveInterval* interval, size_t at);
91
92 // Returns whether `reg` is blocked by the code generator.
93 bool IsBlocked(int reg) const;
94
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010095 // Update the interval for the register in `location` to cover [start, end).
96 void BlockRegister(Location location, size_t start, size_t end, Primitive::Type type);
97
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010098 // Allocate a spill slot for the given interval.
99 void AllocateSpillSlotFor(LiveInterval* interval);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100100 void AllocateOneSpillSlot(LiveInterval* interval, size_t end);
101 void AllocateTwoSpillSlots(LiveInterval* interval, size_t end);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100102
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.
110 void InsertParallelMoveAtExitOf(HBasicBlock* block, Location source, Location destination) const;
111 void InsertParallelMoveAtEntryOf(HBasicBlock* block, Location source, Location destination) const;
112 void InsertMoveAfter(HInstruction* instruction, Location source, Location destination) const;
113 void AddInputMoveFor(HInstruction* instruction, Location source, Location destination) const;
114 void InsertParallelMoveAt(size_t position, Location source, Location destination) const;
115
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100116 // Helper methods.
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100117 void AllocateRegistersInternal();
118 bool ValidateInternal(bool log_fatal_on_failure) const;
119 void DumpInterval(std::ostream& stream, LiveInterval* interval) const;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100120
121 ArenaAllocator* const allocator_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100122 CodeGenerator* const codegen_;
123 const SsaLivenessAnalysis& liveness_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124
125 // List of intervals that must be processed, ordered by start position. Last entry
126 // is the interval that has the lowest start position.
127 GrowableArray<LiveInterval*> unhandled_;
128
129 // List of intervals that have been processed.
130 GrowableArray<LiveInterval*> handled_;
131
132 // List of intervals that are currently active when processing a new live interval.
133 // That is, they have a live range that spans the start of the new interval.
134 GrowableArray<LiveInterval*> active_;
135
136 // List of intervals that are currently inactive when processing a new live interval.
137 // That is, they have a lifetime hole that spans the start of the new interval.
138 GrowableArray<LiveInterval*> inactive_;
139
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100140 // Fixed intervals for physical registers. Such an interval covers the positions
141 // where an instruction requires a specific register.
142 GrowableArray<LiveInterval*> physical_register_intervals_;
143
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100144 // The spill slots allocated for live intervals.
145 GrowableArray<size_t> spill_slots_;
146
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100147 // True if processing core registers. False if processing floating
148 // point registers.
149 bool processing_core_registers_;
150
151 // Number of registers for the current register kind (core or floating point).
152 size_t number_of_registers_;
153
154 // Temporary array, allocated ahead of time for simplicity.
155 size_t* registers_array_;
156
157 // Blocked registers, as decided by the code generator.
158 bool* const blocked_registers_;
159
160 DISALLOW_COPY_AND_ASSIGN(RegisterAllocator);
161};
162
163} // namespace art
164
165#endif // ART_COMPILER_OPTIMIZING_REGISTER_ALLOCATOR_H_