blob: a078dd181954f7cf27c97b2a1f9367f05b9910c7 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
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_MIPS64_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS64_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/mips64/assembler_mips64.h"
26
27namespace art {
28namespace mips64 {
29
30// Use a local definition to prevent copying mistakes.
31static constexpr size_t kMips64WordSize = kMips64PointerSize;
32
33
34// InvokeDexCallingConvention registers
35
36static constexpr GpuRegister kParameterCoreRegisters[] =
37 { A1, A2, A3, A4, A5, A6, A7 };
38static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
39
40static constexpr FpuRegister kParameterFpuRegisters[] =
41 { F13, F14, F15, F16, F17, F18, F19 };
42static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
43
44
45// InvokeRuntimeCallingConvention registers
46
47static constexpr GpuRegister kRuntimeParameterCoreRegisters[] =
48 { A0, A1, A2, A3, A4, A5, A6, A7 };
49static constexpr size_t kRuntimeParameterCoreRegistersLength =
50 arraysize(kRuntimeParameterCoreRegisters);
51
52static constexpr FpuRegister kRuntimeParameterFpuRegisters[] =
53 { F12, F13, F14, F15, F16, F17, F18, F19 };
54static constexpr size_t kRuntimeParameterFpuRegistersLength =
55 arraysize(kRuntimeParameterFpuRegisters);
56
57
58static constexpr GpuRegister kCoreCalleeSaves[] =
59 { S0, S1, S2, S3, S4, S5, S6, S7, GP, S8, RA }; // TODO: review
60static constexpr FpuRegister kFpuCalleeSaves[] =
61 { F24, F25, F26, F27, F28, F29, F30, F31 };
62
63
64class CodeGeneratorMIPS64;
65
66class InvokeDexCallingConvention : public CallingConvention<GpuRegister, FpuRegister> {
67 public:
68 InvokeDexCallingConvention()
69 : CallingConvention(kParameterCoreRegisters,
70 kParameterCoreRegistersLength,
71 kParameterFpuRegisters,
72 kParameterFpuRegistersLength,
73 kMips64PointerSize) {}
74
75 private:
76 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
77};
78
79class InvokeDexCallingConventionVisitorMIPS64 : public InvokeDexCallingConventionVisitor {
80 public:
81 InvokeDexCallingConventionVisitorMIPS64() {}
82 virtual ~InvokeDexCallingConventionVisitorMIPS64() {}
83
84 Location GetNextLocation(Primitive::Type type) OVERRIDE;
85 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
86 Location GetMethodLocation() const OVERRIDE;
87
88 private:
89 InvokeDexCallingConvention calling_convention;
90
91 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorMIPS64);
92};
93
94class InvokeRuntimeCallingConvention : public CallingConvention<GpuRegister, FpuRegister> {
95 public:
96 InvokeRuntimeCallingConvention()
97 : CallingConvention(kRuntimeParameterCoreRegisters,
98 kRuntimeParameterCoreRegistersLength,
99 kRuntimeParameterFpuRegisters,
100 kRuntimeParameterFpuRegistersLength,
101 kMips64PointerSize) {}
102
103 Location GetReturnLocation(Primitive::Type return_type);
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
107};
108
Calin Juravlee460d1d2015-09-29 04:52:17 +0100109class FieldAccessCallingConventionMIPS64 : public FieldAccessCallingConvention {
110 public:
111 FieldAccessCallingConventionMIPS64() {}
112
113 Location GetObjectLocation() const OVERRIDE {
114 return Location::RegisterLocation(A1);
115 }
116 Location GetFieldIndexLocation() const OVERRIDE {
117 return Location::RegisterLocation(A0);
118 }
119 Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Goran Jakovljevic8c34ec12015-10-14 11:23:48 +0200120 return Location::RegisterLocation(V0);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100121 }
Alexey Frunze00580bd2015-11-11 13:31:12 -0800122 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
123 return Primitive::Is64BitType(type)
124 ? Location::RegisterLocation(A2)
125 : (is_instance
126 ? Location::RegisterLocation(A2)
127 : Location::RegisterLocation(A1));
Calin Juravlee460d1d2015-09-29 04:52:17 +0100128 }
129 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
130 return Location::FpuRegisterLocation(F0);
131 }
132
133 private:
134 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS64);
135};
136
Alexey Frunze4dda3372015-06-01 18:31:49 -0700137class ParallelMoveResolverMIPS64 : public ParallelMoveResolverWithSwap {
138 public:
139 ParallelMoveResolverMIPS64(ArenaAllocator* allocator, CodeGeneratorMIPS64* codegen)
140 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
141
142 void EmitMove(size_t index) OVERRIDE;
143 void EmitSwap(size_t index) OVERRIDE;
144 void SpillScratch(int reg) OVERRIDE;
145 void RestoreScratch(int reg) OVERRIDE;
146
147 void Exchange(int index1, int index2, bool double_slot);
148
149 Mips64Assembler* GetAssembler() const;
150
151 private:
152 CodeGeneratorMIPS64* const codegen_;
153
154 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS64);
155};
156
157class SlowPathCodeMIPS64 : public SlowPathCode {
158 public:
159 SlowPathCodeMIPS64() : entry_label_(), exit_label_() {}
160
161 Label* GetEntryLabel() { return &entry_label_; }
162 Label* GetExitLabel() { return &exit_label_; }
163
164 private:
165 Label entry_label_;
166 Label exit_label_;
167
168 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS64);
169};
170
171class LocationsBuilderMIPS64 : public HGraphVisitor {
172 public:
173 LocationsBuilderMIPS64(HGraph* graph, CodeGeneratorMIPS64* codegen)
174 : HGraphVisitor(graph), codegen_(codegen) {}
175
176#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100177 void Visit##name(H##name* instr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700178
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100179 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
180 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(DECLARE_VISIT_INSTRUCTION)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700181
182#undef DECLARE_VISIT_INSTRUCTION
183
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100184 void VisitInstruction(HInstruction* instruction) OVERRIDE {
185 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
186 << " (id " << instruction->GetId() << ")";
187 }
188
Alexey Frunze4dda3372015-06-01 18:31:49 -0700189 private:
190 void HandleInvoke(HInvoke* invoke);
191 void HandleBinaryOp(HBinaryOperation* operation);
192 void HandleShift(HBinaryOperation* operation);
193 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
194 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
195
196 InvokeDexCallingConventionVisitorMIPS64 parameter_visitor_;
197
198 CodeGeneratorMIPS64* const codegen_;
199
200 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS64);
201};
202
203class InstructionCodeGeneratorMIPS64 : public HGraphVisitor {
204 public:
205 InstructionCodeGeneratorMIPS64(HGraph* graph, CodeGeneratorMIPS64* codegen);
206
207#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100208 void Visit##name(H##name* instr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700209
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100210 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
211 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(DECLARE_VISIT_INSTRUCTION)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212
213#undef DECLARE_VISIT_INSTRUCTION
214
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100215 void VisitInstruction(HInstruction* instruction) OVERRIDE {
216 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
217 << " (id " << instruction->GetId() << ")";
218 }
219
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 Mips64Assembler* GetAssembler() const { return assembler_; }
221
222 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700223 void GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path, GpuRegister class_reg);
224 void GenerateMemoryBarrier(MemBarrierKind kind);
225 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
226 void HandleBinaryOp(HBinaryOperation* operation);
227 void HandleShift(HBinaryOperation* operation);
228 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
229 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
230 void GenerateImplicitNullCheck(HNullCheck* instruction);
231 void GenerateExplicitNullCheck(HNullCheck* instruction);
232 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000233 size_t condition_input_index,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700234 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000235 Label* false_target);
Alexey Frunzec857c742015-09-23 15:12:39 -0700236 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
237 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
238 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
239 void GenerateDivRemIntegral(HBinaryOperation* instruction);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000240 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700241
242 Mips64Assembler* const assembler_;
243 CodeGeneratorMIPS64* const codegen_;
244
245 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS64);
246};
247
248class CodeGeneratorMIPS64 : public CodeGenerator {
249 public:
250 CodeGeneratorMIPS64(HGraph* graph,
251 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100252 const CompilerOptions& compiler_options,
253 OptimizingCompilerStats* stats = nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700254 virtual ~CodeGeneratorMIPS64() {}
255
256 void GenerateFrameEntry() OVERRIDE;
257 void GenerateFrameExit() OVERRIDE;
258
259 void Bind(HBasicBlock* block) OVERRIDE;
260
261 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
262
263 size_t GetWordSize() const OVERRIDE { return kMips64WordSize; }
264
265 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return kMips64WordSize; }
266
267 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
268 return GetLabelOf(block)->Position();
269 }
270
271 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
272 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
273 Mips64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100274 const Mips64Assembler& GetAssembler() const OVERRIDE { return assembler_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275
276 void MarkGCCard(GpuRegister object, GpuRegister value);
277
278 // Register allocation.
279
280 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
281 // AllocateFreeRegister() is only used when allocating registers locally
282 // during CompileBaseline().
283 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
284
285 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
286
287 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
288 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
289 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
290 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
291
292 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
293 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
294
295 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips64; }
296
297 const Mips64InstructionSetFeatures& GetInstructionSetFeatures() const {
298 return isa_features_;
299 }
300
301 Label* GetLabelOf(HBasicBlock* block) const {
Vladimir Marko225b6462015-09-28 12:17:40 +0100302 return CommonGetLabelOf<Label>(block_labels_, block);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303 }
304
305 void Initialize() OVERRIDE {
Vladimir Marko225b6462015-09-28 12:17:40 +0100306 block_labels_ = CommonInitializeLabels<Label>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307 }
308
309 void Finalize(CodeAllocator* allocator) OVERRIDE;
310
311 // Code generation helpers.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100312 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313
Calin Juravle175dc732015-08-25 15:42:32 +0100314 void MoveConstant(Location destination, int32_t value) OVERRIDE;
315
Calin Juravlee460d1d2015-09-29 04:52:17 +0100316 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
317
318
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 void SwapLocations(Location loc1, Location loc2, Primitive::Type type);
320
321 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100322 void InvokeRuntime(QuickEntrypointEnum entrypoint,
323 HInstruction* instruction,
324 uint32_t dex_pc,
325 SlowPathCode* slow_path) OVERRIDE;
326
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 void InvokeRuntime(int32_t offset,
328 HInstruction* instruction,
329 uint32_t dex_pc,
330 SlowPathCode* slow_path);
331
332 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
333
334 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const { return false; }
335
Vladimir Markodc151b22015-10-15 18:02:30 +0100336 // Check if the desired_dispatch_info is supported. If it is, return it,
337 // otherwise return a fall-back info that should be used instead.
338 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
339 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
340 MethodReference target_method) OVERRIDE;
341
Andreas Gampe85b62f22015-09-09 13:15:38 -0700342 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
Alexey Frunze53afca12015-11-05 16:34:23 -0800343 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700344
345 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
346 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
347 UNIMPLEMENTED(FATAL);
348 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700349
350 private:
351 // Labels for each block that will be compiled.
Vladimir Marko225b6462015-09-28 12:17:40 +0100352 Label* block_labels_; // Indexed by block id.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353 Label frame_entry_label_;
354 LocationsBuilderMIPS64 location_builder_;
355 InstructionCodeGeneratorMIPS64 instruction_visitor_;
356 ParallelMoveResolverMIPS64 move_resolver_;
357 Mips64Assembler assembler_;
358 const Mips64InstructionSetFeatures& isa_features_;
359
360 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS64);
361};
362
363} // namespace mips64
364} // namespace art
365
366#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS64_H_