blob: 0e3d25f9aa004e883f9c6b0791c24e64bb67e65c [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
142class CodeGeneratorARM64 : public CodeGenerator {
143 public:
144 explicit CodeGeneratorARM64(HGraph* graph);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000145 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100146
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000147 void GenerateFrameEntry() OVERRIDE;
148 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100149
150 static const vixl::CPURegList& GetFramePreservedRegisters() {
151 static const vixl::CPURegList frame_preserved_regs =
152 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
153 return frame_preserved_regs;
154 }
155 static int GetFramePreservedRegistersSize() {
156 return GetFramePreservedRegisters().TotalSizeInBytes();
157 }
158
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000159 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100160
161 vixl::Label* GetLabelOf(HBasicBlock* block) const {
162 return block_labels_ + block->GetBlockId();
163 }
164
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000165 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000167 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100168 return kArm64WordSize;
169 }
170
Alexandre Rames67555f72014-11-18 10:55:16 +0000171 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
172 vixl::Label* block_entry_label = GetLabelOf(block);
173 DCHECK(block_entry_label->IsBound());
174 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000175 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100176
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000177 size_t FrameEntrySpillSize() const OVERRIDE;
178
179 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
180 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
181 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000182 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100183
184 // Emit a write barrier.
185 void MarkGCCard(vixl::Register object, vixl::Register value);
186
187 // Register allocation.
188
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000189 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100190 // AllocateFreeRegister() is only used when allocating registers locally
191 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000192 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100193
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000194 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100195
Alexandre Rames67555f72014-11-18 10:55:16 +0000196 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700197 UNUSED(stack_index);
198 UNUSED(reg_id);
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 LOG(INFO) << "CodeGeneratorARM64::SaveCoreRegister()";
200 return kArm64WordSize;
Alexandre Rames5319def2014-10-23 10:03:10 +0100201 }
202
Alexandre Rames67555f72014-11-18 10:55:16 +0000203 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700204 UNUSED(stack_index);
205 UNUSED(reg_id);
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 LOG(INFO) << "CodeGeneratorARM64::RestoreCoreRegister()";
207 return kArm64WordSize;
Alexandre Rames5319def2014-10-23 10:03:10 +0100208 }
209
210 // The number of registers that can be allocated. The register allocator may
211 // decide to reserve and not use a few of them.
212 // We do not consider registers sp, xzr, wzr. They are either not allocatable
213 // (xzr, wzr), or make for poor allocatable registers (sp alignment
214 // requirements, etc.). This also facilitates our task as all other registers
215 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000216 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
217 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100218 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
219
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000220 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
221 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000223 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 return InstructionSet::kArm64;
225 }
226
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000227 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 HGraph* graph = GetGraph();
229 int length = graph->GetBlocks().Size();
230 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
231 for (int i = 0; i < length; ++i) {
232 new(block_labels_ + i) vixl::Label();
233 }
234 }
235
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000236 void Finalize(CodeAllocator* allocator) OVERRIDE;
237
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000238 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000240 void MoveHelper(Location destination, Location source, Primitive::Type type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000241 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
242 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
243 void LoadCurrentMethod(vixl::Register current_method);
244
245 // Generate code to invoke a runtime entry point.
246 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000247
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000248 ParallelMoveResolver* GetMoveResolver() OVERRIDE {
249 UNIMPLEMENTED(INFO) << "TODO: MoveResolver";
250 return nullptr;
251 }
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 private:
254 // Labels for each block that will be compiled.
255 vixl::Label* block_labels_;
256
257 LocationsBuilderARM64 location_builder_;
258 InstructionCodeGeneratorARM64 instruction_visitor_;
259 Arm64Assembler assembler_;
260
261 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
262};
263
264} // namespace arm64
265} // namespace art
266
267#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_