blob: 5c7cac1e5cf5ebb146e7cbd5803a6ca713e76845 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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_CODE_GENERATOR_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_
19
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010020#include "base/bit_field.h"
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000021#include "globals.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000022#include "instruction_set.h"
23#include "memory_region.h"
24#include "nodes.h"
25#include "utils/assembler.h"
26
27namespace art {
28
Nicolas Geoffraya747a392014-04-17 14:56:23 +010029static size_t constexpr kVRegSize = 4;
30
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000031class DexCompilationUnit;
32
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033class CodeAllocator {
34 public:
35 CodeAllocator() { }
36 virtual ~CodeAllocator() { }
37
38 virtual uint8_t* Allocate(size_t size) = 0;
39
40 private:
41 DISALLOW_COPY_AND_ASSIGN(CodeAllocator);
42};
43
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000044struct PcInfo {
45 uint32_t dex_pc;
46 uintptr_t native_pc;
47};
48
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000049/**
50 * A Location is an abstraction over the potential location
51 * of an instruction. It could be in register or stack.
52 */
53class Location : public ValueObject {
54 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010055 enum Kind {
56 kInvalid = 0,
57 kStackSlot = 1, // Word size slot.
58 kDoubleStackSlot = 2, // 64bit stack slot.
59 kRegister = 3,
60 // On 32bits architectures, quick can pass a long where the
61 // low bits are in the last parameter register, and the high
62 // bits are in a stack slot. The kQuickParameter kind is for
63 // handling this special case.
64 kQuickParameter = 4,
65 };
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000066
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010067 Location() : value_(kInvalid) {
68 DCHECK(!IsValid());
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000069 }
70
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010071 Location(const Location& other) : ValueObject(), value_(other.value_) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000072
73 Location& operator=(const Location& other) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010074 value_ = other.value_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000075 return *this;
76 }
77
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010078 bool IsValid() const {
79 return value_ != kInvalid;
80 }
81
82 // Register locations.
83 static Location RegisterLocation(ManagedRegister reg) {
84 return Location(kRegister, reg.RegId());
85 }
86
87 bool IsRegister() const {
88 return GetKind() == kRegister;
89 }
90
91 ManagedRegister reg() const {
92 DCHECK(IsRegister());
93 return static_cast<ManagedRegister>(GetPayload());
94 }
95
96 static uword EncodeStackIndex(intptr_t stack_index) {
97 DCHECK(-kStackIndexBias <= stack_index);
98 DCHECK(stack_index < kStackIndexBias);
99 return static_cast<uword>(kStackIndexBias + stack_index);
100 }
101
102 static Location StackSlot(intptr_t stack_index) {
103 uword payload = EncodeStackIndex(stack_index);
104 Location loc(kStackSlot, payload);
105 // Ensure that sign is preserved.
106 DCHECK_EQ(loc.GetStackIndex(), stack_index);
107 return loc;
108 }
109
110 bool IsStackSlot() const {
111 return GetKind() == kStackSlot;
112 }
113
114 static Location DoubleStackSlot(intptr_t stack_index) {
115 uword payload = EncodeStackIndex(stack_index);
116 Location loc(kDoubleStackSlot, payload);
117 // Ensure that sign is preserved.
118 DCHECK_EQ(loc.GetStackIndex(), stack_index);
119 return loc;
120 }
121
122 bool IsDoubleStackSlot() const {
123 return GetKind() == kDoubleStackSlot;
124 }
125
126 intptr_t GetStackIndex() const {
127 DCHECK(IsStackSlot() || IsDoubleStackSlot());
128 // Decode stack index manually to preserve sign.
129 return GetPayload() - kStackIndexBias;
130 }
131
132 intptr_t GetHighStackIndex(uintptr_t word_size) const {
133 DCHECK(IsDoubleStackSlot());
134 // Decode stack index manually to preserve sign.
135 return GetPayload() - kStackIndexBias + word_size;
136 }
137
138 static Location QuickParameter(uint32_t parameter_index) {
139 return Location(kQuickParameter, parameter_index);
140 }
141
142 uint32_t GetQuickParameterIndex() const {
143 DCHECK(IsQuickParameter());
144 return GetPayload();
145 }
146
147 bool IsQuickParameter() const {
148 return GetKind() == kQuickParameter;
149 }
150
151 arm::ArmManagedRegister AsArm() const;
152 x86::X86ManagedRegister AsX86() const;
153
154 Kind GetKind() const {
155 return KindField::Decode(value_);
156 }
157
158 bool Equals(Location other) const {
159 return value_ == other.value_;
160 }
161
162 const char* DebugString() const {
163 switch (GetKind()) {
164 case kInvalid: return "?";
165 case kRegister: return "R";
166 case kStackSlot: return "S";
167 case kDoubleStackSlot: return "DS";
168 case kQuickParameter: return "Q";
169 }
170 return "?";
171 }
172
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000173 private:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100174 // Number of bits required to encode Kind value.
175 static constexpr uint32_t kBitsForKind = 4;
176 static constexpr uint32_t kBitsForPayload = kWordSize * kBitsPerByte - kBitsForKind;
177
178 explicit Location(uword value) : value_(value) {}
179
180 Location(Kind kind, uword payload)
181 : value_(KindField::Encode(kind) | PayloadField::Encode(payload)) {}
182
183 uword GetPayload() const {
184 return PayloadField::Decode(value_);
185 }
186
187 typedef BitField<Kind, 0, kBitsForKind> KindField;
188 typedef BitField<uword, kBitsForKind, kBitsForPayload> PayloadField;
189
190 // Layout for stack slots.
191 static const intptr_t kStackIndexBias =
192 static_cast<intptr_t>(1) << (kBitsForPayload - 1);
193
194 // Location either contains kind and payload fields or a tagged handle for
195 // a constant locations. Values of enumeration Kind are selected in such a
196 // way that none of them can be interpreted as a kConstant tag.
197 uword value_;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000198};
199
200/**
201 * The code generator computes LocationSummary for each instruction so that
202 * the instruction itself knows what code to generate: where to find the inputs
203 * and where to place the result.
204 *
205 * The intent is to have the code for generating the instruction independent of
206 * register allocation. A register allocator just has to provide a LocationSummary.
207 */
208class LocationSummary : public ArenaObject {
209 public:
210 explicit LocationSummary(HInstruction* instruction)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000211 : inputs(instruction->GetBlock()->GetGraph()->GetArena(), instruction->InputCount()),
212 temps(instruction->GetBlock()->GetGraph()->GetArena(), 0) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000213 inputs.SetSize(instruction->InputCount());
214 for (int i = 0; i < instruction->InputCount(); i++) {
215 inputs.Put(i, Location());
216 }
217 }
218
219 void SetInAt(uint32_t at, Location location) {
220 inputs.Put(at, location);
221 }
222
223 Location InAt(uint32_t at) const {
224 return inputs.Get(at);
225 }
226
227 void SetOut(Location location) {
228 output = Location(location);
229 }
230
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000231 void AddTemp(Location location) {
232 temps.Add(location);
233 }
234
235 Location GetTemp(uint32_t at) const {
236 return temps.Get(at);
237 }
238
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000239 Location Out() const { return output; }
240
241 private:
242 GrowableArray<Location> inputs;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000243 GrowableArray<Location> temps;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000244 Location output;
245
246 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
247};
248
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000249class CodeGenerator : public ArenaObject {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000250 public:
251 // Compiles the graph to executable instructions. Returns whether the compilation
252 // succeeded.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000253 void Compile(CodeAllocator* allocator);
254 static CodeGenerator* Create(ArenaAllocator* allocator,
255 HGraph* graph,
256 InstructionSet instruction_set);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000257
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000258 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000259
260 Label* GetLabelOf(HBasicBlock* block) const;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000261 bool GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000262
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000263 virtual void GenerateFrameEntry() = 0;
264 virtual void GenerateFrameExit() = 0;
265 virtual void Bind(Label* label) = 0;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100266 virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) = 0;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000267 virtual HGraphVisitor* GetLocationBuilder() = 0;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000268 virtual HGraphVisitor* GetInstructionVisitor() = 0;
269 virtual Assembler* GetAssembler() = 0;
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100270 virtual size_t GetWordSize() const = 0;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000271
272 uint32_t GetFrameSize() const { return frame_size_; }
273 void SetFrameSize(uint32_t size) { frame_size_ = size; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000274 uint32_t GetCoreSpillMask() const { return core_spill_mask_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000275
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000276 void RecordPcInfo(uint32_t dex_pc) {
277 struct PcInfo pc_info;
278 pc_info.dex_pc = dex_pc;
279 pc_info.native_pc = GetAssembler()->CodeSize();
280 pc_infos_.Add(pc_info);
281 }
282
283 void BuildMappingTable(std::vector<uint8_t>* vector) const;
284 void BuildVMapTable(std::vector<uint8_t>* vector) const;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000285 void BuildNativeGCMap(
286 std::vector<uint8_t>* vector, const DexCompilationUnit& dex_compilation_unit) const;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287
288 protected:
289 explicit CodeGenerator(HGraph* graph)
290 : frame_size_(0),
291 graph_(graph),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000292 block_labels_(graph->GetArena(), 0),
293 pc_infos_(graph->GetArena(), 32) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000294 block_labels_.SetSize(graph->GetBlocks()->Size());
295 }
296 ~CodeGenerator() { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000297
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000298 // Frame size required for this method.
299 uint32_t frame_size_;
300 uint32_t core_spill_mask_;
301
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000302 private:
303 void InitLocations(HInstruction* instruction);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000304 void CompileBlock(HBasicBlock* block);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000305
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000306 HGraph* const graph_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000307
308 // Labels for each block that will be compiled.
309 GrowableArray<Label> block_labels_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000310 GrowableArray<PcInfo> pc_infos_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000311
312 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
313};
314
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100315template <typename T>
316class CallingConvention {
317 public:
318 CallingConvention(const T* registers, int number_of_registers)
319 : registers_(registers), number_of_registers_(number_of_registers) {}
320
321 size_t GetNumberOfRegisters() const { return number_of_registers_; }
322
323 T GetRegisterAt(size_t index) const {
324 DCHECK_LT(index, number_of_registers_);
325 return registers_[index];
326 }
327
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100328 uint8_t GetStackOffsetOf(size_t index, size_t word_size) const {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100329 // We still reserve the space for parameters passed by registers.
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100330 // Add word_size for the method pointer.
331 return index * kVRegSize + word_size;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100332 }
333
334 private:
335 const T* registers_;
336 const size_t number_of_registers_;
337
338 DISALLOW_COPY_AND_ASSIGN(CallingConvention);
339};
340
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000341} // namespace art
342
343#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_