blob: a40f27fafabf9ba0fc2d0c75160a30ed2bf9eca1 [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
47const vixl::Register wSuspend = vixl::w19; // Suspend Register
48const vixl::Register xSuspend = vixl::x19;
49
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);
Alexandre Rames5319def2014-10-23 10:03:10 +010052const vixl::CPURegList runtime_reserved_core_registers(tr, xSuspend, vixl::lr);
53const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
54 vixl::kXRegSize,
55 kArm64CalleeSaveRefSpills);
56
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);
113 void HandleBinaryOp(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);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133 void HandleInvoke(HInvoke* instr);
134
135 CodeGeneratorARM64* const codegen_;
136 InvokeDexCallingConventionVisitor parameter_visitor_;
137
138 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
139};
140
141class CodeGeneratorARM64 : public CodeGenerator {
142 public:
143 explicit CodeGeneratorARM64(HGraph* graph);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000144 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100145
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000146 void GenerateFrameEntry() OVERRIDE;
147 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100148
149 static const vixl::CPURegList& GetFramePreservedRegisters() {
150 static const vixl::CPURegList frame_preserved_regs =
151 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
152 return frame_preserved_regs;
153 }
154 static int GetFramePreservedRegistersSize() {
155 return GetFramePreservedRegisters().TotalSizeInBytes();
156 }
157
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000158 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100159
160 vixl::Label* GetLabelOf(HBasicBlock* block) const {
161 return block_labels_ + block->GetBlockId();
162 }
163
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000164 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100165
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000166 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100167 return kArm64WordSize;
168 }
169
Alexandre Rames67555f72014-11-18 10:55:16 +0000170 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
171 vixl::Label* block_entry_label = GetLabelOf(block);
172 DCHECK(block_entry_label->IsBound());
173 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000174 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100175
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000176 size_t FrameEntrySpillSize() const OVERRIDE;
177
178 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
179 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
180 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000181 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100182
183 // Emit a write barrier.
184 void MarkGCCard(vixl::Register object, vixl::Register value);
185
186 // Register allocation.
187
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000188 void SetupBlockedRegisters() const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100189 // AllocateFreeRegister() is only used when allocating registers locally
190 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000191 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100192
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000193 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100194
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700196 UNUSED(stack_index);
197 UNUSED(reg_id);
Alexandre Rames67555f72014-11-18 10:55:16 +0000198 LOG(INFO) << "CodeGeneratorARM64::SaveCoreRegister()";
199 return kArm64WordSize;
Alexandre Rames5319def2014-10-23 10:03:10 +0100200 }
201
Alexandre Rames67555f72014-11-18 10:55:16 +0000202 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700203 UNUSED(stack_index);
204 UNUSED(reg_id);
Alexandre Rames67555f72014-11-18 10:55:16 +0000205 LOG(INFO) << "CodeGeneratorARM64::RestoreCoreRegister()";
206 return kArm64WordSize;
Alexandre Rames5319def2014-10-23 10:03:10 +0100207 }
208
209 // The number of registers that can be allocated. The register allocator may
210 // decide to reserve and not use a few of them.
211 // We do not consider registers sp, xzr, wzr. They are either not allocatable
212 // (xzr, wzr), or make for poor allocatable registers (sp alignment
213 // requirements, etc.). This also facilitates our task as all other registers
214 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000215 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
216 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100217 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
218
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000219 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
220 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100221
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000222 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100223 return InstructionSet::kArm64;
224 }
225
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000226 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 HGraph* graph = GetGraph();
228 int length = graph->GetBlocks().Size();
229 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
230 for (int i = 0; i < length; ++i) {
231 new(block_labels_ + i) vixl::Label();
232 }
233 }
234
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000235 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000237 void MoveHelper(Location destination, Location source, Primitive::Type type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000238 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
239 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
240 void LoadCurrentMethod(vixl::Register current_method);
241
242 // Generate code to invoke a runtime entry point.
243 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000244
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000245 ParallelMoveResolver* GetMoveResolver() OVERRIDE {
246 UNIMPLEMENTED(INFO) << "TODO: MoveResolver";
247 return nullptr;
248 }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
251 // Labels for each block that will be compiled.
252 vixl::Label* block_labels_;
253
254 LocationsBuilderARM64 location_builder_;
255 InstructionCodeGeneratorARM64 instruction_visitor_;
256 Arm64Assembler assembler_;
257
258 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
259};
260
261} // namespace arm64
262} // namespace art
263
264#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_