blob: 377eaf6c40ddc4a56482eb020f3d3b861ee59f58 [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"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000031#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "thread.h"
33#include "utils/arm64/assembler_arm64.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37
38using namespace vixl; // NOLINT(build/namespaces)
39
40#ifdef __
41#error "ARM64 Codegen VIXL macro-assembler macro already defined."
42#endif
43
Alexandre Rames5319def2014-10-23 10:03:10 +010044namespace art {
45
46namespace arm64 {
47
Andreas Gampe878d58c2015-01-15 23:24:00 -080048using helpers::CPURegisterFrom;
49using helpers::DRegisterFrom;
50using helpers::FPRegisterFrom;
51using helpers::HeapOperand;
52using helpers::HeapOperandFrom;
53using helpers::InputCPURegisterAt;
54using helpers::InputFPRegisterAt;
55using helpers::InputRegisterAt;
56using helpers::InputOperandAt;
57using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::LocationFrom;
59using helpers::OperandFromMemOperand;
60using helpers::OutputCPURegister;
61using helpers::OutputFPRegister;
62using helpers::OutputRegister;
63using helpers::RegisterFrom;
64using helpers::StackOperandFrom;
65using helpers::VIXLRegCodeFromART;
66using helpers::WRegisterFrom;
67using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000068using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080069using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070
Alexandre Rames5319def2014-10-23 10:03:10 +010071static constexpr int kCurrentMethodStackOffset = 0;
72
Alexandre Rames5319def2014-10-23 10:03:10 +010073inline Condition ARM64Condition(IfCondition cond) {
74 switch (cond) {
75 case kCondEQ: return eq;
76 case kCondNE: return ne;
77 case kCondLT: return lt;
78 case kCondLE: return le;
79 case kCondGT: return gt;
80 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010081 }
Roland Levillain7f63c522015-07-13 15:54:55 +000082 LOG(FATAL) << "Unreachable";
83 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010084}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010096 } else if (return_type == Primitive::kPrimVoid) {
97 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000098 } else {
99 return LocationFrom(w0);
100 }
101}
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000104 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105}
106
Alexandre Rames67555f72014-11-18 10:55:16 +0000107#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
Zheng Xuda403092015-04-24 17:35:39 +0800110// Calculate memory accessing operand for save/restore live registers.
111static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
112 RegisterSet* register_set,
113 int64_t spill_offset,
114 bool is_save) {
115 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
116 codegen->GetNumberOfCoreRegisters(),
117 register_set->GetFloatingPointRegisters(),
118 codegen->GetNumberOfFloatingPointRegisters()));
119
120 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
121 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
122 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
123 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
124
125 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
126 UseScratchRegisterScope temps(masm);
127
128 Register base = masm->StackPointer();
129 int64_t core_spill_size = core_list.TotalSizeInBytes();
130 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
131 int64_t reg_size = kXRegSizeInBytes;
132 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
133 uint32_t ls_access_size = WhichPowerOf2(reg_size);
134 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
135 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
136 // If the offset does not fit in the instruction's immediate field, use an alternate register
137 // to compute the base address(float point registers spill base address).
138 Register new_base = temps.AcquireSameSizeAs(base);
139 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
140 base = new_base;
141 spill_offset = -core_spill_size;
142 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
143 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
144 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
145 }
146
147 if (is_save) {
148 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
149 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
150 } else {
151 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
152 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 }
154}
155
156void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
157 RegisterSet* register_set = locations->GetLiveRegisters();
158 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
159 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
160 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
161 // If the register holds an object, update the stack mask.
162 if (locations->RegisterContainsObject(i)) {
163 locations->SetStackBit(stack_offset / kVRegSize);
164 }
165 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
166 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
167 saved_core_stack_offsets_[i] = stack_offset;
168 stack_offset += kXRegSizeInBytes;
169 }
170 }
171
172 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
173 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
174 register_set->ContainsFloatingPointRegister(i)) {
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_fpu_stack_offsets_[i] = stack_offset;
178 stack_offset += kDRegSizeInBytes;
179 }
180 }
181
182 SaveRestoreLiveRegistersHelper(codegen, register_set,
183 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
184}
185
186void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 SaveRestoreLiveRegistersHelper(codegen, register_set,
189 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
190}
191
Alexandre Rames5319def2014-10-23 10:03:10 +0100192class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
193 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100194 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100195
Alexandre Rames67555f72014-11-18 10:55:16 +0000196 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100197 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000198 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199
Alexandre Rames5319def2014-10-23 10:03:10 +0100200 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000201 if (instruction_->CanThrowIntoCatchBlock()) {
202 // Live registers will be restored in the catch block if caught.
203 SaveLiveRegisters(codegen, instruction_->GetLocations());
204 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 // We're moving two locations to locations that could overlap, so we need a parallel
206 // move resolver.
207 InvokeRuntimeCallingConvention calling_convention;
208 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100209 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
210 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000211 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000212 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 }
215
Alexandre Rames8158f282015-08-07 10:26:17 +0100216 bool IsFatal() const OVERRIDE { return true; }
217
Alexandre Rames9931f312015-06-19 14:47:01 +0100218 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000222
Alexandre Rames5319def2014-10-23 10:03:10 +0100223 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
224};
225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
227 public:
228 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
232 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000233 if (instruction_->CanThrowIntoCatchBlock()) {
234 // Live registers will be restored in the catch block if caught.
235 SaveLiveRegisters(codegen, instruction_->GetLocations());
236 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000237 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000238 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800239 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000240 }
241
Alexandre Rames8158f282015-08-07 10:26:17 +0100242 bool IsFatal() const OVERRIDE { return true; }
243
Alexandre Rames9931f312015-06-19 14:47:01 +0100244 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
245
Alexandre Rames67555f72014-11-18 10:55:16 +0000246 private:
247 HDivZeroCheck* const instruction_;
248 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
249};
250
251class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
252 public:
253 LoadClassSlowPathARM64(HLoadClass* cls,
254 HInstruction* at,
255 uint32_t dex_pc,
256 bool do_clinit)
257 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
258 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
259 }
260
261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 LocationSummary* locations = at_->GetLocations();
263 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
264
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000267
268 InvokeRuntimeCallingConvention calling_convention;
269 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
271 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000272 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800273 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100274 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100276 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000278
279 // Move the class to the desired location.
280 Location out = locations->Out();
281 if (out.IsValid()) {
282 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
283 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000284 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 }
286
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000288 __ B(GetExitLabel());
289 }
290
Alexandre Rames9931f312015-06-19 14:47:01 +0100291 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
292
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 private:
294 // The class this slow path will load.
295 HLoadClass* const cls_;
296
297 // The instruction where this slow path is happening.
298 // (Might be the load class or an initialization check).
299 HInstruction* const at_;
300
301 // The dex PC of `at_`.
302 const uint32_t dex_pc_;
303
304 // Whether to initialize the class.
305 const bool do_clinit_;
306
307 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
308};
309
310class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
311 public:
312 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
313
314 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
315 LocationSummary* locations = instruction_->GetLocations();
316 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
317 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
318
319 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000320 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000321
322 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800323 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000325 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100326 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000327 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000328 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000329
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000330 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000331 __ B(GetExitLabel());
332 }
333
Alexandre Rames9931f312015-06-19 14:47:01 +0100334 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
335
Alexandre Rames67555f72014-11-18 10:55:16 +0000336 private:
337 HLoadString* const instruction_;
338
339 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
340};
341
Alexandre Rames5319def2014-10-23 10:03:10 +0100342class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
343 public:
344 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
345
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100348 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000349 if (instruction_->CanThrowIntoCatchBlock()) {
350 // Live registers will be restored in the catch block if caught.
351 SaveLiveRegisters(codegen, instruction_->GetLocations());
352 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000354 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800355 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100356 }
357
Alexandre Rames8158f282015-08-07 10:26:17 +0100358 bool IsFatal() const OVERRIDE { return true; }
359
Alexandre Rames9931f312015-06-19 14:47:01 +0100360 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
361
Alexandre Rames5319def2014-10-23 10:03:10 +0100362 private:
363 HNullCheck* const instruction_;
364
365 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
366};
367
368class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
369 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100370 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100371 : instruction_(instruction), successor_(successor) {}
372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000376 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000377 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000378 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800379 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000380 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 if (successor_ == nullptr) {
382 __ B(GetReturnLabel());
383 } else {
384 __ B(arm64_codegen->GetLabelOf(successor_));
385 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100386 }
387
388 vixl::Label* GetReturnLabel() {
389 DCHECK(successor_ == nullptr);
390 return &return_label_;
391 }
392
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100393 HBasicBlock* GetSuccessor() const {
394 return successor_;
395 }
396
Alexandre Rames9931f312015-06-19 14:47:01 +0100397 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
398
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 private:
400 HSuspendCheck* const instruction_;
401 // If not null, the block to branch to after the suspend check.
402 HBasicBlock* const successor_;
403
404 // If `successor_` is null, the label to branch to after the suspend check.
405 vixl::Label return_label_;
406
407 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
408};
409
Alexandre Rames67555f72014-11-18 10:55:16 +0000410class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
411 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100412 explicit TypeCheckSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100416 Location class_to_check = locations->InAt(1);
417 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
418 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000419 DCHECK(instruction_->IsCheckCast()
420 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
421 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100422 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
Alexandre Rames67555f72014-11-18 10:55:16 +0000424 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000425 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426
427 // We're moving two locations to locations that could overlap, so we need a parallel
428 // move resolver.
429 InvokeRuntimeCallingConvention calling_convention;
430 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100431 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
432 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433
434 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000435 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100436 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000437 Primitive::Type ret_type = instruction_->GetType();
438 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
439 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800440 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
441 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 } else {
443 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100444 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800445 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446 }
447
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000448 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000449 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000450 }
451
Alexandre Rames9931f312015-06-19 14:47:01 +0100452 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
453
Alexandre Rames67555f72014-11-18 10:55:16 +0000454 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455 HInstruction* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000456
Alexandre Rames67555f72014-11-18 10:55:16 +0000457 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
458};
459
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700460class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
462 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
463 : instruction_(instruction) {}
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 __ Bind(GetEntryLabel());
467 SaveLiveRegisters(codegen, instruction_->GetLocations());
468 DCHECK(instruction_->IsDeoptimize());
469 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
470 uint32_t dex_pc = deoptimize->GetDexPc();
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
473 }
474
Alexandre Rames9931f312015-06-19 14:47:01 +0100475 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700477 private:
478 HInstruction* const instruction_;
479 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
480};
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482#undef __
483
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100484Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100485 Location next_location;
486 if (type == Primitive::kPrimVoid) {
487 LOG(FATAL) << "Unreachable type " << type;
488 }
489
Alexandre Rames542361f2015-01-29 16:57:31 +0000490 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100491 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
492 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000493 } else if (!Primitive::IsFloatingPointType(type) &&
494 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
496 } else {
497 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000498 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
499 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 }
501
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000502 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000503 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 return next_location;
505}
506
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100507Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100508 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100509}
510
Serban Constantinescu579885a2015-02-22 20:51:33 +0000511CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
512 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100513 const CompilerOptions& compiler_options,
514 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 : CodeGenerator(graph,
516 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000517 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000518 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000519 callee_saved_core_registers.list(),
520 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100521 compiler_options,
522 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100523 block_labels_(nullptr),
524 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000525 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000526 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000527 isa_features_(isa_features),
528 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
529 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
530 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
531 relative_call_patches_(graph->GetArena()->Adapter()),
532 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000533 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000534 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000535}
Alexandre Rames5319def2014-10-23 10:03:10 +0100536
Alexandre Rames67555f72014-11-18 10:55:16 +0000537#undef __
538#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100539
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000540void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
541 // Ensure we emit the literal pool.
542 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000543
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000544 CodeGenerator::Finalize(allocator);
545}
546
Zheng Xuad4450e2015-04-17 18:48:56 +0800547void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
548 // Note: There are 6 kinds of moves:
549 // 1. constant -> GPR/FPR (non-cycle)
550 // 2. constant -> stack (non-cycle)
551 // 3. GPR/FPR -> GPR/FPR
552 // 4. GPR/FPR -> stack
553 // 5. stack -> GPR/FPR
554 // 6. stack -> stack (non-cycle)
555 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
556 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
557 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
558 // dependency.
559 vixl_temps_.Open(GetVIXLAssembler());
560}
561
562void ParallelMoveResolverARM64::FinishEmitNativeCode() {
563 vixl_temps_.Close();
564}
565
566Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
567 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
568 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
569 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
570 Location scratch = GetScratchLocation(kind);
571 if (!scratch.Equals(Location::NoLocation())) {
572 return scratch;
573 }
574 // Allocate from VIXL temp registers.
575 if (kind == Location::kRegister) {
576 scratch = LocationFrom(vixl_temps_.AcquireX());
577 } else {
578 DCHECK(kind == Location::kFpuRegister);
579 scratch = LocationFrom(vixl_temps_.AcquireD());
580 }
581 AddScratchLocation(scratch);
582 return scratch;
583}
584
585void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
586 if (loc.IsRegister()) {
587 vixl_temps_.Release(XRegisterFrom(loc));
588 } else {
589 DCHECK(loc.IsFpuRegister());
590 vixl_temps_.Release(DRegisterFrom(loc));
591 }
592 RemoveScratchLocation(loc);
593}
594
Alexandre Rames3e69f162014-12-10 10:36:50 +0000595void ParallelMoveResolverARM64::EmitMove(size_t index) {
596 MoveOperands* move = moves_.Get(index);
597 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
598}
599
Alexandre Rames5319def2014-10-23 10:03:10 +0100600void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100601 MacroAssembler* masm = GetVIXLAssembler();
602 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000603 __ Bind(&frame_entry_label_);
604
Serban Constantinescu02164b32014-11-13 14:05:07 +0000605 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
606 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100607 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000608 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000609 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000610 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000611 __ Ldr(wzr, MemOperand(temp, 0));
612 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000613 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100614
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000615 if (!HasEmptyFrame()) {
616 int frame_size = GetFrameSize();
617 // Stack layout:
618 // sp[frame_size - 8] : lr.
619 // ... : other preserved core registers.
620 // ... : other preserved fp registers.
621 // ... : reserved frame space.
622 // sp[0] : current method.
623 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100624 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800625 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
626 frame_size - GetCoreSpillSize());
627 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
628 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000629 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100630}
631
632void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100633 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100634 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000635 if (!HasEmptyFrame()) {
636 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800637 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
638 frame_size - FrameEntrySpillSize());
639 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
640 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000641 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100642 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000643 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100644 __ Ret();
645 GetAssembler()->cfi().RestoreState();
646 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100647}
648
Zheng Xuda403092015-04-24 17:35:39 +0800649vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
650 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
651 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
652 core_spill_mask_);
653}
654
655vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
656 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
657 GetNumberOfFloatingPointRegisters()));
658 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
659 fpu_spill_mask_);
660}
661
Alexandre Rames5319def2014-10-23 10:03:10 +0100662void CodeGeneratorARM64::Bind(HBasicBlock* block) {
663 __ Bind(GetLabelOf(block));
664}
665
Alexandre Rames5319def2014-10-23 10:03:10 +0100666void CodeGeneratorARM64::Move(HInstruction* instruction,
667 Location location,
668 HInstruction* move_for) {
669 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100670 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000671 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100672
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100673 if (instruction->IsFakeString()) {
674 // The fake string is an alias for null.
675 DCHECK(IsBaseline());
676 instruction = locations->Out().GetConstant();
677 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
678 }
679
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100680 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700681 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100682 } else if (locations != nullptr && locations->Out().Equals(location)) {
683 return;
684 } else if (instruction->IsIntConstant()
685 || instruction->IsLongConstant()
686 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000687 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100688 if (location.IsRegister()) {
689 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000690 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 (instruction->IsLongConstant() && dst.Is64Bits()));
692 __ Mov(dst, value);
693 } else {
694 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000695 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000696 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
697 ? temps.AcquireW()
698 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100699 __ Mov(temp, value);
700 __ Str(temp, StackOperandFrom(location));
701 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000702 } else if (instruction->IsTemporary()) {
703 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000704 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100705 } else if (instruction->IsLoadLocal()) {
706 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000707 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000708 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000709 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000710 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100711 }
712
713 } else {
714 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000715 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100716 }
717}
718
Alexandre Rames5319def2014-10-23 10:03:10 +0100719Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
720 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000721
Alexandre Rames5319def2014-10-23 10:03:10 +0100722 switch (type) {
723 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000724 case Primitive::kPrimInt:
725 case Primitive::kPrimFloat:
726 return Location::StackSlot(GetStackSlot(load->GetLocal()));
727
728 case Primitive::kPrimLong:
729 case Primitive::kPrimDouble:
730 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
731
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 case Primitive::kPrimBoolean:
733 case Primitive::kPrimByte:
734 case Primitive::kPrimChar:
735 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100736 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100737 LOG(FATAL) << "Unexpected type " << type;
738 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000739
Alexandre Rames5319def2014-10-23 10:03:10 +0100740 LOG(FATAL) << "Unreachable";
741 return Location::NoLocation();
742}
743
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100744void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000745 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100746 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000747 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100749 if (value_can_be_null) {
750 __ Cbz(value, &done);
751 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100752 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
753 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000754 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100755 if (value_can_be_null) {
756 __ Bind(&done);
757 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100758}
759
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000760void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
761 // Blocked core registers:
762 // lr : Runtime reserved.
763 // tr : Runtime reserved.
764 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
765 // ip1 : VIXL core temp.
766 // ip0 : VIXL core temp.
767 //
768 // Blocked fp registers:
769 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100770 CPURegList reserved_core_registers = vixl_reserved_core_registers;
771 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 while (!reserved_core_registers.IsEmpty()) {
773 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
774 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000775
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000776 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800777 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000778 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
779 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000780
781 if (is_baseline) {
782 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
783 while (!reserved_core_baseline_registers.IsEmpty()) {
784 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
785 }
786
787 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
788 while (!reserved_fp_baseline_registers.IsEmpty()) {
789 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
790 }
791 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100792}
793
794Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
795 if (type == Primitive::kPrimVoid) {
796 LOG(FATAL) << "Unreachable type " << type;
797 }
798
Alexandre Rames542361f2015-01-29 16:57:31 +0000799 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000800 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
801 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100802 return Location::FpuRegisterLocation(reg);
803 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000804 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
805 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100806 return Location::RegisterLocation(reg);
807 }
808}
809
Alexandre Rames3e69f162014-12-10 10:36:50 +0000810size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
811 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
812 __ Str(reg, MemOperand(sp, stack_index));
813 return kArm64WordSize;
814}
815
816size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
817 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
818 __ Ldr(reg, MemOperand(sp, stack_index));
819 return kArm64WordSize;
820}
821
822size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
823 FPRegister reg = FPRegister(reg_id, kDRegSize);
824 __ Str(reg, MemOperand(sp, stack_index));
825 return kArm64WordSize;
826}
827
828size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
829 FPRegister reg = FPRegister(reg_id, kDRegSize);
830 __ Ldr(reg, MemOperand(sp, stack_index));
831 return kArm64WordSize;
832}
833
Alexandre Rames5319def2014-10-23 10:03:10 +0100834void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100835 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100836}
837
838void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100839 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100840}
841
Alexandre Rames67555f72014-11-18 10:55:16 +0000842void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000843 if (constant->IsIntConstant()) {
844 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
845 } else if (constant->IsLongConstant()) {
846 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
847 } else if (constant->IsNullConstant()) {
848 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000849 } else if (constant->IsFloatConstant()) {
850 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
851 } else {
852 DCHECK(constant->IsDoubleConstant());
853 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
854 }
855}
856
Alexandre Rames3e69f162014-12-10 10:36:50 +0000857
858static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
859 DCHECK(constant.IsConstant());
860 HConstant* cst = constant.GetConstant();
861 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000862 // Null is mapped to a core W register, which we associate with kPrimInt.
863 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000864 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
865 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
866 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
867}
868
869void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000870 if (source.Equals(destination)) {
871 return;
872 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000873
874 // A valid move can always be inferred from the destination and source
875 // locations. When moving from and to a register, the argument type can be
876 // used to generate 32bit instead of 64bit moves. In debug mode we also
877 // checks the coherency of the locations and the type.
878 bool unspecified_type = (type == Primitive::kPrimVoid);
879
880 if (destination.IsRegister() || destination.IsFpuRegister()) {
881 if (unspecified_type) {
882 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
883 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000884 (src_cst != nullptr && (src_cst->IsIntConstant()
885 || src_cst->IsFloatConstant()
886 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000887 // For stack slots and 32bit constants, a 64bit type is appropriate.
888 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000889 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 // If the source is a double stack slot or a 64bit constant, a 64bit
891 // type is appropriate. Else the source is a register, and since the
892 // type has not been specified, we chose a 64bit type to force a 64bit
893 // move.
894 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000895 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000896 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000897 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
898 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000899 CPURegister dst = CPURegisterFrom(destination, type);
900 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
901 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
902 __ Ldr(dst, StackOperandFrom(source));
903 } else if (source.IsConstant()) {
904 DCHECK(CoherentConstantAndType(source, type));
905 MoveConstant(dst, source.GetConstant());
906 } else {
907 if (destination.IsRegister()) {
908 __ Mov(Register(dst), RegisterFrom(source, type));
909 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800910 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000911 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
912 }
913 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000914 } else { // The destination is not a register. It must be a stack slot.
915 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
916 if (source.IsRegister() || source.IsFpuRegister()) {
917 if (unspecified_type) {
918 if (source.IsRegister()) {
919 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
920 } else {
921 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
922 }
923 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000924 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
925 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000926 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
927 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100928 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000929 UseScratchRegisterScope temps(GetVIXLAssembler());
930 HConstant* src_cst = source.GetConstant();
931 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000932 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000933 temp = temps.AcquireW();
934 } else if (src_cst->IsLongConstant()) {
935 temp = temps.AcquireX();
936 } else if (src_cst->IsFloatConstant()) {
937 temp = temps.AcquireS();
938 } else {
939 DCHECK(src_cst->IsDoubleConstant());
940 temp = temps.AcquireD();
941 }
942 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000943 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000944 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000945 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000946 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000947 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000948 // There is generally less pressure on FP registers.
949 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000950 __ Ldr(temp, StackOperandFrom(source));
951 __ Str(temp, StackOperandFrom(destination));
952 }
953 }
954}
955
956void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957 CPURegister dst,
958 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000959 switch (type) {
960 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000961 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000962 break;
963 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000964 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000965 break;
966 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000967 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000968 break;
969 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000970 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000971 break;
972 case Primitive::kPrimInt:
973 case Primitive::kPrimNot:
974 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000975 case Primitive::kPrimFloat:
976 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000977 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000978 __ Ldr(dst, src);
979 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000980 case Primitive::kPrimVoid:
981 LOG(FATAL) << "Unreachable type " << type;
982 }
983}
984
Calin Juravle77520bc2015-01-12 18:45:46 +0000985void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000986 CPURegister dst,
987 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100988 MacroAssembler* masm = GetVIXLAssembler();
989 BlockPoolsScope block_pools(masm);
990 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000992 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000993
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994 DCHECK(!src.IsPreIndex());
995 DCHECK(!src.IsPostIndex());
996
997 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800998 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 MemOperand base = MemOperand(temp_base);
1000 switch (type) {
1001 case Primitive::kPrimBoolean:
1002 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001003 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 break;
1005 case Primitive::kPrimByte:
1006 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001007 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001008 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1009 break;
1010 case Primitive::kPrimChar:
1011 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001012 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001013 break;
1014 case Primitive::kPrimShort:
1015 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001016 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001017 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1018 break;
1019 case Primitive::kPrimInt:
1020 case Primitive::kPrimNot:
1021 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001022 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001023 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001024 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001025 break;
1026 case Primitive::kPrimFloat:
1027 case Primitive::kPrimDouble: {
1028 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001029 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001030
1031 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1032 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001033 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 __ Fmov(FPRegister(dst), temp);
1035 break;
1036 }
1037 case Primitive::kPrimVoid:
1038 LOG(FATAL) << "Unreachable type " << type;
1039 }
1040}
1041
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001042void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 CPURegister src,
1044 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001045 switch (type) {
1046 case Primitive::kPrimBoolean:
1047 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001048 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001049 break;
1050 case Primitive::kPrimChar:
1051 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001053 break;
1054 case Primitive::kPrimInt:
1055 case Primitive::kPrimNot:
1056 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001057 case Primitive::kPrimFloat:
1058 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001059 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001061 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001062 case Primitive::kPrimVoid:
1063 LOG(FATAL) << "Unreachable type " << type;
1064 }
1065}
1066
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001067void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1068 CPURegister src,
1069 const MemOperand& dst) {
1070 UseScratchRegisterScope temps(GetVIXLAssembler());
1071 Register temp_base = temps.AcquireX();
1072
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001073 DCHECK(!dst.IsPreIndex());
1074 DCHECK(!dst.IsPostIndex());
1075
1076 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001077 Operand op = OperandFromMemOperand(dst);
1078 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001079 MemOperand base = MemOperand(temp_base);
1080 switch (type) {
1081 case Primitive::kPrimBoolean:
1082 case Primitive::kPrimByte:
1083 __ Stlrb(Register(src), base);
1084 break;
1085 case Primitive::kPrimChar:
1086 case Primitive::kPrimShort:
1087 __ Stlrh(Register(src), base);
1088 break;
1089 case Primitive::kPrimInt:
1090 case Primitive::kPrimNot:
1091 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001092 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001093 __ Stlr(Register(src), base);
1094 break;
1095 case Primitive::kPrimFloat:
1096 case Primitive::kPrimDouble: {
1097 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001098 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099
1100 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1101 __ Fmov(temp, FPRegister(src));
1102 __ Stlr(temp, base);
1103 break;
1104 }
1105 case Primitive::kPrimVoid:
1106 LOG(FATAL) << "Unreachable type " << type;
1107 }
1108}
1109
Alexandre Rames67555f72014-11-18 10:55:16 +00001110void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1111 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001112 uint32_t dex_pc,
1113 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001114 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001115 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001116 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1117 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001118 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001119}
1120
1121void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1122 vixl::Register class_reg) {
1123 UseScratchRegisterScope temps(GetVIXLAssembler());
1124 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001126 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001127
Serban Constantinescu02164b32014-11-13 14:05:07 +00001128 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001129 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001130 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1131 __ Add(temp, class_reg, status_offset);
1132 __ Ldar(temp, HeapOperand(temp));
1133 __ Cmp(temp, mirror::Class::kStatusInitialized);
1134 __ B(lt, slow_path->GetEntryLabel());
1135 } else {
1136 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1137 __ Cmp(temp, mirror::Class::kStatusInitialized);
1138 __ B(lt, slow_path->GetEntryLabel());
1139 __ Dmb(InnerShareable, BarrierReads);
1140 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001141 __ Bind(slow_path->GetExitLabel());
1142}
Alexandre Rames5319def2014-10-23 10:03:10 +01001143
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001144void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1145 BarrierType type = BarrierAll;
1146
1147 switch (kind) {
1148 case MemBarrierKind::kAnyAny:
1149 case MemBarrierKind::kAnyStore: {
1150 type = BarrierAll;
1151 break;
1152 }
1153 case MemBarrierKind::kLoadAny: {
1154 type = BarrierReads;
1155 break;
1156 }
1157 case MemBarrierKind::kStoreStore: {
1158 type = BarrierWrites;
1159 break;
1160 }
1161 default:
1162 LOG(FATAL) << "Unexpected memory barrier " << kind;
1163 }
1164 __ Dmb(InnerShareable, type);
1165}
1166
Serban Constantinescu02164b32014-11-13 14:05:07 +00001167void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1168 HBasicBlock* successor) {
1169 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001170 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1171 if (slow_path == nullptr) {
1172 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1173 instruction->SetSlowPath(slow_path);
1174 codegen_->AddSlowPath(slow_path);
1175 if (successor != nullptr) {
1176 DCHECK(successor->IsLoopHeader());
1177 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1178 }
1179 } else {
1180 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1181 }
1182
Serban Constantinescu02164b32014-11-13 14:05:07 +00001183 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1184 Register temp = temps.AcquireW();
1185
1186 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1187 if (successor == nullptr) {
1188 __ Cbnz(temp, slow_path->GetEntryLabel());
1189 __ Bind(slow_path->GetReturnLabel());
1190 } else {
1191 __ Cbz(temp, codegen_->GetLabelOf(successor));
1192 __ B(slow_path->GetEntryLabel());
1193 // slow_path will return to GetLabelOf(successor).
1194 }
1195}
1196
Alexandre Rames5319def2014-10-23 10:03:10 +01001197InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1198 CodeGeneratorARM64* codegen)
1199 : HGraphVisitor(graph),
1200 assembler_(codegen->GetAssembler()),
1201 codegen_(codegen) {}
1202
1203#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001204 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001205
1206#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1207
1208enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001209 // Using a base helps identify when we hit such breakpoints.
1210 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001211#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1212 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1213#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1214};
1215
1216#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1217 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001218 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001219 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1220 } \
1221 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1222 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1223 locations->SetOut(Location::Any()); \
1224 }
1225 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1226#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1227
1228#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001229#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001230
Alexandre Rames67555f72014-11-18 10:55:16 +00001231void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001232 DCHECK_EQ(instr->InputCount(), 2U);
1233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1234 Primitive::Type type = instr->GetResultType();
1235 switch (type) {
1236 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001237 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001238 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001239 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001241 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001242
1243 case Primitive::kPrimFloat:
1244 case Primitive::kPrimDouble:
1245 locations->SetInAt(0, Location::RequiresFpuRegister());
1246 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001247 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001248 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001249
Alexandre Rames5319def2014-10-23 10:03:10 +01001250 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001251 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001252 }
1253}
1254
Alexandre Rames09a99962015-04-15 11:47:56 +01001255void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1256 LocationSummary* locations =
1257 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1258 locations->SetInAt(0, Location::RequiresRegister());
1259 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1260 locations->SetOut(Location::RequiresFpuRegister());
1261 } else {
1262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1263 }
1264}
1265
1266void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1267 const FieldInfo& field_info) {
1268 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001269 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001270 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001271
1272 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1273 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1274
1275 if (field_info.IsVolatile()) {
1276 if (use_acquire_release) {
1277 // NB: LoadAcquire will record the pc info if needed.
1278 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1279 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001280 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001281 codegen_->MaybeRecordImplicitNullCheck(instruction);
1282 // For IRIW sequential consistency kLoadAny is not sufficient.
1283 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1284 }
1285 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001286 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001287 codegen_->MaybeRecordImplicitNullCheck(instruction);
1288 }
Roland Levillain4d027112015-07-01 15:41:14 +01001289
1290 if (field_type == Primitive::kPrimNot) {
1291 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1292 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001293}
1294
1295void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1296 LocationSummary* locations =
1297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1298 locations->SetInAt(0, Location::RequiresRegister());
1299 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1300 locations->SetInAt(1, Location::RequiresFpuRegister());
1301 } else {
1302 locations->SetInAt(1, Location::RequiresRegister());
1303 }
1304}
1305
1306void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001307 const FieldInfo& field_info,
1308 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001309 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001310 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001311
1312 Register obj = InputRegisterAt(instruction, 0);
1313 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001314 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001315 Offset offset = field_info.GetFieldOffset();
1316 Primitive::Type field_type = field_info.GetFieldType();
1317 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1318
Roland Levillain4d027112015-07-01 15:41:14 +01001319 {
1320 // We use a block to end the scratch scope before the write barrier, thus
1321 // freeing the temporary registers so they can be used in `MarkGCCard`.
1322 UseScratchRegisterScope temps(GetVIXLAssembler());
1323
1324 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1325 DCHECK(value.IsW());
1326 Register temp = temps.AcquireW();
1327 __ Mov(temp, value.W());
1328 GetAssembler()->PoisonHeapReference(temp.W());
1329 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001330 }
Roland Levillain4d027112015-07-01 15:41:14 +01001331
1332 if (field_info.IsVolatile()) {
1333 if (use_acquire_release) {
1334 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1335 codegen_->MaybeRecordImplicitNullCheck(instruction);
1336 } else {
1337 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1338 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1339 codegen_->MaybeRecordImplicitNullCheck(instruction);
1340 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1341 }
1342 } else {
1343 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1344 codegen_->MaybeRecordImplicitNullCheck(instruction);
1345 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001346 }
1347
1348 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001349 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001350 }
1351}
1352
Alexandre Rames67555f72014-11-18 10:55:16 +00001353void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001355
1356 switch (type) {
1357 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 case Primitive::kPrimLong: {
1359 Register dst = OutputRegister(instr);
1360 Register lhs = InputRegisterAt(instr, 0);
1361 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 if (instr->IsAdd()) {
1363 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001364 } else if (instr->IsAnd()) {
1365 __ And(dst, lhs, rhs);
1366 } else if (instr->IsOr()) {
1367 __ Orr(dst, lhs, rhs);
1368 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001369 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001370 } else {
1371 DCHECK(instr->IsXor());
1372 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001373 }
1374 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001375 }
1376 case Primitive::kPrimFloat:
1377 case Primitive::kPrimDouble: {
1378 FPRegister dst = OutputFPRegister(instr);
1379 FPRegister lhs = InputFPRegisterAt(instr, 0);
1380 FPRegister rhs = InputFPRegisterAt(instr, 1);
1381 if (instr->IsAdd()) {
1382 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001383 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001384 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001385 } else {
1386 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001387 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001388 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001389 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001391 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001392 }
1393}
1394
Serban Constantinescu02164b32014-11-13 14:05:07 +00001395void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1396 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1397
1398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1399 Primitive::Type type = instr->GetResultType();
1400 switch (type) {
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimLong: {
1403 locations->SetInAt(0, Location::RequiresRegister());
1404 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1405 locations->SetOut(Location::RequiresRegister());
1406 break;
1407 }
1408 default:
1409 LOG(FATAL) << "Unexpected shift type " << type;
1410 }
1411}
1412
1413void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1414 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1415
1416 Primitive::Type type = instr->GetType();
1417 switch (type) {
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimLong: {
1420 Register dst = OutputRegister(instr);
1421 Register lhs = InputRegisterAt(instr, 0);
1422 Operand rhs = InputOperandAt(instr, 1);
1423 if (rhs.IsImmediate()) {
1424 uint32_t shift_value = (type == Primitive::kPrimInt)
1425 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1426 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1427 if (instr->IsShl()) {
1428 __ Lsl(dst, lhs, shift_value);
1429 } else if (instr->IsShr()) {
1430 __ Asr(dst, lhs, shift_value);
1431 } else {
1432 __ Lsr(dst, lhs, shift_value);
1433 }
1434 } else {
1435 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1436
1437 if (instr->IsShl()) {
1438 __ Lsl(dst, lhs, rhs_reg);
1439 } else if (instr->IsShr()) {
1440 __ Asr(dst, lhs, rhs_reg);
1441 } else {
1442 __ Lsr(dst, lhs, rhs_reg);
1443 }
1444 }
1445 break;
1446 }
1447 default:
1448 LOG(FATAL) << "Unexpected shift operation type " << type;
1449 }
1450}
1451
Alexandre Rames5319def2014-10-23 10:03:10 +01001452void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001454}
1455
1456void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 HandleBinaryOp(instruction);
1458}
1459
1460void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1461 HandleBinaryOp(instruction);
1462}
1463
1464void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1465 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001466}
1467
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001468void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1469 LocationSummary* locations =
1470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001473 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1474 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1475 } else {
1476 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1477 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001478}
1479
1480void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1481 LocationSummary* locations = instruction->GetLocations();
1482 Primitive::Type type = instruction->GetType();
1483 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001484 Location index = locations->InAt(1);
1485 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001486 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001487 MacroAssembler* masm = GetVIXLAssembler();
1488 UseScratchRegisterScope temps(masm);
1489 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001490
1491 if (index.IsConstant()) {
1492 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001493 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001494 } else {
1495 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001496 __ Add(temp, obj, offset);
1497 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001498 }
1499
Alexandre Rames67555f72014-11-18 10:55:16 +00001500 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001502
1503 if (type == Primitive::kPrimNot) {
1504 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1505 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001506}
1507
Alexandre Rames5319def2014-10-23 10:03:10 +01001508void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1510 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001512}
1513
1514void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001515 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001516 __ Ldr(OutputRegister(instruction),
1517 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001519}
1520
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001521void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001522 if (instruction->NeedsTypeCheck()) {
1523 LocationSummary* locations =
1524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 InvokeRuntimeCallingConvention calling_convention;
1526 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1527 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1528 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1529 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001530 LocationSummary* locations =
1531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001532 locations->SetInAt(0, Location::RequiresRegister());
1533 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001534 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1535 locations->SetInAt(2, Location::RequiresFpuRegister());
1536 } else {
1537 locations->SetInAt(2, Location::RequiresRegister());
1538 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001539 }
1540}
1541
1542void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1543 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001544 LocationSummary* locations = instruction->GetLocations();
1545 bool needs_runtime_call = locations->WillCall();
1546
1547 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001548 // Note: if heap poisoning is enabled, pAputObject takes cares
1549 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001550 codegen_->InvokeRuntime(
1551 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001552 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001553 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001554 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001555 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001556 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557 Location index = locations->InAt(1);
1558 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001559 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001560 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001561 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001562 {
1563 // We use a block to end the scratch scope before the write barrier, thus
1564 // freeing the temporary registers so they can be used in `MarkGCCard`.
1565 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001566
Roland Levillain4d027112015-07-01 15:41:14 +01001567 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1568 DCHECK(value.IsW());
1569 Register temp = temps.AcquireW();
1570 __ Mov(temp, value.W());
1571 GetAssembler()->PoisonHeapReference(temp.W());
1572 source = temp;
1573 }
1574
Alexandre Rames97833a02015-04-16 15:07:12 +01001575 if (index.IsConstant()) {
1576 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1577 destination = HeapOperand(obj, offset);
1578 } else {
1579 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001580 __ Add(temp, obj, offset);
1581 destination = HeapOperand(temp,
1582 XRegisterFrom(index),
1583 LSL,
1584 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001585 }
1586
Roland Levillain4d027112015-07-01 15:41:14 +01001587 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001588 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001590 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001591 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001592 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 }
1594}
1595
Alexandre Rames67555f72014-11-18 10:55:16 +00001596void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001597 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1598 ? LocationSummary::kCallOnSlowPath
1599 : LocationSummary::kNoCall;
1600 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001602 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001603 if (instruction->HasUses()) {
1604 locations->SetOut(Location::SameAsFirstInput());
1605 }
1606}
1607
1608void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001609 BoundsCheckSlowPathARM64* slow_path =
1610 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001611 codegen_->AddSlowPath(slow_path);
1612
1613 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1614 __ B(slow_path->GetEntryLabel(), hs);
1615}
1616
1617void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1619 instruction, LocationSummary::kCallOnSlowPath);
1620 locations->SetInAt(0, Location::RequiresRegister());
1621 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001622 // Note that TypeCheckSlowPathARM64 uses this register too.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001623 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001624}
1625
1626void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001627 Register obj = InputRegisterAt(instruction, 0);;
1628 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001629 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001630
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001631 SlowPathCodeARM64* slow_path =
1632 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001633 codegen_->AddSlowPath(slow_path);
1634
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001635 // Avoid null check if we know obj is not null.
1636 if (instruction->MustDoNullCheck()) {
1637 __ Cbz(obj, slow_path->GetExitLabel());
1638 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001639 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001640 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001641 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001642 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001643 // The checkcast succeeds if the classes are equal (fast path).
1644 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001645 __ B(ne, slow_path->GetEntryLabel());
1646 __ Bind(slow_path->GetExitLabel());
1647}
1648
1649void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1650 LocationSummary* locations =
1651 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1652 locations->SetInAt(0, Location::RequiresRegister());
1653 if (check->HasUses()) {
1654 locations->SetOut(Location::SameAsFirstInput());
1655 }
1656}
1657
1658void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1659 // We assume the class is not null.
1660 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1661 check->GetLoadClass(), check, check->GetDexPc(), true);
1662 codegen_->AddSlowPath(slow_path);
1663 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1664}
1665
Roland Levillain7f63c522015-07-13 15:54:55 +00001666static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1667 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1668 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1669}
1670
Serban Constantinescu02164b32014-11-13 14:05:07 +00001671void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001672 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001673 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1674 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001675 switch (in_type) {
1676 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001677 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001678 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001679 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1680 break;
1681 }
1682 case Primitive::kPrimFloat:
1683 case Primitive::kPrimDouble: {
1684 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001685 locations->SetInAt(1,
1686 IsFloatingPointZeroConstant(compare->InputAt(1))
1687 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1688 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001689 locations->SetOut(Location::RequiresRegister());
1690 break;
1691 }
1692 default:
1693 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1694 }
1695}
1696
1697void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1698 Primitive::Type in_type = compare->InputAt(0)->GetType();
1699
1700 // 0 if: left == right
1701 // 1 if: left > right
1702 // -1 if: left < right
1703 switch (in_type) {
1704 case Primitive::kPrimLong: {
1705 Register result = OutputRegister(compare);
1706 Register left = InputRegisterAt(compare, 0);
1707 Operand right = InputOperandAt(compare, 1);
1708
1709 __ Cmp(left, right);
1710 __ Cset(result, ne);
1711 __ Cneg(result, result, lt);
1712 break;
1713 }
1714 case Primitive::kPrimFloat:
1715 case Primitive::kPrimDouble: {
1716 Register result = OutputRegister(compare);
1717 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001718 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001719 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1720 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001721 __ Fcmp(left, 0.0);
1722 } else {
1723 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1724 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001725 if (compare->IsGtBias()) {
1726 __ Cset(result, ne);
1727 } else {
1728 __ Csetm(result, ne);
1729 }
1730 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001731 break;
1732 }
1733 default:
1734 LOG(FATAL) << "Unimplemented compare type " << in_type;
1735 }
1736}
1737
1738void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1739 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001740
1741 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1742 locations->SetInAt(0, Location::RequiresFpuRegister());
1743 locations->SetInAt(1,
1744 IsFloatingPointZeroConstant(instruction->InputAt(1))
1745 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1746 : Location::RequiresFpuRegister());
1747 } else {
1748 // Integer cases.
1749 locations->SetInAt(0, Location::RequiresRegister());
1750 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1751 }
1752
Alexandre Rames5319def2014-10-23 10:03:10 +01001753 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001755 }
1756}
1757
1758void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1759 if (!instruction->NeedsMaterialization()) {
1760 return;
1761 }
1762
1763 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001764 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001765 IfCondition if_cond = instruction->GetCondition();
1766 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001767
Roland Levillain7f63c522015-07-13 15:54:55 +00001768 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1769 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1770 if (locations->InAt(1).IsConstant()) {
1771 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1772 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1773 __ Fcmp(lhs, 0.0);
1774 } else {
1775 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1776 }
1777 __ Cset(res, arm64_cond);
1778 if (instruction->IsFPConditionTrueIfNaN()) {
1779 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1780 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1781 } else if (instruction->IsFPConditionFalseIfNaN()) {
1782 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1783 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1784 }
1785 } else {
1786 // Integer cases.
1787 Register lhs = InputRegisterAt(instruction, 0);
1788 Operand rhs = InputOperandAt(instruction, 1);
1789 __ Cmp(lhs, rhs);
1790 __ Cset(res, arm64_cond);
1791 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001792}
1793
1794#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1795 M(Equal) \
1796 M(NotEqual) \
1797 M(LessThan) \
1798 M(LessThanOrEqual) \
1799 M(GreaterThan) \
1800 M(GreaterThanOrEqual)
1801#define DEFINE_CONDITION_VISITORS(Name) \
1802void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1803void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1804FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001805#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001806#undef FOR_EACH_CONDITION_INSTRUCTION
1807
Zheng Xuc6667102015-05-15 16:08:45 +08001808void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1809 DCHECK(instruction->IsDiv() || instruction->IsRem());
1810
1811 LocationSummary* locations = instruction->GetLocations();
1812 Location second = locations->InAt(1);
1813 DCHECK(second.IsConstant());
1814
1815 Register out = OutputRegister(instruction);
1816 Register dividend = InputRegisterAt(instruction, 0);
1817 int64_t imm = Int64FromConstant(second.GetConstant());
1818 DCHECK(imm == 1 || imm == -1);
1819
1820 if (instruction->IsRem()) {
1821 __ Mov(out, 0);
1822 } else {
1823 if (imm == 1) {
1824 __ Mov(out, dividend);
1825 } else {
1826 __ Neg(out, dividend);
1827 }
1828 }
1829}
1830
1831void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1832 DCHECK(instruction->IsDiv() || instruction->IsRem());
1833
1834 LocationSummary* locations = instruction->GetLocations();
1835 Location second = locations->InAt(1);
1836 DCHECK(second.IsConstant());
1837
1838 Register out = OutputRegister(instruction);
1839 Register dividend = InputRegisterAt(instruction, 0);
1840 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001841 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001842 DCHECK(IsPowerOfTwo(abs_imm));
1843 int ctz_imm = CTZ(abs_imm);
1844
1845 UseScratchRegisterScope temps(GetVIXLAssembler());
1846 Register temp = temps.AcquireSameSizeAs(out);
1847
1848 if (instruction->IsDiv()) {
1849 __ Add(temp, dividend, abs_imm - 1);
1850 __ Cmp(dividend, 0);
1851 __ Csel(out, temp, dividend, lt);
1852 if (imm > 0) {
1853 __ Asr(out, out, ctz_imm);
1854 } else {
1855 __ Neg(out, Operand(out, ASR, ctz_imm));
1856 }
1857 } else {
1858 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1859 __ Asr(temp, dividend, bits - 1);
1860 __ Lsr(temp, temp, bits - ctz_imm);
1861 __ Add(out, dividend, temp);
1862 __ And(out, out, abs_imm - 1);
1863 __ Sub(out, out, temp);
1864 }
1865}
1866
1867void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1868 DCHECK(instruction->IsDiv() || instruction->IsRem());
1869
1870 LocationSummary* locations = instruction->GetLocations();
1871 Location second = locations->InAt(1);
1872 DCHECK(second.IsConstant());
1873
1874 Register out = OutputRegister(instruction);
1875 Register dividend = InputRegisterAt(instruction, 0);
1876 int64_t imm = Int64FromConstant(second.GetConstant());
1877
1878 Primitive::Type type = instruction->GetResultType();
1879 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1880
1881 int64_t magic;
1882 int shift;
1883 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1884
1885 UseScratchRegisterScope temps(GetVIXLAssembler());
1886 Register temp = temps.AcquireSameSizeAs(out);
1887
1888 // temp = get_high(dividend * magic)
1889 __ Mov(temp, magic);
1890 if (type == Primitive::kPrimLong) {
1891 __ Smulh(temp, dividend, temp);
1892 } else {
1893 __ Smull(temp.X(), dividend, temp);
1894 __ Lsr(temp.X(), temp.X(), 32);
1895 }
1896
1897 if (imm > 0 && magic < 0) {
1898 __ Add(temp, temp, dividend);
1899 } else if (imm < 0 && magic > 0) {
1900 __ Sub(temp, temp, dividend);
1901 }
1902
1903 if (shift != 0) {
1904 __ Asr(temp, temp, shift);
1905 }
1906
1907 if (instruction->IsDiv()) {
1908 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1909 } else {
1910 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1911 // TODO: Strength reduction for msub.
1912 Register temp_imm = temps.AcquireSameSizeAs(out);
1913 __ Mov(temp_imm, imm);
1914 __ Msub(out, temp, temp_imm, dividend);
1915 }
1916}
1917
1918void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1919 DCHECK(instruction->IsDiv() || instruction->IsRem());
1920 Primitive::Type type = instruction->GetResultType();
1921 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1922
1923 LocationSummary* locations = instruction->GetLocations();
1924 Register out = OutputRegister(instruction);
1925 Location second = locations->InAt(1);
1926
1927 if (second.IsConstant()) {
1928 int64_t imm = Int64FromConstant(second.GetConstant());
1929
1930 if (imm == 0) {
1931 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1932 } else if (imm == 1 || imm == -1) {
1933 DivRemOneOrMinusOne(instruction);
1934 } else if (IsPowerOfTwo(std::abs(imm))) {
1935 DivRemByPowerOfTwo(instruction);
1936 } else {
1937 DCHECK(imm <= -2 || imm >= 2);
1938 GenerateDivRemWithAnyConstant(instruction);
1939 }
1940 } else {
1941 Register dividend = InputRegisterAt(instruction, 0);
1942 Register divisor = InputRegisterAt(instruction, 1);
1943 if (instruction->IsDiv()) {
1944 __ Sdiv(out, dividend, divisor);
1945 } else {
1946 UseScratchRegisterScope temps(GetVIXLAssembler());
1947 Register temp = temps.AcquireSameSizeAs(out);
1948 __ Sdiv(temp, dividend, divisor);
1949 __ Msub(out, temp, divisor, dividend);
1950 }
1951 }
1952}
1953
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001954void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1957 switch (div->GetResultType()) {
1958 case Primitive::kPrimInt:
1959 case Primitive::kPrimLong:
1960 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001961 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1963 break;
1964
1965 case Primitive::kPrimFloat:
1966 case Primitive::kPrimDouble:
1967 locations->SetInAt(0, Location::RequiresFpuRegister());
1968 locations->SetInAt(1, Location::RequiresFpuRegister());
1969 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1970 break;
1971
1972 default:
1973 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1974 }
1975}
1976
1977void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1978 Primitive::Type type = div->GetResultType();
1979 switch (type) {
1980 case Primitive::kPrimInt:
1981 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001982 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001983 break;
1984
1985 case Primitive::kPrimFloat:
1986 case Primitive::kPrimDouble:
1987 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1988 break;
1989
1990 default:
1991 LOG(FATAL) << "Unexpected div type " << type;
1992 }
1993}
1994
Alexandre Rames67555f72014-11-18 10:55:16 +00001995void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001996 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1997 ? LocationSummary::kCallOnSlowPath
1998 : LocationSummary::kNoCall;
1999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002000 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2001 if (instruction->HasUses()) {
2002 locations->SetOut(Location::SameAsFirstInput());
2003 }
2004}
2005
2006void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2007 SlowPathCodeARM64* slow_path =
2008 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2009 codegen_->AddSlowPath(slow_path);
2010 Location value = instruction->GetLocations()->InAt(0);
2011
Alexandre Rames3e69f162014-12-10 10:36:50 +00002012 Primitive::Type type = instruction->GetType();
2013
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002014 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2015 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002016 return;
2017 }
2018
Alexandre Rames67555f72014-11-18 10:55:16 +00002019 if (value.IsConstant()) {
2020 int64_t divisor = Int64ConstantFrom(value);
2021 if (divisor == 0) {
2022 __ B(slow_path->GetEntryLabel());
2023 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002024 // A division by a non-null constant is valid. We don't need to perform
2025 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002026 }
2027 } else {
2028 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2029 }
2030}
2031
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002032void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2033 LocationSummary* locations =
2034 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2035 locations->SetOut(Location::ConstantLocation(constant));
2036}
2037
2038void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2039 UNUSED(constant);
2040 // Will be generated at use site.
2041}
2042
Alexandre Rames5319def2014-10-23 10:03:10 +01002043void LocationsBuilderARM64::VisitExit(HExit* exit) {
2044 exit->SetLocations(nullptr);
2045}
2046
2047void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002048 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002049}
2050
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002051void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2054 locations->SetOut(Location::ConstantLocation(constant));
2055}
2056
2057void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2058 UNUSED(constant);
2059 // Will be generated at use site.
2060}
2061
David Brazdilfc6a86a2015-06-26 10:33:45 +00002062void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002063 DCHECK(!successor->IsExitBlock());
2064 HBasicBlock* block = got->GetBlock();
2065 HInstruction* previous = got->GetPrevious();
2066 HLoopInformation* info = block->GetLoopInformation();
2067
David Brazdil46e2a392015-03-16 17:31:52 +00002068 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002069 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2070 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2071 return;
2072 }
2073 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2074 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2075 }
2076 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002077 __ B(codegen_->GetLabelOf(successor));
2078 }
2079}
2080
David Brazdilfc6a86a2015-06-26 10:33:45 +00002081void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2082 got->SetLocations(nullptr);
2083}
2084
2085void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2086 HandleGoto(got, got->GetSuccessor());
2087}
2088
2089void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2090 try_boundary->SetLocations(nullptr);
2091}
2092
2093void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2094 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2095 if (!successor->IsExitBlock()) {
2096 HandleGoto(try_boundary, successor);
2097 }
2098}
2099
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002100void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2101 vixl::Label* true_target,
2102 vixl::Label* false_target,
2103 vixl::Label* always_true_target) {
2104 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002105 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002106
Serban Constantinescu02164b32014-11-13 14:05:07 +00002107 if (cond->IsIntConstant()) {
2108 int32_t cond_value = cond->AsIntConstant()->GetValue();
2109 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002110 if (always_true_target != nullptr) {
2111 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002112 }
2113 return;
2114 } else {
2115 DCHECK_EQ(cond_value, 0);
2116 }
2117 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002118 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002119 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002120 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002121 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002122 } else {
2123 // The condition instruction has not been materialized, use its inputs as
2124 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002125 Primitive::Type type =
2126 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2127
2128 if (Primitive::IsFloatingPointType(type)) {
2129 // FP compares don't like null false_targets.
2130 if (false_target == nullptr) {
2131 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002132 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002133 FPRegister lhs = InputFPRegisterAt(condition, 0);
2134 if (condition->GetLocations()->InAt(1).IsConstant()) {
2135 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2136 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2137 __ Fcmp(lhs, 0.0);
2138 } else {
2139 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2140 }
2141 if (condition->IsFPConditionTrueIfNaN()) {
2142 __ B(vs, true_target); // VS for unordered.
2143 } else if (condition->IsFPConditionFalseIfNaN()) {
2144 __ B(vs, false_target); // VS for unordered.
2145 }
2146 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002147 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002148 // Integer cases.
2149 Register lhs = InputRegisterAt(condition, 0);
2150 Operand rhs = InputOperandAt(condition, 1);
2151 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2152 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2153 switch (arm64_cond) {
2154 case eq:
2155 __ Cbz(lhs, true_target);
2156 break;
2157 case ne:
2158 __ Cbnz(lhs, true_target);
2159 break;
2160 case lt:
2161 // Test the sign bit and branch accordingly.
2162 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2163 break;
2164 case ge:
2165 // Test the sign bit and branch accordingly.
2166 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2167 break;
2168 default:
2169 // Without the `static_cast` the compiler throws an error for
2170 // `-Werror=sign-promo`.
2171 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2172 }
2173 } else {
2174 __ Cmp(lhs, rhs);
2175 __ B(arm64_cond, true_target);
2176 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002177 }
2178 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002179 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002180 __ B(false_target);
2181 }
2182}
2183
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002184void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2185 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2186 HInstruction* cond = if_instr->InputAt(0);
2187 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2188 locations->SetInAt(0, Location::RequiresRegister());
2189 }
2190}
2191
2192void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2193 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2194 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2195 vixl::Label* always_true_target = true_target;
2196 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2197 if_instr->IfTrueSuccessor())) {
2198 always_true_target = nullptr;
2199 }
2200 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2201 if_instr->IfFalseSuccessor())) {
2202 false_target = nullptr;
2203 }
2204 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2205}
2206
2207void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2208 LocationSummary* locations = new (GetGraph()->GetArena())
2209 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2210 HInstruction* cond = deoptimize->InputAt(0);
2211 DCHECK(cond->IsCondition());
2212 if (cond->AsCondition()->NeedsMaterialization()) {
2213 locations->SetInAt(0, Location::RequiresRegister());
2214 }
2215}
2216
2217void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2218 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2219 DeoptimizationSlowPathARM64(deoptimize);
2220 codegen_->AddSlowPath(slow_path);
2221 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2222 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2223}
2224
Alexandre Rames5319def2014-10-23 10:03:10 +01002225void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002226 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002227}
2228
2229void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002230 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002231}
2232
2233void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002234 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002235}
2236
2237void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002238 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002239}
2240
Alexandre Rames67555f72014-11-18 10:55:16 +00002241void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2242 LocationSummary::CallKind call_kind =
2243 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002247 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002248 // Note that TypeCheckSlowPathARM64 uses this register too.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002249 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002250}
2251
2252void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2253 LocationSummary* locations = instruction->GetLocations();
2254 Register obj = InputRegisterAt(instruction, 0);;
2255 Register cls = InputRegisterAt(instruction, 1);;
2256 Register out = OutputRegister(instruction);
2257
2258 vixl::Label done;
2259
2260 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002261 // Avoid null check if we know `obj` is not null.
2262 if (instruction->MustDoNullCheck()) {
2263 __ Mov(out, 0);
2264 __ Cbz(obj, &done);
2265 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002266
2267 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002268 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002269 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002270 __ Cmp(out, cls);
2271 if (instruction->IsClassFinal()) {
2272 // Classes must be equal for the instanceof to succeed.
2273 __ Cset(out, eq);
2274 } else {
2275 // If the classes are not equal, we go into a slow path.
2276 DCHECK(locations->OnlyCallsOnSlowPath());
2277 SlowPathCodeARM64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002278 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002279 codegen_->AddSlowPath(slow_path);
2280 __ B(ne, slow_path->GetEntryLabel());
2281 __ Mov(out, 1);
2282 __ Bind(slow_path->GetExitLabel());
2283 }
2284
2285 __ Bind(&done);
2286}
2287
Alexandre Rames5319def2014-10-23 10:03:10 +01002288void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2290 locations->SetOut(Location::ConstantLocation(constant));
2291}
2292
2293void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2294 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002295 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002296}
2297
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002298void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2300 locations->SetOut(Location::ConstantLocation(constant));
2301}
2302
2303void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2304 // Will be generated at use site.
2305 UNUSED(constant);
2306}
2307
Alexandre Rames5319def2014-10-23 10:03:10 +01002308void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002309 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002310 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002311}
2312
Alexandre Rames67555f72014-11-18 10:55:16 +00002313void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2314 HandleInvoke(invoke);
2315}
2316
2317void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2318 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2320 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2321 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002322 Location receiver = invoke->GetLocations()->InAt(0);
2323 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002324 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002325
2326 // The register ip1 is required to be used for the hidden argument in
2327 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002328 MacroAssembler* masm = GetVIXLAssembler();
2329 UseScratchRegisterScope scratch_scope(masm);
2330 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002331 scratch_scope.Exclude(ip1);
2332 __ Mov(ip1, invoke->GetDexMethodIndex());
2333
2334 // temp = object->GetClass();
2335 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336 __ Ldr(temp.W(), StackOperandFrom(receiver));
2337 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002338 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002341 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002342 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002343 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002344 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002345 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002346 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002347 // lr();
2348 __ Blr(lr);
2349 DCHECK(!codegen_->IsLeafMethod());
2350 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2351}
2352
2353void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002354 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2355 if (intrinsic.TryDispatch(invoke)) {
2356 return;
2357 }
2358
Alexandre Rames67555f72014-11-18 10:55:16 +00002359 HandleInvoke(invoke);
2360}
2361
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002362void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002363 // When we do not run baseline, explicit clinit checks triggered by static
2364 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2365 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002366
Andreas Gampe878d58c2015-01-15 23:24:00 -08002367 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2368 if (intrinsic.TryDispatch(invoke)) {
2369 return;
2370 }
2371
Alexandre Rames67555f72014-11-18 10:55:16 +00002372 HandleInvoke(invoke);
2373}
2374
Andreas Gampe878d58c2015-01-15 23:24:00 -08002375static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2376 if (invoke->GetLocations()->Intrinsified()) {
2377 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2378 intrinsic.Dispatch(invoke);
2379 return true;
2380 }
2381 return false;
2382}
2383
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002384void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002385 // For better instruction scheduling we load the direct code pointer before the method pointer.
2386 bool direct_code_loaded = false;
2387 switch (invoke->GetCodePtrLocation()) {
2388 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2389 // LR = code address from literal pool with link-time patch.
2390 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2391 direct_code_loaded = true;
2392 break;
2393 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2394 // LR = invoke->GetDirectCodePtr();
2395 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2396 direct_code_loaded = true;
2397 break;
2398 default:
2399 break;
2400 }
2401
Andreas Gampe878d58c2015-01-15 23:24:00 -08002402 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002403 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2404 switch (invoke->GetMethodLoadKind()) {
2405 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2406 // temp = thread->string_init_entrypoint
2407 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2408 break;
2409 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2410 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2411 break;
2412 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2413 // Load method address from literal pool.
2414 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2415 break;
2416 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2417 // Load method address from literal pool with a link-time patch.
2418 __ Ldr(XRegisterFrom(temp).X(),
2419 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2420 break;
2421 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2422 // Add ADRP with its PC-relative DexCache access patch.
2423 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2424 invoke->GetDexCacheArrayOffset());
2425 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2426 {
2427 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2428 __ adrp(XRegisterFrom(temp).X(), 0);
2429 }
2430 __ Bind(pc_insn_label); // Bind after ADRP.
2431 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2432 // Add LDR with its PC-relative DexCache access patch.
2433 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2434 invoke->GetDexCacheArrayOffset());
2435 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2436 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2437 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2438 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002439 }
Vladimir Marko58155012015-08-19 12:49:41 +00002440 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2441 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2442 Register reg = XRegisterFrom(temp);
2443 Register method_reg;
2444 if (current_method.IsRegister()) {
2445 method_reg = XRegisterFrom(current_method);
2446 } else {
2447 DCHECK(invoke->GetLocations()->Intrinsified());
2448 DCHECK(!current_method.IsValid());
2449 method_reg = reg;
2450 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2451 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002452
Vladimir Marko58155012015-08-19 12:49:41 +00002453 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002454 __ Ldr(reg.X(),
2455 MemOperand(method_reg.X(),
2456 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002457 // temp = temp[index_in_cache];
2458 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2459 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2460 break;
2461 }
2462 }
2463
2464 switch (invoke->GetCodePtrLocation()) {
2465 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2466 __ Bl(&frame_entry_label_);
2467 break;
2468 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2469 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2470 vixl::Label* label = &relative_call_patches_.back().label;
2471 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2472 __ Bind(label); // Bind after BL.
2473 break;
2474 }
2475 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2476 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2477 // LR prepared above for better instruction scheduling.
2478 DCHECK(direct_code_loaded);
2479 // lr()
2480 __ Blr(lr);
2481 break;
2482 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2483 // LR = callee_method->entry_point_from_quick_compiled_code_;
2484 __ Ldr(lr, MemOperand(
2485 XRegisterFrom(callee_method).X(),
2486 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2487 // lr()
2488 __ Blr(lr);
2489 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002490 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002491
Andreas Gampe878d58c2015-01-15 23:24:00 -08002492 DCHECK(!IsLeafMethod());
2493}
2494
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002495void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2496 LocationSummary* locations = invoke->GetLocations();
2497 Location receiver = locations->InAt(0);
2498 Register temp = XRegisterFrom(temp_in);
2499 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2500 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2501 Offset class_offset = mirror::Object::ClassOffset();
2502 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2503
2504 BlockPoolsScope block_pools(GetVIXLAssembler());
2505
2506 DCHECK(receiver.IsRegister());
2507 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2508 MaybeRecordImplicitNullCheck(invoke);
2509 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2510 // temp = temp->GetMethodAt(method_offset);
2511 __ Ldr(temp, MemOperand(temp, method_offset));
2512 // lr = temp->GetEntryPoint();
2513 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2514 // lr();
2515 __ Blr(lr);
2516}
2517
Vladimir Marko58155012015-08-19 12:49:41 +00002518void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2519 DCHECK(linker_patches->empty());
2520 size_t size =
2521 method_patches_.size() +
2522 call_patches_.size() +
2523 relative_call_patches_.size() +
2524 pc_rel_dex_cache_patches_.size();
2525 linker_patches->reserve(size);
2526 for (const auto& entry : method_patches_) {
2527 const MethodReference& target_method = entry.first;
2528 vixl::Literal<uint64_t>* literal = entry.second;
2529 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2530 target_method.dex_file,
2531 target_method.dex_method_index));
2532 }
2533 for (const auto& entry : call_patches_) {
2534 const MethodReference& target_method = entry.first;
2535 vixl::Literal<uint64_t>* literal = entry.second;
2536 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2537 target_method.dex_file,
2538 target_method.dex_method_index));
2539 }
2540 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2541 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2542 info.target_method.dex_file,
2543 info.target_method.dex_method_index));
2544 }
2545 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2546 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2547 &info.target_dex_file,
2548 info.pc_insn_label->location() - 4u,
2549 info.element_offset));
2550 }
2551}
2552
2553vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2554 // Look up the literal for value.
2555 auto lb = uint64_literals_.lower_bound(value);
2556 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2557 return lb->second;
2558 }
2559 // We don't have a literal for this value, insert a new one.
2560 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2561 uint64_literals_.PutBefore(lb, value, literal);
2562 return literal;
2563}
2564
2565vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2566 MethodReference target_method,
2567 MethodToLiteralMap* map) {
2568 // Look up the literal for target_method.
2569 auto lb = map->lower_bound(target_method);
2570 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2571 return lb->second;
2572 }
2573 // We don't have a literal for this method yet, insert a new one.
2574 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2575 map->PutBefore(lb, target_method, literal);
2576 return literal;
2577}
2578
2579vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2580 MethodReference target_method) {
2581 return DeduplicateMethodLiteral(target_method, &method_patches_);
2582}
2583
2584vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2585 MethodReference target_method) {
2586 return DeduplicateMethodLiteral(target_method, &call_patches_);
2587}
2588
2589
Andreas Gampe878d58c2015-01-15 23:24:00 -08002590void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002591 // When we do not run baseline, explicit clinit checks triggered by static
2592 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2593 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002594
Andreas Gampe878d58c2015-01-15 23:24:00 -08002595 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2596 return;
2597 }
2598
Alexandre Ramesd921d642015-04-16 15:07:16 +01002599 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002600 LocationSummary* locations = invoke->GetLocations();
2601 codegen_->GenerateStaticOrDirectCall(
2602 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002603 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002604}
2605
2606void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002607 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2608 return;
2609 }
2610
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002611 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002612 DCHECK(!codegen_->IsLeafMethod());
2613 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2614}
2615
Alexandre Rames67555f72014-11-18 10:55:16 +00002616void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2617 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2618 : LocationSummary::kNoCall;
2619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002620 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002621 locations->SetOut(Location::RequiresRegister());
2622}
2623
2624void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2625 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002626 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002627 if (cls->IsReferrersClass()) {
2628 DCHECK(!cls->CanCallRuntime());
2629 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002630 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002631 } else {
2632 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002633 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
2634 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
2635 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2636 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002637
2638 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2639 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2640 codegen_->AddSlowPath(slow_path);
2641 __ Cbz(out, slow_path->GetEntryLabel());
2642 if (cls->MustGenerateClinitCheck()) {
2643 GenerateClassInitializationCheck(slow_path, out);
2644 } else {
2645 __ Bind(slow_path->GetExitLabel());
2646 }
2647 }
2648}
2649
David Brazdilcb1c0552015-08-04 16:22:25 +01002650static MemOperand GetExceptionTlsAddress() {
2651 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2652}
2653
Alexandre Rames67555f72014-11-18 10:55:16 +00002654void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2655 LocationSummary* locations =
2656 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2657 locations->SetOut(Location::RequiresRegister());
2658}
2659
2660void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002661 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2662}
2663
2664void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2665 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2666}
2667
2668void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2669 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002670}
2671
Alexandre Rames5319def2014-10-23 10:03:10 +01002672void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2673 load->SetLocations(nullptr);
2674}
2675
2676void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2677 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002678 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002679}
2680
Alexandre Rames67555f72014-11-18 10:55:16 +00002681void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2682 LocationSummary* locations =
2683 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002684 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002685 locations->SetOut(Location::RequiresRegister());
2686}
2687
2688void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2689 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2690 codegen_->AddSlowPath(slow_path);
2691
2692 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002693 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002694 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002695 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
2696 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2697 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002698 __ Cbz(out, slow_path->GetEntryLabel());
2699 __ Bind(slow_path->GetExitLabel());
2700}
2701
Alexandre Rames5319def2014-10-23 10:03:10 +01002702void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2703 local->SetLocations(nullptr);
2704}
2705
2706void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2707 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2708}
2709
2710void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2712 locations->SetOut(Location::ConstantLocation(constant));
2713}
2714
2715void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2716 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002717 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002718}
2719
Alexandre Rames67555f72014-11-18 10:55:16 +00002720void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2723 InvokeRuntimeCallingConvention calling_convention;
2724 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2725}
2726
2727void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2728 codegen_->InvokeRuntime(instruction->IsEnter()
2729 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2730 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002731 instruction->GetDexPc(),
2732 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002733 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002734}
2735
Alexandre Rames42d641b2014-10-27 14:00:51 +00002736void LocationsBuilderARM64::VisitMul(HMul* mul) {
2737 LocationSummary* locations =
2738 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2739 switch (mul->GetResultType()) {
2740 case Primitive::kPrimInt:
2741 case Primitive::kPrimLong:
2742 locations->SetInAt(0, Location::RequiresRegister());
2743 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002745 break;
2746
2747 case Primitive::kPrimFloat:
2748 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002749 locations->SetInAt(0, Location::RequiresFpuRegister());
2750 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002751 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002752 break;
2753
2754 default:
2755 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2756 }
2757}
2758
2759void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2760 switch (mul->GetResultType()) {
2761 case Primitive::kPrimInt:
2762 case Primitive::kPrimLong:
2763 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2764 break;
2765
2766 case Primitive::kPrimFloat:
2767 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002768 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002769 break;
2770
2771 default:
2772 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2773 }
2774}
2775
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002776void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2777 LocationSummary* locations =
2778 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2779 switch (neg->GetResultType()) {
2780 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002781 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002782 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002784 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002785
2786 case Primitive::kPrimFloat:
2787 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002788 locations->SetInAt(0, Location::RequiresFpuRegister());
2789 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002790 break;
2791
2792 default:
2793 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2794 }
2795}
2796
2797void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2798 switch (neg->GetResultType()) {
2799 case Primitive::kPrimInt:
2800 case Primitive::kPrimLong:
2801 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2802 break;
2803
2804 case Primitive::kPrimFloat:
2805 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002806 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002807 break;
2808
2809 default:
2810 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2811 }
2812}
2813
2814void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2815 LocationSummary* locations =
2816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2817 InvokeRuntimeCallingConvention calling_convention;
2818 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002819 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002820 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002821 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002822 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002823 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002824}
2825
2826void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2827 LocationSummary* locations = instruction->GetLocations();
2828 InvokeRuntimeCallingConvention calling_convention;
2829 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2830 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002831 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002832 // Note: if heap poisoning is enabled, the entry point takes cares
2833 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002834 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002835 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2836 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002837 instruction->GetDexPc(),
2838 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002839 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002840}
2841
Alexandre Rames5319def2014-10-23 10:03:10 +01002842void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2843 LocationSummary* locations =
2844 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2845 InvokeRuntimeCallingConvention calling_convention;
2846 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002847 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002848 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002849 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002850}
2851
2852void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2853 LocationSummary* locations = instruction->GetLocations();
2854 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2855 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002856 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002857 // Note: if heap poisoning is enabled, the entry point takes cares
2858 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002859 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002860 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2861 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002862 instruction->GetDexPc(),
2863 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002864 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002865}
2866
2867void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2868 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002869 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002871}
2872
2873void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002874 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002875 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002876 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002877 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002878 break;
2879
2880 default:
2881 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2882 }
2883}
2884
David Brazdil66d126e2015-04-03 16:02:44 +01002885void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2887 locations->SetInAt(0, Location::RequiresRegister());
2888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2889}
2890
2891void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002892 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2893}
2894
Alexandre Rames5319def2014-10-23 10:03:10 +01002895void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002896 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2897 ? LocationSummary::kCallOnSlowPath
2898 : LocationSummary::kNoCall;
2899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01002900 locations->SetInAt(0, Location::RequiresRegister());
2901 if (instruction->HasUses()) {
2902 locations->SetOut(Location::SameAsFirstInput());
2903 }
2904}
2905
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002906void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002907 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2908 return;
2909 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002910
Alexandre Ramesd921d642015-04-16 15:07:16 +01002911 BlockPoolsScope block_pools(GetVIXLAssembler());
2912 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002913 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2914 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2915}
2916
2917void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002918 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2919 codegen_->AddSlowPath(slow_path);
2920
2921 LocationSummary* locations = instruction->GetLocations();
2922 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002923
2924 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002925}
2926
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002927void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002928 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002929 GenerateImplicitNullCheck(instruction);
2930 } else {
2931 GenerateExplicitNullCheck(instruction);
2932 }
2933}
2934
Alexandre Rames67555f72014-11-18 10:55:16 +00002935void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2936 HandleBinaryOp(instruction);
2937}
2938
2939void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2940 HandleBinaryOp(instruction);
2941}
2942
Alexandre Rames3e69f162014-12-10 10:36:50 +00002943void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2944 LOG(FATAL) << "Unreachable";
2945}
2946
2947void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2948 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2949}
2950
Alexandre Rames5319def2014-10-23 10:03:10 +01002951void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2953 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2954 if (location.IsStackSlot()) {
2955 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2956 } else if (location.IsDoubleStackSlot()) {
2957 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2958 }
2959 locations->SetOut(location);
2960}
2961
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002962void InstructionCodeGeneratorARM64::VisitParameterValue(
2963 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002964 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002965}
2966
2967void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2968 LocationSummary* locations =
2969 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002970 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002971}
2972
2973void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2974 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2975 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002976}
2977
2978void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2980 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2981 locations->SetInAt(i, Location::Any());
2982 }
2983 locations->SetOut(Location::Any());
2984}
2985
2986void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002987 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002988 LOG(FATAL) << "Unreachable";
2989}
2990
Serban Constantinescu02164b32014-11-13 14:05:07 +00002991void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002992 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002993 LocationSummary::CallKind call_kind =
2994 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2996
2997 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002998 case Primitive::kPrimInt:
2999 case Primitive::kPrimLong:
3000 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003001 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3003 break;
3004
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003005 case Primitive::kPrimFloat:
3006 case Primitive::kPrimDouble: {
3007 InvokeRuntimeCallingConvention calling_convention;
3008 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3009 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3010 locations->SetOut(calling_convention.GetReturnLocation(type));
3011
3012 break;
3013 }
3014
Serban Constantinescu02164b32014-11-13 14:05:07 +00003015 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003016 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003017 }
3018}
3019
3020void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3021 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003022
Serban Constantinescu02164b32014-11-13 14:05:07 +00003023 switch (type) {
3024 case Primitive::kPrimInt:
3025 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003026 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003027 break;
3028 }
3029
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003030 case Primitive::kPrimFloat:
3031 case Primitive::kPrimDouble: {
3032 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3033 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003034 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003035 break;
3036 }
3037
Serban Constantinescu02164b32014-11-13 14:05:07 +00003038 default:
3039 LOG(FATAL) << "Unexpected rem type " << type;
3040 }
3041}
3042
Calin Juravle27df7582015-04-17 19:12:31 +01003043void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3044 memory_barrier->SetLocations(nullptr);
3045}
3046
3047void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3048 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3049}
3050
Alexandre Rames5319def2014-10-23 10:03:10 +01003051void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3052 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3053 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003054 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003055}
3056
3057void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003058 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003059 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003060}
3061
3062void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3063 instruction->SetLocations(nullptr);
3064}
3065
3066void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003067 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003068 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003069}
3070
Serban Constantinescu02164b32014-11-13 14:05:07 +00003071void LocationsBuilderARM64::VisitShl(HShl* shl) {
3072 HandleShift(shl);
3073}
3074
3075void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3076 HandleShift(shl);
3077}
3078
3079void LocationsBuilderARM64::VisitShr(HShr* shr) {
3080 HandleShift(shr);
3081}
3082
3083void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3084 HandleShift(shr);
3085}
3086
Alexandre Rames5319def2014-10-23 10:03:10 +01003087void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3089 Primitive::Type field_type = store->InputAt(1)->GetType();
3090 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003091 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003092 case Primitive::kPrimBoolean:
3093 case Primitive::kPrimByte:
3094 case Primitive::kPrimChar:
3095 case Primitive::kPrimShort:
3096 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003097 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003098 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3099 break;
3100
3101 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003102 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003103 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3104 break;
3105
3106 default:
3107 LOG(FATAL) << "Unimplemented local type " << field_type;
3108 }
3109}
3110
3111void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003112 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003113}
3114
3115void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003116 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003117}
3118
3119void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003120 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003121}
3122
Alexandre Rames67555f72014-11-18 10:55:16 +00003123void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003124 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003125}
3126
3127void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003128 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003129}
3130
3131void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003132 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003133}
3134
Alexandre Rames67555f72014-11-18 10:55:16 +00003135void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003136 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003137}
3138
3139void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3141}
3142
3143void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003144 HBasicBlock* block = instruction->GetBlock();
3145 if (block->GetLoopInformation() != nullptr) {
3146 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3147 // The back edge will generate the suspend check.
3148 return;
3149 }
3150 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3151 // The goto will generate the suspend check.
3152 return;
3153 }
3154 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003155}
3156
3157void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3158 temp->SetLocations(nullptr);
3159}
3160
3161void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3162 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003163 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003164}
3165
Alexandre Rames67555f72014-11-18 10:55:16 +00003166void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3167 LocationSummary* locations =
3168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3169 InvokeRuntimeCallingConvention calling_convention;
3170 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3171}
3172
3173void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3174 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003175 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003176 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003177}
3178
3179void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3180 LocationSummary* locations =
3181 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3182 Primitive::Type input_type = conversion->GetInputType();
3183 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003184 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003185 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3186 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3187 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3188 }
3189
Alexandre Rames542361f2015-01-29 16:57:31 +00003190 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003191 locations->SetInAt(0, Location::RequiresFpuRegister());
3192 } else {
3193 locations->SetInAt(0, Location::RequiresRegister());
3194 }
3195
Alexandre Rames542361f2015-01-29 16:57:31 +00003196 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3198 } else {
3199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3200 }
3201}
3202
3203void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3204 Primitive::Type result_type = conversion->GetResultType();
3205 Primitive::Type input_type = conversion->GetInputType();
3206
3207 DCHECK_NE(input_type, result_type);
3208
Alexandre Rames542361f2015-01-29 16:57:31 +00003209 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003210 int result_size = Primitive::ComponentSize(result_type);
3211 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003212 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003213 Register output = OutputRegister(conversion);
3214 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003215 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3216 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003217 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3218 // 'int' values are used directly as W registers, discarding the top
3219 // bits, so we don't need to sign-extend and can just perform a move.
3220 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3221 // top 32 bits of the target register. We theoretically could leave those
3222 // bits unchanged, but we would have to make sure that no code uses a
3223 // 32bit input value as a 64bit value assuming that the top 32 bits are
3224 // zero.
3225 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003226 } else if ((result_type == Primitive::kPrimChar) ||
3227 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3228 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003229 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003230 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003231 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003232 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003233 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003234 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003235 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3236 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003237 } else if (Primitive::IsFloatingPointType(result_type) &&
3238 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003239 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3240 } else {
3241 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3242 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003243 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003244}
Alexandre Rames67555f72014-11-18 10:55:16 +00003245
Serban Constantinescu02164b32014-11-13 14:05:07 +00003246void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3247 HandleShift(ushr);
3248}
3249
3250void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3251 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003252}
3253
3254void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3255 HandleBinaryOp(instruction);
3256}
3257
3258void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3259 HandleBinaryOp(instruction);
3260}
3261
Calin Juravleb1498f62015-02-16 13:13:29 +00003262void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3263 // Nothing to do, this should be removed during prepare for register allocator.
3264 UNUSED(instruction);
3265 LOG(FATAL) << "Unreachable";
3266}
3267
3268void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3269 // Nothing to do, this should be removed during prepare for register allocator.
3270 UNUSED(instruction);
3271 LOG(FATAL) << "Unreachable";
3272}
3273
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003274void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3275 DCHECK(codegen_->IsBaseline());
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3278 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3279}
3280
3281void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3282 DCHECK(codegen_->IsBaseline());
3283 // Will be generated at use site.
3284}
3285
Alexandre Rames67555f72014-11-18 10:55:16 +00003286#undef __
3287#undef QUICK_ENTRY_POINT
3288
Alexandre Rames5319def2014-10-23 10:03:10 +01003289} // namespace arm64
3290} // namespace art