blob: 1afa1b9d439b4f4b933bb05cdae429103705a158 [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"
Andreas Gampe8a0128a2016-11-28 07:38:35 -080021#include "dex_file_types.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020022#include "driver/compiler_options.h"
23#include "nodes.h"
24#include "parallel_move_resolver.h"
Alexey Frunze06a46c42016-07-19 15:00:40 -070025#include "string_reference.h"
Mathieu Chartierdbddc222017-05-24 12:04:13 -070026#include "type_reference.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020027#include "utils/mips/assembler_mips.h"
28
29namespace art {
30namespace mips {
31
32// InvokeDexCallingConvention registers
33
34static constexpr Register kParameterCoreRegisters[] =
Alexey Frunze1b8464d2016-11-12 17:22:05 -080035 { A1, A2, A3, T0, T1 };
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020036static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
37
38static constexpr FRegister kParameterFpuRegisters[] =
Alexey Frunze1b8464d2016-11-12 17:22:05 -080039 { F8, F10, F12, F14, F16, F18 };
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020040static constexpr size_t kParameterFpuRegistersLength = arraysize(kParameterFpuRegisters);
41
42
43// InvokeRuntimeCallingConvention registers
44
45static constexpr Register kRuntimeParameterCoreRegisters[] =
46 { A0, A1, A2, A3 };
47static constexpr size_t kRuntimeParameterCoreRegistersLength =
48 arraysize(kRuntimeParameterCoreRegisters);
49
50static constexpr FRegister kRuntimeParameterFpuRegisters[] =
Alexey Frunze1b8464d2016-11-12 17:22:05 -080051 { F12, F14 };
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020052static constexpr size_t kRuntimeParameterFpuRegistersLength =
53 arraysize(kRuntimeParameterFpuRegisters);
54
55
56static constexpr Register kCoreCalleeSaves[] =
57 { S0, S1, S2, S3, S4, S5, S6, S7, FP, RA };
58static constexpr FRegister kFpuCalleeSaves[] =
59 { F20, F22, F24, F26, F28, F30 };
60
61
62class CodeGeneratorMIPS;
63
Lena Djokicca8c2952017-05-29 11:31:46 +020064VectorRegister VectorRegisterFrom(Location location);
65
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020066class InvokeDexCallingConvention : public CallingConvention<Register, FRegister> {
67 public:
68 InvokeDexCallingConvention()
69 : CallingConvention(kParameterCoreRegisters,
70 kParameterCoreRegistersLength,
71 kParameterFpuRegisters,
72 kParameterFpuRegistersLength,
73 kMipsPointerSize) {}
74
75 private:
76 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
77};
78
79class InvokeDexCallingConventionVisitorMIPS : public InvokeDexCallingConventionVisitor {
80 public:
81 InvokeDexCallingConventionVisitorMIPS() {}
82 virtual ~InvokeDexCallingConventionVisitorMIPS() {}
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(InvokeDexCallingConventionVisitorMIPS);
92};
93
94class InvokeRuntimeCallingConvention : public CallingConvention<Register, FRegister> {
95 public:
96 InvokeRuntimeCallingConvention()
97 : CallingConvention(kRuntimeParameterCoreRegisters,
98 kRuntimeParameterCoreRegistersLength,
99 kRuntimeParameterFpuRegisters,
100 kRuntimeParameterFpuRegistersLength,
101 kMipsPointerSize) {}
102
103 Location GetReturnLocation(Primitive::Type return_type);
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
107};
108
109class FieldAccessCallingConventionMIPS : public FieldAccessCallingConvention {
110 public:
111 FieldAccessCallingConventionMIPS() {}
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) const OVERRIDE {
120 return Primitive::Is64BitType(type)
121 ? Location::RegisterPairLocation(V0, V1)
122 : Location::RegisterLocation(V0);
123 }
124 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
125 return Primitive::Is64BitType(type)
126 ? Location::RegisterPairLocation(A2, A3)
127 : (is_instance ? Location::RegisterLocation(A2) : Location::RegisterLocation(A1));
128 }
129 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
130 return Location::FpuRegisterLocation(F0);
131 }
132
133 private:
134 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionMIPS);
135};
136
137class ParallelMoveResolverMIPS : public ParallelMoveResolverWithSwap {
138 public:
139 ParallelMoveResolverMIPS(ArenaAllocator* allocator, CodeGeneratorMIPS* 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 MipsAssembler* GetAssembler() const;
150
151 private:
152 CodeGeneratorMIPS* const codegen_;
153
154 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS);
155};
156
157class SlowPathCodeMIPS : public SlowPathCode {
158 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000159 explicit SlowPathCodeMIPS(HInstruction* instruction)
160 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162 MipsLabel* GetEntryLabel() { return &entry_label_; }
163 MipsLabel* GetExitLabel() { return &exit_label_; }
164
165 private:
166 MipsLabel entry_label_;
167 MipsLabel exit_label_;
168
169 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS);
170};
171
172class LocationsBuilderMIPS : public HGraphVisitor {
173 public:
174 LocationsBuilderMIPS(HGraph* graph, CodeGeneratorMIPS* codegen)
175 : HGraphVisitor(graph), codegen_(codegen) {}
176
177#define DECLARE_VISIT_INSTRUCTION(name, super) \
178 void Visit##name(H##name* instr) OVERRIDE;
179
180 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
181 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
182
183#undef DECLARE_VISIT_INSTRUCTION
184
185 void VisitInstruction(HInstruction* instruction) OVERRIDE {
186 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
187 << " (id " << instruction->GetId() << ")";
188 }
189
190 private:
191 void HandleInvoke(HInvoke* invoke);
192 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000193 void HandleCondition(HCondition* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200194 void HandleShift(HBinaryOperation* operation);
195 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
196 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700197 Location RegisterOrZeroConstant(HInstruction* instruction);
198 Location FpuRegisterOrConstantForStore(HInstruction* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200199
200 InvokeDexCallingConventionVisitorMIPS parameter_visitor_;
201
202 CodeGeneratorMIPS* const codegen_;
203
204 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS);
205};
206
Aart Bik42249c32016-01-07 15:33:50 -0800207class InstructionCodeGeneratorMIPS : public InstructionCodeGenerator {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200208 public:
209 InstructionCodeGeneratorMIPS(HGraph* graph, CodeGeneratorMIPS* codegen);
210
211#define DECLARE_VISIT_INSTRUCTION(name, super) \
212 void Visit##name(H##name* instr) OVERRIDE;
213
214 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
215 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
216
217#undef DECLARE_VISIT_INSTRUCTION
218
219 void VisitInstruction(HInstruction* instruction) OVERRIDE {
220 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
221 << " (id " << instruction->GetId() << ")";
222 }
223
224 MipsAssembler* GetAssembler() const { return assembler_; }
225
Alexey Frunze96b66822016-09-10 02:32:44 -0700226 // Compare-and-jump packed switch generates approx. 3 + 2.5 * N 32-bit
227 // instructions for N cases.
228 // Table-based packed switch generates approx. 11 32-bit instructions
229 // and N 32-bit data words for N cases.
230 // At N = 6 they come out as 18 and 17 32-bit words respectively.
231 // We switch to the table-based method starting with 7 cases.
232 static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
233
Chris Larsen5633ce72017-04-10 15:47:40 -0700234 void GenerateMemoryBarrier(MemBarrierKind kind);
235
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200236 private:
237 void GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path, Register class_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
239 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000240 void HandleCondition(HCondition* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 void HandleShift(HBinaryOperation* operation);
Goran Jakovljevice114da22016-12-26 14:21:43 +0100242 void HandleFieldSet(HInstruction* instruction,
243 const FieldInfo& field_info,
244 uint32_t dex_pc,
245 bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200246 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc);
Alexey Frunze15958152017-02-09 19:08:30 -0800247
248 // Generate a heap reference load using one register `out`:
249 //
250 // out <- *(out + offset)
251 //
252 // while honoring heap poisoning and/or read barriers (if any).
253 //
254 // Location `maybe_temp` is used when generating a read barrier and
255 // shall be a register in that case; it may be an invalid location
256 // otherwise.
257 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
258 Location out,
259 uint32_t offset,
260 Location maybe_temp,
261 ReadBarrierOption read_barrier_option);
262 // Generate a heap reference load using two different registers
263 // `out` and `obj`:
264 //
265 // out <- *(obj + offset)
266 //
267 // while honoring heap poisoning and/or read barriers (if any).
268 //
269 // Location `maybe_temp` is used when generating a Baker's (fast
270 // path) read barrier and shall be a register in that case; it may
271 // be an invalid location otherwise.
272 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
273 Location out,
274 Location obj,
275 uint32_t offset,
276 Location maybe_temp,
277 ReadBarrierOption read_barrier_option);
278
Alexey Frunze06a46c42016-07-19 15:00:40 -0700279 // Generate a GC root reference load:
280 //
281 // root <- *(obj + offset)
282 //
283 // while honoring read barriers (if any).
284 void GenerateGcRootFieldLoad(HInstruction* instruction,
285 Location root,
286 Register obj,
Alexey Frunze15958152017-02-09 19:08:30 -0800287 uint32_t offset,
288 ReadBarrierOption read_barrier_option);
289
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800290 void GenerateIntCompare(IfCondition cond, LocationSummary* locations);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700291 // When the function returns `false` it means that the condition holds if `dst` is non-zero
292 // and doesn't hold if `dst` is zero. If it returns `true`, the roles of zero and non-zero
293 // `dst` are exchanged.
294 bool MaterializeIntCompare(IfCondition cond,
295 LocationSummary* input_locations,
296 Register dst);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800297 void GenerateIntCompareAndBranch(IfCondition cond,
298 LocationSummary* locations,
299 MipsLabel* label);
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +0100300 void GenerateLongCompare(IfCondition cond, LocationSummary* locations);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800301 void GenerateLongCompareAndBranch(IfCondition cond,
302 LocationSummary* locations,
303 MipsLabel* label);
Alexey Frunze2ddb7172016-09-06 17:04:55 -0700304 void GenerateFpCompare(IfCondition cond,
305 bool gt_bias,
306 Primitive::Type type,
307 LocationSummary* locations);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700308 // When the function returns `false` it means that the condition holds if the condition
309 // code flag `cc` is non-zero and doesn't hold if `cc` is zero. If it returns `true`,
310 // the roles of zero and non-zero values of the `cc` flag are exchanged.
311 bool MaterializeFpCompareR2(IfCondition cond,
312 bool gt_bias,
313 Primitive::Type type,
314 LocationSummary* input_locations,
315 int cc);
316 // When the function returns `false` it means that the condition holds if `dst` is non-zero
317 // and doesn't hold if `dst` is zero. If it returns `true`, the roles of zero and non-zero
318 // `dst` are exchanged.
319 bool MaterializeFpCompareR6(IfCondition cond,
320 bool gt_bias,
321 Primitive::Type type,
322 LocationSummary* input_locations,
323 FRegister dst);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800324 void GenerateFpCompareAndBranch(IfCondition cond,
325 bool gt_bias,
326 Primitive::Type type,
327 LocationSummary* locations,
328 MipsLabel* label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000330 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200331 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000332 MipsLabel* false_target);
Alexey Frunze7e99e052015-11-24 19:28:01 -0800333 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
334 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
335 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
336 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexey Frunze96b66822016-09-10 02:32:44 -0700338 void GenPackedSwitchWithCompares(Register value_reg,
339 int32_t lower_bound,
340 uint32_t num_entries,
341 HBasicBlock* switch_block,
342 HBasicBlock* default_block);
343 void GenTableBasedPackedSwitch(Register value_reg,
344 Register constant_area,
345 int32_t lower_bound,
346 uint32_t num_entries,
347 HBasicBlock* switch_block,
348 HBasicBlock* default_block);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700349 void GenConditionalMoveR2(HSelect* select);
350 void GenConditionalMoveR6(HSelect* select);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351
352 MipsAssembler* const assembler_;
353 CodeGeneratorMIPS* const codegen_;
354
355 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS);
356};
357
358class CodeGeneratorMIPS : public CodeGenerator {
359 public:
360 CodeGeneratorMIPS(HGraph* graph,
361 const MipsInstructionSetFeatures& isa_features,
362 const CompilerOptions& compiler_options,
363 OptimizingCompilerStats* stats = nullptr);
364 virtual ~CodeGeneratorMIPS() {}
365
Alexey Frunze73296a72016-06-03 22:51:46 -0700366 void ComputeSpillMask() OVERRIDE;
Alexey Frunze58320ce2016-08-30 21:40:46 -0700367 bool HasAllocatedCalleeSaveRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368 void GenerateFrameEntry() OVERRIDE;
369 void GenerateFrameExit() OVERRIDE;
370
371 void Bind(HBasicBlock* block) OVERRIDE;
372
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200373 void MoveConstant(Location location, HConstant* c);
374
375 size_t GetWordSize() const OVERRIDE { return kMipsWordSize; }
376
Lena Djokicca8c2952017-05-29 11:31:46 +0200377 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
378 return GetGraph()->HasSIMD()
379 ? 2 * kMipsDoublewordSize // 16 bytes for each spill.
380 : 1 * kMipsDoublewordSize; // 8 bytes for each spill.
381 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100383 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200384 return assembler_.GetLabelLocation(GetLabelOf(block));
385 }
386
387 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
388 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
389 MipsAssembler* GetAssembler() OVERRIDE { return &assembler_; }
390 const MipsAssembler& GetAssembler() const OVERRIDE { return assembler_; }
391
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700392 // Emit linker patches.
393 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
Alexey Frunze627c1a02017-01-30 19:28:14 -0800394 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700395
Alexey Frunze15958152017-02-09 19:08:30 -0800396 // Fast path implementation of ReadBarrier::Barrier for a heap
397 // reference field load when Baker's read barriers are used.
398 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
399 Location ref,
400 Register obj,
401 uint32_t offset,
402 Location temp,
403 bool needs_null_check);
404 // Fast path implementation of ReadBarrier::Barrier for a heap
405 // reference array load when Baker's read barriers are used.
406 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
407 Location ref,
408 Register obj,
409 uint32_t data_offset,
410 Location index,
411 Location temp,
412 bool needs_null_check);
413
414 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
415 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
416 //
417 // Load the object reference located at the address
418 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
419 // `ref`, and mark it if needed.
420 //
421 // If `always_update_field` is true, the value of the reference is
422 // atomically updated in the holder (`obj`).
423 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
424 Location ref,
425 Register obj,
426 uint32_t offset,
427 Location index,
428 ScaleFactor scale_factor,
429 Location temp,
430 bool needs_null_check,
431 bool always_update_field = false);
432
433 // Generate a read barrier for a heap reference within `instruction`
434 // using a slow path.
435 //
436 // A read barrier for an object reference read from the heap is
437 // implemented as a call to the artReadBarrierSlow runtime entry
438 // point, which is passed the values in locations `ref`, `obj`, and
439 // `offset`:
440 //
441 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
442 // mirror::Object* obj,
443 // uint32_t offset);
444 //
445 // The `out` location contains the value returned by
446 // artReadBarrierSlow.
447 //
448 // When `index` is provided (i.e. for array accesses), the offset
449 // value passed to artReadBarrierSlow is adjusted to take `index`
450 // into account.
451 void GenerateReadBarrierSlow(HInstruction* instruction,
452 Location out,
453 Location ref,
454 Location obj,
455 uint32_t offset,
456 Location index = Location::NoLocation());
457
458 // If read barriers are enabled, generate a read barrier for a heap
459 // reference using a slow path. If heap poisoning is enabled, also
460 // unpoison the reference in `out`.
461 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
462 Location out,
463 Location ref,
464 Location obj,
465 uint32_t offset,
466 Location index = Location::NoLocation());
467
468 // Generate a read barrier for a GC root within `instruction` using
469 // a slow path.
470 //
471 // A read barrier for an object reference GC root is implemented as
472 // a call to the artReadBarrierForRootSlow runtime entry point,
473 // which is passed the value in location `root`:
474 //
475 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
476 //
477 // The `out` location contains the value returned by
478 // artReadBarrierForRootSlow.
479 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
480
Goran Jakovljevice114da22016-12-26 14:21:43 +0100481 void MarkGCCard(Register object, Register value, bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482
483 // Register allocation.
484
David Brazdil58282f42016-01-14 12:45:10 +0000485 void SetupBlockedRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200486
Roland Levillainf41f9562016-09-14 19:26:48 +0100487 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
488 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
489 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
490 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700491 void ClobberRA() {
492 clobbered_ra_ = true;
493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494
495 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
496 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
497
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips; }
499
500 const MipsInstructionSetFeatures& GetInstructionSetFeatures() const {
501 return isa_features_;
502 }
503
504 MipsLabel* GetLabelOf(HBasicBlock* block) const {
505 return CommonGetLabelOf<MipsLabel>(block_labels_, block);
506 }
507
508 void Initialize() OVERRIDE {
509 block_labels_ = CommonInitializeLabels<MipsLabel>();
510 }
511
512 void Finalize(CodeAllocator* allocator) OVERRIDE;
513
514 // Code generation helpers.
515
516 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
517
Roland Levillainf41f9562016-09-14 19:26:48 +0100518 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200519
520 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
521
522 // Generate code to invoke a runtime entry point.
523 void InvokeRuntime(QuickEntrypointEnum entrypoint,
524 HInstruction* instruction,
525 uint32_t dex_pc,
Serban Constantinescufca16662016-07-14 09:21:59 +0100526 SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200527
Alexey Frunze15958152017-02-09 19:08:30 -0800528 // Generate code to invoke a runtime entry point, but do not record
529 // PC-related information in a stack map.
530 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
531 HInstruction* instruction,
532 SlowPathCode* slow_path,
533 bool direct);
534
535 void GenerateInvokeRuntime(int32_t entry_point_offset, bool direct);
536
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200537 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
538
Roland Levillainf41f9562016-09-14 19:26:48 +0100539 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200540 return type == Primitive::kPrimLong;
541 }
542
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000543 // Check if the desired_string_load_kind is supported. If it is, return it,
544 // otherwise return a fall-back kind that should be used instead.
545 HLoadString::LoadKind GetSupportedLoadStringKind(
546 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
547
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100548 // Check if the desired_class_load_kind is supported. If it is, return it,
549 // otherwise return a fall-back kind that should be used instead.
550 HLoadClass::LoadKind GetSupportedLoadClassKind(
551 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
552
Vladimir Markodc151b22015-10-15 18:02:30 +0100553 // Check if the desired_dispatch_info is supported. If it is, return it,
554 // otherwise return a fall-back info that should be used instead.
555 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
556 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100557 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100558
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100559 void GenerateStaticOrDirectCall(
560 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
561 void GenerateVirtualCall(
562 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200563
564 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
565 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
566 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS";
567 }
568
Roland Levillainf41f9562016-09-14 19:26:48 +0100569 void GenerateNop() OVERRIDE;
570 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
571 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
David Srbeckyc7098ff2016-02-09 14:30:11 +0000572
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700573 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
574 // and boot image strings. The only difference is the interpretation of the offset_or_index.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700575 // The 16-bit halves of the 32-bit PC-relative offset are patched separately, necessitating
576 // two patches/infos. There can be more than two patches/infos if the instruction supplying
577 // the high half is shared with e.g. a slow path, while the low half is supplied by separate
578 // instructions, e.g.:
579 // lui r1, high // patch
580 // addu r1, r1, rbase
581 // lw r2, low(r1) // patch
582 // beqz r2, slow_path
583 // back:
584 // ...
585 // slow_path:
586 // ...
587 // sw r2, low(r1) // patch
588 // b back
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700589 struct PcRelativePatchInfo {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700590 PcRelativePatchInfo(const DexFile& dex_file,
591 uint32_t off_or_idx,
592 const PcRelativePatchInfo* info_high)
593 : target_dex_file(dex_file),
594 offset_or_index(off_or_idx),
595 label(),
596 pc_rel_label(),
597 patch_info_high(info_high) { }
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700598
599 const DexFile& target_dex_file;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700600 // Either the dex cache array element offset or the string/type index.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700601 uint32_t offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700602 // Label for the instruction to patch.
603 MipsLabel label;
604 // Label for the instruction corresponding to PC+0. Not bound or used in low half patches.
605 // Not bound in high half patches on R2 when using HMipsComputeBaseMethodAddress.
606 // Bound in high half patches on R2 when using the NAL instruction instead of
607 // HMipsComputeBaseMethodAddress.
608 // Bound in high half patches on R6.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700609 MipsLabel pc_rel_label;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700610 // Pointer to the info for the high half patch or nullptr if this is the high half patch info.
611 const PcRelativePatchInfo* patch_info_high;
612
613 private:
614 PcRelativePatchInfo(PcRelativePatchInfo&& other) = delete;
615 DISALLOW_COPY_AND_ASSIGN(PcRelativePatchInfo);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700616 };
617
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700618 PcRelativePatchInfo* NewPcRelativeMethodPatch(MethodReference target_method,
619 const PcRelativePatchInfo* info_high = nullptr);
620 PcRelativePatchInfo* NewMethodBssEntryPatch(MethodReference target_method,
621 const PcRelativePatchInfo* info_high = nullptr);
622 PcRelativePatchInfo* NewPcRelativeTypePatch(const DexFile& dex_file,
623 dex::TypeIndex type_index,
624 const PcRelativePatchInfo* info_high = nullptr);
625 PcRelativePatchInfo* NewTypeBssEntryPatch(const DexFile& dex_file,
626 dex::TypeIndex type_index,
627 const PcRelativePatchInfo* info_high = nullptr);
Vladimir Marko65979462017-05-19 17:25:12 +0100628 PcRelativePatchInfo* NewPcRelativeStringPatch(const DexFile& dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700629 dex::StringIndex string_index,
630 const PcRelativePatchInfo* info_high = nullptr);
Alexey Frunze06a46c42016-07-19 15:00:40 -0700631 Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700632
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700633 void EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
634 Register out,
635 Register base,
636 PcRelativePatchInfo* info_low);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000637
Alexey Frunze627c1a02017-01-30 19:28:14 -0800638 // The JitPatchInfo is used for JIT string and class loads.
639 struct JitPatchInfo {
640 JitPatchInfo(const DexFile& dex_file, uint64_t idx)
641 : target_dex_file(dex_file), index(idx) { }
642 JitPatchInfo(JitPatchInfo&& other) = default;
643
644 const DexFile& target_dex_file;
645 // String/type index.
646 uint64_t index;
647 // Label for the instruction loading the most significant half of the address.
648 // The least significant half is loaded with the instruction that follows immediately.
649 MipsLabel high_label;
650 };
651
652 void PatchJitRootUse(uint8_t* code,
653 const uint8_t* roots_data,
654 const JitPatchInfo& info,
655 uint64_t index_in_table) const;
656 JitPatchInfo* NewJitRootStringPatch(const DexFile& dex_file,
657 dex::StringIndex dex_index,
658 Handle<mirror::String> handle);
659 JitPatchInfo* NewJitRootClassPatch(const DexFile& dex_file,
660 dex::TypeIndex dex_index,
661 Handle<mirror::Class> handle);
662
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200663 private:
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700664 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
665
Alexey Frunze06a46c42016-07-19 15:00:40 -0700666 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, Literal*>;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700667
Alexey Frunze06a46c42016-07-19 15:00:40 -0700668 Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700669 PcRelativePatchInfo* NewPcRelativePatch(const DexFile& dex_file,
670 uint32_t offset_or_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700671 const PcRelativePatchInfo* info_high,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700672 ArenaDeque<PcRelativePatchInfo>* patches);
673
Vladimir Markoaad75c62016-10-03 08:46:48 +0000674 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
675 void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
676 ArenaVector<LinkerPatch>* linker_patches);
677
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200678 // Labels for each block that will be compiled.
679 MipsLabel* block_labels_;
680 MipsLabel frame_entry_label_;
681 LocationsBuilderMIPS location_builder_;
682 InstructionCodeGeneratorMIPS instruction_visitor_;
683 ParallelMoveResolverMIPS move_resolver_;
684 MipsAssembler assembler_;
685 const MipsInstructionSetFeatures& isa_features_;
686
Alexey Frunze06a46c42016-07-19 15:00:40 -0700687 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
688 Uint32ToLiteralMap uint32_literals_;
Vladimir Marko65979462017-05-19 17:25:12 +0100689 // PC-relative method patch info for kBootImageLinkTimePcRelative.
690 ArenaDeque<PcRelativePatchInfo> pc_relative_method_patches_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100691 // PC-relative method patch info for kBssEntry.
692 ArenaDeque<PcRelativePatchInfo> method_bss_entry_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000693 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700694 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000695 // PC-relative type patch info for kBssEntry.
696 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100697 // PC-relative String patch info; type depends on configuration (app .bss or boot image PIC).
698 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
699
Alexey Frunze627c1a02017-01-30 19:28:14 -0800700 // Patches for string root accesses in JIT compiled code.
701 ArenaDeque<JitPatchInfo> jit_string_patches_;
702 // Patches for class root accesses in JIT compiled code.
703 ArenaDeque<JitPatchInfo> jit_class_patches_;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700704
705 // PC-relative loads on R2 clobber RA, which may need to be preserved explicitly in leaf methods.
706 // This is a flag set by pc_relative_fixups_mips and dex_cache_array_fixups_mips optimizations.
707 bool clobbered_ra_;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700708
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200709 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS);
710};
711
712} // namespace mips
713} // namespace art
714
715#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_