blob: e4da07be438b22243c677e4ee572d5f28c61e78b [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"
Alexandre Rames5319def2014-10-23 10:03:10 +010022#include "nodes.h"
23#include "parallel_move_resolver.h"
24#include "utils/arm64/assembler_arm64.h"
25#include "a64/disasm-a64.h"
26#include "a64/macro-assembler-a64.h"
27#include "arch/arm64/quick_method_frame_info_arm64.h"
28
29namespace art {
30namespace arm64 {
31
32class CodeGeneratorARM64;
Alexandre Rames67555f72014-11-18 10:55:16 +000033class SlowPathCodeARM64;
Alexandre Rames5319def2014-10-23 10:03:10 +010034
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000035// Use a local definition to prevent copying mistakes.
36static constexpr size_t kArm64WordSize = kArm64PointerSize;
37
Alexandre Rames5319def2014-10-23 10:03:10 +010038static const vixl::Register kParameterCoreRegisters[] = {
39 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
40};
41static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
42static const vixl::FPRegister kParameterFPRegisters[] = {
43 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
44};
45static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
46
47const vixl::Register tr = vixl::x18; // Thread Register
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +000050const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31);
Serban Constantinescu02164b32014-11-13 14:05:07 +000051const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +000052const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
53 vixl::kXRegSize,
54 kArm64CalleeSaveRefSpills);
Alexandre Rames5319def2014-10-23 10:03:10 +010055
Alexandre Ramesa89086e2014-11-07 17:13:25 +000056Location ARM64ReturnLocation(Primitive::Type return_type);
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
59 public:
60 InvokeDexCallingConvention()
61 : CallingConvention(kParameterCoreRegisters,
62 kParameterCoreRegistersLength,
63 kParameterFPRegisters,
64 kParameterFPRegistersLength) {}
65
66 Location GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000067 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +010068 }
69
70
71 private:
72 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
73};
74
75class InvokeDexCallingConventionVisitor {
76 public:
Alexandre Ramesa89086e2014-11-07 17:13:25 +000077 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Alexandre Rames5319def2014-10-23 10:03:10 +010078
79 Location GetNextLocation(Primitive::Type type);
80 Location GetReturnLocation(Primitive::Type return_type) {
81 return calling_convention.GetReturnLocation(return_type);
82 }
83
84 private:
85 InvokeDexCallingConvention calling_convention;
86 // The current index for core registers.
87 uint32_t gp_index_;
Alexandre Ramesa89086e2014-11-07 17:13:25 +000088 // The current index for floating-point registers.
89 uint32_t fp_index_;
Alexandre Rames5319def2014-10-23 10:03:10 +010090 // The current stack index.
91 uint32_t stack_index_;
92
93 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
94};
95
96class InstructionCodeGeneratorARM64 : public HGraphVisitor {
97 public:
98 InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen);
99
100#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000101 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100102 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
103#undef DECLARE_VISIT_INSTRUCTION
104
105 void LoadCurrentMethod(XRegister reg);
106
107 Arm64Assembler* GetAssembler() const { return assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000108 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
110 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000111 void GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path, vixl::Register class_reg);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000112 void GenerateMemoryBarrier(MemBarrierKind kind);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000113 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Alexandre Rames67555f72014-11-18 10:55:16 +0000114 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000115 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100116
117 Arm64Assembler* const assembler_;
118 CodeGeneratorARM64* const codegen_;
119
120 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
121};
122
123class LocationsBuilderARM64 : public HGraphVisitor {
124 public:
125 explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
126 : HGraphVisitor(graph), codegen_(codegen) {}
127
128#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000129 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100130 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
131#undef DECLARE_VISIT_INSTRUCTION
132
133 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000134 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000135 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100136 void HandleInvoke(HInvoke* instr);
137
138 CodeGeneratorARM64* const codegen_;
139 InvokeDexCallingConventionVisitor parameter_visitor_;
140
141 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
142};
143
Alexandre Rames3e69f162014-12-10 10:36:50 +0000144class ParallelMoveResolverARM64 : public ParallelMoveResolver {
145 public:
146 ParallelMoveResolverARM64(ArenaAllocator* allocator, CodeGeneratorARM64* codegen)
147 : ParallelMoveResolver(allocator), codegen_(codegen) {}
148
149 void EmitMove(size_t index) OVERRIDE;
150 void EmitSwap(size_t index) OVERRIDE;
151 void RestoreScratch(int reg) OVERRIDE;
152 void SpillScratch(int reg) OVERRIDE;
153
154 private:
155 Arm64Assembler* GetAssembler() const;
156 vixl::MacroAssembler* GetVIXLAssembler() const {
157 return GetAssembler()->vixl_masm_;
158 }
159
160 CodeGeneratorARM64* const codegen_;
161
162 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARM64);
163};
164
Alexandre Rames5319def2014-10-23 10:03:10 +0100165class CodeGeneratorARM64 : public CodeGenerator {
166 public:
167 explicit CodeGeneratorARM64(HGraph* graph);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000168 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100169
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000170 void GenerateFrameEntry() OVERRIDE;
171 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100172
173 static const vixl::CPURegList& GetFramePreservedRegisters() {
174 static const vixl::CPURegList frame_preserved_regs =
175 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
176 return frame_preserved_regs;
177 }
178 static int GetFramePreservedRegistersSize() {
179 return GetFramePreservedRegisters().TotalSizeInBytes();
180 }
181
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000182 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100183
184 vixl::Label* GetLabelOf(HBasicBlock* block) const {
185 return block_labels_ + block->GetBlockId();
186 }
187
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000188 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100189
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000190 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100191 return kArm64WordSize;
192 }
193
Alexandre Rames67555f72014-11-18 10:55:16 +0000194 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
195 vixl::Label* block_entry_label = GetLabelOf(block);
196 DCHECK(block_entry_label->IsBound());
197 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000198 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100199
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000200 size_t FrameEntrySpillSize() const OVERRIDE;
201
202 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
203 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
204 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000205 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100206
207 // Emit a write barrier.
208 void MarkGCCard(vixl::Register object, vixl::Register value);
209
210 // Register allocation.
211
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000212 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 // AllocateFreeRegister() is only used when allocating registers locally
214 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000215 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100216
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000217 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100218
Alexandre Rames3e69f162014-12-10 10:36:50 +0000219 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
220 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
221 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
222 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
224 // The number of registers that can be allocated. The register allocator may
225 // decide to reserve and not use a few of them.
226 // We do not consider registers sp, xzr, wzr. They are either not allocatable
227 // (xzr, wzr), or make for poor allocatable registers (sp alignment
228 // requirements, etc.). This also facilitates our task as all other registers
229 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000230 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
231 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100232 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
233
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000234 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
235 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100236
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000237 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100238 return InstructionSet::kArm64;
239 }
240
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000241 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100242 HGraph* graph = GetGraph();
243 int length = graph->GetBlocks().Size();
244 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
245 for (int i = 0; i < length; ++i) {
246 new(block_labels_ + i) vixl::Label();
247 }
248 }
249
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000250 void Finalize(CodeAllocator* allocator) OVERRIDE;
251
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000252 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000254 // The type is optional. When specified it must be coherent with the
255 // locations, and is used for optimisation and debugging.
256 void MoveLocation(Location destination, Location source,
257 Primitive::Type type = Primitive::kPrimVoid);
258 void SwapLocations(Location loc_1, Location loc_2);
Alexandre Rames67555f72014-11-18 10:55:16 +0000259 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
260 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
261 void LoadCurrentMethod(vixl::Register current_method);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000262 void LoadAcquire(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
263 void StoreRelease(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000264
265 // Generate code to invoke a runtime entry point.
266 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000267
Alexandre Rames3e69f162014-12-10 10:36:50 +0000268 ParallelMoveResolverARM64* GetMoveResolver() { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000269
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000270 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
271 return false;
272 }
273
Alexandre Rames5319def2014-10-23 10:03:10 +0100274 private:
275 // Labels for each block that will be compiled.
276 vixl::Label* block_labels_;
277
278 LocationsBuilderARM64 location_builder_;
279 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000280 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 Arm64Assembler assembler_;
282
283 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
284};
285
Alexandre Rames3e69f162014-12-10 10:36:50 +0000286inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
287 return codegen_->GetAssembler();
288}
289
Alexandre Rames5319def2014-10-23 10:03:10 +0100290} // namespace arm64
291} // namespace art
292
293#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_