blob: e20d02ee2dff70774ace2f6fb7d9ce6ad7b9406a [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
20#include "code_generator.h"
Serban Constantinescu02d81cc2015-01-05 16:08:49 +000021#include "dex/compiler_enums.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000022#include "driver/compiler_options.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "nodes.h"
24#include "parallel_move_resolver.h"
25#include "utils/arm64/assembler_arm64.h"
26#include "a64/disasm-a64.h"
27#include "a64/macro-assembler-a64.h"
28#include "arch/arm64/quick_method_frame_info_arm64.h"
29
30namespace art {
31namespace arm64 {
32
33class CodeGeneratorARM64;
Alexandre Rames67555f72014-11-18 10:55:16 +000034class SlowPathCodeARM64;
Alexandre Rames5319def2014-10-23 10:03:10 +010035
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000036// Use a local definition to prevent copying mistakes.
37static constexpr size_t kArm64WordSize = kArm64PointerSize;
38
Alexandre Rames5319def2014-10-23 10:03:10 +010039static const vixl::Register kParameterCoreRegisters[] = {
40 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
41};
42static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
43static const vixl::FPRegister kParameterFPRegisters[] = {
44 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
45};
46static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
47
48const vixl::Register tr = vixl::x18; // Thread Register
Alexandre Rames5319def2014-10-23 10:03:10 +010049
50const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +000051const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31);
Serban Constantinescu02164b32014-11-13 14:05:07 +000052const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +000053const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
54 vixl::kXRegSize,
55 kArm64CalleeSaveRefSpills);
Alexandre Rames5319def2014-10-23 10:03:10 +010056
Alexandre Ramesa89086e2014-11-07 17:13:25 +000057Location ARM64ReturnLocation(Primitive::Type return_type);
58
Alexandre Rames5319def2014-10-23 10:03:10 +010059class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
60 public:
61 InvokeDexCallingConvention()
62 : CallingConvention(kParameterCoreRegisters,
63 kParameterCoreRegistersLength,
64 kParameterFPRegisters,
65 kParameterFPRegistersLength) {}
66
67 Location GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000068 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +010069 }
70
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
74};
75
76class InvokeDexCallingConventionVisitor {
77 public:
Alexandre Ramesa89086e2014-11-07 17:13:25 +000078 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Alexandre Rames5319def2014-10-23 10:03:10 +010079
80 Location GetNextLocation(Primitive::Type type);
81 Location GetReturnLocation(Primitive::Type return_type) {
82 return calling_convention.GetReturnLocation(return_type);
83 }
84
85 private:
86 InvokeDexCallingConvention calling_convention;
87 // The current index for core registers.
88 uint32_t gp_index_;
Alexandre Ramesa89086e2014-11-07 17:13:25 +000089 // The current index for floating-point registers.
90 uint32_t fp_index_;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 // The current stack index.
92 uint32_t stack_index_;
93
94 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
95};
96
97class InstructionCodeGeneratorARM64 : public HGraphVisitor {
98 public:
99 InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen);
100
101#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000102 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100103 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
104#undef DECLARE_VISIT_INSTRUCTION
105
106 void LoadCurrentMethod(XRegister reg);
107
108 Arm64Assembler* GetAssembler() const { return assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000109 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100110
111 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000112 void GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path, vixl::Register class_reg);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000113 void GenerateMemoryBarrier(MemBarrierKind kind);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000114 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000116 void HandleShift(HBinaryOperation* instr);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000117 void GenerateImplicitNullCheck(HNullCheck* instruction);
118 void GenerateExplicitNullCheck(HNullCheck* instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +0100119
120 Arm64Assembler* const assembler_;
121 CodeGeneratorARM64* const codegen_;
122
123 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
124};
125
126class LocationsBuilderARM64 : public HGraphVisitor {
127 public:
128 explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
129 : HGraphVisitor(graph), codegen_(codegen) {}
130
131#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000132 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100133 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
134#undef DECLARE_VISIT_INSTRUCTION
135
136 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000137 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000138 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100139 void HandleInvoke(HInvoke* instr);
140
141 CodeGeneratorARM64* const codegen_;
142 InvokeDexCallingConventionVisitor parameter_visitor_;
143
144 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
145};
146
Alexandre Rames3e69f162014-12-10 10:36:50 +0000147class ParallelMoveResolverARM64 : public ParallelMoveResolver {
148 public:
149 ParallelMoveResolverARM64(ArenaAllocator* allocator, CodeGeneratorARM64* codegen)
150 : ParallelMoveResolver(allocator), codegen_(codegen) {}
151
152 void EmitMove(size_t index) OVERRIDE;
153 void EmitSwap(size_t index) OVERRIDE;
154 void RestoreScratch(int reg) OVERRIDE;
155 void SpillScratch(int reg) OVERRIDE;
156
157 private:
158 Arm64Assembler* GetAssembler() const;
159 vixl::MacroAssembler* GetVIXLAssembler() const {
160 return GetAssembler()->vixl_masm_;
161 }
162
163 CodeGeneratorARM64* const codegen_;
164
165 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARM64);
166};
167
Alexandre Rames5319def2014-10-23 10:03:10 +0100168class CodeGeneratorARM64 : public CodeGenerator {
169 public:
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000170 CodeGeneratorARM64(HGraph* graph, const CompilerOptions& compiler_options);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000171 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100172
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000173 void GenerateFrameEntry() OVERRIDE;
174 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100175
176 static const vixl::CPURegList& GetFramePreservedRegisters() {
177 static const vixl::CPURegList frame_preserved_regs =
178 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
179 return frame_preserved_regs;
180 }
181 static int GetFramePreservedRegistersSize() {
182 return GetFramePreservedRegisters().TotalSizeInBytes();
183 }
184
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000185 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100186
187 vixl::Label* GetLabelOf(HBasicBlock* block) const {
188 return block_labels_ + block->GetBlockId();
189 }
190
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000191 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100192
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000193 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100194 return kArm64WordSize;
195 }
196
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500197 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
198 // Allocated in D registers, which are word sized.
199 return kArm64WordSize;
200 }
201
Alexandre Rames67555f72014-11-18 10:55:16 +0000202 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
203 vixl::Label* block_entry_label = GetLabelOf(block);
204 DCHECK(block_entry_label->IsBound());
205 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000206 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100207
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000208 size_t FrameEntrySpillSize() const OVERRIDE;
209
210 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
211 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
212 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000213 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100214
215 // Emit a write barrier.
216 void MarkGCCard(vixl::Register object, vixl::Register value);
217
218 // Register allocation.
219
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000220 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100221 // AllocateFreeRegister() is only used when allocating registers locally
222 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000223 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100224
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000225 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100226
Alexandre Rames3e69f162014-12-10 10:36:50 +0000227 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
228 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
229 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
230 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100231
232 // The number of registers that can be allocated. The register allocator may
233 // decide to reserve and not use a few of them.
234 // We do not consider registers sp, xzr, wzr. They are either not allocatable
235 // (xzr, wzr), or make for poor allocatable registers (sp alignment
236 // requirements, etc.). This also facilitates our task as all other registers
237 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000238 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
239 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100240 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000242 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
243 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100244
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000245 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100246 return InstructionSet::kArm64;
247 }
248
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000249 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 HGraph* graph = GetGraph();
251 int length = graph->GetBlocks().Size();
252 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
253 for (int i = 0; i < length; ++i) {
254 new(block_labels_ + i) vixl::Label();
255 }
256 }
257
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000258 void Finalize(CodeAllocator* allocator) OVERRIDE;
259
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000260 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000261 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000262 // The type is optional. When specified it must be coherent with the
263 // locations, and is used for optimisation and debugging.
264 void MoveLocation(Location destination, Location source,
265 Primitive::Type type = Primitive::kPrimVoid);
266 void SwapLocations(Location loc_1, Location loc_2);
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
268 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
269 void LoadCurrentMethod(vixl::Register current_method);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000270 void LoadAcquire(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
271 void StoreRelease(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000272
273 // Generate code to invoke a runtime entry point.
274 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000275
Alexandre Rames3e69f162014-12-10 10:36:50 +0000276 ParallelMoveResolverARM64* GetMoveResolver() { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000277
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000278 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
279 return false;
280 }
281
Alexandre Rames5319def2014-10-23 10:03:10 +0100282 private:
283 // Labels for each block that will be compiled.
284 vixl::Label* block_labels_;
285
286 LocationsBuilderARM64 location_builder_;
287 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000288 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100289 Arm64Assembler assembler_;
290
291 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
292};
293
Alexandre Rames3e69f162014-12-10 10:36:50 +0000294inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
295 return codegen_->GetAssembler();
296}
297
Alexandre Rames5319def2014-10-23 10:03:10 +0100298} // namespace arm64
299} // namespace art
300
301#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_