blob: d09ab7cce0976c164602c62c7df0394e1469cba7 [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"
David Sehr9e734c72018-01-04 17:56:19 -080021#include "dex/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
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084 Location GetNextLocation(DataType::Type type) OVERRIDE;
85 Location GetReturnLocation(DataType::Type type) const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020086 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
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 Location GetReturnLocation(DataType::Type return_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200104
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 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100119 Location GetReturnLocation(DataType::Type type) const OVERRIDE {
120 return DataType::Is64BitType(type)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200121 ? Location::RegisterPairLocation(V0, V1)
122 : Location::RegisterLocation(V0);
123 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100124 Location GetSetValueLocation(DataType::Type type, bool is_instance) const OVERRIDE {
125 return DataType::Is64BitType(type)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200126 ? Location::RegisterPairLocation(A2, A3)
127 : (is_instance ? Location::RegisterLocation(A2) : Location::RegisterLocation(A1));
128 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 Location GetFpuLocation(DataType::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200130 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);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +0100148 void ExchangeQuadSlots(int index1, int index2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149
150 MipsAssembler* GetAssembler() const;
151
152 private:
153 CodeGeneratorMIPS* const codegen_;
154
155 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverMIPS);
156};
157
158class SlowPathCodeMIPS : public SlowPathCode {
159 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000160 explicit SlowPathCodeMIPS(HInstruction* instruction)
161 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200162
163 MipsLabel* GetEntryLabel() { return &entry_label_; }
164 MipsLabel* GetExitLabel() { return &exit_label_; }
165
166 private:
167 MipsLabel entry_label_;
168 MipsLabel exit_label_;
169
170 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeMIPS);
171};
172
173class LocationsBuilderMIPS : public HGraphVisitor {
174 public:
175 LocationsBuilderMIPS(HGraph* graph, CodeGeneratorMIPS* codegen)
176 : HGraphVisitor(graph), codegen_(codegen) {}
177
178#define DECLARE_VISIT_INSTRUCTION(name, super) \
179 void Visit##name(H##name* instr) OVERRIDE;
180
181 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
182 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
183
184#undef DECLARE_VISIT_INSTRUCTION
185
186 void VisitInstruction(HInstruction* instruction) OVERRIDE {
187 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
188 << " (id " << instruction->GetId() << ")";
189 }
190
191 private:
192 void HandleInvoke(HInvoke* invoke);
193 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000194 void HandleCondition(HCondition* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200195 void HandleShift(HBinaryOperation* operation);
196 void HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info);
197 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Alexey Frunzef58b2482016-09-02 22:14:06 -0700198 Location RegisterOrZeroConstant(HInstruction* instruction);
199 Location FpuRegisterOrConstantForStore(HInstruction* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200200
201 InvokeDexCallingConventionVisitorMIPS parameter_visitor_;
202
203 CodeGeneratorMIPS* const codegen_;
204
205 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderMIPS);
206};
207
Aart Bik42249c32016-01-07 15:33:50 -0800208class InstructionCodeGeneratorMIPS : public InstructionCodeGenerator {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200209 public:
210 InstructionCodeGeneratorMIPS(HGraph* graph, CodeGeneratorMIPS* codegen);
211
212#define DECLARE_VISIT_INSTRUCTION(name, super) \
213 void Visit##name(H##name* instr) OVERRIDE;
214
215 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
216 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(DECLARE_VISIT_INSTRUCTION)
217
218#undef DECLARE_VISIT_INSTRUCTION
219
220 void VisitInstruction(HInstruction* instruction) OVERRIDE {
221 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
222 << " (id " << instruction->GetId() << ")";
223 }
224
225 MipsAssembler* GetAssembler() const { return assembler_; }
226
Alexey Frunze96b66822016-09-10 02:32:44 -0700227 // Compare-and-jump packed switch generates approx. 3 + 2.5 * N 32-bit
228 // instructions for N cases.
229 // Table-based packed switch generates approx. 11 32-bit instructions
230 // and N 32-bit data words for N cases.
231 // At N = 6 they come out as 18 and 17 32-bit words respectively.
232 // We switch to the table-based method starting with 7 cases.
233 static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
234
Chris Larsen5633ce72017-04-10 15:47:40 -0700235 void GenerateMemoryBarrier(MemBarrierKind kind);
236
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200237 private:
238 void GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path, Register class_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200239 void GenerateSuspendCheck(HSuspendCheck* check, HBasicBlock* successor);
240 void HandleBinaryOp(HBinaryOperation* operation);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000241 void HandleCondition(HCondition* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200242 void HandleShift(HBinaryOperation* operation);
Goran Jakovljevice114da22016-12-26 14:21:43 +0100243 void HandleFieldSet(HInstruction* instruction,
244 const FieldInfo& field_info,
245 uint32_t dex_pc,
246 bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200247 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info, uint32_t dex_pc);
Alexey Frunze15958152017-02-09 19:08:30 -0800248
Aart Bik3dad3412018-02-28 12:01:46 -0800249 void GenerateAbsFP(LocationSummary* locations, DataType::Type type, bool isR2OrNewer, bool isR6);
250
Alexey Frunze15958152017-02-09 19:08:30 -0800251 // Generate a heap reference load using one register `out`:
252 //
253 // out <- *(out + offset)
254 //
255 // while honoring heap poisoning and/or read barriers (if any).
256 //
257 // Location `maybe_temp` is used when generating a read barrier and
258 // shall be a register in that case; it may be an invalid location
259 // otherwise.
260 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
261 Location out,
262 uint32_t offset,
263 Location maybe_temp,
264 ReadBarrierOption read_barrier_option);
265 // Generate a heap reference load using two different registers
266 // `out` and `obj`:
267 //
268 // out <- *(obj + offset)
269 //
270 // while honoring heap poisoning and/or read barriers (if any).
271 //
272 // Location `maybe_temp` is used when generating a Baker's (fast
273 // path) read barrier and shall be a register in that case; it may
274 // be an invalid location otherwise.
275 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
276 Location out,
277 Location obj,
278 uint32_t offset,
279 Location maybe_temp,
280 ReadBarrierOption read_barrier_option);
281
Alexey Frunze06a46c42016-07-19 15:00:40 -0700282 // Generate a GC root reference load:
283 //
284 // root <- *(obj + offset)
285 //
286 // while honoring read barriers (if any).
287 void GenerateGcRootFieldLoad(HInstruction* instruction,
288 Location root,
289 Register obj,
Alexey Frunze15958152017-02-09 19:08:30 -0800290 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -0700291 ReadBarrierOption read_barrier_option,
292 MipsLabel* label_low = nullptr);
Alexey Frunze15958152017-02-09 19:08:30 -0800293
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800294 void GenerateIntCompare(IfCondition cond, LocationSummary* locations);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700295 // When the function returns `false` it means that the condition holds if `dst` is non-zero
296 // and doesn't hold if `dst` is zero. If it returns `true`, the roles of zero and non-zero
297 // `dst` are exchanged.
298 bool MaterializeIntCompare(IfCondition cond,
299 LocationSummary* input_locations,
300 Register dst);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800301 void GenerateIntCompareAndBranch(IfCondition cond,
302 LocationSummary* locations,
303 MipsLabel* label);
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +0100304 void GenerateLongCompare(IfCondition cond, LocationSummary* locations);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800305 void GenerateLongCompareAndBranch(IfCondition cond,
306 LocationSummary* locations,
307 MipsLabel* label);
Alexey Frunze2ddb7172016-09-06 17:04:55 -0700308 void GenerateFpCompare(IfCondition cond,
309 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100310 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -0700311 LocationSummary* locations);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700312 // When the function returns `false` it means that the condition holds if the condition
313 // code flag `cc` is non-zero and doesn't hold if `cc` is zero. If it returns `true`,
314 // the roles of zero and non-zero values of the `cc` flag are exchanged.
315 bool MaterializeFpCompareR2(IfCondition cond,
316 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100317 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700318 LocationSummary* input_locations,
319 int cc);
320 // When the function returns `false` it means that the condition holds if `dst` is non-zero
321 // and doesn't hold if `dst` is zero. If it returns `true`, the roles of zero and non-zero
322 // `dst` are exchanged.
323 bool MaterializeFpCompareR6(IfCondition cond,
324 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100325 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700326 LocationSummary* input_locations,
327 FRegister dst);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800328 void GenerateFpCompareAndBranch(IfCondition cond,
329 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100330 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -0800331 LocationSummary* locations,
332 MipsLabel* label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000334 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000336 MipsLabel* false_target);
Alexey Frunze7e99e052015-11-24 19:28:01 -0800337 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
338 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
339 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
340 void GenerateDivRemIntegral(HBinaryOperation* instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexey Frunze96b66822016-09-10 02:32:44 -0700342 void GenPackedSwitchWithCompares(Register value_reg,
343 int32_t lower_bound,
344 uint32_t num_entries,
345 HBasicBlock* switch_block,
346 HBasicBlock* default_block);
347 void GenTableBasedPackedSwitch(Register value_reg,
348 Register constant_area,
349 int32_t lower_bound,
350 uint32_t num_entries,
351 HBasicBlock* switch_block,
352 HBasicBlock* default_block);
Lena Djokic51765b02017-06-22 13:49:59 +0200353
354 int32_t VecAddress(LocationSummary* locations,
355 size_t size,
356 /* out */ Register* adjusted_base);
Alexey Frunze674b9ee2016-09-20 14:54:15 -0700357 void GenConditionalMoveR2(HSelect* select);
358 void GenConditionalMoveR6(HSelect* select);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200359
360 MipsAssembler* const assembler_;
361 CodeGeneratorMIPS* const codegen_;
362
363 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorMIPS);
364};
365
366class CodeGeneratorMIPS : public CodeGenerator {
367 public:
368 CodeGeneratorMIPS(HGraph* graph,
369 const MipsInstructionSetFeatures& isa_features,
370 const CompilerOptions& compiler_options,
371 OptimizingCompilerStats* stats = nullptr);
372 virtual ~CodeGeneratorMIPS() {}
373
Alexey Frunze73296a72016-06-03 22:51:46 -0700374 void ComputeSpillMask() OVERRIDE;
Alexey Frunze58320ce2016-08-30 21:40:46 -0700375 bool HasAllocatedCalleeSaveRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 void GenerateFrameEntry() OVERRIDE;
377 void GenerateFrameExit() OVERRIDE;
378
379 void Bind(HBasicBlock* block) OVERRIDE;
380
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 void MoveConstant(Location location, HConstant* c);
382
383 size_t GetWordSize() const OVERRIDE { return kMipsWordSize; }
384
Lena Djokicca8c2952017-05-29 11:31:46 +0200385 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
386 return GetGraph()->HasSIMD()
387 ? 2 * kMipsDoublewordSize // 16 bytes for each spill.
388 : 1 * kMipsDoublewordSize; // 8 bytes for each spill.
389 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200390
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100391 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200392 return assembler_.GetLabelLocation(GetLabelOf(block));
393 }
394
395 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
396 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
397 MipsAssembler* GetAssembler() OVERRIDE { return &assembler_; }
398 const MipsAssembler& GetAssembler() const OVERRIDE { return assembler_; }
399
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700400 // Emit linker patches.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100401 void EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) OVERRIDE;
Alexey Frunze627c1a02017-01-30 19:28:14 -0800402 void EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) OVERRIDE;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700403
Alexey Frunze15958152017-02-09 19:08:30 -0800404 // Fast path implementation of ReadBarrier::Barrier for a heap
405 // reference field load when Baker's read barriers are used.
406 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
407 Location ref,
408 Register obj,
409 uint32_t offset,
410 Location temp,
411 bool needs_null_check);
412 // Fast path implementation of ReadBarrier::Barrier for a heap
413 // reference array load when Baker's read barriers are used.
414 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
415 Location ref,
416 Register obj,
417 uint32_t data_offset,
418 Location index,
419 Location temp,
420 bool needs_null_check);
421
422 // Factored implementation, used by GenerateFieldLoadWithBakerReadBarrier,
423 // GenerateArrayLoadWithBakerReadBarrier and some intrinsics.
424 //
425 // Load the object reference located at the address
426 // `obj + offset + (index << scale_factor)`, held by object `obj`, into
427 // `ref`, and mark it if needed.
428 //
429 // If `always_update_field` is true, the value of the reference is
430 // atomically updated in the holder (`obj`).
431 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
432 Location ref,
433 Register obj,
434 uint32_t offset,
435 Location index,
436 ScaleFactor scale_factor,
437 Location temp,
438 bool needs_null_check,
439 bool always_update_field = false);
440
441 // Generate a read barrier for a heap reference within `instruction`
442 // using a slow path.
443 //
444 // A read barrier for an object reference read from the heap is
445 // implemented as a call to the artReadBarrierSlow runtime entry
446 // point, which is passed the values in locations `ref`, `obj`, and
447 // `offset`:
448 //
449 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
450 // mirror::Object* obj,
451 // uint32_t offset);
452 //
453 // The `out` location contains the value returned by
454 // artReadBarrierSlow.
455 //
456 // When `index` is provided (i.e. for array accesses), the offset
457 // value passed to artReadBarrierSlow is adjusted to take `index`
458 // into account.
459 void GenerateReadBarrierSlow(HInstruction* instruction,
460 Location out,
461 Location ref,
462 Location obj,
463 uint32_t offset,
464 Location index = Location::NoLocation());
465
466 // If read barriers are enabled, generate a read barrier for a heap
467 // reference using a slow path. If heap poisoning is enabled, also
468 // unpoison the reference in `out`.
469 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
470 Location out,
471 Location ref,
472 Location obj,
473 uint32_t offset,
474 Location index = Location::NoLocation());
475
476 // Generate a read barrier for a GC root within `instruction` using
477 // a slow path.
478 //
479 // A read barrier for an object reference GC root is implemented as
480 // a call to the artReadBarrierForRootSlow runtime entry point,
481 // which is passed the value in location `root`:
482 //
483 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
484 //
485 // The `out` location contains the value returned by
486 // artReadBarrierForRootSlow.
487 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
488
Goran Jakovljevice114da22016-12-26 14:21:43 +0100489 void MarkGCCard(Register object, Register value, bool value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490
491 // Register allocation.
492
David Brazdil58282f42016-01-14 12:45:10 +0000493 void SetupBlockedRegisters() const OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494
Roland Levillainf41f9562016-09-14 19:26:48 +0100495 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
496 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
497 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
498 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700499 void ClobberRA() {
500 clobbered_ra_ = true;
501 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200502
503 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
504 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
505
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200506 InstructionSet GetInstructionSet() const OVERRIDE { return InstructionSet::kMips; }
507
508 const MipsInstructionSetFeatures& GetInstructionSetFeatures() const {
509 return isa_features_;
510 }
511
512 MipsLabel* GetLabelOf(HBasicBlock* block) const {
513 return CommonGetLabelOf<MipsLabel>(block_labels_, block);
514 }
515
516 void Initialize() OVERRIDE {
517 block_labels_ = CommonInitializeLabels<MipsLabel>();
518 }
519
520 void Finalize(CodeAllocator* allocator) OVERRIDE;
521
522 // Code generation helpers.
523
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100524 void MoveLocation(Location dst, Location src, DataType::Type dst_type) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200525
Roland Levillainf41f9562016-09-14 19:26:48 +0100526 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200527
528 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
529
530 // Generate code to invoke a runtime entry point.
531 void InvokeRuntime(QuickEntrypointEnum entrypoint,
532 HInstruction* instruction,
533 uint32_t dex_pc,
Serban Constantinescufca16662016-07-14 09:21:59 +0100534 SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200535
Alexey Frunze15958152017-02-09 19:08:30 -0800536 // Generate code to invoke a runtime entry point, but do not record
537 // PC-related information in a stack map.
538 void InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
539 HInstruction* instruction,
540 SlowPathCode* slow_path,
541 bool direct);
542
543 void GenerateInvokeRuntime(int32_t entry_point_offset, bool direct);
544
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200545 ParallelMoveResolver* GetMoveResolver() OVERRIDE { return &move_resolver_; }
546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100547 bool NeedsTwoRegisters(DataType::Type type) const OVERRIDE {
548 return type == DataType::Type::kInt64;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200549 }
550
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000551 // Check if the desired_string_load_kind is supported. If it is, return it,
552 // otherwise return a fall-back kind that should be used instead.
553 HLoadString::LoadKind GetSupportedLoadStringKind(
554 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
555
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100556 // Check if the desired_class_load_kind is supported. If it is, return it,
557 // otherwise return a fall-back kind that should be used instead.
558 HLoadClass::LoadKind GetSupportedLoadClassKind(
559 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
560
Vladimir Markodc151b22015-10-15 18:02:30 +0100561 // Check if the desired_dispatch_info is supported. If it is, return it,
562 // otherwise return a fall-back info that should be used instead.
563 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
564 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100565 HInvokeStaticOrDirect* invoke) OVERRIDE;
Vladimir Markodc151b22015-10-15 18:02:30 +0100566
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100567 void GenerateStaticOrDirectCall(
568 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
569 void GenerateVirtualCall(
570 HInvokeVirtual* invoke, Location temp, SlowPathCode* slow_path = nullptr) OVERRIDE;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200571
572 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100573 DataType::Type type ATTRIBUTE_UNUSED) OVERRIDE {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200574 UNIMPLEMENTED(FATAL) << "Not implemented on MIPS";
575 }
576
Roland Levillainf41f9562016-09-14 19:26:48 +0100577 void GenerateNop() OVERRIDE;
578 void GenerateImplicitNullCheck(HNullCheck* instruction) OVERRIDE;
579 void GenerateExplicitNullCheck(HNullCheck* instruction) OVERRIDE;
David Srbeckyc7098ff2016-02-09 14:30:11 +0000580
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000581 // The PcRelativePatchInfo is used for PC-relative addressing of methods/strings/types,
582 // whether through .data.bimg.rel.ro, .bss, or directly in the boot image.
583 //
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700584 // The 16-bit halves of the 32-bit PC-relative offset are patched separately, necessitating
585 // two patches/infos. There can be more than two patches/infos if the instruction supplying
586 // the high half is shared with e.g. a slow path, while the low half is supplied by separate
587 // instructions, e.g.:
588 // lui r1, high // patch
589 // addu r1, r1, rbase
590 // lw r2, low(r1) // patch
591 // beqz r2, slow_path
592 // back:
593 // ...
594 // slow_path:
595 // ...
596 // sw r2, low(r1) // patch
597 // b back
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000598 struct PcRelativePatchInfo : PatchInfo<MipsLabel> {
599 PcRelativePatchInfo(const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700600 uint32_t off_or_idx,
601 const PcRelativePatchInfo* info_high)
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000602 : PatchInfo<MipsLabel>(dex_file, off_or_idx),
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700603 pc_rel_label(),
604 patch_info_high(info_high) { }
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700605
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700606 // Label for the instruction corresponding to PC+0. Not bound or used in low half patches.
607 // Not bound in high half patches on R2 when using HMipsComputeBaseMethodAddress.
608 // Bound in high half patches on R2 when using the NAL instruction instead of
609 // HMipsComputeBaseMethodAddress.
610 // Bound in high half patches on R6.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700611 MipsLabel pc_rel_label;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700612 // Pointer to the info for the high half patch or nullptr if this is the high half patch info.
613 const PcRelativePatchInfo* patch_info_high;
614
615 private:
616 PcRelativePatchInfo(PcRelativePatchInfo&& other) = delete;
617 DISALLOW_COPY_AND_ASSIGN(PcRelativePatchInfo);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700618 };
619
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000620 PcRelativePatchInfo* NewBootImageMethodPatch(MethodReference target_method,
621 const PcRelativePatchInfo* info_high = nullptr);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700622 PcRelativePatchInfo* NewMethodBssEntryPatch(MethodReference target_method,
623 const PcRelativePatchInfo* info_high = nullptr);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000624 PcRelativePatchInfo* NewBootImageTypePatch(const DexFile& dex_file,
625 dex::TypeIndex type_index,
626 const PcRelativePatchInfo* info_high = nullptr);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700627 PcRelativePatchInfo* NewTypeBssEntryPatch(const DexFile& dex_file,
628 dex::TypeIndex type_index,
629 const PcRelativePatchInfo* info_high = nullptr);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000630 PcRelativePatchInfo* NewBootImageStringPatch(const DexFile& dex_file,
631 dex::StringIndex string_index,
632 const PcRelativePatchInfo* info_high = nullptr);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100633 PcRelativePatchInfo* NewStringBssEntryPatch(const DexFile& dex_file,
634 dex::StringIndex string_index,
635 const PcRelativePatchInfo* info_high = nullptr);
Alexey Frunze06a46c42016-07-19 15:00:40 -0700636 Literal* DeduplicateBootImageAddressLiteral(uint32_t address);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700637
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700638 void EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
639 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700640 Register base);
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.
Alexey Frunze627c1a02017-01-30 19:28:14 -0800652 MipsLabel high_label;
Alexey Frunze4147fcc2017-06-17 19:57:27 -0700653 // Label for the instruction supplying the least significant half of the address.
654 MipsLabel low_label;
Alexey Frunze627c1a02017-01-30 19:28:14 -0800655 };
656
657 void PatchJitRootUse(uint8_t* code,
658 const uint8_t* roots_data,
659 const JitPatchInfo& info,
660 uint64_t index_in_table) const;
661 JitPatchInfo* NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +0100662 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -0800663 Handle<mirror::String> handle);
664 JitPatchInfo* NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +0100665 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -0800666 Handle<mirror::Class> handle);
667
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200668 private:
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700669 Register GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke, Register temp);
670
Alexey Frunze06a46c42016-07-19 15:00:40 -0700671 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, Literal*>;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700672
Alexey Frunze06a46c42016-07-19 15:00:40 -0700673 Literal* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000674 PcRelativePatchInfo* NewPcRelativePatch(const DexFile* dex_file,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700675 uint32_t offset_or_index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700676 const PcRelativePatchInfo* info_high,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700677 ArenaDeque<PcRelativePatchInfo>* patches);
678
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100679 template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +0000680 void EmitPcRelativeLinkerPatches(const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100681 ArenaVector<linker::LinkerPatch>* linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000682
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200683 // Labels for each block that will be compiled.
684 MipsLabel* block_labels_;
685 MipsLabel frame_entry_label_;
686 LocationsBuilderMIPS location_builder_;
687 InstructionCodeGeneratorMIPS instruction_visitor_;
688 ParallelMoveResolverMIPS move_resolver_;
689 MipsAssembler assembler_;
690 const MipsInstructionSetFeatures& isa_features_;
691
Alexey Frunze06a46c42016-07-19 15:00:40 -0700692 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
693 Uint32ToLiteralMap uint32_literals_;
Vladimir Marko65979462017-05-19 17:25:12 +0100694 // PC-relative method patch info for kBootImageLinkTimePcRelative.
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000695 ArenaDeque<PcRelativePatchInfo> boot_image_method_patches_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100696 // PC-relative method patch info for kBssEntry.
697 ArenaDeque<PcRelativePatchInfo> method_bss_entry_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000698 // PC-relative type patch info for kBootImageLinkTimePcRelative.
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000699 ArenaDeque<PcRelativePatchInfo> boot_image_type_patches_;
Vladimir Marko1998cd02017-01-13 13:02:58 +0000700 // PC-relative type patch info for kBssEntry.
701 ArenaDeque<PcRelativePatchInfo> type_bss_entry_patches_;
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100702 // PC-relative String patch info; type depends on configuration (intern table or boot image PIC).
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000703 ArenaDeque<PcRelativePatchInfo> boot_image_string_patches_;
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100704 // PC-relative String patch info for kBssEntry.
705 ArenaDeque<PcRelativePatchInfo> string_bss_entry_patches_;
Vladimir Marko65979462017-05-19 17:25:12 +0100706
Alexey Frunze627c1a02017-01-30 19:28:14 -0800707 // Patches for string root accesses in JIT compiled code.
708 ArenaDeque<JitPatchInfo> jit_string_patches_;
709 // Patches for class root accesses in JIT compiled code.
710 ArenaDeque<JitPatchInfo> jit_class_patches_;
Alexey Frunze06a46c42016-07-19 15:00:40 -0700711
712 // PC-relative loads on R2 clobber RA, which may need to be preserved explicitly in leaf methods.
713 // This is a flag set by pc_relative_fixups_mips and dex_cache_array_fixups_mips optimizations.
714 bool clobbered_ra_;
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700715
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200716 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorMIPS);
717};
718
719} // namespace mips
720} // namespace art
721
722#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_MIPS_H_