blob: 96013e55c604c56e5da011545b1372b7358b979d [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"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000022#include "driver/compiler_options.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "nodes.h"
24#include "parallel_move_resolver.h"
25#include "utils/arm64/assembler_arm64.h"
26#include "a64/disasm-a64.h"
27#include "a64/macro-assembler-a64.h"
28#include "arch/arm64/quick_method_frame_info_arm64.h"
29
30namespace art {
31namespace arm64 {
32
33class CodeGeneratorARM64;
Alexandre Rames67555f72014-11-18 10:55:16 +000034class SlowPathCodeARM64;
Alexandre Rames5319def2014-10-23 10:03:10 +010035
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000036// Use a local definition to prevent copying mistakes.
37static constexpr size_t kArm64WordSize = kArm64PointerSize;
38
Alexandre Rames5319def2014-10-23 10:03:10 +010039static const vixl::Register kParameterCoreRegisters[] = {
40 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
41};
42static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
43static const vixl::FPRegister kParameterFPRegisters[] = {
44 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
45};
46static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
47
48const vixl::Register tr = vixl::x18; // Thread Register
Alexandre Rames5319def2014-10-23 10:03:10 +010049
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);
Serban Constantinescu02164b32014-11-13 14:05:07 +000052const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +000053const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
54 vixl::kXRegSize,
55 kArm64CalleeSaveRefSpills);
Alexandre Rames5319def2014-10-23 10:03:10 +010056
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);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000113 void GenerateMemoryBarrier(MemBarrierKind kind);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000114 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000116 void HandleShift(HBinaryOperation* instr);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000117 void GenerateImplicitNullCheck(HNullCheck* instruction);
118 void GenerateExplicitNullCheck(HNullCheck* instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +0100119
120 Arm64Assembler* const assembler_;
121 CodeGeneratorARM64* const codegen_;
122
123 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
124};
125
126class LocationsBuilderARM64 : public HGraphVisitor {
127 public:
128 explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
129 : HGraphVisitor(graph), codegen_(codegen) {}
130
131#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000132 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100133 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
134#undef DECLARE_VISIT_INSTRUCTION
135
136 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000137 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000138 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100139 void HandleInvoke(HInvoke* instr);
140
141 CodeGeneratorARM64* const codegen_;
142 InvokeDexCallingConventionVisitor parameter_visitor_;
143
144 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
145};
146
Alexandre Rames3e69f162014-12-10 10:36:50 +0000147class ParallelMoveResolverARM64 : public ParallelMoveResolver {
148 public:
149 ParallelMoveResolverARM64(ArenaAllocator* allocator, CodeGeneratorARM64* codegen)
150 : ParallelMoveResolver(allocator), codegen_(codegen) {}
151
152 void EmitMove(size_t index) OVERRIDE;
153 void EmitSwap(size_t index) OVERRIDE;
154 void RestoreScratch(int reg) OVERRIDE;
155 void SpillScratch(int reg) OVERRIDE;
156
157 private:
158 Arm64Assembler* GetAssembler() const;
159 vixl::MacroAssembler* GetVIXLAssembler() const {
160 return GetAssembler()->vixl_masm_;
161 }
162
163 CodeGeneratorARM64* const codegen_;
164
165 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARM64);
166};
167
Alexandre Rames5319def2014-10-23 10:03:10 +0100168class CodeGeneratorARM64 : public CodeGenerator {
169 public:
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000170 CodeGeneratorARM64(HGraph* graph, const CompilerOptions& compiler_options);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000171 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100172
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000173 void GenerateFrameEntry() OVERRIDE;
174 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100175
176 static const vixl::CPURegList& GetFramePreservedRegisters() {
177 static const vixl::CPURegList frame_preserved_regs =
178 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
179 return frame_preserved_regs;
180 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100181
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 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
206 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
207 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000208 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100209
210 // Emit a write barrier.
211 void MarkGCCard(vixl::Register object, vixl::Register value);
212
213 // Register allocation.
214
Nicolas Geoffray98893962015-01-21 12:32:32 +0000215 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100216 // AllocateFreeRegister() is only used when allocating registers locally
217 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000218 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100219
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000220 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100221
Alexandre Rames3e69f162014-12-10 10:36:50 +0000222 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
223 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
224 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
225 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100226
227 // The number of registers that can be allocated. The register allocator may
228 // decide to reserve and not use a few of them.
229 // We do not consider registers sp, xzr, wzr. They are either not allocatable
230 // (xzr, wzr), or make for poor allocatable registers (sp alignment
231 // requirements, etc.). This also facilitates our task as all other registers
232 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000233 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
234 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100235 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
236
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000237 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
238 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100239
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000240 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100241 return InstructionSet::kArm64;
242 }
243
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000244 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100245 HGraph* graph = GetGraph();
246 int length = graph->GetBlocks().Size();
247 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
248 for (int i = 0; i < length; ++i) {
249 new(block_labels_ + i) vixl::Label();
250 }
251 }
252
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000253 void Finalize(CodeAllocator* allocator) OVERRIDE;
254
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000255 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000257 // The type is optional. When specified it must be coherent with the
258 // locations, and is used for optimisation and debugging.
259 void MoveLocation(Location destination, Location source,
260 Primitive::Type type = Primitive::kPrimVoid);
261 void SwapLocations(Location loc_1, Location loc_2);
Alexandre Rames67555f72014-11-18 10:55:16 +0000262 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
263 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
264 void LoadCurrentMethod(vixl::Register current_method);
Calin Juravle77520bc2015-01-12 18:45:46 +0000265 void LoadAcquire(HInstruction* instruction, vixl::CPURegister dst, const vixl::MemOperand& src);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000266 void StoreRelease(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000267
268 // Generate code to invoke a runtime entry point.
269 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000270
Alexandre Rames3e69f162014-12-10 10:36:50 +0000271 ParallelMoveResolverARM64* GetMoveResolver() { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000272
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000273 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
274 return false;
275 }
276
Alexandre Rames5319def2014-10-23 10:03:10 +0100277 private:
278 // Labels for each block that will be compiled.
279 vixl::Label* block_labels_;
280
281 LocationsBuilderARM64 location_builder_;
282 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000283 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100284 Arm64Assembler assembler_;
285
286 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
287};
288
Alexandre Rames3e69f162014-12-10 10:36:50 +0000289inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
290 return codegen_->GetAssembler();
291}
292
Alexandre Rames5319def2014-10-23 10:03:10 +0100293} // namespace arm64
294} // namespace art
295
296#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_