blob: c774bf027660b78312e010cefb5aacb0550186b2 [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#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100193 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100194
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000197 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100198
Alexandre Rames5319def2014-10-23 10:03:10 +0100199 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100208 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
223};
224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
226 public:
227 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
231 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000232 if (instruction_->CanThrowIntoCatchBlock()) {
233 // Live registers will be restored in the catch block if caught.
234 SaveLiveRegisters(codegen, instruction_->GetLocations());
235 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000237 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800238 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 }
240
Alexandre Rames8158f282015-08-07 10:26:17 +0100241 bool IsFatal() const OVERRIDE { return true; }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
244
Alexandre Rames67555f72014-11-18 10:55:16 +0000245 private:
246 HDivZeroCheck* const instruction_;
247 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
248};
249
250class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
252 LoadClassSlowPathARM64(HLoadClass* cls,
253 HInstruction* at,
254 uint32_t dex_pc,
255 bool do_clinit)
256 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
257 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
258 }
259
260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
261 LocationSummary* locations = at_->GetLocations();
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
268 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
270 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 // Move the class to the desired location.
279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000283 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 }
285
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 __ B(GetExitLabel());
288 }
289
Alexandre Rames9931f312015-06-19 14:47:01 +0100290 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 private:
293 // The class this slow path will load.
294 HLoadClass* const cls_;
295
296 // The instruction where this slow path is happening.
297 // (Might be the load class or an initialization check).
298 HInstruction* const at_;
299
300 // The dex PC of `at_`.
301 const uint32_t dex_pc_;
302
303 // Whether to initialize the class.
304 const bool do_clinit_;
305
306 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
307};
308
309class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
312
313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
314 LocationSummary* locations = instruction_->GetLocations();
315 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
316 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
317
318 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320
321 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000324 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100325 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000327 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000329 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 __ B(GetExitLabel());
331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
334
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 private:
336 HLoadString* const instruction_;
337
338 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
339};
340
Alexandre Rames5319def2014-10-23 10:03:10 +0100341class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
342 public:
343 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
344
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100347 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000348 if (instruction_->CanThrowIntoCatchBlock()) {
349 // Live registers will be restored in the catch block if caught.
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 }
356
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 bool IsFatal() const OVERRIDE { return true; }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361 private:
362 HNullCheck* const instruction_;
363
364 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
365};
366
367class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100369 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100370 : instruction_(instruction), successor_(successor) {}
371
Alexandre Rames67555f72014-11-18 10:55:16 +0000372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000377 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800378 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000379 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 if (successor_ == nullptr) {
381 __ B(GetReturnLabel());
382 } else {
383 __ B(arm64_codegen->GetLabelOf(successor_));
384 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 }
386
387 vixl::Label* GetReturnLabel() {
388 DCHECK(successor_ == nullptr);
389 return &return_label_;
390 }
391
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100392 HBasicBlock* GetSuccessor() const {
393 return successor_;
394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
397
Alexandre Rames5319def2014-10-23 10:03:10 +0100398 private:
399 HSuspendCheck* const instruction_;
400 // If not null, the block to branch to after the suspend check.
401 HBasicBlock* const successor_;
402
403 // If `successor_` is null, the label to branch to after the suspend check.
404 vixl::Label return_label_;
405
406 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
407};
408
Alexandre Rames67555f72014-11-18 10:55:16 +0000409class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
Nicolas Geoffray75374372015-09-17 17:12:19 +0000411 explicit TypeCheckSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000412
413 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000414 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100415 Location class_to_check = locations->InAt(1);
416 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
417 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000418 DCHECK(instruction_->IsCheckCast()
419 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
420 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100421 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000422
Alexandre Rames67555f72014-11-18 10:55:16 +0000423 __ Bind(GetEntryLabel());
Nicolas Geoffray75374372015-09-17 17:12:19 +0000424 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000425
426 // We're moving two locations to locations that could overlap, so we need a parallel
427 // move resolver.
428 InvokeRuntimeCallingConvention calling_convention;
429 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100430 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
431 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000432
433 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000434 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100435 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000436 Primitive::Type ret_type = instruction_->GetType();
437 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
438 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800439 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
440 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441 } else {
442 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100443 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800444 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445 }
446
Nicolas Geoffray75374372015-09-17 17:12:19 +0000447 RestoreLiveRegisters(codegen, locations);
448 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000449 }
450
Alexandre Rames9931f312015-06-19 14:47:01 +0100451 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
452
Alexandre Rames67555f72014-11-18 10:55:16 +0000453 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000454 HInstruction* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455
Alexandre Rames67555f72014-11-18 10:55:16 +0000456 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
457};
458
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700459class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
460 public:
461 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
462 : instruction_(instruction) {}
463
464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 __ Bind(GetEntryLabel());
466 SaveLiveRegisters(codegen, instruction_->GetLocations());
467 DCHECK(instruction_->IsDeoptimize());
468 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
469 uint32_t dex_pc = deoptimize->GetDexPc();
470 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
471 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
472 }
473
Alexandre Rames9931f312015-06-19 14:47:01 +0100474 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700476 private:
477 HInstruction* const instruction_;
478 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
479};
480
Alexandre Rames5319def2014-10-23 10:03:10 +0100481#undef __
482
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100483Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 Location next_location;
485 if (type == Primitive::kPrimVoid) {
486 LOG(FATAL) << "Unreachable type " << type;
487 }
488
Alexandre Rames542361f2015-01-29 16:57:31 +0000489 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100490 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
491 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000492 } else if (!Primitive::IsFloatingPointType(type) &&
493 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000494 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
495 } else {
496 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000497 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
498 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100499 }
500
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000501 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000502 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100503 return next_location;
504}
505
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100506Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100507 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100508}
509
Serban Constantinescu579885a2015-02-22 20:51:33 +0000510CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
511 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100512 const CompilerOptions& compiler_options,
513 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100514 : CodeGenerator(graph,
515 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000516 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000517 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000518 callee_saved_core_registers.list(),
519 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100520 compiler_options,
521 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100522 block_labels_(nullptr),
523 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000524 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000525 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000526 isa_features_(isa_features),
527 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
528 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
529 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
530 relative_call_patches_(graph->GetArena()->Adapter()),
531 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000532 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000533 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000534}
Alexandre Rames5319def2014-10-23 10:03:10 +0100535
Alexandre Rames67555f72014-11-18 10:55:16 +0000536#undef __
537#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100538
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000539void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
540 // Ensure we emit the literal pool.
541 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000542
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000543 CodeGenerator::Finalize(allocator);
544}
545
Zheng Xuad4450e2015-04-17 18:48:56 +0800546void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
547 // Note: There are 6 kinds of moves:
548 // 1. constant -> GPR/FPR (non-cycle)
549 // 2. constant -> stack (non-cycle)
550 // 3. GPR/FPR -> GPR/FPR
551 // 4. GPR/FPR -> stack
552 // 5. stack -> GPR/FPR
553 // 6. stack -> stack (non-cycle)
554 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
555 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
556 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
557 // dependency.
558 vixl_temps_.Open(GetVIXLAssembler());
559}
560
561void ParallelMoveResolverARM64::FinishEmitNativeCode() {
562 vixl_temps_.Close();
563}
564
565Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
566 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
567 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
568 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
569 Location scratch = GetScratchLocation(kind);
570 if (!scratch.Equals(Location::NoLocation())) {
571 return scratch;
572 }
573 // Allocate from VIXL temp registers.
574 if (kind == Location::kRegister) {
575 scratch = LocationFrom(vixl_temps_.AcquireX());
576 } else {
577 DCHECK(kind == Location::kFpuRegister);
578 scratch = LocationFrom(vixl_temps_.AcquireD());
579 }
580 AddScratchLocation(scratch);
581 return scratch;
582}
583
584void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
585 if (loc.IsRegister()) {
586 vixl_temps_.Release(XRegisterFrom(loc));
587 } else {
588 DCHECK(loc.IsFpuRegister());
589 vixl_temps_.Release(DRegisterFrom(loc));
590 }
591 RemoveScratchLocation(loc);
592}
593
Alexandre Rames3e69f162014-12-10 10:36:50 +0000594void ParallelMoveResolverARM64::EmitMove(size_t index) {
595 MoveOperands* move = moves_.Get(index);
596 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
597}
598
Alexandre Rames5319def2014-10-23 10:03:10 +0100599void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100600 MacroAssembler* masm = GetVIXLAssembler();
601 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000602 __ Bind(&frame_entry_label_);
603
Serban Constantinescu02164b32014-11-13 14:05:07 +0000604 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
605 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100606 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000607 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000608 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000609 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000610 __ Ldr(wzr, MemOperand(temp, 0));
611 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000612 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100613
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000614 if (!HasEmptyFrame()) {
615 int frame_size = GetFrameSize();
616 // Stack layout:
617 // sp[frame_size - 8] : lr.
618 // ... : other preserved core registers.
619 // ... : other preserved fp registers.
620 // ... : reserved frame space.
621 // sp[0] : current method.
622 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100623 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800624 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
625 frame_size - GetCoreSpillSize());
626 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
627 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000628 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100629}
630
631void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100632 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100633 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000634 if (!HasEmptyFrame()) {
635 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800636 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
637 frame_size - FrameEntrySpillSize());
638 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
639 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000640 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100641 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000642 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100643 __ Ret();
644 GetAssembler()->cfi().RestoreState();
645 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100646}
647
Zheng Xuda403092015-04-24 17:35:39 +0800648vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
649 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
650 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
651 core_spill_mask_);
652}
653
654vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
655 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
656 GetNumberOfFloatingPointRegisters()));
657 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
658 fpu_spill_mask_);
659}
660
Alexandre Rames5319def2014-10-23 10:03:10 +0100661void CodeGeneratorARM64::Bind(HBasicBlock* block) {
662 __ Bind(GetLabelOf(block));
663}
664
Alexandre Rames5319def2014-10-23 10:03:10 +0100665void CodeGeneratorARM64::Move(HInstruction* instruction,
666 Location location,
667 HInstruction* move_for) {
668 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100669 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000670 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100671
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100672 if (instruction->IsFakeString()) {
673 // The fake string is an alias for null.
674 DCHECK(IsBaseline());
675 instruction = locations->Out().GetConstant();
676 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
677 }
678
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100679 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700680 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100681 } else if (locations != nullptr && locations->Out().Equals(location)) {
682 return;
683 } else if (instruction->IsIntConstant()
684 || instruction->IsLongConstant()
685 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000686 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100687 if (location.IsRegister()) {
688 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000689 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100690 (instruction->IsLongConstant() && dst.Is64Bits()));
691 __ Mov(dst, value);
692 } else {
693 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000694 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000695 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
696 ? temps.AcquireW()
697 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100698 __ Mov(temp, value);
699 __ Str(temp, StackOperandFrom(location));
700 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000701 } else if (instruction->IsTemporary()) {
702 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000703 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100704 } else if (instruction->IsLoadLocal()) {
705 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000706 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000707 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000708 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000709 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100710 }
711
712 } else {
713 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000714 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100715 }
716}
717
Calin Juravle175dc732015-08-25 15:42:32 +0100718void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
719 DCHECK(location.IsRegister());
720 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
721}
722
Calin Juravle23a8e352015-09-08 19:56:31 +0100723void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
724 if (location.IsRegister()) {
725 locations->AddTemp(location);
726 } else {
727 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
728 }
729}
730
731void CodeGeneratorARM64::MoveLocationToTemp(Location source,
732 const LocationSummary& locations,
733 int temp_index,
734 Primitive::Type type) {
735 if (!Primitive::IsFloatingPointType(type)) {
736 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
737 }
738
739 DCHECK(source.IsFpuRegister()) << source;
740 Primitive::Type temp_type = Primitive::Is64BitType(type)
741 ? Primitive::kPrimLong
742 : Primitive::kPrimInt;
743 __ Fmov(RegisterFrom(locations.GetTemp(temp_index), temp_type),
744 FPRegisterFrom(source, type));
745}
746
747void CodeGeneratorARM64::MoveTempToLocation(const LocationSummary& locations,
748 int temp_index,
749 Location destination,
750 Primitive::Type type) {
751 if (!Primitive::IsFloatingPointType(type)) {
752 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
753 }
754
755 DCHECK(destination.IsFpuRegister()) << destination;
756 Primitive::Type temp_type = Primitive::Is64BitType(type)
757 ? Primitive::kPrimLong
758 : Primitive::kPrimInt;
759 __ Fmov(FPRegisterFrom(destination, type),
760 RegisterFrom(locations.GetTemp(temp_index), temp_type));
761}
762
Alexandre Rames5319def2014-10-23 10:03:10 +0100763Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
764 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000765
Alexandre Rames5319def2014-10-23 10:03:10 +0100766 switch (type) {
767 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000768 case Primitive::kPrimInt:
769 case Primitive::kPrimFloat:
770 return Location::StackSlot(GetStackSlot(load->GetLocal()));
771
772 case Primitive::kPrimLong:
773 case Primitive::kPrimDouble:
774 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
775
Alexandre Rames5319def2014-10-23 10:03:10 +0100776 case Primitive::kPrimBoolean:
777 case Primitive::kPrimByte:
778 case Primitive::kPrimChar:
779 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100780 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100781 LOG(FATAL) << "Unexpected type " << type;
782 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000783
Alexandre Rames5319def2014-10-23 10:03:10 +0100784 LOG(FATAL) << "Unreachable";
785 return Location::NoLocation();
786}
787
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100788void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000789 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100790 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000791 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100792 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100793 if (value_can_be_null) {
794 __ Cbz(value, &done);
795 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100796 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
797 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000798 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100799 if (value_can_be_null) {
800 __ Bind(&done);
801 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100802}
803
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000804void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
805 // Blocked core registers:
806 // lr : Runtime reserved.
807 // tr : Runtime reserved.
808 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
809 // ip1 : VIXL core temp.
810 // ip0 : VIXL core temp.
811 //
812 // Blocked fp registers:
813 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100814 CPURegList reserved_core_registers = vixl_reserved_core_registers;
815 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100816 while (!reserved_core_registers.IsEmpty()) {
817 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
818 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000819
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000820 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800821 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000822 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
823 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000824
825 if (is_baseline) {
826 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
827 while (!reserved_core_baseline_registers.IsEmpty()) {
828 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
829 }
830
831 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
832 while (!reserved_fp_baseline_registers.IsEmpty()) {
833 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
834 }
835 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100836}
837
838Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
839 if (type == Primitive::kPrimVoid) {
840 LOG(FATAL) << "Unreachable type " << type;
841 }
842
Alexandre Rames542361f2015-01-29 16:57:31 +0000843 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000844 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
845 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100846 return Location::FpuRegisterLocation(reg);
847 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000848 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
849 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100850 return Location::RegisterLocation(reg);
851 }
852}
853
Alexandre Rames3e69f162014-12-10 10:36:50 +0000854size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
855 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
856 __ Str(reg, MemOperand(sp, stack_index));
857 return kArm64WordSize;
858}
859
860size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
861 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
862 __ Ldr(reg, MemOperand(sp, stack_index));
863 return kArm64WordSize;
864}
865
866size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
867 FPRegister reg = FPRegister(reg_id, kDRegSize);
868 __ Str(reg, MemOperand(sp, stack_index));
869 return kArm64WordSize;
870}
871
872size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
873 FPRegister reg = FPRegister(reg_id, kDRegSize);
874 __ Ldr(reg, MemOperand(sp, stack_index));
875 return kArm64WordSize;
876}
877
Alexandre Rames5319def2014-10-23 10:03:10 +0100878void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100879 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100880}
881
882void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100883 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100884}
885
Alexandre Rames67555f72014-11-18 10:55:16 +0000886void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000887 if (constant->IsIntConstant()) {
888 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
889 } else if (constant->IsLongConstant()) {
890 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
891 } else if (constant->IsNullConstant()) {
892 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000893 } else if (constant->IsFloatConstant()) {
894 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
895 } else {
896 DCHECK(constant->IsDoubleConstant());
897 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
898 }
899}
900
Alexandre Rames3e69f162014-12-10 10:36:50 +0000901
902static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
903 DCHECK(constant.IsConstant());
904 HConstant* cst = constant.GetConstant();
905 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000906 // Null is mapped to a core W register, which we associate with kPrimInt.
907 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000908 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
909 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
910 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
911}
912
913void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000914 if (source.Equals(destination)) {
915 return;
916 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000917
918 // A valid move can always be inferred from the destination and source
919 // locations. When moving from and to a register, the argument type can be
920 // used to generate 32bit instead of 64bit moves. In debug mode we also
921 // checks the coherency of the locations and the type.
922 bool unspecified_type = (type == Primitive::kPrimVoid);
923
924 if (destination.IsRegister() || destination.IsFpuRegister()) {
925 if (unspecified_type) {
926 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
927 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000928 (src_cst != nullptr && (src_cst->IsIntConstant()
929 || src_cst->IsFloatConstant()
930 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000931 // For stack slots and 32bit constants, a 64bit type is appropriate.
932 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000933 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000934 // If the source is a double stack slot or a 64bit constant, a 64bit
935 // type is appropriate. Else the source is a register, and since the
936 // type has not been specified, we chose a 64bit type to force a 64bit
937 // move.
938 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000939 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000940 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000941 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
942 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000943 CPURegister dst = CPURegisterFrom(destination, type);
944 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
945 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
946 __ Ldr(dst, StackOperandFrom(source));
947 } else if (source.IsConstant()) {
948 DCHECK(CoherentConstantAndType(source, type));
949 MoveConstant(dst, source.GetConstant());
950 } else {
951 if (destination.IsRegister()) {
952 __ Mov(Register(dst), RegisterFrom(source, type));
953 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800954 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000955 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
956 }
957 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000958 } else { // The destination is not a register. It must be a stack slot.
959 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
960 if (source.IsRegister() || source.IsFpuRegister()) {
961 if (unspecified_type) {
962 if (source.IsRegister()) {
963 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
964 } else {
965 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
966 }
967 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000968 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
969 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000970 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
971 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100972 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000973 UseScratchRegisterScope temps(GetVIXLAssembler());
974 HConstant* src_cst = source.GetConstant();
975 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000976 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000977 temp = temps.AcquireW();
978 } else if (src_cst->IsLongConstant()) {
979 temp = temps.AcquireX();
980 } else if (src_cst->IsFloatConstant()) {
981 temp = temps.AcquireS();
982 } else {
983 DCHECK(src_cst->IsDoubleConstant());
984 temp = temps.AcquireD();
985 }
986 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000987 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000988 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000989 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000990 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000991 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000992 // There is generally less pressure on FP registers.
993 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000994 __ Ldr(temp, StackOperandFrom(source));
995 __ Str(temp, StackOperandFrom(destination));
996 }
997 }
998}
999
1000void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001001 CPURegister dst,
1002 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001003 switch (type) {
1004 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001005 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001006 break;
1007 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001008 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001009 break;
1010 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001011 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001012 break;
1013 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001014 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001015 break;
1016 case Primitive::kPrimInt:
1017 case Primitive::kPrimNot:
1018 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001019 case Primitive::kPrimFloat:
1020 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001021 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001022 __ Ldr(dst, src);
1023 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001024 case Primitive::kPrimVoid:
1025 LOG(FATAL) << "Unreachable type " << type;
1026 }
1027}
1028
Calin Juravle77520bc2015-01-12 18:45:46 +00001029void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001030 CPURegister dst,
1031 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001032 MacroAssembler* masm = GetVIXLAssembler();
1033 BlockPoolsScope block_pools(masm);
1034 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001035 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001036 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001037
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001038 DCHECK(!src.IsPreIndex());
1039 DCHECK(!src.IsPostIndex());
1040
1041 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001042 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 MemOperand base = MemOperand(temp_base);
1044 switch (type) {
1045 case Primitive::kPrimBoolean:
1046 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001047 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001048 break;
1049 case Primitive::kPrimByte:
1050 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001051 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1053 break;
1054 case Primitive::kPrimChar:
1055 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001056 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001057 break;
1058 case Primitive::kPrimShort:
1059 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001060 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001061 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1062 break;
1063 case Primitive::kPrimInt:
1064 case Primitive::kPrimNot:
1065 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001066 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001067 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001068 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001069 break;
1070 case Primitive::kPrimFloat:
1071 case Primitive::kPrimDouble: {
1072 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001073 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001074
1075 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1076 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001077 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001078 __ Fmov(FPRegister(dst), temp);
1079 break;
1080 }
1081 case Primitive::kPrimVoid:
1082 LOG(FATAL) << "Unreachable type " << type;
1083 }
1084}
1085
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001086void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001087 CPURegister src,
1088 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001089 switch (type) {
1090 case Primitive::kPrimBoolean:
1091 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001092 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001093 break;
1094 case Primitive::kPrimChar:
1095 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001097 break;
1098 case Primitive::kPrimInt:
1099 case Primitive::kPrimNot:
1100 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001101 case Primitive::kPrimFloat:
1102 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001103 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001104 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001105 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001106 case Primitive::kPrimVoid:
1107 LOG(FATAL) << "Unreachable type " << type;
1108 }
1109}
1110
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001111void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1112 CPURegister src,
1113 const MemOperand& dst) {
1114 UseScratchRegisterScope temps(GetVIXLAssembler());
1115 Register temp_base = temps.AcquireX();
1116
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001117 DCHECK(!dst.IsPreIndex());
1118 DCHECK(!dst.IsPostIndex());
1119
1120 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001121 Operand op = OperandFromMemOperand(dst);
1122 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001123 MemOperand base = MemOperand(temp_base);
1124 switch (type) {
1125 case Primitive::kPrimBoolean:
1126 case Primitive::kPrimByte:
1127 __ Stlrb(Register(src), base);
1128 break;
1129 case Primitive::kPrimChar:
1130 case Primitive::kPrimShort:
1131 __ Stlrh(Register(src), base);
1132 break;
1133 case Primitive::kPrimInt:
1134 case Primitive::kPrimNot:
1135 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001136 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001137 __ Stlr(Register(src), base);
1138 break;
1139 case Primitive::kPrimFloat:
1140 case Primitive::kPrimDouble: {
1141 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001142 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001143
1144 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1145 __ Fmov(temp, FPRegister(src));
1146 __ Stlr(temp, base);
1147 break;
1148 }
1149 case Primitive::kPrimVoid:
1150 LOG(FATAL) << "Unreachable type " << type;
1151 }
1152}
1153
Calin Juravle175dc732015-08-25 15:42:32 +01001154void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1155 HInstruction* instruction,
1156 uint32_t dex_pc,
1157 SlowPathCode* slow_path) {
1158 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1159 instruction,
1160 dex_pc,
1161 slow_path);
1162}
1163
Alexandre Rames67555f72014-11-18 10:55:16 +00001164void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1165 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001166 uint32_t dex_pc,
1167 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001168 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001169 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001170 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1171 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001172 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001173}
1174
1175void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1176 vixl::Register class_reg) {
1177 UseScratchRegisterScope temps(GetVIXLAssembler());
1178 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001179 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001180 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001181
Serban Constantinescu02164b32014-11-13 14:05:07 +00001182 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001183 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001184 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1185 __ Add(temp, class_reg, status_offset);
1186 __ Ldar(temp, HeapOperand(temp));
1187 __ Cmp(temp, mirror::Class::kStatusInitialized);
1188 __ B(lt, slow_path->GetEntryLabel());
1189 } else {
1190 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1191 __ Cmp(temp, mirror::Class::kStatusInitialized);
1192 __ B(lt, slow_path->GetEntryLabel());
1193 __ Dmb(InnerShareable, BarrierReads);
1194 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001195 __ Bind(slow_path->GetExitLabel());
1196}
Alexandre Rames5319def2014-10-23 10:03:10 +01001197
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001198void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1199 BarrierType type = BarrierAll;
1200
1201 switch (kind) {
1202 case MemBarrierKind::kAnyAny:
1203 case MemBarrierKind::kAnyStore: {
1204 type = BarrierAll;
1205 break;
1206 }
1207 case MemBarrierKind::kLoadAny: {
1208 type = BarrierReads;
1209 break;
1210 }
1211 case MemBarrierKind::kStoreStore: {
1212 type = BarrierWrites;
1213 break;
1214 }
1215 default:
1216 LOG(FATAL) << "Unexpected memory barrier " << kind;
1217 }
1218 __ Dmb(InnerShareable, type);
1219}
1220
Serban Constantinescu02164b32014-11-13 14:05:07 +00001221void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1222 HBasicBlock* successor) {
1223 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001224 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1225 if (slow_path == nullptr) {
1226 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1227 instruction->SetSlowPath(slow_path);
1228 codegen_->AddSlowPath(slow_path);
1229 if (successor != nullptr) {
1230 DCHECK(successor->IsLoopHeader());
1231 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1232 }
1233 } else {
1234 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1235 }
1236
Serban Constantinescu02164b32014-11-13 14:05:07 +00001237 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1238 Register temp = temps.AcquireW();
1239
1240 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1241 if (successor == nullptr) {
1242 __ Cbnz(temp, slow_path->GetEntryLabel());
1243 __ Bind(slow_path->GetReturnLabel());
1244 } else {
1245 __ Cbz(temp, codegen_->GetLabelOf(successor));
1246 __ B(slow_path->GetEntryLabel());
1247 // slow_path will return to GetLabelOf(successor).
1248 }
1249}
1250
Alexandre Rames5319def2014-10-23 10:03:10 +01001251InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1252 CodeGeneratorARM64* codegen)
1253 : HGraphVisitor(graph),
1254 assembler_(codegen->GetAssembler()),
1255 codegen_(codegen) {}
1256
1257#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001259
1260#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1261
1262enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001263 // Using a base helps identify when we hit such breakpoints.
1264 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001265#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1266 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1267#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1268};
1269
1270#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1271 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001272 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001273 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1274 } \
1275 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1277 locations->SetOut(Location::Any()); \
1278 }
1279 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1280#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1281
1282#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001283#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001284
Alexandre Rames67555f72014-11-18 10:55:16 +00001285void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001286 DCHECK_EQ(instr->InputCount(), 2U);
1287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1288 Primitive::Type type = instr->GetResultType();
1289 switch (type) {
1290 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001291 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001292 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001293 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001295 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001296
1297 case Primitive::kPrimFloat:
1298 case Primitive::kPrimDouble:
1299 locations->SetInAt(0, Location::RequiresFpuRegister());
1300 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001301 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001302 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001303
Alexandre Rames5319def2014-10-23 10:03:10 +01001304 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001305 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001306 }
1307}
1308
Alexandre Rames09a99962015-04-15 11:47:56 +01001309void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1310 LocationSummary* locations =
1311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1312 locations->SetInAt(0, Location::RequiresRegister());
1313 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1314 locations->SetOut(Location::RequiresFpuRegister());
1315 } else {
1316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1317 }
1318}
1319
1320void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1321 const FieldInfo& field_info) {
1322 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001323 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001324 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001325
1326 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1327 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1328
1329 if (field_info.IsVolatile()) {
1330 if (use_acquire_release) {
1331 // NB: LoadAcquire will record the pc info if needed.
1332 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1333 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001334 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001335 codegen_->MaybeRecordImplicitNullCheck(instruction);
1336 // For IRIW sequential consistency kLoadAny is not sufficient.
1337 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1338 }
1339 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001340 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001341 codegen_->MaybeRecordImplicitNullCheck(instruction);
1342 }
Roland Levillain4d027112015-07-01 15:41:14 +01001343
1344 if (field_type == Primitive::kPrimNot) {
1345 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1346 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001347}
1348
1349void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1350 LocationSummary* locations =
1351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1352 locations->SetInAt(0, Location::RequiresRegister());
1353 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1354 locations->SetInAt(1, Location::RequiresFpuRegister());
1355 } else {
1356 locations->SetInAt(1, Location::RequiresRegister());
1357 }
1358}
1359
1360void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001361 const FieldInfo& field_info,
1362 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001363 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001364 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001365
1366 Register obj = InputRegisterAt(instruction, 0);
1367 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001368 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001369 Offset offset = field_info.GetFieldOffset();
1370 Primitive::Type field_type = field_info.GetFieldType();
1371 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1372
Roland Levillain4d027112015-07-01 15:41:14 +01001373 {
1374 // We use a block to end the scratch scope before the write barrier, thus
1375 // freeing the temporary registers so they can be used in `MarkGCCard`.
1376 UseScratchRegisterScope temps(GetVIXLAssembler());
1377
1378 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1379 DCHECK(value.IsW());
1380 Register temp = temps.AcquireW();
1381 __ Mov(temp, value.W());
1382 GetAssembler()->PoisonHeapReference(temp.W());
1383 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001384 }
Roland Levillain4d027112015-07-01 15:41:14 +01001385
1386 if (field_info.IsVolatile()) {
1387 if (use_acquire_release) {
1388 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1389 codegen_->MaybeRecordImplicitNullCheck(instruction);
1390 } else {
1391 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1392 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1393 codegen_->MaybeRecordImplicitNullCheck(instruction);
1394 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1395 }
1396 } else {
1397 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1398 codegen_->MaybeRecordImplicitNullCheck(instruction);
1399 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001400 }
1401
1402 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001403 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001404 }
1405}
1406
Alexandre Rames67555f72014-11-18 10:55:16 +00001407void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001408 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001409
1410 switch (type) {
1411 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001412 case Primitive::kPrimLong: {
1413 Register dst = OutputRegister(instr);
1414 Register lhs = InputRegisterAt(instr, 0);
1415 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001416 if (instr->IsAdd()) {
1417 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001418 } else if (instr->IsAnd()) {
1419 __ And(dst, lhs, rhs);
1420 } else if (instr->IsOr()) {
1421 __ Orr(dst, lhs, rhs);
1422 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001423 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001424 } else {
1425 DCHECK(instr->IsXor());
1426 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001427 }
1428 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001429 }
1430 case Primitive::kPrimFloat:
1431 case Primitive::kPrimDouble: {
1432 FPRegister dst = OutputFPRegister(instr);
1433 FPRegister lhs = InputFPRegisterAt(instr, 0);
1434 FPRegister rhs = InputFPRegisterAt(instr, 1);
1435 if (instr->IsAdd()) {
1436 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001437 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001438 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001439 } else {
1440 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001441 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001443 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001444 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001445 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001446 }
1447}
1448
Serban Constantinescu02164b32014-11-13 14:05:07 +00001449void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1450 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1451
1452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1453 Primitive::Type type = instr->GetResultType();
1454 switch (type) {
1455 case Primitive::kPrimInt:
1456 case Primitive::kPrimLong: {
1457 locations->SetInAt(0, Location::RequiresRegister());
1458 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1459 locations->SetOut(Location::RequiresRegister());
1460 break;
1461 }
1462 default:
1463 LOG(FATAL) << "Unexpected shift type " << type;
1464 }
1465}
1466
1467void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1468 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1469
1470 Primitive::Type type = instr->GetType();
1471 switch (type) {
1472 case Primitive::kPrimInt:
1473 case Primitive::kPrimLong: {
1474 Register dst = OutputRegister(instr);
1475 Register lhs = InputRegisterAt(instr, 0);
1476 Operand rhs = InputOperandAt(instr, 1);
1477 if (rhs.IsImmediate()) {
1478 uint32_t shift_value = (type == Primitive::kPrimInt)
1479 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1480 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1481 if (instr->IsShl()) {
1482 __ Lsl(dst, lhs, shift_value);
1483 } else if (instr->IsShr()) {
1484 __ Asr(dst, lhs, shift_value);
1485 } else {
1486 __ Lsr(dst, lhs, shift_value);
1487 }
1488 } else {
1489 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1490
1491 if (instr->IsShl()) {
1492 __ Lsl(dst, lhs, rhs_reg);
1493 } else if (instr->IsShr()) {
1494 __ Asr(dst, lhs, rhs_reg);
1495 } else {
1496 __ Lsr(dst, lhs, rhs_reg);
1497 }
1498 }
1499 break;
1500 }
1501 default:
1502 LOG(FATAL) << "Unexpected shift operation type " << type;
1503 }
1504}
1505
Alexandre Rames5319def2014-10-23 10:03:10 +01001506void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001507 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001508}
1509
1510void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001511 HandleBinaryOp(instruction);
1512}
1513
1514void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1515 HandleBinaryOp(instruction);
1516}
1517
1518void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1519 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001520}
1521
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001522void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1523 LocationSummary* locations =
1524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1525 locations->SetInAt(0, Location::RequiresRegister());
1526 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001527 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1528 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1529 } else {
1530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1531 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532}
1533
1534void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1535 LocationSummary* locations = instruction->GetLocations();
1536 Primitive::Type type = instruction->GetType();
1537 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001538 Location index = locations->InAt(1);
1539 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001540 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001541 MacroAssembler* masm = GetVIXLAssembler();
1542 UseScratchRegisterScope temps(masm);
1543 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001544
1545 if (index.IsConstant()) {
1546 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001547 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001548 } else {
1549 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001550 __ Add(temp, obj, offset);
1551 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001552 }
1553
Alexandre Rames67555f72014-11-18 10:55:16 +00001554 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001555 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001556
1557 if (type == Primitive::kPrimNot) {
1558 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1559 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001560}
1561
Alexandre Rames5319def2014-10-23 10:03:10 +01001562void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1564 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001566}
1567
1568void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001569 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001570 __ Ldr(OutputRegister(instruction),
1571 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001572 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001573}
1574
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001575void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001576 if (instruction->NeedsTypeCheck()) {
1577 LocationSummary* locations =
1578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001579 InvokeRuntimeCallingConvention calling_convention;
1580 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1581 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1582 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1583 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001584 LocationSummary* locations =
1585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001586 locations->SetInAt(0, Location::RequiresRegister());
1587 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001588 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1589 locations->SetInAt(2, Location::RequiresFpuRegister());
1590 } else {
1591 locations->SetInAt(2, Location::RequiresRegister());
1592 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 }
1594}
1595
1596void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1597 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001598 LocationSummary* locations = instruction->GetLocations();
1599 bool needs_runtime_call = locations->WillCall();
1600
1601 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001602 // Note: if heap poisoning is enabled, pAputObject takes cares
1603 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001604 codegen_->InvokeRuntime(
1605 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001606 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001607 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001608 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001609 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001610 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001611 Location index = locations->InAt(1);
1612 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001613 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001614 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001615 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001616 {
1617 // We use a block to end the scratch scope before the write barrier, thus
1618 // freeing the temporary registers so they can be used in `MarkGCCard`.
1619 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001620
Roland Levillain4d027112015-07-01 15:41:14 +01001621 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1622 DCHECK(value.IsW());
1623 Register temp = temps.AcquireW();
1624 __ Mov(temp, value.W());
1625 GetAssembler()->PoisonHeapReference(temp.W());
1626 source = temp;
1627 }
1628
Alexandre Rames97833a02015-04-16 15:07:12 +01001629 if (index.IsConstant()) {
1630 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1631 destination = HeapOperand(obj, offset);
1632 } else {
1633 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001634 __ Add(temp, obj, offset);
1635 destination = HeapOperand(temp,
1636 XRegisterFrom(index),
1637 LSL,
1638 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001639 }
1640
Roland Levillain4d027112015-07-01 15:41:14 +01001641 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001642 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001643 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001644 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001645 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001646 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 }
1648}
1649
Alexandre Rames67555f72014-11-18 10:55:16 +00001650void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001651 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1652 ? LocationSummary::kCallOnSlowPath
1653 : LocationSummary::kNoCall;
1654 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001655 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001656 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001657 if (instruction->HasUses()) {
1658 locations->SetOut(Location::SameAsFirstInput());
1659 }
1660}
1661
1662void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001663 BoundsCheckSlowPathARM64* slow_path =
1664 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001665 codegen_->AddSlowPath(slow_path);
1666
1667 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1668 __ B(slow_path->GetEntryLabel(), hs);
1669}
1670
Nicolas Geoffray75374372015-09-17 17:12:19 +00001671void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1673 instruction, LocationSummary::kCallOnSlowPath);
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::RequiresRegister());
1676 // Note that TypeCheckSlowPathARM64 uses this register too.
1677 locations->AddTemp(Location::RequiresRegister());
1678}
1679
1680void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
1681 Register obj = InputRegisterAt(instruction, 0);;
1682 Register cls = InputRegisterAt(instruction, 1);;
1683 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
1684
1685 SlowPathCodeARM64* slow_path =
1686 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction);
1687 codegen_->AddSlowPath(slow_path);
1688
1689 // Avoid null check if we know obj is not null.
1690 if (instruction->MustDoNullCheck()) {
1691 __ Cbz(obj, slow_path->GetExitLabel());
1692 }
1693 // Compare the class of `obj` with `cls`.
1694 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1695 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
1696 __ Cmp(obj_cls, cls);
1697 // The checkcast succeeds if the classes are equal (fast path).
1698 // Otherwise, we need to go into the slow path to check the types.
1699 __ B(ne, slow_path->GetEntryLabel());
1700 __ Bind(slow_path->GetExitLabel());
1701}
1702
Alexandre Rames67555f72014-11-18 10:55:16 +00001703void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1704 LocationSummary* locations =
1705 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1706 locations->SetInAt(0, Location::RequiresRegister());
1707 if (check->HasUses()) {
1708 locations->SetOut(Location::SameAsFirstInput());
1709 }
1710}
1711
1712void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1713 // We assume the class is not null.
1714 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1715 check->GetLoadClass(), check, check->GetDexPc(), true);
1716 codegen_->AddSlowPath(slow_path);
1717 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1718}
1719
Roland Levillain7f63c522015-07-13 15:54:55 +00001720static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1721 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1722 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1723}
1724
Serban Constantinescu02164b32014-11-13 14:05:07 +00001725void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001726 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001727 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1728 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001729 switch (in_type) {
1730 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001731 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001732 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1734 break;
1735 }
1736 case Primitive::kPrimFloat:
1737 case Primitive::kPrimDouble: {
1738 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001739 locations->SetInAt(1,
1740 IsFloatingPointZeroConstant(compare->InputAt(1))
1741 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1742 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001743 locations->SetOut(Location::RequiresRegister());
1744 break;
1745 }
1746 default:
1747 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1748 }
1749}
1750
1751void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1752 Primitive::Type in_type = compare->InputAt(0)->GetType();
1753
1754 // 0 if: left == right
1755 // 1 if: left > right
1756 // -1 if: left < right
1757 switch (in_type) {
1758 case Primitive::kPrimLong: {
1759 Register result = OutputRegister(compare);
1760 Register left = InputRegisterAt(compare, 0);
1761 Operand right = InputOperandAt(compare, 1);
1762
1763 __ Cmp(left, right);
1764 __ Cset(result, ne);
1765 __ Cneg(result, result, lt);
1766 break;
1767 }
1768 case Primitive::kPrimFloat:
1769 case Primitive::kPrimDouble: {
1770 Register result = OutputRegister(compare);
1771 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001772 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001773 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1774 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001775 __ Fcmp(left, 0.0);
1776 } else {
1777 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1778 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001779 if (compare->IsGtBias()) {
1780 __ Cset(result, ne);
1781 } else {
1782 __ Csetm(result, ne);
1783 }
1784 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 break;
1786 }
1787 default:
1788 LOG(FATAL) << "Unimplemented compare type " << in_type;
1789 }
1790}
1791
1792void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001794
1795 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1796 locations->SetInAt(0, Location::RequiresFpuRegister());
1797 locations->SetInAt(1,
1798 IsFloatingPointZeroConstant(instruction->InputAt(1))
1799 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1800 : Location::RequiresFpuRegister());
1801 } else {
1802 // Integer cases.
1803 locations->SetInAt(0, Location::RequiresRegister());
1804 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1805 }
1806
Alexandre Rames5319def2014-10-23 10:03:10 +01001807 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001808 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001809 }
1810}
1811
1812void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1813 if (!instruction->NeedsMaterialization()) {
1814 return;
1815 }
1816
1817 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001818 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001819 IfCondition if_cond = instruction->GetCondition();
1820 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001821
Roland Levillain7f63c522015-07-13 15:54:55 +00001822 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1823 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1824 if (locations->InAt(1).IsConstant()) {
1825 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1826 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1827 __ Fcmp(lhs, 0.0);
1828 } else {
1829 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1830 }
1831 __ Cset(res, arm64_cond);
1832 if (instruction->IsFPConditionTrueIfNaN()) {
1833 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1834 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1835 } else if (instruction->IsFPConditionFalseIfNaN()) {
1836 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1837 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1838 }
1839 } else {
1840 // Integer cases.
1841 Register lhs = InputRegisterAt(instruction, 0);
1842 Operand rhs = InputOperandAt(instruction, 1);
1843 __ Cmp(lhs, rhs);
1844 __ Cset(res, arm64_cond);
1845 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001846}
1847
1848#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1849 M(Equal) \
1850 M(NotEqual) \
1851 M(LessThan) \
1852 M(LessThanOrEqual) \
1853 M(GreaterThan) \
1854 M(GreaterThanOrEqual)
1855#define DEFINE_CONDITION_VISITORS(Name) \
1856void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1857void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1858FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001859#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001860#undef FOR_EACH_CONDITION_INSTRUCTION
1861
Zheng Xuc6667102015-05-15 16:08:45 +08001862void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1863 DCHECK(instruction->IsDiv() || instruction->IsRem());
1864
1865 LocationSummary* locations = instruction->GetLocations();
1866 Location second = locations->InAt(1);
1867 DCHECK(second.IsConstant());
1868
1869 Register out = OutputRegister(instruction);
1870 Register dividend = InputRegisterAt(instruction, 0);
1871 int64_t imm = Int64FromConstant(second.GetConstant());
1872 DCHECK(imm == 1 || imm == -1);
1873
1874 if (instruction->IsRem()) {
1875 __ Mov(out, 0);
1876 } else {
1877 if (imm == 1) {
1878 __ Mov(out, dividend);
1879 } else {
1880 __ Neg(out, dividend);
1881 }
1882 }
1883}
1884
1885void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1886 DCHECK(instruction->IsDiv() || instruction->IsRem());
1887
1888 LocationSummary* locations = instruction->GetLocations();
1889 Location second = locations->InAt(1);
1890 DCHECK(second.IsConstant());
1891
1892 Register out = OutputRegister(instruction);
1893 Register dividend = InputRegisterAt(instruction, 0);
1894 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001895 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001896 DCHECK(IsPowerOfTwo(abs_imm));
1897 int ctz_imm = CTZ(abs_imm);
1898
1899 UseScratchRegisterScope temps(GetVIXLAssembler());
1900 Register temp = temps.AcquireSameSizeAs(out);
1901
1902 if (instruction->IsDiv()) {
1903 __ Add(temp, dividend, abs_imm - 1);
1904 __ Cmp(dividend, 0);
1905 __ Csel(out, temp, dividend, lt);
1906 if (imm > 0) {
1907 __ Asr(out, out, ctz_imm);
1908 } else {
1909 __ Neg(out, Operand(out, ASR, ctz_imm));
1910 }
1911 } else {
1912 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1913 __ Asr(temp, dividend, bits - 1);
1914 __ Lsr(temp, temp, bits - ctz_imm);
1915 __ Add(out, dividend, temp);
1916 __ And(out, out, abs_imm - 1);
1917 __ Sub(out, out, temp);
1918 }
1919}
1920
1921void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1922 DCHECK(instruction->IsDiv() || instruction->IsRem());
1923
1924 LocationSummary* locations = instruction->GetLocations();
1925 Location second = locations->InAt(1);
1926 DCHECK(second.IsConstant());
1927
1928 Register out = OutputRegister(instruction);
1929 Register dividend = InputRegisterAt(instruction, 0);
1930 int64_t imm = Int64FromConstant(second.GetConstant());
1931
1932 Primitive::Type type = instruction->GetResultType();
1933 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1934
1935 int64_t magic;
1936 int shift;
1937 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1938
1939 UseScratchRegisterScope temps(GetVIXLAssembler());
1940 Register temp = temps.AcquireSameSizeAs(out);
1941
1942 // temp = get_high(dividend * magic)
1943 __ Mov(temp, magic);
1944 if (type == Primitive::kPrimLong) {
1945 __ Smulh(temp, dividend, temp);
1946 } else {
1947 __ Smull(temp.X(), dividend, temp);
1948 __ Lsr(temp.X(), temp.X(), 32);
1949 }
1950
1951 if (imm > 0 && magic < 0) {
1952 __ Add(temp, temp, dividend);
1953 } else if (imm < 0 && magic > 0) {
1954 __ Sub(temp, temp, dividend);
1955 }
1956
1957 if (shift != 0) {
1958 __ Asr(temp, temp, shift);
1959 }
1960
1961 if (instruction->IsDiv()) {
1962 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1963 } else {
1964 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1965 // TODO: Strength reduction for msub.
1966 Register temp_imm = temps.AcquireSameSizeAs(out);
1967 __ Mov(temp_imm, imm);
1968 __ Msub(out, temp, temp_imm, dividend);
1969 }
1970}
1971
1972void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1973 DCHECK(instruction->IsDiv() || instruction->IsRem());
1974 Primitive::Type type = instruction->GetResultType();
1975 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1976
1977 LocationSummary* locations = instruction->GetLocations();
1978 Register out = OutputRegister(instruction);
1979 Location second = locations->InAt(1);
1980
1981 if (second.IsConstant()) {
1982 int64_t imm = Int64FromConstant(second.GetConstant());
1983
1984 if (imm == 0) {
1985 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1986 } else if (imm == 1 || imm == -1) {
1987 DivRemOneOrMinusOne(instruction);
1988 } else if (IsPowerOfTwo(std::abs(imm))) {
1989 DivRemByPowerOfTwo(instruction);
1990 } else {
1991 DCHECK(imm <= -2 || imm >= 2);
1992 GenerateDivRemWithAnyConstant(instruction);
1993 }
1994 } else {
1995 Register dividend = InputRegisterAt(instruction, 0);
1996 Register divisor = InputRegisterAt(instruction, 1);
1997 if (instruction->IsDiv()) {
1998 __ Sdiv(out, dividend, divisor);
1999 } else {
2000 UseScratchRegisterScope temps(GetVIXLAssembler());
2001 Register temp = temps.AcquireSameSizeAs(out);
2002 __ Sdiv(temp, dividend, divisor);
2003 __ Msub(out, temp, divisor, dividend);
2004 }
2005 }
2006}
2007
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002008void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2011 switch (div->GetResultType()) {
2012 case Primitive::kPrimInt:
2013 case Primitive::kPrimLong:
2014 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002015 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2017 break;
2018
2019 case Primitive::kPrimFloat:
2020 case Primitive::kPrimDouble:
2021 locations->SetInAt(0, Location::RequiresFpuRegister());
2022 locations->SetInAt(1, Location::RequiresFpuRegister());
2023 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2024 break;
2025
2026 default:
2027 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2028 }
2029}
2030
2031void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2032 Primitive::Type type = div->GetResultType();
2033 switch (type) {
2034 case Primitive::kPrimInt:
2035 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002036 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002037 break;
2038
2039 case Primitive::kPrimFloat:
2040 case Primitive::kPrimDouble:
2041 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2042 break;
2043
2044 default:
2045 LOG(FATAL) << "Unexpected div type " << type;
2046 }
2047}
2048
Alexandre Rames67555f72014-11-18 10:55:16 +00002049void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002050 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2051 ? LocationSummary::kCallOnSlowPath
2052 : LocationSummary::kNoCall;
2053 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002054 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2055 if (instruction->HasUses()) {
2056 locations->SetOut(Location::SameAsFirstInput());
2057 }
2058}
2059
2060void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2061 SlowPathCodeARM64* slow_path =
2062 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2063 codegen_->AddSlowPath(slow_path);
2064 Location value = instruction->GetLocations()->InAt(0);
2065
Alexandre Rames3e69f162014-12-10 10:36:50 +00002066 Primitive::Type type = instruction->GetType();
2067
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002068 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2069 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002070 return;
2071 }
2072
Alexandre Rames67555f72014-11-18 10:55:16 +00002073 if (value.IsConstant()) {
2074 int64_t divisor = Int64ConstantFrom(value);
2075 if (divisor == 0) {
2076 __ B(slow_path->GetEntryLabel());
2077 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002078 // A division by a non-null constant is valid. We don't need to perform
2079 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002080 }
2081 } else {
2082 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2083 }
2084}
2085
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002086void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2087 LocationSummary* locations =
2088 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2089 locations->SetOut(Location::ConstantLocation(constant));
2090}
2091
2092void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2093 UNUSED(constant);
2094 // Will be generated at use site.
2095}
2096
Alexandre Rames5319def2014-10-23 10:03:10 +01002097void LocationsBuilderARM64::VisitExit(HExit* exit) {
2098 exit->SetLocations(nullptr);
2099}
2100
2101void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002102 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002103}
2104
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002105void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2106 LocationSummary* locations =
2107 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2108 locations->SetOut(Location::ConstantLocation(constant));
2109}
2110
2111void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2112 UNUSED(constant);
2113 // Will be generated at use site.
2114}
2115
David Brazdilfc6a86a2015-06-26 10:33:45 +00002116void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002117 DCHECK(!successor->IsExitBlock());
2118 HBasicBlock* block = got->GetBlock();
2119 HInstruction* previous = got->GetPrevious();
2120 HLoopInformation* info = block->GetLoopInformation();
2121
David Brazdil46e2a392015-03-16 17:31:52 +00002122 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002123 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2124 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2125 return;
2126 }
2127 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2128 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2129 }
2130 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002131 __ B(codegen_->GetLabelOf(successor));
2132 }
2133}
2134
David Brazdilfc6a86a2015-06-26 10:33:45 +00002135void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2136 got->SetLocations(nullptr);
2137}
2138
2139void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2140 HandleGoto(got, got->GetSuccessor());
2141}
2142
2143void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2144 try_boundary->SetLocations(nullptr);
2145}
2146
2147void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2148 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2149 if (!successor->IsExitBlock()) {
2150 HandleGoto(try_boundary, successor);
2151 }
2152}
2153
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002154void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2155 vixl::Label* true_target,
2156 vixl::Label* false_target,
2157 vixl::Label* always_true_target) {
2158 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002159 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002160
Serban Constantinescu02164b32014-11-13 14:05:07 +00002161 if (cond->IsIntConstant()) {
2162 int32_t cond_value = cond->AsIntConstant()->GetValue();
2163 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002164 if (always_true_target != nullptr) {
2165 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002166 }
2167 return;
2168 } else {
2169 DCHECK_EQ(cond_value, 0);
2170 }
2171 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002173 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002174 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002175 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002176 } else {
2177 // The condition instruction has not been materialized, use its inputs as
2178 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002179 Primitive::Type type =
2180 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2181
2182 if (Primitive::IsFloatingPointType(type)) {
2183 // FP compares don't like null false_targets.
2184 if (false_target == nullptr) {
2185 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002186 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002187 FPRegister lhs = InputFPRegisterAt(condition, 0);
2188 if (condition->GetLocations()->InAt(1).IsConstant()) {
2189 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2190 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2191 __ Fcmp(lhs, 0.0);
2192 } else {
2193 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2194 }
2195 if (condition->IsFPConditionTrueIfNaN()) {
2196 __ B(vs, true_target); // VS for unordered.
2197 } else if (condition->IsFPConditionFalseIfNaN()) {
2198 __ B(vs, false_target); // VS for unordered.
2199 }
2200 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002201 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002202 // Integer cases.
2203 Register lhs = InputRegisterAt(condition, 0);
2204 Operand rhs = InputOperandAt(condition, 1);
2205 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2206 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2207 switch (arm64_cond) {
2208 case eq:
2209 __ Cbz(lhs, true_target);
2210 break;
2211 case ne:
2212 __ Cbnz(lhs, true_target);
2213 break;
2214 case lt:
2215 // Test the sign bit and branch accordingly.
2216 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2217 break;
2218 case ge:
2219 // Test the sign bit and branch accordingly.
2220 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2221 break;
2222 default:
2223 // Without the `static_cast` the compiler throws an error for
2224 // `-Werror=sign-promo`.
2225 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2226 }
2227 } else {
2228 __ Cmp(lhs, rhs);
2229 __ B(arm64_cond, true_target);
2230 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002231 }
2232 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002233 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002234 __ B(false_target);
2235 }
2236}
2237
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002238void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2239 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2240 HInstruction* cond = if_instr->InputAt(0);
2241 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2242 locations->SetInAt(0, Location::RequiresRegister());
2243 }
2244}
2245
2246void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2247 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2248 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2249 vixl::Label* always_true_target = true_target;
2250 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2251 if_instr->IfTrueSuccessor())) {
2252 always_true_target = nullptr;
2253 }
2254 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2255 if_instr->IfFalseSuccessor())) {
2256 false_target = nullptr;
2257 }
2258 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2259}
2260
2261void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2262 LocationSummary* locations = new (GetGraph()->GetArena())
2263 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2264 HInstruction* cond = deoptimize->InputAt(0);
2265 DCHECK(cond->IsCondition());
2266 if (cond->AsCondition()->NeedsMaterialization()) {
2267 locations->SetInAt(0, Location::RequiresRegister());
2268 }
2269}
2270
2271void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2272 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2273 DeoptimizationSlowPathARM64(deoptimize);
2274 codegen_->AddSlowPath(slow_path);
2275 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2276 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2277}
2278
Alexandre Rames5319def2014-10-23 10:03:10 +01002279void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002280 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002281}
2282
2283void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002284 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002285}
2286
2287void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002288 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002289}
2290
2291void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002292 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002293}
2294
Alexandre Rames67555f72014-11-18 10:55:16 +00002295void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray75374372015-09-17 17:12:19 +00002296 LocationSummary::CallKind call_kind =
2297 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexandre Rames67555f72014-11-18 10:55:16 +00002298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray75374372015-09-17 17:12:19 +00002299 locations->SetInAt(0, Location::RequiresRegister());
2300 locations->SetInAt(1, Location::RequiresRegister());
2301 // The output does overlap inputs.
2302 // Note that TypeCheckSlowPathARM64 uses this register too.
2303 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002304}
2305
2306void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2307 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray75374372015-09-17 17:12:19 +00002308 Register obj = InputRegisterAt(instruction, 0);;
2309 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames67555f72014-11-18 10:55:16 +00002310 Register out = OutputRegister(instruction);
2311
Nicolas Geoffray75374372015-09-17 17:12:19 +00002312 vixl::Label done;
Alexandre Rames67555f72014-11-18 10:55:16 +00002313
2314 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002315 // Avoid null check if we know `obj` is not null.
2316 if (instruction->MustDoNullCheck()) {
2317 __ Mov(out, 0);
2318 __ Cbz(obj, &done);
2319 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002320
Nicolas Geoffray75374372015-09-17 17:12:19 +00002321 // Compare the class of `obj` with `cls`.
2322 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
2323 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
2324 __ Cmp(out, cls);
2325 if (instruction->IsClassFinal()) {
2326 // Classes must be equal for the instanceof to succeed.
2327 __ Cset(out, eq);
Alexandre Rames67555f72014-11-18 10:55:16 +00002328 } else {
Nicolas Geoffray75374372015-09-17 17:12:19 +00002329 // If the classes are not equal, we go into a slow path.
2330 DCHECK(locations->OnlyCallsOnSlowPath());
2331 SlowPathCodeARM64* slow_path =
2332 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction);
2333 codegen_->AddSlowPath(slow_path);
2334 __ B(ne, slow_path->GetEntryLabel());
2335 __ Mov(out, 1);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002336 __ Bind(slow_path->GetExitLabel());
2337 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002338
2339 __ Bind(&done);
Alexandre Rames67555f72014-11-18 10:55:16 +00002340}
2341
Alexandre Rames5319def2014-10-23 10:03:10 +01002342void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2344 locations->SetOut(Location::ConstantLocation(constant));
2345}
2346
2347void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2348 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002349 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002350}
2351
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002352void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2354 locations->SetOut(Location::ConstantLocation(constant));
2355}
2356
2357void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2358 // Will be generated at use site.
2359 UNUSED(constant);
2360}
2361
Calin Juravle175dc732015-08-25 15:42:32 +01002362void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2363 // The trampoline uses the same calling convention as dex calling conventions,
2364 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2365 // the method_idx.
2366 HandleInvoke(invoke);
2367}
2368
2369void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2370 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2371}
2372
Alexandre Rames5319def2014-10-23 10:03:10 +01002373void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002374 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002375 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002376}
2377
Alexandre Rames67555f72014-11-18 10:55:16 +00002378void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2379 HandleInvoke(invoke);
2380}
2381
2382void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2383 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002384 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2385 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2386 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002387 Location receiver = invoke->GetLocations()->InAt(0);
2388 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002389 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002390
2391 // The register ip1 is required to be used for the hidden argument in
2392 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002393 MacroAssembler* masm = GetVIXLAssembler();
2394 UseScratchRegisterScope scratch_scope(masm);
2395 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002396 scratch_scope.Exclude(ip1);
2397 __ Mov(ip1, invoke->GetDexMethodIndex());
2398
2399 // temp = object->GetClass();
2400 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401 __ Ldr(temp.W(), StackOperandFrom(receiver));
2402 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002403 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002404 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002405 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002406 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002407 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002408 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002409 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002410 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002411 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002412 // lr();
2413 __ Blr(lr);
2414 DCHECK(!codegen_->IsLeafMethod());
2415 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2416}
2417
2418void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002419 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2420 if (intrinsic.TryDispatch(invoke)) {
2421 return;
2422 }
2423
Alexandre Rames67555f72014-11-18 10:55:16 +00002424 HandleInvoke(invoke);
2425}
2426
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002427void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002428 // When we do not run baseline, explicit clinit checks triggered by static
2429 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2430 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002431
Andreas Gampe878d58c2015-01-15 23:24:00 -08002432 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2433 if (intrinsic.TryDispatch(invoke)) {
2434 return;
2435 }
2436
Alexandre Rames67555f72014-11-18 10:55:16 +00002437 HandleInvoke(invoke);
2438}
2439
Andreas Gampe878d58c2015-01-15 23:24:00 -08002440static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2441 if (invoke->GetLocations()->Intrinsified()) {
2442 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2443 intrinsic.Dispatch(invoke);
2444 return true;
2445 }
2446 return false;
2447}
2448
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002449void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002450 // For better instruction scheduling we load the direct code pointer before the method pointer.
2451 bool direct_code_loaded = false;
2452 switch (invoke->GetCodePtrLocation()) {
2453 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2454 // LR = code address from literal pool with link-time patch.
2455 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2456 direct_code_loaded = true;
2457 break;
2458 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2459 // LR = invoke->GetDirectCodePtr();
2460 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2461 direct_code_loaded = true;
2462 break;
2463 default:
2464 break;
2465 }
2466
Andreas Gampe878d58c2015-01-15 23:24:00 -08002467 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002468 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2469 switch (invoke->GetMethodLoadKind()) {
2470 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2471 // temp = thread->string_init_entrypoint
2472 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2473 break;
2474 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2475 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2476 break;
2477 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2478 // Load method address from literal pool.
2479 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2480 break;
2481 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2482 // Load method address from literal pool with a link-time patch.
2483 __ Ldr(XRegisterFrom(temp).X(),
2484 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2485 break;
2486 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2487 // Add ADRP with its PC-relative DexCache access patch.
2488 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2489 invoke->GetDexCacheArrayOffset());
2490 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2491 {
2492 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2493 __ adrp(XRegisterFrom(temp).X(), 0);
2494 }
2495 __ Bind(pc_insn_label); // Bind after ADRP.
2496 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2497 // Add LDR with its PC-relative DexCache access patch.
2498 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2499 invoke->GetDexCacheArrayOffset());
2500 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2501 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2502 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2503 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002504 }
Vladimir Marko58155012015-08-19 12:49:41 +00002505 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2506 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2507 Register reg = XRegisterFrom(temp);
2508 Register method_reg;
2509 if (current_method.IsRegister()) {
2510 method_reg = XRegisterFrom(current_method);
2511 } else {
2512 DCHECK(invoke->GetLocations()->Intrinsified());
2513 DCHECK(!current_method.IsValid());
2514 method_reg = reg;
2515 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2516 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002517
Vladimir Marko58155012015-08-19 12:49:41 +00002518 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002519 __ Ldr(reg.X(),
2520 MemOperand(method_reg.X(),
2521 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002522 // temp = temp[index_in_cache];
2523 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2524 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2525 break;
2526 }
2527 }
2528
2529 switch (invoke->GetCodePtrLocation()) {
2530 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2531 __ Bl(&frame_entry_label_);
2532 break;
2533 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2534 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2535 vixl::Label* label = &relative_call_patches_.back().label;
2536 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2537 __ Bind(label); // Bind after BL.
2538 break;
2539 }
2540 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2541 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2542 // LR prepared above for better instruction scheduling.
2543 DCHECK(direct_code_loaded);
2544 // lr()
2545 __ Blr(lr);
2546 break;
2547 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2548 // LR = callee_method->entry_point_from_quick_compiled_code_;
2549 __ Ldr(lr, MemOperand(
2550 XRegisterFrom(callee_method).X(),
2551 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2552 // lr()
2553 __ Blr(lr);
2554 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002555 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002556
Andreas Gampe878d58c2015-01-15 23:24:00 -08002557 DCHECK(!IsLeafMethod());
2558}
2559
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002560void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2561 LocationSummary* locations = invoke->GetLocations();
2562 Location receiver = locations->InAt(0);
2563 Register temp = XRegisterFrom(temp_in);
2564 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2565 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2566 Offset class_offset = mirror::Object::ClassOffset();
2567 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2568
2569 BlockPoolsScope block_pools(GetVIXLAssembler());
2570
2571 DCHECK(receiver.IsRegister());
2572 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2573 MaybeRecordImplicitNullCheck(invoke);
2574 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2575 // temp = temp->GetMethodAt(method_offset);
2576 __ Ldr(temp, MemOperand(temp, method_offset));
2577 // lr = temp->GetEntryPoint();
2578 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2579 // lr();
2580 __ Blr(lr);
2581}
2582
Vladimir Marko58155012015-08-19 12:49:41 +00002583void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2584 DCHECK(linker_patches->empty());
2585 size_t size =
2586 method_patches_.size() +
2587 call_patches_.size() +
2588 relative_call_patches_.size() +
2589 pc_rel_dex_cache_patches_.size();
2590 linker_patches->reserve(size);
2591 for (const auto& entry : method_patches_) {
2592 const MethodReference& target_method = entry.first;
2593 vixl::Literal<uint64_t>* literal = entry.second;
2594 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2595 target_method.dex_file,
2596 target_method.dex_method_index));
2597 }
2598 for (const auto& entry : call_patches_) {
2599 const MethodReference& target_method = entry.first;
2600 vixl::Literal<uint64_t>* literal = entry.second;
2601 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2602 target_method.dex_file,
2603 target_method.dex_method_index));
2604 }
2605 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2606 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2607 info.target_method.dex_file,
2608 info.target_method.dex_method_index));
2609 }
2610 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2611 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2612 &info.target_dex_file,
2613 info.pc_insn_label->location() - 4u,
2614 info.element_offset));
2615 }
2616}
2617
2618vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2619 // Look up the literal for value.
2620 auto lb = uint64_literals_.lower_bound(value);
2621 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2622 return lb->second;
2623 }
2624 // We don't have a literal for this value, insert a new one.
2625 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2626 uint64_literals_.PutBefore(lb, value, literal);
2627 return literal;
2628}
2629
2630vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2631 MethodReference target_method,
2632 MethodToLiteralMap* map) {
2633 // Look up the literal for target_method.
2634 auto lb = map->lower_bound(target_method);
2635 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2636 return lb->second;
2637 }
2638 // We don't have a literal for this method yet, insert a new one.
2639 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2640 map->PutBefore(lb, target_method, literal);
2641 return literal;
2642}
2643
2644vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2645 MethodReference target_method) {
2646 return DeduplicateMethodLiteral(target_method, &method_patches_);
2647}
2648
2649vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2650 MethodReference target_method) {
2651 return DeduplicateMethodLiteral(target_method, &call_patches_);
2652}
2653
2654
Andreas Gampe878d58c2015-01-15 23:24:00 -08002655void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002656 // When we do not run baseline, explicit clinit checks triggered by static
2657 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2658 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002659
Andreas Gampe878d58c2015-01-15 23:24:00 -08002660 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2661 return;
2662 }
2663
Alexandre Ramesd921d642015-04-16 15:07:16 +01002664 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002665 LocationSummary* locations = invoke->GetLocations();
2666 codegen_->GenerateStaticOrDirectCall(
2667 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002668 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002669}
2670
2671void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002672 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2673 return;
2674 }
2675
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002676 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002677 DCHECK(!codegen_->IsLeafMethod());
2678 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2679}
2680
Alexandre Rames67555f72014-11-18 10:55:16 +00002681void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2682 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2683 : LocationSummary::kNoCall;
2684 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002685 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002686 locations->SetOut(Location::RequiresRegister());
2687}
2688
2689void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2690 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002691 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002692 if (cls->IsReferrersClass()) {
2693 DCHECK(!cls->CanCallRuntime());
2694 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002695 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002696 } else {
2697 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002698 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
2699 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
2700 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2701 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002702
2703 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2704 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2705 codegen_->AddSlowPath(slow_path);
2706 __ Cbz(out, slow_path->GetEntryLabel());
2707 if (cls->MustGenerateClinitCheck()) {
2708 GenerateClassInitializationCheck(slow_path, out);
2709 } else {
2710 __ Bind(slow_path->GetExitLabel());
2711 }
2712 }
2713}
2714
David Brazdilcb1c0552015-08-04 16:22:25 +01002715static MemOperand GetExceptionTlsAddress() {
2716 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2717}
2718
Alexandre Rames67555f72014-11-18 10:55:16 +00002719void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2720 LocationSummary* locations =
2721 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2722 locations->SetOut(Location::RequiresRegister());
2723}
2724
2725void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002726 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2727}
2728
2729void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2730 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2731}
2732
2733void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2734 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002735}
2736
Alexandre Rames5319def2014-10-23 10:03:10 +01002737void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2738 load->SetLocations(nullptr);
2739}
2740
2741void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2742 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002743 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002744}
2745
Alexandre Rames67555f72014-11-18 10:55:16 +00002746void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2747 LocationSummary* locations =
2748 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002749 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002750 locations->SetOut(Location::RequiresRegister());
2751}
2752
2753void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2754 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2755 codegen_->AddSlowPath(slow_path);
2756
2757 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002758 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002759 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002760 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
2761 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2762 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002763 __ Cbz(out, slow_path->GetEntryLabel());
2764 __ Bind(slow_path->GetExitLabel());
2765}
2766
Alexandre Rames5319def2014-10-23 10:03:10 +01002767void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2768 local->SetLocations(nullptr);
2769}
2770
2771void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2772 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2773}
2774
2775void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2777 locations->SetOut(Location::ConstantLocation(constant));
2778}
2779
2780void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2781 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002782 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002783}
2784
Alexandre Rames67555f72014-11-18 10:55:16 +00002785void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2786 LocationSummary* locations =
2787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2788 InvokeRuntimeCallingConvention calling_convention;
2789 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2790}
2791
2792void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2793 codegen_->InvokeRuntime(instruction->IsEnter()
2794 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2795 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002796 instruction->GetDexPc(),
2797 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002798 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002799}
2800
Alexandre Rames42d641b2014-10-27 14:00:51 +00002801void LocationsBuilderARM64::VisitMul(HMul* mul) {
2802 LocationSummary* locations =
2803 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2804 switch (mul->GetResultType()) {
2805 case Primitive::kPrimInt:
2806 case Primitive::kPrimLong:
2807 locations->SetInAt(0, Location::RequiresRegister());
2808 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002809 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002810 break;
2811
2812 case Primitive::kPrimFloat:
2813 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002814 locations->SetInAt(0, Location::RequiresFpuRegister());
2815 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002816 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002817 break;
2818
2819 default:
2820 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2821 }
2822}
2823
2824void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2825 switch (mul->GetResultType()) {
2826 case Primitive::kPrimInt:
2827 case Primitive::kPrimLong:
2828 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2829 break;
2830
2831 case Primitive::kPrimFloat:
2832 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002833 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002834 break;
2835
2836 default:
2837 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2838 }
2839}
2840
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002841void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2842 LocationSummary* locations =
2843 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2844 switch (neg->GetResultType()) {
2845 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002846 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002847 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002848 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002849 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002850
2851 case Primitive::kPrimFloat:
2852 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002853 locations->SetInAt(0, Location::RequiresFpuRegister());
2854 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002855 break;
2856
2857 default:
2858 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2859 }
2860}
2861
2862void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2863 switch (neg->GetResultType()) {
2864 case Primitive::kPrimInt:
2865 case Primitive::kPrimLong:
2866 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2867 break;
2868
2869 case Primitive::kPrimFloat:
2870 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002871 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002872 break;
2873
2874 default:
2875 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2876 }
2877}
2878
2879void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2880 LocationSummary* locations =
2881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2882 InvokeRuntimeCallingConvention calling_convention;
2883 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002884 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002885 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002886 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002887 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002888 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002889}
2890
2891void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2892 LocationSummary* locations = instruction->GetLocations();
2893 InvokeRuntimeCallingConvention calling_convention;
2894 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2895 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002896 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002897 // Note: if heap poisoning is enabled, the entry point takes cares
2898 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01002899 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2900 instruction,
2901 instruction->GetDexPc(),
2902 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002903 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002904}
2905
Alexandre Rames5319def2014-10-23 10:03:10 +01002906void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2907 LocationSummary* locations =
2908 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2909 InvokeRuntimeCallingConvention calling_convention;
2910 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002911 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002912 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002913 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002914}
2915
2916void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2917 LocationSummary* locations = instruction->GetLocations();
2918 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2919 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002920 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002921 // Note: if heap poisoning is enabled, the entry point takes cares
2922 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01002923 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2924 instruction,
2925 instruction->GetDexPc(),
2926 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002927 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002928}
2929
2930void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002932 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002934}
2935
2936void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002937 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002938 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002939 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002940 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002941 break;
2942
2943 default:
2944 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2945 }
2946}
2947
David Brazdil66d126e2015-04-03 16:02:44 +01002948void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2952}
2953
2954void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002955 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2956}
2957
Alexandre Rames5319def2014-10-23 10:03:10 +01002958void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002959 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2960 ? LocationSummary::kCallOnSlowPath
2961 : LocationSummary::kNoCall;
2962 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01002963 locations->SetInAt(0, Location::RequiresRegister());
2964 if (instruction->HasUses()) {
2965 locations->SetOut(Location::SameAsFirstInput());
2966 }
2967}
2968
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002969void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002970 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2971 return;
2972 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002973
Alexandre Ramesd921d642015-04-16 15:07:16 +01002974 BlockPoolsScope block_pools(GetVIXLAssembler());
2975 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002976 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2977 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2978}
2979
2980void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002981 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2982 codegen_->AddSlowPath(slow_path);
2983
2984 LocationSummary* locations = instruction->GetLocations();
2985 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002986
2987 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002988}
2989
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002990void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002991 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002992 GenerateImplicitNullCheck(instruction);
2993 } else {
2994 GenerateExplicitNullCheck(instruction);
2995 }
2996}
2997
Alexandre Rames67555f72014-11-18 10:55:16 +00002998void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2999 HandleBinaryOp(instruction);
3000}
3001
3002void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3003 HandleBinaryOp(instruction);
3004}
3005
Alexandre Rames3e69f162014-12-10 10:36:50 +00003006void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3007 LOG(FATAL) << "Unreachable";
3008}
3009
3010void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3011 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3012}
3013
Alexandre Rames5319def2014-10-23 10:03:10 +01003014void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3016 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3017 if (location.IsStackSlot()) {
3018 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3019 } else if (location.IsDoubleStackSlot()) {
3020 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3021 }
3022 locations->SetOut(location);
3023}
3024
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003025void InstructionCodeGeneratorARM64::VisitParameterValue(
3026 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003027 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003028}
3029
3030void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3031 LocationSummary* locations =
3032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003033 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003034}
3035
3036void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3037 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3038 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003039}
3040
3041void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3043 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3044 locations->SetInAt(i, Location::Any());
3045 }
3046 locations->SetOut(Location::Any());
3047}
3048
3049void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003050 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003051 LOG(FATAL) << "Unreachable";
3052}
3053
Serban Constantinescu02164b32014-11-13 14:05:07 +00003054void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003055 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003056 LocationSummary::CallKind call_kind =
3057 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3059
3060 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003061 case Primitive::kPrimInt:
3062 case Primitive::kPrimLong:
3063 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003064 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3066 break;
3067
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003068 case Primitive::kPrimFloat:
3069 case Primitive::kPrimDouble: {
3070 InvokeRuntimeCallingConvention calling_convention;
3071 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3072 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3073 locations->SetOut(calling_convention.GetReturnLocation(type));
3074
3075 break;
3076 }
3077
Serban Constantinescu02164b32014-11-13 14:05:07 +00003078 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003079 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003080 }
3081}
3082
3083void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3084 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003085
Serban Constantinescu02164b32014-11-13 14:05:07 +00003086 switch (type) {
3087 case Primitive::kPrimInt:
3088 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003089 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003090 break;
3091 }
3092
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003093 case Primitive::kPrimFloat:
3094 case Primitive::kPrimDouble: {
3095 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3096 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003097 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003098 break;
3099 }
3100
Serban Constantinescu02164b32014-11-13 14:05:07 +00003101 default:
3102 LOG(FATAL) << "Unexpected rem type " << type;
3103 }
3104}
3105
Calin Juravle27df7582015-04-17 19:12:31 +01003106void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3107 memory_barrier->SetLocations(nullptr);
3108}
3109
3110void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3111 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3112}
3113
Alexandre Rames5319def2014-10-23 10:03:10 +01003114void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3115 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3116 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003117 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003118}
3119
3120void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003121 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003122 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003123}
3124
3125void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3126 instruction->SetLocations(nullptr);
3127}
3128
3129void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003130 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003131 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003132}
3133
Serban Constantinescu02164b32014-11-13 14:05:07 +00003134void LocationsBuilderARM64::VisitShl(HShl* shl) {
3135 HandleShift(shl);
3136}
3137
3138void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3139 HandleShift(shl);
3140}
3141
3142void LocationsBuilderARM64::VisitShr(HShr* shr) {
3143 HandleShift(shr);
3144}
3145
3146void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3147 HandleShift(shr);
3148}
3149
Alexandre Rames5319def2014-10-23 10:03:10 +01003150void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3152 Primitive::Type field_type = store->InputAt(1)->GetType();
3153 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003154 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003155 case Primitive::kPrimBoolean:
3156 case Primitive::kPrimByte:
3157 case Primitive::kPrimChar:
3158 case Primitive::kPrimShort:
3159 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003160 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003161 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3162 break;
3163
3164 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003165 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003166 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3167 break;
3168
3169 default:
3170 LOG(FATAL) << "Unimplemented local type " << field_type;
3171 }
3172}
3173
3174void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003175 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003176}
3177
3178void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003179 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003180}
3181
3182void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003183 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003184}
3185
Alexandre Rames67555f72014-11-18 10:55:16 +00003186void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003187 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003188}
3189
3190void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003191 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003192}
3193
3194void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003195 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003196}
3197
Alexandre Rames67555f72014-11-18 10:55:16 +00003198void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003199 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003200}
3201
Calin Juravle23a8e352015-09-08 19:56:31 +01003202void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3203 HUnresolvedInstanceFieldGet* instruction) {
3204 FieldAccessCallingConvetionARM64 calling_convention;
3205 codegen_->CreateUnresolvedFieldLocationSummary(
3206 instruction, instruction->GetFieldType(), calling_convention);
3207}
3208
3209void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3210 HUnresolvedInstanceFieldGet* instruction) {
3211 codegen_->GenerateUnresolvedFieldAccess(instruction,
3212 instruction->GetFieldType(),
3213 instruction->GetFieldIndex(),
3214 instruction->GetDexPc());
3215}
3216
3217void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3218 HUnresolvedInstanceFieldSet* instruction) {
3219 FieldAccessCallingConvetionARM64 calling_convention;
3220 codegen_->CreateUnresolvedFieldLocationSummary(
3221 instruction, instruction->GetFieldType(), calling_convention);
3222}
3223
3224void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3225 HUnresolvedInstanceFieldSet* instruction) {
3226 codegen_->GenerateUnresolvedFieldAccess(instruction,
3227 instruction->GetFieldType(),
3228 instruction->GetFieldIndex(),
3229 instruction->GetDexPc());
3230}
3231
3232void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3233 HUnresolvedStaticFieldGet* instruction) {
3234 FieldAccessCallingConvetionARM64 calling_convention;
3235 codegen_->CreateUnresolvedFieldLocationSummary(
3236 instruction, instruction->GetFieldType(), calling_convention);
3237}
3238
3239void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3240 HUnresolvedStaticFieldGet* instruction) {
3241 codegen_->GenerateUnresolvedFieldAccess(instruction,
3242 instruction->GetFieldType(),
3243 instruction->GetFieldIndex(),
3244 instruction->GetDexPc());
3245}
3246
3247void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3248 HUnresolvedStaticFieldSet* instruction) {
3249 FieldAccessCallingConvetionARM64 calling_convention;
3250 codegen_->CreateUnresolvedFieldLocationSummary(
3251 instruction, instruction->GetFieldType(), calling_convention);
3252}
3253
3254void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3255 HUnresolvedStaticFieldSet* instruction) {
3256 codegen_->GenerateUnresolvedFieldAccess(instruction,
3257 instruction->GetFieldType(),
3258 instruction->GetFieldIndex(),
3259 instruction->GetDexPc());
3260}
3261
Alexandre Rames5319def2014-10-23 10:03:10 +01003262void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3264}
3265
3266void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003267 HBasicBlock* block = instruction->GetBlock();
3268 if (block->GetLoopInformation() != nullptr) {
3269 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3270 // The back edge will generate the suspend check.
3271 return;
3272 }
3273 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3274 // The goto will generate the suspend check.
3275 return;
3276 }
3277 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003278}
3279
3280void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3281 temp->SetLocations(nullptr);
3282}
3283
3284void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3285 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003286 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003287}
3288
Alexandre Rames67555f72014-11-18 10:55:16 +00003289void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3290 LocationSummary* locations =
3291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3292 InvokeRuntimeCallingConvention calling_convention;
3293 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3294}
3295
3296void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3297 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003298 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003299 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003300}
3301
3302void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3303 LocationSummary* locations =
3304 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3305 Primitive::Type input_type = conversion->GetInputType();
3306 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003307 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003308 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3309 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3310 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3311 }
3312
Alexandre Rames542361f2015-01-29 16:57:31 +00003313 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003314 locations->SetInAt(0, Location::RequiresFpuRegister());
3315 } else {
3316 locations->SetInAt(0, Location::RequiresRegister());
3317 }
3318
Alexandre Rames542361f2015-01-29 16:57:31 +00003319 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003320 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3321 } else {
3322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3323 }
3324}
3325
3326void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3327 Primitive::Type result_type = conversion->GetResultType();
3328 Primitive::Type input_type = conversion->GetInputType();
3329
3330 DCHECK_NE(input_type, result_type);
3331
Alexandre Rames542361f2015-01-29 16:57:31 +00003332 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003333 int result_size = Primitive::ComponentSize(result_type);
3334 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003335 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003336 Register output = OutputRegister(conversion);
3337 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003338 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3339 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003340 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3341 // 'int' values are used directly as W registers, discarding the top
3342 // bits, so we don't need to sign-extend and can just perform a move.
3343 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3344 // top 32 bits of the target register. We theoretically could leave those
3345 // bits unchanged, but we would have to make sure that no code uses a
3346 // 32bit input value as a 64bit value assuming that the top 32 bits are
3347 // zero.
3348 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003349 } else if ((result_type == Primitive::kPrimChar) ||
3350 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3351 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003352 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003353 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003354 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003355 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003356 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003357 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003358 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3359 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003360 } else if (Primitive::IsFloatingPointType(result_type) &&
3361 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003362 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3363 } else {
3364 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3365 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003366 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003367}
Alexandre Rames67555f72014-11-18 10:55:16 +00003368
Serban Constantinescu02164b32014-11-13 14:05:07 +00003369void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3370 HandleShift(ushr);
3371}
3372
3373void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3374 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003375}
3376
3377void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3378 HandleBinaryOp(instruction);
3379}
3380
3381void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3382 HandleBinaryOp(instruction);
3383}
3384
Calin Juravleb1498f62015-02-16 13:13:29 +00003385void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3386 // Nothing to do, this should be removed during prepare for register allocator.
3387 UNUSED(instruction);
3388 LOG(FATAL) << "Unreachable";
3389}
3390
3391void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3392 // Nothing to do, this should be removed during prepare for register allocator.
3393 UNUSED(instruction);
3394 LOG(FATAL) << "Unreachable";
3395}
3396
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003397void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3398 DCHECK(codegen_->IsBaseline());
3399 LocationSummary* locations =
3400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3401 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3402}
3403
3404void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3405 DCHECK(codegen_->IsBaseline());
3406 // Will be generated at use site.
3407}
3408
Alexandre Rames67555f72014-11-18 10:55:16 +00003409#undef __
3410#undef QUICK_ENTRY_POINT
3411
Alexandre Rames5319def2014-10-23 10:03:10 +01003412} // namespace arm64
3413} // namespace art