blob: 1d5bfb734eee9b38a59ea3585c979878bcc98eb1 [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"
21#include "nodes.h"
22#include "parallel_move_resolver.h"
23#include "utils/arm64/assembler_arm64.h"
24#include "a64/disasm-a64.h"
25#include "a64/macro-assembler-a64.h"
26#include "arch/arm64/quick_method_frame_info_arm64.h"
27
28namespace art {
29namespace arm64 {
30
31class CodeGeneratorARM64;
Alexandre Rames67555f72014-11-18 10:55:16 +000032class SlowPathCodeARM64;
Alexandre Rames5319def2014-10-23 10:03:10 +010033
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000034// Use a local definition to prevent copying mistakes.
35static constexpr size_t kArm64WordSize = kArm64PointerSize;
36
Alexandre Rames5319def2014-10-23 10:03:10 +010037static const vixl::Register kParameterCoreRegisters[] = {
38 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
39};
40static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
41static const vixl::FPRegister kParameterFPRegisters[] = {
42 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
43};
44static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
45
46const vixl::Register tr = vixl::x18; // Thread Register
Alexandre Rames5319def2014-10-23 10:03:10 +010047
48const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +000049const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31);
Serban Constantinescu02164b32014-11-13 14:05:07 +000050const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr);
Alexandre Rames5319def2014-10-23 10:03:10 +010051const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
52 vixl::kXRegSize,
53 kArm64CalleeSaveRefSpills);
54
Alexandre Ramesa89086e2014-11-07 17:13:25 +000055Location ARM64ReturnLocation(Primitive::Type return_type);
56
Alexandre Rames5319def2014-10-23 10:03:10 +010057class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
58 public:
59 InvokeDexCallingConvention()
60 : CallingConvention(kParameterCoreRegisters,
61 kParameterCoreRegistersLength,
62 kParameterFPRegisters,
63 kParameterFPRegistersLength) {}
64
65 Location GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000066 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +010067 }
68
69
70 private:
71 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
72};
73
74class InvokeDexCallingConventionVisitor {
75 public:
Alexandre Ramesa89086e2014-11-07 17:13:25 +000076 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Alexandre Rames5319def2014-10-23 10:03:10 +010077
78 Location GetNextLocation(Primitive::Type type);
79 Location GetReturnLocation(Primitive::Type return_type) {
80 return calling_convention.GetReturnLocation(return_type);
81 }
82
83 private:
84 InvokeDexCallingConvention calling_convention;
85 // The current index for core registers.
86 uint32_t gp_index_;
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // The current index for floating-point registers.
88 uint32_t fp_index_;
Alexandre Rames5319def2014-10-23 10:03:10 +010089 // The current stack index.
90 uint32_t stack_index_;
91
92 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
93};
94
95class InstructionCodeGeneratorARM64 : public HGraphVisitor {
96 public:
97 InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen);
98
99#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000100 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100101 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
102#undef DECLARE_VISIT_INSTRUCTION
103
104 void LoadCurrentMethod(XRegister reg);
105
106 Arm64Assembler* GetAssembler() const { return assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000107 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
109 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000110 void GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path, vixl::Register class_reg);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000111 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Alexandre Rames67555f72014-11-18 10:55:16 +0000112 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000113 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100114
115 Arm64Assembler* const assembler_;
116 CodeGeneratorARM64* const codegen_;
117
118 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
119};
120
121class LocationsBuilderARM64 : public HGraphVisitor {
122 public:
123 explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
124 : HGraphVisitor(graph), codegen_(codegen) {}
125
126#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000127 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100128 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
129#undef DECLARE_VISIT_INSTRUCTION
130
131 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000132 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000133 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134 void HandleInvoke(HInvoke* instr);
135
136 CodeGeneratorARM64* const codegen_;
137 InvokeDexCallingConventionVisitor parameter_visitor_;
138
139 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
140};
141
Alexandre Rames3e69f162014-12-10 10:36:50 +0000142class ParallelMoveResolverARM64 : public ParallelMoveResolver {
143 public:
144 ParallelMoveResolverARM64(ArenaAllocator* allocator, CodeGeneratorARM64* codegen)
145 : ParallelMoveResolver(allocator), codegen_(codegen) {}
146
147 void EmitMove(size_t index) OVERRIDE;
148 void EmitSwap(size_t index) OVERRIDE;
149 void RestoreScratch(int reg) OVERRIDE;
150 void SpillScratch(int reg) OVERRIDE;
151
152 private:
153 Arm64Assembler* GetAssembler() const;
154 vixl::MacroAssembler* GetVIXLAssembler() const {
155 return GetAssembler()->vixl_masm_;
156 }
157
158 CodeGeneratorARM64* const codegen_;
159
160 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARM64);
161};
162
Alexandre Rames5319def2014-10-23 10:03:10 +0100163class CodeGeneratorARM64 : public CodeGenerator {
164 public:
165 explicit CodeGeneratorARM64(HGraph* graph);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000166 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100167
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000168 void GenerateFrameEntry() OVERRIDE;
169 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100170
171 static const vixl::CPURegList& GetFramePreservedRegisters() {
172 static const vixl::CPURegList frame_preserved_regs =
173 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
174 return frame_preserved_regs;
175 }
176 static int GetFramePreservedRegistersSize() {
177 return GetFramePreservedRegisters().TotalSizeInBytes();
178 }
179
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000180 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100181
182 vixl::Label* GetLabelOf(HBasicBlock* block) const {
183 return block_labels_ + block->GetBlockId();
184 }
185
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000186 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100187
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000188 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100189 return kArm64WordSize;
190 }
191
Alexandre Rames67555f72014-11-18 10:55:16 +0000192 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
193 vixl::Label* block_entry_label = GetLabelOf(block);
194 DCHECK(block_entry_label->IsBound());
195 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000196 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100197
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000198 size_t FrameEntrySpillSize() const OVERRIDE;
199
200 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
201 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
202 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000203 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100204
205 // Emit a write barrier.
206 void MarkGCCard(vixl::Register object, vixl::Register value);
207
208 // Register allocation.
209
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000210 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100211 // AllocateFreeRegister() is only used when allocating registers locally
212 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000213 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100214
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000215 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100216
Alexandre Rames3e69f162014-12-10 10:36:50 +0000217 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
218 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
219 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
220 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100221
222 // The number of registers that can be allocated. The register allocator may
223 // decide to reserve and not use a few of them.
224 // We do not consider registers sp, xzr, wzr. They are either not allocatable
225 // (xzr, wzr), or make for poor allocatable registers (sp alignment
226 // requirements, etc.). This also facilitates our task as all other registers
227 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000228 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
229 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
231
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000232 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
233 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100234
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000235 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100236 return InstructionSet::kArm64;
237 }
238
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000239 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100240 HGraph* graph = GetGraph();
241 int length = graph->GetBlocks().Size();
242 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
243 for (int i = 0; i < length; ++i) {
244 new(block_labels_ + i) vixl::Label();
245 }
246 }
247
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000248 void Finalize(CodeAllocator* allocator) OVERRIDE;
249
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000250 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000252 // The type is optional. When specified it must be coherent with the
253 // locations, and is used for optimisation and debugging.
254 void MoveLocation(Location destination, Location source,
255 Primitive::Type type = Primitive::kPrimVoid);
256 void SwapLocations(Location loc_1, Location loc_2);
Alexandre Rames67555f72014-11-18 10:55:16 +0000257 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
258 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
259 void LoadCurrentMethod(vixl::Register current_method);
260
261 // Generate code to invoke a runtime entry point.
262 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000263
Alexandre Rames3e69f162014-12-10 10:36:50 +0000264 ParallelMoveResolverARM64* GetMoveResolver() { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000265
Alexandre Rames5319def2014-10-23 10:03:10 +0100266 private:
267 // Labels for each block that will be compiled.
268 vixl::Label* block_labels_;
269
270 LocationsBuilderARM64 location_builder_;
271 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000272 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100273 Arm64Assembler assembler_;
274
275 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
276};
277
Alexandre Rames3e69f162014-12-10 10:36:50 +0000278inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
279 return codegen_->GetAssembler();
280}
281
Alexandre Rames5319def2014-10-23 10:03:10 +0100282} // namespace arm64
283} // namespace art
284
285#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_