blob: 52ee852269779d11b73bfe5136d90f995b0d8eb0 [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);
Lena Djokic51765b02017-06-22 13:49:59 +0200349
350 int32_t VecAddress(LocationSummary* locations,
351 size_t size,
352 /* out */ Register* adjusted_base);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700353 void GenConditionalMoveR2(HSelect* select);
354 void GenConditionalMoveR6(HSelect* select);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355
356 MipsAssembler* const assembler_;
357 CodeGeneratorMIPS* const codegen_;
358
359 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS);
360};
361
362class CodeGeneratorMIPS : public CodeGenerator {
363 public:
364 CodeGeneratorMIPS(HGraph* graph,
365 const MipsInstructionSetFeatures& isa_features,
366 const CompilerOptions& compiler_options,
367 OptimizingCompilerStats* stats = nullptr);
368 virtual ~CodeGeneratorMIPS() {}
369
Alexey Frunze73296a72016-06-03 22:51:46 -0700370 void ComputeSpillMask() OVERRIDE;
Alexey Frunze58320ce2016-08-30 21:40:46 -0700371 bool HasAllocatedCalleeSaveRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200372 void GenerateFrameEntry() OVERRIDE;
373 void GenerateFrameExit() OVERRIDE;
374
375 void Bind(HBasicBlock* block) OVERRIDE;
376
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 void MoveConstant(Location location, HConstant* c);
378
379 size_t GetWordSize() const OVERRIDE { return kMipsWordSize; }
380
Lena Djokicca8c2952017-05-29 11:31:46 +0200381 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
382 return GetGraph()->HasSIMD()
383 ? 2 * kMipsDoublewordSize // 16 bytes for each spill.
384 : 1 * kMipsDoublewordSize; // 8 bytes for each spill.
385 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200386
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100387 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200388 return assembler_.GetLabelLocation(GetLabelOf(block));
389 }
390
391 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
392 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
393 MipsAssembler* GetAssembler() OVERRIDE { return &assembler_; }
394 const MipsAssembler& GetAssembler() const OVERRIDE { return assembler_; }
395
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700396 // Emit linker patches.
397 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
Alexey Frunze627c1a02017-01-30 19:28:14 -0800398 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700399
Alexey Frunze15958152017-02-09 19:08:30 -0800400 // Fast path implementation of ReadBarrier::Barrier for a heap
401 // reference field load when Baker's read barriers are used.
402 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
403 Location ref,
404 Register obj,
405 uint32_t offset,
406 Location temp,
407 bool needs_null_check);
408 // Fast path implementation of ReadBarrier::Barrier for a heap
409 // reference array load when Baker's read barriers are used.
410 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
411 Location ref,
412 Register obj,
413 uint32_t data_offset,
414 Location index,
415 Location temp,
416 bool needs_null_check);
417
418 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
419 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
420 //
421 // Load the object reference located at the address
422 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
423 // `ref`, and mark it if needed.
424 //
425 // If `always_update_field` is true, the value of the reference is
426 // atomically updated in the holder (`obj`).
427 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
428 Location ref,
429 Register obj,
430 uint32_t offset,
431 Location index,
432 ScaleFactor scale_factor,
433 Location temp,
434 bool needs_null_check,
435 bool always_update_field = false);
436
437 // Generate a read barrier for a heap reference within `instruction`
438 // using a slow path.
439 //
440 // A read barrier for an object reference read from the heap is
441 // implemented as a call to the artReadBarrierSlow runtime entry
442 // point, which is passed the values in locations `ref`, `obj`, and
443 // `offset`:
444 //
445 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
446 // mirror::Object* obj,
447 // uint32_t offset);
448 //
449 // The `out` location contains the value returned by
450 // artReadBarrierSlow.
451 //
452 // When `index` is provided (i.e. for array accesses), the offset
453 // value passed to artReadBarrierSlow is adjusted to take `index`
454 // into account.
455 void GenerateReadBarrierSlow(HInstruction* instruction,
456 Location out,
457 Location ref,
458 Location obj,
459 uint32_t offset,
460 Location index = Location::NoLocation());
461
462 // If read barriers are enabled, generate a read barrier for a heap
463 // reference using a slow path. If heap poisoning is enabled, also
464 // unpoison the reference in `out`.
465 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
466 Location out,
467 Location ref,
468 Location obj,
469 uint32_t offset,
470 Location index = Location::NoLocation());
471
472 // Generate a read barrier for a GC root within `instruction` using
473 // a slow path.
474 //
475 // A read barrier for an object reference GC root is implemented as
476 // a call to the artReadBarrierForRootSlow runtime entry point,
477 // which is passed the value in location `root`:
478 //
479 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
480 //
481 // The `out` location contains the value returned by
482 // artReadBarrierForRootSlow.
483 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
484
Goran Jakovljevice114da22016-12-26 14:21:43 +0100485 void MarkGCCard(Register object, Register value, bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200486
487 // Register allocation.
488
David Brazdil58282f42016-01-14 12:45:10 +0000489 void SetupBlockedRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490
Roland Levillainf41f9562016-09-14 19:26:48 +0100491 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
492 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
493 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
494 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700495 void ClobberRA() {
496 clobbered_ra_ = true;
497 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498
499 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
500 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
501
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200502 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips; }
503
504 const MipsInstructionSetFeatures& GetInstructionSetFeatures() const {
505 return isa_features_;
506 }
507
508 MipsLabel* GetLabelOf(HBasicBlock* block) const {
509 return CommonGetLabelOf<MipsLabel>(block_labels_, block);
510 }
511
512 void Initialize() OVERRIDE {
513 block_labels_ = CommonInitializeLabels<MipsLabel>();
514 }
515
516 void Finalize(CodeAllocator* allocator) OVERRIDE;
517
518 // Code generation helpers.
519
520 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
521
Roland Levillainf41f9562016-09-14 19:26:48 +0100522 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200523
524 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
525
526 // Generate code to invoke a runtime entry point.
527 void InvokeRuntime(QuickEntrypointEnum entrypoint,
528 HInstruction* instruction,
529 uint32_t dex_pc,
Serban Constantinescufca16662016-07-14 09:21:59 +0100530 SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200531
Alexey Frunze15958152017-02-09 19:08:30 -0800532 // Generate code to invoke a runtime entry point, but do not record
533 // PC-related information in a stack map.
534 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
535 HInstruction* instruction,
536 SlowPathCode* slow_path,
537 bool direct);
538
539 void GenerateInvokeRuntime(int32_t entry_point_offset, bool direct);
540
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200541 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
542
Roland Levillainf41f9562016-09-14 19:26:48 +0100543 bool NeedsTwoRegisters(Primitive::Type type) const OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200544 return type == Primitive::kPrimLong;
545 }
546
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000547 // Check if the desired_string_load_kind is supported. If it is, return it,
548 // otherwise return a fall-back kind that should be used instead.
549 HLoadString::LoadKind GetSupportedLoadStringKind(
550 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
551
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100552 // Check if the desired_class_load_kind is supported. If it is, return it,
553 // otherwise return a fall-back kind that should be used instead.
554 HLoadClass::LoadKind GetSupportedLoadClassKind(
555 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
556
Vladimir Markodc151b22015-10-15 18:02:30 +0100557 // Check if the desired_dispatch_info is supported. If it is, return it,
558 // otherwise return a fall-back info that should be used instead.
559 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
560 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100561 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100562
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100563 void GenerateStaticOrDirectCall(
564 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
565 void GenerateVirtualCall(
566 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200567
568 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
569 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
570 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS";
571 }
572
Roland Levillainf41f9562016-09-14 19:26:48 +0100573 void GenerateNop() OVERRIDE;
574 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
575 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
David Srbeckyc7098ff2016-02-09 14:30:11 +0000576
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700577 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
578 // and boot image strings. The only difference is the interpretation of the offset_or_index.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700579 // The 16-bit halves of the 32-bit PC-relative offset are patched separately, necessitating
580 // two patches/infos. There can be more than two patches/infos if the instruction supplying
581 // the high half is shared with e.g. a slow path, while the low half is supplied by separate
582 // instructions, e.g.:
583 // lui r1, high // patch
584 // addu r1, r1, rbase
585 // lw r2, low(r1) // patch
586 // beqz r2, slow_path
587 // back:
588 // ...
589 // slow_path:
590 // ...
591 // sw r2, low(r1) // patch
592 // b back
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700593 struct PcRelativePatchInfo {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700594 PcRelativePatchInfo(const DexFile& dex_file,
595 uint32_t off_or_idx,
596 const PcRelativePatchInfo* info_high)
597 : target_dex_file(dex_file),
598 offset_or_index(off_or_idx),
599 label(),
600 pc_rel_label(),
601 patch_info_high(info_high) { }
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700602
603 const DexFile& target_dex_file;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700604 // Either the dex cache array element offset or the string/type index.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700605 uint32_t offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700606 // Label for the instruction to patch.
607 MipsLabel label;
608 // Label for the instruction corresponding to PC+0. Not bound or used in low half patches.
609 // Not bound in high half patches on R2 when using HMipsComputeBaseMethodAddress.
610 // Bound in high half patches on R2 when using the NAL instruction instead of
611 // HMipsComputeBaseMethodAddress.
612 // Bound in high half patches on R6.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700613 MipsLabel pc_rel_label;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700614 // Pointer to the info for the high half patch or nullptr if this is the high half patch info.
615 const PcRelativePatchInfo* patch_info_high;
616
617 private:
618 PcRelativePatchInfo(PcRelativePatchInfo&& other) = delete;
619 DISALLOW_COPY_AND_ASSIGN(PcRelativePatchInfo);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700620 };
621
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700622 PcRelativePatchInfo* NewPcRelativeMethodPatch(MethodReference target_method,
623 const PcRelativePatchInfo* info_high = nullptr);
624 PcRelativePatchInfo* NewMethodBssEntryPatch(MethodReference target_method,
625 const PcRelativePatchInfo* info_high = nullptr);
626 PcRelativePatchInfo* NewPcRelativeTypePatch(const DexFile& dex_file,
627 dex::TypeIndex type_index,
628 const PcRelativePatchInfo* info_high = nullptr);
629 PcRelativePatchInfo* NewTypeBssEntryPatch(const DexFile& dex_file,
630 dex::TypeIndex type_index,
631 const PcRelativePatchInfo* info_high = nullptr);
Vladimir Marko65979462017-05-19 17:25:12 +0100632 PcRelativePatchInfo* NewPcRelativeStringPatch(const DexFile& dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700633 dex::StringIndex string_index,
634 const PcRelativePatchInfo* info_high = nullptr);
Alexey Frunze06a46c42016-07-19 15:00:40 -0700635 Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700636
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700637 void EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
638 Register out,
639 Register base,
640 PcRelativePatchInfo* info_low);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000641
Alexey Frunze627c1a02017-01-30 19:28:14 -0800642 // The JitPatchInfo is used for JIT string and class loads.
643 struct JitPatchInfo {
644 JitPatchInfo(const DexFile& dex_file, uint64_t idx)
645 : target_dex_file(dex_file), index(idx) { }
646 JitPatchInfo(JitPatchInfo&& other) = default;
647
648 const DexFile& target_dex_file;
649 // String/type index.
650 uint64_t index;
651 // Label for the instruction loading the most significant half of the address.
652 // The least significant half is loaded with the instruction that follows immediately.
653 MipsLabel high_label;
654 };
655
656 void PatchJitRootUse(uint8_t* code,
657 const uint8_t* roots_data,
658 const JitPatchInfo& info,
659 uint64_t index_in_table) const;
660 JitPatchInfo* NewJitRootStringPatch(const DexFile& dex_file,
661 dex::StringIndex dex_index,
662 Handle<mirror::String> handle);
663 JitPatchInfo* NewJitRootClassPatch(const DexFile& dex_file,
664 dex::TypeIndex dex_index,
665 Handle<mirror::Class> handle);
666
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200667 private:
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700668 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
669
Alexey Frunze06a46c42016-07-19 15:00:40 -0700670 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, Literal*>;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700671
Alexey Frunze06a46c42016-07-19 15:00:40 -0700672 Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700673 PcRelativePatchInfo* NewPcRelativePatch(const DexFile& dex_file,
674 uint32_t offset_or_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700675 const PcRelativePatchInfo* info_high,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700676 ArenaDeque<PcRelativePatchInfo>* patches);
677
Vladimir Markoaad75c62016-10-03 08:46:48 +0000678 template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
679 void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
680 ArenaVector<LinkerPatch>* linker_patches);
681
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200682 // Labels for each block that will be compiled.
683 MipsLabel* block_labels_;
684 MipsLabel frame_entry_label_;
685 LocationsBuilderMIPS location_builder_;
686 InstructionCodeGeneratorMIPS instruction_visitor_;
687 ParallelMoveResolverMIPS move_resolver_;
688 MipsAssembler assembler_;
689 const MipsInstructionSetFeatures& isa_features_;
690
Alexey Frunze06a46c42016-07-19 15:00:40 -0700691 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
692 Uint32ToLiteralMap uint32_literals_;
Vladimir Marko65979462017-05-19 17:25:12 +0100693 // PC-relative method patch info for kBootImageLinkTimePcRelative.
694 ArenaDeque<PcRelativePatchInfo> pc_relative_method_patches_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100695 // PC-relative method patch info for kBssEntry.
696 ArenaDeque<PcRelativePatchInfo> method_bss_entry_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000697 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700698 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000699 // PC-relative type patch info for kBssEntry.
700 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100701 // PC-relative String patch info; type depends on configuration (app .bss or boot image PIC).
702 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
703
Alexey Frunze627c1a02017-01-30 19:28:14 -0800704 // Patches for string root accesses in JIT compiled code.
705 ArenaDeque<JitPatchInfo> jit_string_patches_;
706 // Patches for class root accesses in JIT compiled code.
707 ArenaDeque<JitPatchInfo> jit_class_patches_;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700708
709 // PC-relative loads on R2 clobber RA, which may need to be preserved explicitly in leaf methods.
710 // This is a flag set by pc_relative_fixups_mips and dex_cache_array_fixups_mips optimizations.
711 bool clobbered_ra_;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700712
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200713 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS);
714};
715
716} // namespace mips
717} // namespace art
718
719#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_