blob: 100dafe7247e770af2d779ecf8d33ae37ef177ed [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;
Andreas Gampe878d58c2015-01-15 23:24:00 -080034
35// TODO: Tune the use of Load-Acquire, Store-Release vs Data Memory Barriers.
36// For now we prefer the use of load-acquire, store-release over explicit memory barriers.
37static constexpr bool kUseAcquireRelease = true;
Alexandre Rames5319def2014-10-23 10:03:10 +010038
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +000039// Use a local definition to prevent copying mistakes.
40static constexpr size_t kArm64WordSize = kArm64PointerSize;
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042static const vixl::Register kParameterCoreRegisters[] = {
43 vixl::x1, vixl::x2, vixl::x3, vixl::x4, vixl::x5, vixl::x6, vixl::x7
44};
45static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters);
46static const vixl::FPRegister kParameterFPRegisters[] = {
47 vixl::d0, vixl::d1, vixl::d2, vixl::d3, vixl::d4, vixl::d5, vixl::d6, vixl::d7
48};
49static constexpr size_t kParameterFPRegistersLength = arraysize(kParameterFPRegisters);
50
Andreas Gampe878d58c2015-01-15 23:24:00 -080051const vixl::Register tr = vixl::x18; // Thread Register
52static const vixl::Register kArtMethodRegister = vixl::w0; // Method register on invoke.
Alexandre Rames5319def2014-10-23 10:03:10 +010053
54const vixl::CPURegList vixl_reserved_core_registers(vixl::ip0, vixl::ip1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +000055const vixl::CPURegList vixl_reserved_fp_registers(vixl::d31);
Serban Constantinescu02164b32014-11-13 14:05:07 +000056const vixl::CPURegList runtime_reserved_core_registers(tr, vixl::lr);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +000057const vixl::CPURegList quick_callee_saved_registers(vixl::CPURegister::kRegister,
58 vixl::kXRegSize,
59 kArm64CalleeSaveRefSpills);
Alexandre Rames5319def2014-10-23 10:03:10 +010060
Alexandre Ramesa89086e2014-11-07 17:13:25 +000061Location ARM64ReturnLocation(Primitive::Type return_type);
62
Andreas Gampe878d58c2015-01-15 23:24:00 -080063class SlowPathCodeARM64 : public SlowPathCode {
64 public:
65 SlowPathCodeARM64() : entry_label_(), exit_label_() {}
66
67 vixl::Label* GetEntryLabel() { return &entry_label_; }
68 vixl::Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 vixl::Label entry_label_;
72 vixl::Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM64);
75};
76
Alexandre Rames5319def2014-10-23 10:03:10 +010077class InvokeDexCallingConvention : public CallingConvention<vixl::Register, vixl::FPRegister> {
78 public:
79 InvokeDexCallingConvention()
80 : CallingConvention(kParameterCoreRegisters,
81 kParameterCoreRegistersLength,
82 kParameterFPRegisters,
83 kParameterFPRegistersLength) {}
84
85 Location GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +010087 }
88
89
90 private:
91 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention);
92};
93
94class InvokeDexCallingConventionVisitor {
95 public:
Alexandre Ramesa89086e2014-11-07 17:13:25 +000096 InvokeDexCallingConventionVisitor() : gp_index_(0), fp_index_(0), stack_index_(0) {}
Alexandre Rames5319def2014-10-23 10:03:10 +010097
98 Location GetNextLocation(Primitive::Type type);
99 Location GetReturnLocation(Primitive::Type return_type) {
100 return calling_convention.GetReturnLocation(return_type);
101 }
102
103 private:
104 InvokeDexCallingConvention calling_convention;
105 // The current index for core registers.
106 uint32_t gp_index_;
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000107 // The current index for floating-point registers.
108 uint32_t fp_index_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100109 // The current stack index.
110 uint32_t stack_index_;
111
112 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor);
113};
114
115class InstructionCodeGeneratorARM64 : public HGraphVisitor {
116 public:
117 InstructionCodeGeneratorARM64(HGraph* graph, CodeGeneratorARM64* codegen);
118
119#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000120 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100121 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
122#undef DECLARE_VISIT_INSTRUCTION
123
124 void LoadCurrentMethod(XRegister reg);
125
126 Arm64Assembler* GetAssembler() const { return assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000127 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100128
129 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000130 void GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path, vixl::Register class_reg);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000131 void GenerateMemoryBarrier(MemBarrierKind kind);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000132 void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
Alexandre Rames67555f72014-11-18 10:55:16 +0000133 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000134 void HandleShift(HBinaryOperation* instr);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000135 void GenerateImplicitNullCheck(HNullCheck* instruction);
136 void GenerateExplicitNullCheck(HNullCheck* instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
138 Arm64Assembler* const assembler_;
139 CodeGeneratorARM64* const codegen_;
140
141 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorARM64);
142};
143
144class LocationsBuilderARM64 : public HGraphVisitor {
145 public:
146 explicit LocationsBuilderARM64(HGraph* graph, CodeGeneratorARM64* codegen)
147 : HGraphVisitor(graph), codegen_(codegen) {}
148
149#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000150 void Visit##name(H##name* instr) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100151 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
152#undef DECLARE_VISIT_INSTRUCTION
153
154 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000155 void HandleBinaryOp(HBinaryOperation* instr);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000156 void HandleShift(HBinaryOperation* instr);
Alexandre Rames5319def2014-10-23 10:03:10 +0100157 void HandleInvoke(HInvoke* instr);
158
159 CodeGeneratorARM64* const codegen_;
160 InvokeDexCallingConventionVisitor parameter_visitor_;
161
162 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderARM64);
163};
164
Alexandre Rames3e69f162014-12-10 10:36:50 +0000165class ParallelMoveResolverARM64 : public ParallelMoveResolver {
166 public:
167 ParallelMoveResolverARM64(ArenaAllocator* allocator, CodeGeneratorARM64* codegen)
168 : ParallelMoveResolver(allocator), codegen_(codegen) {}
169
170 void EmitMove(size_t index) OVERRIDE;
171 void EmitSwap(size_t index) OVERRIDE;
172 void RestoreScratch(int reg) OVERRIDE;
173 void SpillScratch(int reg) OVERRIDE;
174
175 private:
176 Arm64Assembler* GetAssembler() const;
177 vixl::MacroAssembler* GetVIXLAssembler() const {
178 return GetAssembler()->vixl_masm_;
179 }
180
181 CodeGeneratorARM64* const codegen_;
182
183 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverARM64);
184};
185
Alexandre Rames5319def2014-10-23 10:03:10 +0100186class CodeGeneratorARM64 : public CodeGenerator {
187 public:
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000188 CodeGeneratorARM64(HGraph* graph, const CompilerOptions& compiler_options);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000189 virtual ~CodeGeneratorARM64() {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100190
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000191 void GenerateFrameEntry() OVERRIDE;
192 void GenerateFrameExit() OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100193
194 static const vixl::CPURegList& GetFramePreservedRegisters() {
195 static const vixl::CPURegList frame_preserved_regs =
196 vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize, vixl::lr.Bit());
197 return frame_preserved_regs;
198 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100199
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000200 void Bind(HBasicBlock* block) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100201
202 vixl::Label* GetLabelOf(HBasicBlock* block) const {
203 return block_labels_ + block->GetBlockId();
204 }
205
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000206 void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100207
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000208 size_t GetWordSize() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100209 return kArm64WordSize;
210 }
211
Mark Mendellf85a9ca2015-01-13 09:20:58 -0500212 size_t GetFloatingPointSpillSlotSize() const OVERRIDE {
213 // Allocated in D registers, which are word sized.
214 return kArm64WordSize;
215 }
216
Alexandre Rames67555f72014-11-18 10:55:16 +0000217 uintptr_t GetAddressOf(HBasicBlock* block) const OVERRIDE {
218 vixl::Label* block_entry_label = GetLabelOf(block);
219 DCHECK(block_entry_label->IsBound());
220 return block_entry_label->location();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000221 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000223 HGraphVisitor* GetLocationBuilder() OVERRIDE { return &location_builder_; }
224 HGraphVisitor* GetInstructionVisitor() OVERRIDE { return &instruction_visitor_; }
225 Arm64Assembler* GetAssembler() OVERRIDE { return &assembler_; }
Alexandre Rames67555f72014-11-18 10:55:16 +0000226 vixl::MacroAssembler* GetVIXLAssembler() { return GetAssembler()->vixl_masm_; }
Alexandre Rames5319def2014-10-23 10:03:10 +0100227
228 // Emit a write barrier.
229 void MarkGCCard(vixl::Register object, vixl::Register value);
230
231 // Register allocation.
232
Nicolas Geoffray98893962015-01-21 12:32:32 +0000233 void SetupBlockedRegisters(bool is_baseline) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100234 // AllocateFreeRegister() is only used when allocating registers locally
235 // during CompileBaseline().
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000236 Location AllocateFreeRegister(Primitive::Type type) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100237
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000238 Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100239
Alexandre Rames3e69f162014-12-10 10:36:50 +0000240 size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id);
241 size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id);
242 size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id);
243 size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id);
Alexandre Rames5319def2014-10-23 10:03:10 +0100244
245 // The number of registers that can be allocated. The register allocator may
246 // decide to reserve and not use a few of them.
247 // We do not consider registers sp, xzr, wzr. They are either not allocatable
248 // (xzr, wzr), or make for poor allocatable registers (sp alignment
249 // requirements, etc.). This also facilitates our task as all other registers
250 // can easily be mapped via to or from their type and index or code.
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000251 static const int kNumberOfAllocatableRegisters = vixl::kNumberOfRegisters - 1;
252 static const int kNumberOfAllocatableFPRegisters = vixl::kNumberOfFPRegisters;
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 static constexpr int kNumberOfAllocatableRegisterPairs = 0;
254
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000255 void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
256 void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
Alexandre Rames5319def2014-10-23 10:03:10 +0100257
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000258 InstructionSet GetInstructionSet() const OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100259 return InstructionSet::kArm64;
260 }
261
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000262 void Initialize() OVERRIDE {
Alexandre Rames5319def2014-10-23 10:03:10 +0100263 HGraph* graph = GetGraph();
264 int length = graph->GetBlocks().Size();
265 block_labels_ = graph->GetArena()->AllocArray<vixl::Label>(length);
266 for (int i = 0; i < length; ++i) {
267 new(block_labels_ + i) vixl::Label();
268 }
269 }
270
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000271 void Finalize(CodeAllocator* allocator) OVERRIDE;
272
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000273 // Code generation helpers.
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 void MoveConstant(vixl::CPURegister destination, HConstant* constant);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000275 // The type is optional. When specified it must be coherent with the
276 // locations, and is used for optimisation and debugging.
277 void MoveLocation(Location destination, Location source,
278 Primitive::Type type = Primitive::kPrimVoid);
279 void SwapLocations(Location loc_1, Location loc_2);
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 void Load(Primitive::Type type, vixl::CPURegister dst, const vixl::MemOperand& src);
281 void Store(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
282 void LoadCurrentMethod(vixl::Register current_method);
Calin Juravle77520bc2015-01-12 18:45:46 +0000283 void LoadAcquire(HInstruction* instruction, vixl::CPURegister dst, const vixl::MemOperand& src);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000284 void StoreRelease(Primitive::Type type, vixl::CPURegister rt, const vixl::MemOperand& dst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 // Generate code to invoke a runtime entry point.
287 void InvokeRuntime(int32_t offset, HInstruction* instruction, uint32_t dex_pc);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000288
Alexandre Rames3e69f162014-12-10 10:36:50 +0000289 ParallelMoveResolverARM64* GetMoveResolver() { return &move_resolver_; }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000290
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000291 bool NeedsTwoRegisters(Primitive::Type type ATTRIBUTE_UNUSED) const OVERRIDE {
292 return false;
293 }
294
Andreas Gampe878d58c2015-01-15 23:24:00 -0800295 void GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, vixl::Register temp);
296
Alexandre Rames5319def2014-10-23 10:03:10 +0100297 private:
298 // Labels for each block that will be compiled.
299 vixl::Label* block_labels_;
300
301 LocationsBuilderARM64 location_builder_;
302 InstructionCodeGeneratorARM64 instruction_visitor_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000303 ParallelMoveResolverARM64 move_resolver_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100304 Arm64Assembler assembler_;
305
306 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorARM64);
307};
308
Alexandre Rames3e69f162014-12-10 10:36:50 +0000309inline Arm64Assembler* ParallelMoveResolverARM64::GetAssembler() const {
310 return codegen_->GetAssembler();
311}
312
Alexandre Rames5319def2014-10-23 10:03:10 +0100313} // namespace arm64
314} // namespace art
315
316#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_ARM64_H_