blob: caf3174455731f05a31165652ec27b0097f26523 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 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_MIPS_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_
19
20#include "code_generator.h"
21#include "dex/compiler_enums.h"
22#include "driver/compiler_options.h"
23#include "nodes.h"
24#include "parallel_move_resolver.h"
25#include "utils/mips/assembler_mips.h"
26
27namespace art {
28namespace mips {
29
30// InvokeDexCallingConvention registers
31
32static constexpr Register kParameterCoreRegisters[] =
33 { A1, A2, A3 };
34static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
35
36static constexpr FRegister kParameterFpuRegisters[] =
37 { F12, F14 };
38static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
39
40
41// InvokeRuntimeCallingConvention registers
42
43static constexpr Register kRuntimeParameterCoreRegisters[] =
44 { A0, A1, A2, A3 };
45static constexpr size_t kRuntimeParameterCoreRegistersLength =
46 arraysize(kRuntimeParameterCoreRegisters);
47
48static constexpr FRegister kRuntimeParameterFpuRegisters[] =
49 { F12, F14};
50static constexpr size_t kRuntimeParameterFpuRegistersLength =
51 arraysize(kRuntimeParameterFpuRegisters);
52
53
54static constexpr Register kCoreCalleeSaves[] =
55 { S0, S1, S2, S3, S4, S5, S6, S7, FP, RA };
56static constexpr FRegister kFpuCalleeSaves[] =
57 { F20, F22, F24, F26, F28, F30 };
58
59
60class CodeGeneratorMIPS;
61
62class InvokeDexCallingConvention : public CallingConvention<Register, FRegister> {
63 public:
64 InvokeDexCallingConvention()
65 : CallingConvention(kParameterCoreRegisters,
66 kParameterCoreRegistersLength,
67 kParameterFpuRegisters,
68 kParameterFpuRegistersLength,
69 kMipsPointerSize) {}
70
71 private:
72 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
73};
74
75class InvokeDexCallingConventionVisitorMIPS : public InvokeDexCallingConventionVisitor {
76 public:
77 InvokeDexCallingConventionVisitorMIPS() {}
78 virtual ~InvokeDexCallingConventionVisitorMIPS() {}
79
80 Location GetNextLocation(Primitive::Type type) OVERRIDE;
81 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
82 Location GetMethodLocation() const OVERRIDE;
83
84 private:
85 InvokeDexCallingConvention calling_convention;
86
87 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorMIPS);
88};
89
90class InvokeRuntimeCallingConvention : public CallingConvention<Register, FRegister> {
91 public:
92 InvokeRuntimeCallingConvention()
93 : CallingConvention(kRuntimeParameterCoreRegisters,
94 kRuntimeParameterCoreRegistersLength,
95 kRuntimeParameterFpuRegisters,
96 kRuntimeParameterFpuRegistersLength,
97 kMipsPointerSize) {}
98
99 Location GetReturnLocation(Primitive::Type return_type);
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
103};
104
105class FieldAccessCallingConventionMIPS : public FieldAccessCallingConvention {
106 public:
107 FieldAccessCallingConventionMIPS() {}
108
109 Location GetObjectLocation() const OVERRIDE {
110 return Location::RegisterLocation(A1);
111 }
112 Location GetFieldIndexLocation() const OVERRIDE {
113 return Location::RegisterLocation(A0);
114 }
115 Location GetReturnLocation(Primitive::Type type) const OVERRIDE {
116 return Primitive::Is64BitType(type)
117 ? Location::RegisterPairLocation(V0, V1)
118 : Location::RegisterLocation(V0);
119 }
120 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
121 return Primitive::Is64BitType(type)
122 ? Location::RegisterPairLocation(A2, A3)
123 : (is_instance ? Location::RegisterLocation(A2) : Location::RegisterLocation(A1));
124 }
125 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
126 return Location::FpuRegisterLocation(F0);
127 }
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS);
131};
132
133class ParallelMoveResolverMIPS : public ParallelMoveResolverWithSwap {
134 public:
135 ParallelMoveResolverMIPS(ArenaAllocator* allocator, CodeGeneratorMIPS* codegen)
136 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
137
138 void EmitMove(size_t index) OVERRIDE;
139 void EmitSwap(size_t index) OVERRIDE;
140 void SpillScratch(int reg) OVERRIDE;
141 void RestoreScratch(int reg) OVERRIDE;
142
143 void Exchange(int index1, int index2, bool double_slot);
144
145 MipsAssembler* GetAssembler() const;
146
147 private:
148 CodeGeneratorMIPS* const codegen_;
149
150 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS);
151};
152
153class SlowPathCodeMIPS : public SlowPathCode {
154 public:
155 SlowPathCodeMIPS() : entry_label_(), exit_label_() {}
156
157 MipsLabel* GetEntryLabel() { return &entry_label_; }
158 MipsLabel* GetExitLabel() { return &exit_label_; }
159
160 private:
161 MipsLabel entry_label_;
162 MipsLabel exit_label_;
163
164 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS);
165};
166
167class LocationsBuilderMIPS : public HGraphVisitor {
168 public:
169 LocationsBuilderMIPS(HGraph* graph, CodeGeneratorMIPS* codegen)
170 : HGraphVisitor(graph), codegen_(codegen) {}
171
172#define DECLARE_VISIT_INSTRUCTION(name, super) \
173 void Visit##name(H##name* instr) OVERRIDE;
174
175 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
176 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
177
178#undef DECLARE_VISIT_INSTRUCTION
179
180 void VisitInstruction(HInstruction* instruction) OVERRIDE {
181 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
182 << " (id " << instruction->GetId() << ")";
183 }
184
185 private:
186 void HandleInvoke(HInvoke* invoke);
187 void HandleBinaryOp(HBinaryOperation* operation);
188 void HandleShift(HBinaryOperation* operation);
189 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
190 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
191
192 InvokeDexCallingConventionVisitorMIPS parameter_visitor_;
193
194 CodeGeneratorMIPS* const codegen_;
195
196 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS);
197};
198
199class InstructionCodeGeneratorMIPS : public HGraphVisitor {
200 public:
201 InstructionCodeGeneratorMIPS(HGraph* graph, CodeGeneratorMIPS* codegen);
202
203#define DECLARE_VISIT_INSTRUCTION(name, super) \
204 void Visit##name(H##name* instr) OVERRIDE;
205
206 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
207 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
208
209#undef DECLARE_VISIT_INSTRUCTION
210
211 void VisitInstruction(HInstruction* instruction) OVERRIDE {
212 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
213 << " (id " << instruction->GetId() << ")";
214 }
215
216 MipsAssembler* GetAssembler() const { return assembler_; }
217
218 private:
219 void GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path, Register class_reg);
220 void GenerateMemoryBarrier(MemBarrierKind kind);
221 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
222 void HandleBinaryOp(HBinaryOperation* operation);
223 void HandleShift(HBinaryOperation* operation);
224 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc);
225 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc);
226 void GenerateImplicitNullCheck(HNullCheck* instruction);
227 void GenerateExplicitNullCheck(HNullCheck* instruction);
228 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000229 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200230 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000231 MipsLabel* false_target);
Alexey Frunze7e99e052015-11-24 19:28:01 -0800232 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
233 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
234 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
235 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 void HandleGoto(HInstruction* got, HBasicBlock* successor);
237
238 MipsAssembler* const assembler_;
239 CodeGeneratorMIPS* const codegen_;
240
241 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS);
242};
243
244class CodeGeneratorMIPS : public CodeGenerator {
245 public:
246 CodeGeneratorMIPS(HGraph* graph,
247 const MipsInstructionSetFeatures& isa_features,
248 const CompilerOptions& compiler_options,
249 OptimizingCompilerStats* stats = nullptr);
250 virtual ~CodeGeneratorMIPS() {}
251
252 void GenerateFrameEntry() OVERRIDE;
253 void GenerateFrameExit() OVERRIDE;
254
255 void Bind(HBasicBlock* block) OVERRIDE;
256
257 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
258 void Move32(Location destination, Location source);
259 void Move64(Location destination, Location source);
260 void MoveConstant(Location location, HConstant* c);
261
262 size_t GetWordSize() const OVERRIDE { return kMipsWordSize; }
263
264 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return kMipsDoublewordSize; }
265
266 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
267 return assembler_.GetLabelLocation(GetLabelOf(block));
268 }
269
270 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
271 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
272 MipsAssembler* GetAssembler() OVERRIDE { return &assembler_; }
273 const MipsAssembler& GetAssembler() const OVERRIDE { return assembler_; }
274
275 void MarkGCCard(Register object, Register value);
276
277 // Register allocation.
278
279 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
280 // AllocateFreeRegister() is only used when allocating registers locally
281 // during CompileBaseline().
282 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
283
284 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
285
286 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
287 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
288 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
289 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
290
291 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
292 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
293
294 // Blocks all register pairs made out of blocked core registers.
295 void UpdateBlockedPairRegisters() const;
296
297 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips; }
298
299 const MipsInstructionSetFeatures& GetInstructionSetFeatures() const {
300 return isa_features_;
301 }
302
303 MipsLabel* GetLabelOf(HBasicBlock* block) const {
304 return CommonGetLabelOf<MipsLabel>(block_labels_, block);
305 }
306
307 void Initialize() OVERRIDE {
308 block_labels_ = CommonInitializeLabels<MipsLabel>();
309 }
310
311 void Finalize(CodeAllocator* allocator) OVERRIDE;
312
313 // Code generation helpers.
314
315 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
316
317 void MoveConstant(Location destination, int32_t value);
318
319 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
320
321 // Generate code to invoke a runtime entry point.
322 void InvokeRuntime(QuickEntrypointEnum entrypoint,
323 HInstruction* instruction,
324 uint32_t dex_pc,
325 SlowPathCode* slow_path) OVERRIDE;
326
327 void InvokeRuntime(int32_t offset,
328 HInstruction* instruction,
329 uint32_t dex_pc,
330 SlowPathCode* slow_path,
331 bool is_direct_entrypoint);
332
333 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
334
335 bool NeedsTwoRegisters(Primitive::Type type) const {
336 return type == Primitive::kPrimLong;
337 }
338
Vladimir Markodc151b22015-10-15 18:02:30 +0100339 // Check if the desired_dispatch_info is supported. If it is, return it,
340 // otherwise return a fall-back info that should be used instead.
341 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
342 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
343 MethodReference target_method) OVERRIDE;
344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp);
346 void GenerateVirtualCall(HInvokeVirtual* invoke ATTRIBUTE_UNUSED,
347 Location temp ATTRIBUTE_UNUSED) OVERRIDE {
348 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS";
349 }
350
351 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
352 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
353 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS";
354 }
355
356 private:
357 // Labels for each block that will be compiled.
358 MipsLabel* block_labels_;
359 MipsLabel frame_entry_label_;
360 LocationsBuilderMIPS location_builder_;
361 InstructionCodeGeneratorMIPS instruction_visitor_;
362 ParallelMoveResolverMIPS move_resolver_;
363 MipsAssembler assembler_;
364 const MipsInstructionSetFeatures& isa_features_;
365
366 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS);
367};
368
369} // namespace mips
370} // namespace art
371
372#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_