blob: b35c520678805c5eb8d44d9906136c0a9e1e8a05 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 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_ARM64_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_
19
Vladimir Markocac5a7e2016-02-22 10:39:50 +000020#include "arch/arm64/quick_method_frame_info_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010021#include "code_generator.h"
Calin Juravlee460d1d2015-09-29 04:52:17 +010022#include "common_arm64.h"
Serban Constantinescu02d81cc2015-01-05 16:08:49 +000023#include "dex/compiler_enums.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000024#include "driver/compiler_options.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "nodes.h"
26#include "parallel_move_resolver.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070027#include "string_reference.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "utils/arm64/assembler_arm64.h"
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010029#include "utils/type_reference.h"
Serban Constantinescu82e52ce2015-03-26 16:50:57 +000030#include "vixl/a64/disasm-a64.h"
31#include "vixl/a64/macro-assembler-a64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032
33namespace art {
34namespace arm64 {
35
36class CodeGeneratorARM64;
Andreas Gampe878d58c2015-01-15 23:24:00 -080037
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000038// Use a local definition to prevent copying mistakes.
39static constexpr size_t kArm64WordSize = kArm64PointerSize;
40
Alexandre Rames5319def2014-10-23 10:03:10 +010041static const vixl::Register kParameterCoreRegisters[] = {
42 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
43};
44static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
45static const vixl::FPRegister kParameterFPRegisters[] = {
46 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
47};
48static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
49
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010050const vixl::Register tr = vixl::x19; // Thread Register
Mathieu Chartiere401d142015-04-22 13:56:20 -070051static const vixl::Register kArtMethodRegister = vixl::x0; // Method register on invoke.
Alexandre Rames5319def2014-10-23 10:03:10 +010052
53const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +000054const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31);
Alexandre Rames5319def2014-10-23 10:03:10 +010055
Zheng Xu69a50302015-04-14 20:04:41 +080056const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr);
Serban Constantinescu3d087de2015-01-28 11:57:05 +000057
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010058// Callee-saved registers AAPCS64 (without x19 - Thread Register)
Serban Constantinescu3d087de2015-01-28 11:57:05 +000059const vixl::CPURegList callee_saved_core_registers(vixl::CPURegister::kRegister,
60 vixl::kXRegSize,
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010061 vixl::x20.code(),
Serban Constantinescu3d087de2015-01-28 11:57:05 +000062 vixl::x30.code());
63const vixl::CPURegList callee_saved_fp_registers(vixl::CPURegister::kFPRegister,
64 vixl::kDRegSize,
65 vixl::d8.code(),
66 vixl::d15.code());
Alexandre Ramesa89086e2014-11-07 17:13:25 +000067Location ARM64ReturnLocation(Primitive::Type return_type);
68
Andreas Gampe878d58c2015-01-15 23:24:00 -080069class SlowPathCodeARM64 : public SlowPathCode {
70 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000071 explicit SlowPathCodeARM64(HInstruction* instruction)
72 : SlowPathCode(instruction), entry_label_(), exit_label_() {}
Andreas Gampe878d58c2015-01-15 23:24:00 -080073
74 vixl::Label* GetEntryLabel() { return &entry_label_; }
75 vixl::Label* GetExitLabel() { return &exit_label_; }
76
Zheng Xuda403092015-04-24 17:35:39 +080077 void SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) OVERRIDE;
78 void RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) OVERRIDE;
79
Andreas Gampe878d58c2015-01-15 23:24:00 -080080 private:
81 vixl::Label entry_label_;
82 vixl::Label exit_label_;
83
84 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM64);
85};
86
Alexandre Ramesc01a6642016-04-15 11:54:06 +010087class JumpTableARM64 : public DeletableArenaObject<kArenaAllocSwitchTable> {
Zheng Xu3927c8b2015-11-18 17:46:25 +080088 public:
89 explicit JumpTableARM64(HPackedSwitch* switch_instr)
90 : switch_instr_(switch_instr), table_start_() {}
91
92 vixl::Label* GetTableStartLabel() { return &table_start_; }
93
94 void EmitTable(CodeGeneratorARM64* codegen);
95
96 private:
97 HPackedSwitch* const switch_instr_;
98 vixl::Label table_start_;
99
100 DISALLOW_COPY_AND_ASSIGN(JumpTableARM64);
101};
102
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000103static const vixl::Register kRuntimeParameterCoreRegisters[] =
104 { vixl::x0, vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7 };
105static constexpr size_t kRuntimeParameterCoreRegistersLength =
106 arraysize(kRuntimeParameterCoreRegisters);
107static const vixl::FPRegister kRuntimeParameterFpuRegisters[] =
108 { vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7 };
109static constexpr size_t kRuntimeParameterFpuRegistersLength =
110 arraysize(kRuntimeParameterCoreRegisters);
111
112class InvokeRuntimeCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
113 public:
114 static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
115
116 InvokeRuntimeCallingConvention()
117 : CallingConvention(kRuntimeParameterCoreRegisters,
118 kRuntimeParameterCoreRegistersLength,
119 kRuntimeParameterFpuRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 kRuntimeParameterFpuRegistersLength,
121 kArm64PointerSize) {}
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000122
123 Location GetReturnLocation(Primitive::Type return_type);
124
125 private:
126 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
127};
128
Alexandre Rames5319def2014-10-23 10:03:10 +0100129class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
130 public:
131 InvokeDexCallingConvention()
132 : CallingConvention(kParameterCoreRegisters,
133 kParameterCoreRegistersLength,
134 kParameterFPRegisters,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 kParameterFPRegistersLength,
136 kArm64PointerSize) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100138 Location GetReturnLocation(Primitive::Type return_type) const {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000139 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100140 }
141
142
143 private:
144 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
145};
146
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100147class InvokeDexCallingConventionVisitorARM64 : public InvokeDexCallingConventionVisitor {
Alexandre Rames5319def2014-10-23 10:03:10 +0100148 public:
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100149 InvokeDexCallingConventionVisitorARM64() {}
150 virtual ~InvokeDexCallingConventionVisitorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100151
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100152 Location GetNextLocation(Primitive::Type type) OVERRIDE;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100153 Location GetReturnLocation(Primitive::Type return_type) const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100154 return calling_convention.GetReturnLocation(return_type);
155 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100156 Location GetMethodLocation() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100157
158 private:
159 InvokeDexCallingConvention calling_convention;
Alexandre Rames5319def2014-10-23 10:03:10 +0100160
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100161 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitorARM64);
Alexandre Rames5319def2014-10-23 10:03:10 +0100162};
163
Calin Juravlee460d1d2015-09-29 04:52:17 +0100164class FieldAccessCallingConventionARM64 : public FieldAccessCallingConvention {
165 public:
166 FieldAccessCallingConventionARM64() {}
167
168 Location GetObjectLocation() const OVERRIDE {
169 return helpers::LocationFrom(vixl::x1);
170 }
171 Location GetFieldIndexLocation() const OVERRIDE {
172 return helpers::LocationFrom(vixl::x0);
173 }
174 Location GetReturnLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
175 return helpers::LocationFrom(vixl::x0);
176 }
177 Location GetSetValueLocation(Primitive::Type type, bool is_instance) const OVERRIDE {
178 return Primitive::Is64BitType(type)
179 ? helpers::LocationFrom(vixl::x2)
180 : (is_instance
181 ? helpers::LocationFrom(vixl::x2)
182 : helpers::LocationFrom(vixl::x1));
183 }
184 Location GetFpuLocation(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
185 return helpers::LocationFrom(vixl::d0);
186 }
187
188 private:
189 DISALLOW_COPY_AND_ASSIGN(FieldAccessCallingConventionARM64);
190};
191
Aart Bik42249c32016-01-07 15:33:50 -0800192class InstructionCodeGeneratorARM64 : public InstructionCodeGenerator {
Alexandre Rames5319def2014-10-23 10:03:10 +0100193 public:
194 InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen);
195
196#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000197 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Ramesef20f712015-06-09 10:29:30 +0100198
199 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
200 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(DECLARE_VISIT_INSTRUCTION)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300201 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
Alexandre Ramesef20f712015-06-09 10:29:30 +0100202
Alexandre Rames5319def2014-10-23 10:03:10 +0100203#undef DECLARE_VISIT_INSTRUCTION
204
Alexandre Ramesef20f712015-06-09 10:29:30 +0100205 void VisitInstruction(HInstruction* instruction) OVERRIDE {
206 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
207 << " (id " << instruction->GetId() << ")";
208 }
209
Alexandre Rames5319def2014-10-23 10:03:10 +0100210 Arm64Assembler* GetAssembler() const { return assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000211 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100212
213 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000214 void GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path, vixl::Register class_reg);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000215 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Alexandre Rames67555f72014-11-18 10:55:16 +0000216 void HandleBinaryOp(HBinaryOperation* instr);
Roland Levillain44015862016-01-22 11:47:17 +0000217
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100218 void HandleFieldSet(HInstruction* instruction,
219 const FieldInfo& field_info,
220 bool value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +0100221 void HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000222 void HandleCondition(HCondition* instruction);
Roland Levillain44015862016-01-22 11:47:17 +0000223
224 // Generate a heap reference load using one register `out`:
225 //
226 // out <- *(out + offset)
227 //
228 // while honoring heap poisoning and/or read barriers (if any).
229 //
230 // Location `maybe_temp` is used when generating a read barrier and
231 // shall be a register in that case; it may be an invalid location
232 // otherwise.
233 void GenerateReferenceLoadOneRegister(HInstruction* instruction,
234 Location out,
235 uint32_t offset,
236 Location maybe_temp);
237 // Generate a heap reference load using two different registers
238 // `out` and `obj`:
239 //
240 // out <- *(obj + offset)
241 //
242 // while honoring heap poisoning and/or read barriers (if any).
243 //
244 // Location `maybe_temp` is used when generating a Baker's (fast
245 // path) read barrier and shall be a register in that case; it may
246 // be an invalid location otherwise.
247 void GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
248 Location out,
249 Location obj,
250 uint32_t offset,
251 Location maybe_temp);
252 // Generate a GC root reference load:
253 //
254 // root <- *(obj + offset)
255 //
256 // while honoring read barriers (if any).
257 void GenerateGcRootFieldLoad(HInstruction* instruction,
258 Location root,
259 vixl::Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000260 uint32_t offset,
261 vixl::Label* fixup_label = nullptr);
Roland Levillain44015862016-01-22 11:47:17 +0000262
Roland Levillain1a653882016-03-18 18:05:57 +0000263 // Generate a floating-point comparison.
264 void GenerateFcmp(HInstruction* instruction);
265
Serban Constantinescu02164b32014-11-13 14:05:07 +0000266 void HandleShift(HBinaryOperation* instr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700267 void GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +0000268 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700269 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +0000270 vixl::Label* false_target);
Zheng Xuc6667102015-05-15 16:08:45 +0800271 void DivRemOneOrMinusOne(HBinaryOperation* instruction);
272 void DivRemByPowerOfTwo(HBinaryOperation* instruction);
273 void GenerateDivRemWithAnyConstant(HBinaryOperation* instruction);
274 void GenerateDivRemIntegral(HBinaryOperation* instruction);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000275 void HandleGoto(HInstruction* got, HBasicBlock* successor);
Alexandre Rames5319def2014-10-23 10:03:10 +0100276
277 Arm64Assembler* const assembler_;
278 CodeGeneratorARM64* const codegen_;
279
280 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
281};
282
283class LocationsBuilderARM64 : public HGraphVisitor {
284 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100285 LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 : HGraphVisitor(graph), codegen_(codegen) {}
287
288#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000289 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Ramesef20f712015-06-09 10:29:30 +0100290
291 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(DECLARE_VISIT_INSTRUCTION)
292 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(DECLARE_VISIT_INSTRUCTION)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300293 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(DECLARE_VISIT_INSTRUCTION)
Alexandre Ramesef20f712015-06-09 10:29:30 +0100294
Alexandre Rames5319def2014-10-23 10:03:10 +0100295#undef DECLARE_VISIT_INSTRUCTION
296
Alexandre Ramesef20f712015-06-09 10:29:30 +0100297 void VisitInstruction(HInstruction* instruction) OVERRIDE {
298 LOG(FATAL) << "Unreachable instruction " << instruction->DebugName()
299 << " (id " << instruction->GetId() << ")";
300 }
301
Alexandre Rames5319def2014-10-23 10:03:10 +0100302 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 void HandleBinaryOp(HBinaryOperation* instr);
Alexandre Rames09a99962015-04-15 11:47:56 +0100304 void HandleFieldSet(HInstruction* instruction);
305 void HandleFieldGet(HInstruction* instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +0100306 void HandleInvoke(HInvoke* instr);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +0000307 void HandleCondition(HCondition* instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +0100308 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100309
310 CodeGeneratorARM64* const codegen_;
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100311 InvokeDexCallingConventionVisitorARM64 parameter_visitor_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100312
313 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
314};
315
Zheng Xuad4450e2015-04-17 18:48:56 +0800316class ParallelMoveResolverARM64 : public ParallelMoveResolverNoSwap {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000317 public:
318 ParallelMoveResolverARM64(ArenaAllocator* allocator, CodeGeneratorARM64* codegen)
Zheng Xuad4450e2015-04-17 18:48:56 +0800319 : ParallelMoveResolverNoSwap(allocator), codegen_(codegen), vixl_temps_() {}
Alexandre Rames3e69f162014-12-10 10:36:50 +0000320
Zheng Xuad4450e2015-04-17 18:48:56 +0800321 protected:
322 void PrepareForEmitNativeCode() OVERRIDE;
323 void FinishEmitNativeCode() OVERRIDE;
324 Location AllocateScratchLocationFor(Location::Kind kind) OVERRIDE;
325 void FreeScratchLocation(Location loc) OVERRIDE;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000326 void EmitMove(size_t index) OVERRIDE;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000327
328 private:
329 Arm64Assembler* GetAssembler() const;
330 vixl::MacroAssembler* GetVIXLAssembler() const {
331 return GetAssembler()->vixl_masm_;
332 }
333
334 CodeGeneratorARM64* const codegen_;
Zheng Xuad4450e2015-04-17 18:48:56 +0800335 vixl::UseScratchRegisterScope vixl_temps_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000336
337 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARM64);
338};
339
Alexandre Rames5319def2014-10-23 10:03:10 +0100340class CodeGeneratorARM64 : public CodeGenerator {
341 public:
Serban Constantinescu579885a2015-02-22 20:51:33 +0000342 CodeGeneratorARM64(HGraph* graph,
343 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100344 const CompilerOptions& compiler_options,
345 OptimizingCompilerStats* stats = nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000346 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100347
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000348 void GenerateFrameEntry() OVERRIDE;
349 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100350
Zheng Xuda403092015-04-24 17:35:39 +0800351 vixl::CPURegList GetFramePreservedCoreRegisters() const;
352 vixl::CPURegList GetFramePreservedFPRegisters() const;
Alexandre Rames5319def2014-10-23 10:03:10 +0100353
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000354 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100355
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100356 vixl::Label* GetLabelOf(HBasicBlock* block) {
357 block = FirstNonEmptyBlock(block);
358 return &(block_labels_[block->GetBlockId()]);
Alexandre Rames5319def2014-10-23 10:03:10 +0100359 }
360
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000361 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100362 return kArm64WordSize;
363 }
364
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500365 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
366 // Allocated in D registers, which are word sized.
367 return kArm64WordSize;
368 }
369
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100370 uintptr_t GetAddressOf(HBasicBlock* block) OVERRIDE {
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 vixl::Label* block_entry_label = GetLabelOf(block);
372 DCHECK(block_entry_label->IsBound());
373 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000374 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100375
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000376 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
377 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
378 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100379 const Arm64Assembler& GetAssembler() const OVERRIDE { return assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100381
382 // Emit a write barrier.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100383 void MarkGCCard(vixl::Register object, vixl::Register value, bool value_can_be_null);
Alexandre Rames5319def2014-10-23 10:03:10 +0100384
Roland Levillain44015862016-01-22 11:47:17 +0000385 void GenerateMemoryBarrier(MemBarrierKind kind);
386
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 // Register allocation.
388
David Brazdil58282f42016-01-14 12:45:10 +0000389 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100390
Zheng Xuda403092015-04-24 17:35:39 +0800391 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
392 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
393 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
394 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100395
396 // The number of registers that can be allocated. The register allocator may
397 // decide to reserve and not use a few of them.
398 // We do not consider registers sp, xzr, wzr. They are either not allocatable
399 // (xzr, wzr), or make for poor allocatable registers (sp alignment
400 // requirements, etc.). This also facilitates our task as all other registers
401 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000402 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
403 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100404 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
405
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000406 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
407 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100408
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000409 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 return InstructionSet::kArm64;
411 }
412
Serban Constantinescu579885a2015-02-22 20:51:33 +0000413 const Arm64InstructionSetFeatures& GetInstructionSetFeatures() const {
414 return isa_features_;
415 }
416
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000417 void Initialize() OVERRIDE {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100418 block_labels_.resize(GetGraph()->GetBlocks().size());
Alexandre Rames5319def2014-10-23 10:03:10 +0100419 }
420
Alexandre Rames68bd9b92016-07-15 17:41:13 +0100421 // We want to use the STP and LDP instructions to spill and restore registers for slow paths.
422 // These instructions can only encode offsets that are multiples of the register size accessed.
423 uint32_t GetPreferredSlotsAlignment() const OVERRIDE { return vixl::kXRegSizeInBytes; }
424
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100425 JumpTableARM64* CreateJumpTable(HPackedSwitch* switch_instr) {
426 jump_tables_.emplace_back(new (GetGraph()->GetArena()) JumpTableARM64(switch_instr));
427 return jump_tables_.back().get();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800428 }
429
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000430 void Finalize(CodeAllocator* allocator) OVERRIDE;
431
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000432 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000433 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Calin Juravle175dc732015-08-25 15:42:32 +0100434 void MoveConstant(Location destination, int32_t value) OVERRIDE;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100435 void MoveLocation(Location dst, Location src, Primitive::Type dst_type) OVERRIDE;
436 void AddLocationAsTemp(Location location, LocationSummary* locations) OVERRIDE;
437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
Roland Levillain44015862016-01-22 11:47:17 +0000439 void Store(Primitive::Type type, vixl::CPURegister src, const vixl::MemOperand& dst);
440 void LoadAcquire(HInstruction* instruction,
441 vixl::CPURegister dst,
442 const vixl::MemOperand& src,
443 bool needs_null_check);
444 void StoreRelease(Primitive::Type type, vixl::CPURegister src, const vixl::MemOperand& dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000445
446 // Generate code to invoke a runtime entry point.
Calin Juravle175dc732015-08-25 15:42:32 +0100447 void InvokeRuntime(QuickEntrypointEnum entrypoint,
448 HInstruction* instruction,
449 uint32_t dex_pc,
450 SlowPathCode* slow_path) OVERRIDE;
451
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000452 void InvokeRuntime(int32_t offset,
453 HInstruction* instruction,
454 uint32_t dex_pc,
455 SlowPathCode* slow_path);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000456
Alexandre Ramese6dbf482015-10-19 10:10:41 +0100457 ParallelMoveResolverARM64* GetMoveResolver() OVERRIDE { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000458
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000459 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
460 return false;
461 }
462
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000463 // Check if the desired_string_load_kind is supported. If it is, return it,
464 // otherwise return a fall-back kind that should be used instead.
465 HLoadString::LoadKind GetSupportedLoadStringKind(
466 HLoadString::LoadKind desired_string_load_kind) OVERRIDE;
467
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100468 // Check if the desired_class_load_kind is supported. If it is, return it,
469 // otherwise return a fall-back kind that should be used instead.
470 HLoadClass::LoadKind GetSupportedLoadClassKind(
471 HLoadClass::LoadKind desired_class_load_kind) OVERRIDE;
472
Vladimir Markodc151b22015-10-15 18:02:30 +0100473 // Check if the desired_dispatch_info is supported. If it is, return it,
474 // otherwise return a fall-back info that should be used instead.
475 HInvokeStaticOrDirect::DispatchInfo GetSupportedInvokeStaticOrDirectDispatch(
476 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
477 MethodReference target_method) OVERRIDE;
478
Andreas Gampe85b62f22015-09-09 13:15:38 -0700479 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) OVERRIDE;
480 void GenerateVirtualCall(HInvokeVirtual* invoke, Location temp) OVERRIDE;
481
482 void MoveFromReturnRegister(Location trg ATTRIBUTE_UNUSED,
483 Primitive::Type type ATTRIBUTE_UNUSED) OVERRIDE {
484 UNIMPLEMENTED(FATAL);
485 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800486
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000487 // Add a new PC-relative string patch for an instruction and return the label
488 // to be bound before the instruction. The instruction will be either the
489 // ADRP (pass `adrp_label = null`) or the ADD (pass `adrp_label` pointing
490 // to the associated ADRP patch label).
491 vixl::Label* NewPcRelativeStringPatch(const DexFile& dex_file,
492 uint32_t string_index,
493 vixl::Label* adrp_label = nullptr);
494
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100495 // Add a new PC-relative type patch for an instruction and return the label
496 // to be bound before the instruction. The instruction will be either the
497 // ADRP (pass `adrp_label = null`) or the ADD (pass `adrp_label` pointing
498 // to the associated ADRP patch label).
499 vixl::Label* NewPcRelativeTypePatch(const DexFile& dex_file,
500 uint32_t type_index,
501 vixl::Label* adrp_label = nullptr);
502
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000503 // Add a new PC-relative dex cache array patch for an instruction and return
504 // the label to be bound before the instruction. The instruction will be
505 // either the ADRP (pass `adrp_label = null`) or the LDR (pass `adrp_label`
506 // pointing to the associated ADRP patch label).
507 vixl::Label* NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
508 uint32_t element_offset,
509 vixl::Label* adrp_label = nullptr);
510
511 vixl::Literal<uint32_t>* DeduplicateBootImageStringLiteral(const DexFile& dex_file,
512 uint32_t string_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100513 vixl::Literal<uint32_t>* DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
514 uint32_t type_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000515 vixl::Literal<uint32_t>* DeduplicateBootImageAddressLiteral(uint64_t address);
516 vixl::Literal<uint64_t>* DeduplicateDexCacheAddressLiteral(uint64_t address);
517
Vladimir Marko58155012015-08-19 12:49:41 +0000518 void EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) OVERRIDE;
519
Roland Levillain44015862016-01-22 11:47:17 +0000520 // Fast path implementation of ReadBarrier::Barrier for a heap
521 // reference field load when Baker's read barriers are used.
522 void GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
523 Location ref,
524 vixl::Register obj,
525 uint32_t offset,
526 vixl::Register temp,
527 bool needs_null_check,
528 bool use_load_acquire);
529 // Fast path implementation of ReadBarrier::Barrier for a heap
530 // reference array load when Baker's read barriers are used.
531 void GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
532 Location ref,
533 vixl::Register obj,
534 uint32_t data_offset,
535 Location index,
536 vixl::Register temp,
537 bool needs_null_check);
Roland Levillainbfea3352016-06-23 13:48:47 +0100538 // Factored implementation used by GenerateFieldLoadWithBakerReadBarrier
539 // and GenerateArrayLoadWithBakerReadBarrier.
540 void GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
541 Location ref,
542 vixl::Register obj,
543 uint32_t offset,
544 Location index,
545 size_t scale_factor,
546 vixl::Register temp,
547 bool needs_null_check,
548 bool use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +0000549
550 // Generate a read barrier for a heap reference within `instruction`
551 // using a slow path.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000552 //
553 // A read barrier for an object reference read from the heap is
554 // implemented as a call to the artReadBarrierSlow runtime entry
555 // point, which is passed the values in locations `ref`, `obj`, and
556 // `offset`:
557 //
558 // mirror::Object* artReadBarrierSlow(mirror::Object* ref,
559 // mirror::Object* obj,
560 // uint32_t offset);
561 //
562 // The `out` location contains the value returned by
563 // artReadBarrierSlow.
564 //
565 // When `index` is provided (i.e. for array accesses), the offset
566 // value passed to artReadBarrierSlow is adjusted to take `index`
567 // into account.
Roland Levillain44015862016-01-22 11:47:17 +0000568 void GenerateReadBarrierSlow(HInstruction* instruction,
569 Location out,
570 Location ref,
571 Location obj,
572 uint32_t offset,
573 Location index = Location::NoLocation());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000574
Roland Levillain44015862016-01-22 11:47:17 +0000575 // If read barriers are enabled, generate a read barrier for a heap
576 // reference using a slow path. If heap poisoning is enabled, also
577 // unpoison the reference in `out`.
578 void MaybeGenerateReadBarrierSlow(HInstruction* instruction,
579 Location out,
580 Location ref,
581 Location obj,
582 uint32_t offset,
583 Location index = Location::NoLocation());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000584
Roland Levillain44015862016-01-22 11:47:17 +0000585 // Generate a read barrier for a GC root within `instruction` using
586 // a slow path.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000587 //
588 // A read barrier for an object reference GC root is implemented as
589 // a call to the artReadBarrierForRootSlow runtime entry point,
590 // which is passed the value in location `root`:
591 //
592 // mirror::Object* artReadBarrierForRootSlow(GcRoot<mirror::Object>* root);
593 //
594 // The `out` location contains the value returned by
595 // artReadBarrierForRootSlow.
Roland Levillain44015862016-01-22 11:47:17 +0000596 void GenerateReadBarrierForRootSlow(HInstruction* instruction, Location out, Location root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000597
David Srbeckyc7098ff2016-02-09 14:30:11 +0000598 void GenerateNop();
599
Calin Juravle2ae48182016-03-16 14:05:09 +0000600 void GenerateImplicitNullCheck(HNullCheck* instruction);
601 void GenerateExplicitNullCheck(HNullCheck* instruction);
602
Alexandre Rames5319def2014-10-23 10:03:10 +0100603 private:
Vladimir Marko58155012015-08-19 12:49:41 +0000604 using Uint64ToLiteralMap = ArenaSafeMap<uint64_t, vixl::Literal<uint64_t>*>;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000605 using Uint32ToLiteralMap = ArenaSafeMap<uint32_t, vixl::Literal<uint32_t>*>;
Vladimir Marko58155012015-08-19 12:49:41 +0000606 using MethodToLiteralMap = ArenaSafeMap<MethodReference,
607 vixl::Literal<uint64_t>*,
608 MethodReferenceComparator>;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000609 using BootStringToLiteralMap = ArenaSafeMap<StringReference,
610 vixl::Literal<uint32_t>*,
611 StringReferenceValueComparator>;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100612 using BootTypeToLiteralMap = ArenaSafeMap<TypeReference,
613 vixl::Literal<uint32_t>*,
614 TypeReferenceValueComparator>;
Vladimir Marko58155012015-08-19 12:49:41 +0000615
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000616 vixl::Literal<uint32_t>* DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map);
Vladimir Marko58155012015-08-19 12:49:41 +0000617 vixl::Literal<uint64_t>* DeduplicateUint64Literal(uint64_t value);
618 vixl::Literal<uint64_t>* DeduplicateMethodLiteral(MethodReference target_method,
619 MethodToLiteralMap* map);
620 vixl::Literal<uint64_t>* DeduplicateMethodAddressLiteral(MethodReference target_method);
621 vixl::Literal<uint64_t>* DeduplicateMethodCodeLiteral(MethodReference target_method);
622
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000623 // The PcRelativePatchInfo is used for PC-relative addressing of dex cache arrays
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100624 // and boot image strings/types. The only difference is the interpretation of the
625 // offset_or_index.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000626 struct PcRelativePatchInfo {
627 PcRelativePatchInfo(const DexFile& dex_file, uint32_t off_or_idx)
628 : target_dex_file(dex_file), offset_or_index(off_or_idx), label(), pc_insn_label() { }
Vladimir Marko58155012015-08-19 12:49:41 +0000629
630 const DexFile& target_dex_file;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100631 // Either the dex cache array element offset or the string/type index.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000632 uint32_t offset_or_index;
Vladimir Marko58155012015-08-19 12:49:41 +0000633 vixl::Label label;
634 vixl::Label* pc_insn_label;
635 };
636
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000637 vixl::Label* NewPcRelativePatch(const DexFile& dex_file,
638 uint32_t offset_or_index,
639 vixl::Label* adrp_label,
640 ArenaDeque<PcRelativePatchInfo>* patches);
641
Zheng Xu3927c8b2015-11-18 17:46:25 +0800642 void EmitJumpTables();
643
Alexandre Rames5319def2014-10-23 10:03:10 +0100644 // Labels for each block that will be compiled.
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100645 // We use a deque so that the `vixl::Label` objects do not move in memory.
646 ArenaDeque<vixl::Label> block_labels_; // Indexed by block id.
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000647 vixl::Label frame_entry_label_;
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100648 ArenaVector<std::unique_ptr<JumpTableARM64>> jump_tables_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100649
650 LocationsBuilderARM64 location_builder_;
651 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000652 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100653 Arm64Assembler assembler_;
Serban Constantinescu579885a2015-02-22 20:51:33 +0000654 const Arm64InstructionSetFeatures& isa_features_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100655
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000656 // Deduplication map for 32-bit literals, used for non-patchable boot image addresses.
657 Uint32ToLiteralMap uint32_literals_;
658 // Deduplication map for 64-bit literals, used for non-patchable method address, method code
659 // or string dex cache address.
Vladimir Marko58155012015-08-19 12:49:41 +0000660 Uint64ToLiteralMap uint64_literals_;
661 // Method patch info, map MethodReference to a literal for method address and method code.
662 MethodToLiteralMap method_patches_;
663 MethodToLiteralMap call_patches_;
664 // Relative call patch info.
665 // Using ArenaDeque<> which retains element addresses on push/emplace_back().
666 ArenaDeque<MethodPatchInfo<vixl::Label>> relative_call_patches_;
667 // PC-relative DexCache access info.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000668 ArenaDeque<PcRelativePatchInfo> pc_relative_dex_cache_patches_;
669 // Deduplication map for boot string literals for kBootImageLinkTimeAddress.
670 BootStringToLiteralMap boot_image_string_patches_;
671 // PC-relative String patch info.
672 ArenaDeque<PcRelativePatchInfo> pc_relative_string_patches_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100673 // Deduplication map for boot type literals for kBootImageLinkTimeAddress.
674 BootTypeToLiteralMap boot_image_type_patches_;
675 // PC-relative type patch info.
676 ArenaDeque<PcRelativePatchInfo> pc_relative_type_patches_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000677 // Deduplication map for patchable boot image addresses.
678 Uint32ToLiteralMap boot_image_address_patches_;
Vladimir Marko58155012015-08-19 12:49:41 +0000679
Alexandre Rames5319def2014-10-23 10:03:10 +0100680 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
681};
682
Alexandre Rames3e69f162014-12-10 10:36:50 +0000683inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
684 return codegen_->GetAssembler();
685}
686
Alexandre Rames5319def2014-10-23 10:03:10 +0100687} // namespace arm64
688} // namespace art
689
690#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_