blob: 690eccb7d8b8cb8975ac970feb2f7766cc87332f [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"
Alexey Frunze4dda3372015-06-01 18:31:49 -070021#include "driver/compiler_options.h"
22#include "nodes.h"
23#include "parallel_move_resolver.h"
24#include "utils/mips64/assembler_mips64.h"
25
26namespace art {
27namespace mips64 {
28
Alexey Frunze4dda3372015-06-01 18:31:49 -070029// InvokeDexCallingConvention registers
30
31static constexpr GpuRegister kParameterCoreRegisters[] =
32 { A1, A2, A3, A4, A5, A6, A7 };
33static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
34
35static constexpr FpuRegister kParameterFpuRegisters[] =
36 { F13, F14, F15, F16, F17, F18, F19 };
37static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
38
39
40// InvokeRuntimeCallingConvention registers
41
42static constexpr GpuRegister kRuntimeParameterCoreRegisters[] =
43 { A0, A1, A2, A3, A4, A5, A6, A7 };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
46
47static constexpr FpuRegister kRuntimeParameterFpuRegisters[] =
48 { F12, F13, F14, F15, F16, F17, F18, F19 };
49static constexpr size_t kRuntimeParameterFpuRegistersLength =
50 arraysize(kRuntimeParameterFpuRegisters);
51
52
53static constexpr GpuRegister kCoreCalleeSaves[] =
54 { S0, S1, S2, S3, S4, S5, S6, S7, GP, S8, RA }; // TODO: review
55static constexpr FpuRegister kFpuCalleeSaves[] =
56 { F24, F25, F26, F27, F28, F29, F30, F31 };
57
58
59class CodeGeneratorMIPS64;
60
61class InvokeDexCallingConvention : public CallingConvention<GpuRegister, FpuRegister> {
62 public:
63 InvokeDexCallingConvention()
64 : CallingConvention(kParameterCoreRegisters,
65 kParameterCoreRegistersLength,
66 kParameterFpuRegisters,
67 kParameterFpuRegistersLength,
68 kMips64PointerSize) {}
69
70 private:
71 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
72};
73
74class InvokeDexCallingConventionVisitorMIPS64 : public InvokeDexCallingConventionVisitor {
75 public:
76 InvokeDexCallingConventionVisitorMIPS64() {}
77 virtual ~InvokeDexCallingConventionVisitorMIPS64() {}
78
79 Location GetNextLocation(Primitive::Type type) OVERRIDE;
80 Location GetReturnLocation(Primitive::Type type) const OVERRIDE;
81 Location GetMethodLocation() const OVERRIDE;
82
83 private:
84 InvokeDexCallingConvention calling_convention;
85
86 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorMIPS64);
87};
88
89class InvokeRuntimeCallingConvention : public CallingConvention<GpuRegister, FpuRegister> {
90 public:
91 InvokeRuntimeCallingConvention()
92 : CallingConvention(kRuntimeParameterCoreRegisters,
93 kRuntimeParameterCoreRegistersLength,
94 kRuntimeParameterFpuRegisters,
95 kRuntimeParameterFpuRegistersLength,
96 kMips64PointerSize) {}
97
98 Location GetReturnLocation(Primitive::Type return_type);
99
100 private:
101 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
102};
103
Calin Juravlee460d1d2015-09-29 04:52:17 +0100104class FieldAccessCallingConventionMIPS64 : public FieldAccessCallingConvention {
105 public:
106 FieldAccessCallingConventionMIPS64() {}
107
108 Location GetObjectLocation() const OVERRIDE {
109 return Location::RegisterLocation(A1);
110 }
111 Location GetFieldIndexLocation() const OVERRIDE {
112 return Location::RegisterLocation(A0);
113 }
114 Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Goran Jakovljevic8c34ec12015-10-14 11:23:48 +0200115 return Location::RegisterLocation(V0);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100116 }
Alexey Frunze00580bd2015-11-11 13:31:12 -0800117 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
118 return Primitive::Is64BitType(type)
119 ? Location::RegisterLocation(A2)
120 : (is_instance
121 ? Location::RegisterLocation(A2)
122 : Location::RegisterLocation(A1));
Calin Juravlee460d1d2015-09-29 04:52:17 +0100123 }
124 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
125 return Location::FpuRegisterLocation(F0);
126 }
127
128 private:
129 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS64);
130};
131
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132class ParallelMoveResolverMIPS64 : public ParallelMoveResolverWithSwap {
133 public:
134 ParallelMoveResolverMIPS64(ArenaAllocator* allocator, CodeGeneratorMIPS64* codegen)
135 : ParallelMoveResolverWithSwap(allocator), codegen_(codegen) {}
136
137 void EmitMove(size_t index) OVERRIDE;
138 void EmitSwap(size_t index) OVERRIDE;
139 void SpillScratch(int reg) OVERRIDE;
140 void RestoreScratch(int reg) OVERRIDE;
141
142 void Exchange(int index1, int index2, bool double_slot);
143
144 Mips64Assembler* GetAssembler() const;
145
146 private:
147 CodeGeneratorMIPS64* const codegen_;
148
149 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS64);
150};
151
152class SlowPathCodeMIPS64 : public SlowPathCode {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit SlowPathCodeMIPS64(HInstruction* instruction)
155 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700156
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700157 Mips64Label* GetEntryLabel() { return &entry_label_; }
158 Mips64Label* GetExitLabel() { return &exit_label_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159
160 private:
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700161 Mips64Label entry_label_;
162 Mips64Label exit_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700163
164 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS64);
165};
166
167class LocationsBuilderMIPS64 : public HGraphVisitor {
168 public:
169 LocationsBuilderMIPS64(HGraph* graph, CodeGeneratorMIPS64* codegen)
170 : HGraphVisitor(graph), codegen_(codegen) {}
171
172#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100173 void Visit##name(H##name* instr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700174
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100175 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
176 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(DECLARE_VISIT_INSTRUCTION)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700177
178#undef DECLARE_VISIT_INSTRUCTION
179
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100180 void VisitInstruction(HInstruction* instruction) OVERRIDE {
181 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
182 << " (id " << instruction->GetId() << ")";
183 }
184
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 private:
186 void HandleInvoke(HInvoke* invoke);
187 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000188 void HandleCondition(HCondition* instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700189 void HandleShift(HBinaryOperation* operation);
190 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
191 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
192
193 InvokeDexCallingConventionVisitorMIPS64 parameter_visitor_;
194
195 CodeGeneratorMIPS64* const codegen_;
196
197 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS64);
198};
199
Aart Bik42249c32016-01-07 15:33:50 -0800200class InstructionCodeGeneratorMIPS64 : public InstructionCodeGenerator {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700201 public:
202 InstructionCodeGeneratorMIPS64(HGraph* graph, CodeGeneratorMIPS64* codegen);
203
204#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100205 void Visit##name(H##name* instr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700206
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100207 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
208 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(DECLARE_VISIT_INSTRUCTION)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700209
210#undef DECLARE_VISIT_INSTRUCTION
211
Alexandre Ramesf39e0642015-06-23 11:33:45 +0100212 void VisitInstruction(HInstruction* instruction) OVERRIDE {
213 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
214 << " (id " << instruction->GetId() << ")";
215 }
216
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 Mips64Assembler* GetAssembler() const { return assembler_; }
218
219 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 void GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path, GpuRegister class_reg);
221 void GenerateMemoryBarrier(MemBarrierKind kind);
222 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
223 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000224 void HandleCondition(HCondition* instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700225 void HandleShift(HBinaryOperation* operation);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100226 void HandleFieldSet(HInstruction* instruction,
227 const FieldInfo& field_info,
228 bool value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700229 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700230 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000231 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700232 Mips64Label* true_target,
233 Mips64Label* false_target);
Alexey Frunzec857c742015-09-23 15:12:39 -0700234 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
235 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
236 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
237 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -0800238 void GenerateIntLongCompare(IfCondition cond, bool is64bit, LocationSummary* locations);
239 void GenerateIntLongCompareAndBranch(IfCondition cond,
240 bool is64bit,
241 LocationSummary* locations,
242 Mips64Label* label);
243 void GenerateFpCompareAndBranch(IfCondition cond,
244 bool gt_bias,
245 Primitive::Type type,
246 LocationSummary* locations,
247 Mips64Label* label);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000248 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700249
250 Mips64Assembler* const assembler_;
251 CodeGeneratorMIPS64* const codegen_;
252
253 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS64);
254};
255
256class CodeGeneratorMIPS64 : public CodeGenerator {
257 public:
258 CodeGeneratorMIPS64(HGraph* graph,
259 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100260 const CompilerOptions& compiler_options,
261 OptimizingCompilerStats* stats = nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700262 virtual ~CodeGeneratorMIPS64() {}
263
264 void GenerateFrameEntry() OVERRIDE;
265 void GenerateFrameExit() OVERRIDE;
266
267 void Bind(HBasicBlock* block) OVERRIDE;
268
Lazar Trsicd9672662015-09-03 17:33:01 +0200269 size_t GetWordSize() const OVERRIDE { return kMips64DoublewordSize; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700270
Lazar Trsicd9672662015-09-03 17:33:01 +0200271 size_t GetFloatingPointSpillSlotSize() const OVERRIDE { return kMips64DoublewordSize; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700272
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100273 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700274 return assembler_.GetLabelLocation(GetLabelOf(block));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275 }
276
277 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
278 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
279 Mips64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100280 const Mips64Assembler& GetAssembler() const OVERRIDE { return assembler_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700281
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100282 void MarkGCCard(GpuRegister object, GpuRegister value, bool value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700283
284 // Register allocation.
285
David Brazdil58282f42016-01-14 12:45:10 +0000286 void SetupBlockedRegisters() const OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700287
Roland Levillainf41f9562016-09-14 19:26:48 +0100288 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
289 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
290 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
291 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700292
293 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
294 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
295
296 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips64; }
297
298 const Mips64InstructionSetFeatures& GetInstructionSetFeatures() const {
299 return isa_features_;
300 }
301
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700302 Mips64Label* GetLabelOf(HBasicBlock* block) const {
303 return CommonGetLabelOf<Mips64Label>(block_labels_, block);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700304 }
305
306 void Initialize() OVERRIDE {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700307 block_labels_ = CommonInitializeLabels<Mips64Label>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 }
309
310 void Finalize(CodeAllocator* allocator) OVERRIDE;
311
312 // Code generation helpers.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100313 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314
Calin Juravle175dc732015-08-25 15:42:32 +0100315 void MoveConstant(Location destination, int32_t value) OVERRIDE;
316
Calin Juravlee460d1d2015-09-29 04:52:17 +0100317 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
318
319
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 void SwapLocations(Location loc1, Location loc2, Primitive::Type type);
321
322 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100323 void InvokeRuntime(QuickEntrypointEnum entrypoint,
324 HInstruction* instruction,
325 uint32_t dex_pc,
Serban Constantinescufc734082016-07-19 17:18:07 +0100326 SlowPathCode* slow_path = nullptr) OVERRIDE;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327
328 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
329
Roland Levillainf41f9562016-09-14 19:26:48 +0100330 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE { return false; }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700331
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000332 // Check if the desired_string_load_kind is supported. If it is, return it,
333 // otherwise return a fall-back kind that should be used instead.
334 HLoadString::LoadKind GetSupportedLoadStringKind(
335 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
336
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100337 // Check if the desired_class_load_kind is supported. If it is, return it,
338 // otherwise return a fall-back kind that should be used instead.
339 HLoadClass::LoadKind GetSupportedLoadClassKind(
340 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
341
Vladimir Markodc151b22015-10-15 18:02:30 +0100342 // Check if the desired_dispatch_info is supported. If it is, return it,
343 // otherwise return a fall-back info that should be used instead.
344 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
345 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100346 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100347
Andreas Gampe85b62f22015-09-09 13:15:38 -0700348 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
Alexey Frunze53afca12015-11-05 16:34:23 -0800349 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700350
351 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
352 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
Chris Larsen3acee732015-11-18 13:31:08 -0800353 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS64";
Andreas Gampe85b62f22015-09-09 13:15:38 -0700354 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700355
Roland Levillainf41f9562016-09-14 19:26:48 +0100356 void GenerateNop() OVERRIDE;
357 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
358 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
David Srbeckyc7098ff2016-02-09 14:30:11 +0000359
Alexey Frunze4dda3372015-06-01 18:31:49 -0700360 private:
361 // Labels for each block that will be compiled.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700362 Mips64Label* block_labels_; // Indexed by block id.
363 Mips64Label frame_entry_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 LocationsBuilderMIPS64 location_builder_;
365 InstructionCodeGeneratorMIPS64 instruction_visitor_;
366 ParallelMoveResolverMIPS64 move_resolver_;
367 Mips64Assembler assembler_;
368 const Mips64InstructionSetFeatures& isa_features_;
369
370 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS64);
371};
372
373} // namespace mips64
374} // namespace art
375
376#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS64_H_