blob: 590bc1d778d62794ea5115fe1ad2533649953f81 [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
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500194 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
195 // Allocated in D registers, which are word sized.
196 return kArm64WordSize;
197 }
198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
200 vixl::Label* block_entry_label = GetLabelOf(block);
201 DCHECK(block_entry_label->IsBound());
202 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000203 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000205 size_t FrameEntrySpillSize() const OVERRIDE;
206
207 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
208 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
209 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000210 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100211
212 // Emit a write barrier.
213 void MarkGCCard(vixl::Register object, vixl::Register value);
214
215 // Register allocation.
216
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000217 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100218 // AllocateFreeRegister() is only used when allocating registers locally
219 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000220 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100221
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000222 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames3e69f162014-12-10 10:36:50 +0000224 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
225 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
226 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
227 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100228
229 // The number of registers that can be allocated. The register allocator may
230 // decide to reserve and not use a few of them.
231 // We do not consider registers sp, xzr, wzr. They are either not allocatable
232 // (xzr, wzr), or make for poor allocatable registers (sp alignment
233 // requirements, etc.). This also facilitates our task as all other registers
234 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000235 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
236 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100237 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
238
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000239 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
240 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000242 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100243 return InstructionSet::kArm64;
244 }
245
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000246 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100247 HGraph* graph = GetGraph();
248 int length = graph->GetBlocks().Size();
249 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
250 for (int i = 0; i < length; ++i) {
251 new(block_labels_ + i) vixl::Label();
252 }
253 }
254
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000255 void Finalize(CodeAllocator* allocator) OVERRIDE;
256
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000257 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000258 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000259 // The type is optional. When specified it must be coherent with the
260 // locations, and is used for optimisation and debugging.
261 void MoveLocation(Location destination, Location source,
262 Primitive::Type type = Primitive::kPrimVoid);
263 void SwapLocations(Location loc_1, Location loc_2);
Alexandre Rames67555f72014-11-18 10:55:16 +0000264 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
265 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
266 void LoadCurrentMethod(vixl::Register current_method);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000267 void LoadAcquire(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
268 void StoreRelease(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000269
270 // Generate code to invoke a runtime entry point.
271 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000272
Alexandre Rames3e69f162014-12-10 10:36:50 +0000273 ParallelMoveResolverARM64* GetMoveResolver() { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000274
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000275 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
276 return false;
277 }
278
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 private:
280 // Labels for each block that will be compiled.
281 vixl::Label* block_labels_;
282
283 LocationsBuilderARM64 location_builder_;
284 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000285 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100286 Arm64Assembler assembler_;
287
288 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
289};
290
Alexandre Rames3e69f162014-12-10 10:36:50 +0000291inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
292 return codegen_->GetAssembler();
293}
294
Alexandre Rames5319def2014-10-23 10:03:10 +0100295} // namespace arm64
296} // namespace art
297
298#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_