blob: c97e367b1849c07730ba0f9a8dfd0aa2c37fd837 [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"
Calin Juravlee6f49b42015-09-17 14:04:33 +000022#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:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000412 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
413 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100417 Location class_to_check = locations->InAt(1);
418 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
419 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000420 DCHECK(instruction_->IsCheckCast()
421 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
422 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100423 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000424
Alexandre Rames67555f72014-11-18 10:55:16 +0000425 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000426
427 if (instruction_->IsCheckCast()) {
428 // The codegen for the instruction overwrites `temp`, so put it back in place.
429 Register obj = InputRegisterAt(instruction_, 0);
430 Register temp = WRegisterFrom(locations->GetTemp(0));
431 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
432 __ Ldr(temp, HeapOperand(obj, class_offset));
433 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
434 }
435
436 if (!is_fatal_) {
437 SaveLiveRegisters(codegen, locations);
438 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439
440 // We're moving two locations to locations that could overlap, so we need a parallel
441 // move resolver.
442 InvokeRuntimeCallingConvention calling_convention;
443 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100444 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
445 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
447 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000448 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100449 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000450 Primitive::Type ret_type = instruction_->GetType();
451 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
452 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800453 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
454 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455 } else {
456 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100457 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800458 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459 }
460
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000461 if (!is_fatal_) {
462 RestoreLiveRegisters(codegen, locations);
463 __ B(GetExitLabel());
464 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000465 }
466
Alexandre Rames9931f312015-06-19 14:47:01 +0100467 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000468 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100469
Alexandre Rames67555f72014-11-18 10:55:16 +0000470 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000472 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
Alexandre Rames67555f72014-11-18 10:55:16 +0000474 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
475};
476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700477class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
478 public:
479 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
480 : instruction_(instruction) {}
481
482 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
483 __ Bind(GetEntryLabel());
484 SaveLiveRegisters(codegen, instruction_->GetLocations());
485 DCHECK(instruction_->IsDeoptimize());
486 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
487 uint32_t dex_pc = deoptimize->GetDexPc();
488 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
489 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
490 }
491
Alexandre Rames9931f312015-06-19 14:47:01 +0100492 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
493
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700494 private:
495 HInstruction* const instruction_;
496 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
497};
498
Alexandre Rames5319def2014-10-23 10:03:10 +0100499#undef __
500
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100501Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 Location next_location;
503 if (type == Primitive::kPrimVoid) {
504 LOG(FATAL) << "Unreachable type " << type;
505 }
506
Alexandre Rames542361f2015-01-29 16:57:31 +0000507 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100508 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
509 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000510 } else if (!Primitive::IsFloatingPointType(type) &&
511 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000512 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
513 } else {
514 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000515 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
516 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 }
518
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000519 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000520 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100521 return next_location;
522}
523
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100524Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100525 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100526}
527
Serban Constantinescu579885a2015-02-22 20:51:33 +0000528CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
529 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100530 const CompilerOptions& compiler_options,
531 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100532 : CodeGenerator(graph,
533 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000534 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000535 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000536 callee_saved_core_registers.list(),
537 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100538 compiler_options,
539 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100540 block_labels_(nullptr),
541 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000542 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000543 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000544 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100545 uint64_literals_(std::less<uint64_t>(),
546 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
547 method_patches_(MethodReferenceComparator(),
548 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
549 call_patches_(MethodReferenceComparator(),
550 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
551 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
552 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000553 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000554 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000555}
Alexandre Rames5319def2014-10-23 10:03:10 +0100556
Alexandre Rames67555f72014-11-18 10:55:16 +0000557#undef __
558#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100559
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000560void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
561 // Ensure we emit the literal pool.
562 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000563
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000564 CodeGenerator::Finalize(allocator);
565}
566
Zheng Xuad4450e2015-04-17 18:48:56 +0800567void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
568 // Note: There are 6 kinds of moves:
569 // 1. constant -> GPR/FPR (non-cycle)
570 // 2. constant -> stack (non-cycle)
571 // 3. GPR/FPR -> GPR/FPR
572 // 4. GPR/FPR -> stack
573 // 5. stack -> GPR/FPR
574 // 6. stack -> stack (non-cycle)
575 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
576 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
577 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
578 // dependency.
579 vixl_temps_.Open(GetVIXLAssembler());
580}
581
582void ParallelMoveResolverARM64::FinishEmitNativeCode() {
583 vixl_temps_.Close();
584}
585
586Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
587 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
588 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
589 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
590 Location scratch = GetScratchLocation(kind);
591 if (!scratch.Equals(Location::NoLocation())) {
592 return scratch;
593 }
594 // Allocate from VIXL temp registers.
595 if (kind == Location::kRegister) {
596 scratch = LocationFrom(vixl_temps_.AcquireX());
597 } else {
598 DCHECK(kind == Location::kFpuRegister);
599 scratch = LocationFrom(vixl_temps_.AcquireD());
600 }
601 AddScratchLocation(scratch);
602 return scratch;
603}
604
605void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
606 if (loc.IsRegister()) {
607 vixl_temps_.Release(XRegisterFrom(loc));
608 } else {
609 DCHECK(loc.IsFpuRegister());
610 vixl_temps_.Release(DRegisterFrom(loc));
611 }
612 RemoveScratchLocation(loc);
613}
614
Alexandre Rames3e69f162014-12-10 10:36:50 +0000615void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100616 DCHECK_LT(index, moves_.size());
617 MoveOperands* move = moves_[index];
Alexandre Rames3e69f162014-12-10 10:36:50 +0000618 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
619}
620
Alexandre Rames5319def2014-10-23 10:03:10 +0100621void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100622 MacroAssembler* masm = GetVIXLAssembler();
623 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000624 __ Bind(&frame_entry_label_);
625
Serban Constantinescu02164b32014-11-13 14:05:07 +0000626 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
627 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100628 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000629 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000630 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000631 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000632 __ Ldr(wzr, MemOperand(temp, 0));
633 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000634 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100635
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000636 if (!HasEmptyFrame()) {
637 int frame_size = GetFrameSize();
638 // Stack layout:
639 // sp[frame_size - 8] : lr.
640 // ... : other preserved core registers.
641 // ... : other preserved fp registers.
642 // ... : reserved frame space.
643 // sp[0] : current method.
644 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100645 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800646 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
647 frame_size - GetCoreSpillSize());
648 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
649 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000650 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100651}
652
653void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100654 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100655 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000656 if (!HasEmptyFrame()) {
657 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800658 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
659 frame_size - FrameEntrySpillSize());
660 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
661 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000662 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100663 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000664 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100665 __ Ret();
666 GetAssembler()->cfi().RestoreState();
667 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100668}
669
Zheng Xuda403092015-04-24 17:35:39 +0800670vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
671 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
672 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
673 core_spill_mask_);
674}
675
676vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
677 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
678 GetNumberOfFloatingPointRegisters()));
679 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
680 fpu_spill_mask_);
681}
682
Alexandre Rames5319def2014-10-23 10:03:10 +0100683void CodeGeneratorARM64::Bind(HBasicBlock* block) {
684 __ Bind(GetLabelOf(block));
685}
686
Alexandre Rames5319def2014-10-23 10:03:10 +0100687void CodeGeneratorARM64::Move(HInstruction* instruction,
688 Location location,
689 HInstruction* move_for) {
690 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000692 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100693
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100694 if (instruction->IsFakeString()) {
695 // The fake string is an alias for null.
696 DCHECK(IsBaseline());
697 instruction = locations->Out().GetConstant();
698 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
699 }
700
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100701 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700702 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100703 } else if (locations != nullptr && locations->Out().Equals(location)) {
704 return;
705 } else if (instruction->IsIntConstant()
706 || instruction->IsLongConstant()
707 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000708 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100709 if (location.IsRegister()) {
710 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000711 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100712 (instruction->IsLongConstant() && dst.Is64Bits()));
713 __ Mov(dst, value);
714 } else {
715 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000716 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000717 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
718 ? temps.AcquireW()
719 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100720 __ Mov(temp, value);
721 __ Str(temp, StackOperandFrom(location));
722 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000723 } else if (instruction->IsTemporary()) {
724 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000725 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100726 } else if (instruction->IsLoadLocal()) {
727 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000728 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000729 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000730 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000731 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 }
733
734 } else {
735 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000736 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100737 }
738}
739
Calin Juravle175dc732015-08-25 15:42:32 +0100740void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
741 DCHECK(location.IsRegister());
742 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
743}
744
Alexandre Rames5319def2014-10-23 10:03:10 +0100745Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
746 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000747
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 switch (type) {
749 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000750 case Primitive::kPrimInt:
751 case Primitive::kPrimFloat:
752 return Location::StackSlot(GetStackSlot(load->GetLocal()));
753
754 case Primitive::kPrimLong:
755 case Primitive::kPrimDouble:
756 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
757
Alexandre Rames5319def2014-10-23 10:03:10 +0100758 case Primitive::kPrimBoolean:
759 case Primitive::kPrimByte:
760 case Primitive::kPrimChar:
761 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100762 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100763 LOG(FATAL) << "Unexpected type " << type;
764 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000765
Alexandre Rames5319def2014-10-23 10:03:10 +0100766 LOG(FATAL) << "Unreachable";
767 return Location::NoLocation();
768}
769
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100770void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000771 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000773 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100774 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100775 if (value_can_be_null) {
776 __ Cbz(value, &done);
777 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100778 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
779 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000780 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100781 if (value_can_be_null) {
782 __ Bind(&done);
783 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100784}
785
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000786void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
787 // Blocked core registers:
788 // lr : Runtime reserved.
789 // tr : Runtime reserved.
790 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
791 // ip1 : VIXL core temp.
792 // ip0 : VIXL core temp.
793 //
794 // Blocked fp registers:
795 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100796 CPURegList reserved_core_registers = vixl_reserved_core_registers;
797 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100798 while (!reserved_core_registers.IsEmpty()) {
799 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
800 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000801
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000802 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800803 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000804 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
805 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000806
807 if (is_baseline) {
808 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
809 while (!reserved_core_baseline_registers.IsEmpty()) {
810 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
811 }
812
813 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
814 while (!reserved_fp_baseline_registers.IsEmpty()) {
815 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
816 }
817 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100818}
819
820Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
821 if (type == Primitive::kPrimVoid) {
822 LOG(FATAL) << "Unreachable type " << type;
823 }
824
Alexandre Rames542361f2015-01-29 16:57:31 +0000825 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000826 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
827 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100828 return Location::FpuRegisterLocation(reg);
829 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000830 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
831 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 return Location::RegisterLocation(reg);
833 }
834}
835
Alexandre Rames3e69f162014-12-10 10:36:50 +0000836size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
837 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
838 __ Str(reg, MemOperand(sp, stack_index));
839 return kArm64WordSize;
840}
841
842size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
843 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
844 __ Ldr(reg, MemOperand(sp, stack_index));
845 return kArm64WordSize;
846}
847
848size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
849 FPRegister reg = FPRegister(reg_id, kDRegSize);
850 __ Str(reg, MemOperand(sp, stack_index));
851 return kArm64WordSize;
852}
853
854size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
855 FPRegister reg = FPRegister(reg_id, kDRegSize);
856 __ Ldr(reg, MemOperand(sp, stack_index));
857 return kArm64WordSize;
858}
859
Alexandre Rames5319def2014-10-23 10:03:10 +0100860void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100861 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100862}
863
864void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100865 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100866}
867
Alexandre Rames67555f72014-11-18 10:55:16 +0000868void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000869 if (constant->IsIntConstant()) {
870 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
871 } else if (constant->IsLongConstant()) {
872 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
873 } else if (constant->IsNullConstant()) {
874 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000875 } else if (constant->IsFloatConstant()) {
876 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
877 } else {
878 DCHECK(constant->IsDoubleConstant());
879 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
880 }
881}
882
Alexandre Rames3e69f162014-12-10 10:36:50 +0000883
884static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
885 DCHECK(constant.IsConstant());
886 HConstant* cst = constant.GetConstant();
887 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000888 // Null is mapped to a core W register, which we associate with kPrimInt.
889 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
891 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
892 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
893}
894
895void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000896 if (source.Equals(destination)) {
897 return;
898 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000899
900 // A valid move can always be inferred from the destination and source
901 // locations. When moving from and to a register, the argument type can be
902 // used to generate 32bit instead of 64bit moves. In debug mode we also
903 // checks the coherency of the locations and the type.
904 bool unspecified_type = (type == Primitive::kPrimVoid);
905
906 if (destination.IsRegister() || destination.IsFpuRegister()) {
907 if (unspecified_type) {
908 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
909 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000910 (src_cst != nullptr && (src_cst->IsIntConstant()
911 || src_cst->IsFloatConstant()
912 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000913 // For stack slots and 32bit constants, a 64bit type is appropriate.
914 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000915 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000916 // If the source is a double stack slot or a 64bit constant, a 64bit
917 // type is appropriate. Else the source is a register, and since the
918 // type has not been specified, we chose a 64bit type to force a 64bit
919 // move.
920 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000921 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000922 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000923 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
924 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000925 CPURegister dst = CPURegisterFrom(destination, type);
926 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
927 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
928 __ Ldr(dst, StackOperandFrom(source));
929 } else if (source.IsConstant()) {
930 DCHECK(CoherentConstantAndType(source, type));
931 MoveConstant(dst, source.GetConstant());
932 } else {
933 if (destination.IsRegister()) {
934 __ Mov(Register(dst), RegisterFrom(source, type));
935 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800936 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000937 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
938 }
939 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000940 } else { // The destination is not a register. It must be a stack slot.
941 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
942 if (source.IsRegister() || source.IsFpuRegister()) {
943 if (unspecified_type) {
944 if (source.IsRegister()) {
945 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
946 } else {
947 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
948 }
949 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000950 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
951 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000952 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
953 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100954 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000955 UseScratchRegisterScope temps(GetVIXLAssembler());
956 HConstant* src_cst = source.GetConstant();
957 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000958 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000959 temp = temps.AcquireW();
960 } else if (src_cst->IsLongConstant()) {
961 temp = temps.AcquireX();
962 } else if (src_cst->IsFloatConstant()) {
963 temp = temps.AcquireS();
964 } else {
965 DCHECK(src_cst->IsDoubleConstant());
966 temp = temps.AcquireD();
967 }
968 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000969 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000970 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000971 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000972 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000973 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000974 // There is generally less pressure on FP registers.
975 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000976 __ Ldr(temp, StackOperandFrom(source));
977 __ Str(temp, StackOperandFrom(destination));
978 }
979 }
980}
981
982void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000983 CPURegister dst,
984 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000985 switch (type) {
986 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000987 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000988 break;
989 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000990 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000991 break;
992 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000993 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000994 break;
995 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000996 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000997 break;
998 case Primitive::kPrimInt:
999 case Primitive::kPrimNot:
1000 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001001 case Primitive::kPrimFloat:
1002 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001003 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001004 __ Ldr(dst, src);
1005 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001006 case Primitive::kPrimVoid:
1007 LOG(FATAL) << "Unreachable type " << type;
1008 }
1009}
1010
Calin Juravle77520bc2015-01-12 18:45:46 +00001011void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001012 CPURegister dst,
1013 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001014 MacroAssembler* masm = GetVIXLAssembler();
1015 BlockPoolsScope block_pools(masm);
1016 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001017 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001018 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001019
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001020 DCHECK(!src.IsPreIndex());
1021 DCHECK(!src.IsPostIndex());
1022
1023 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001024 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001025 MemOperand base = MemOperand(temp_base);
1026 switch (type) {
1027 case Primitive::kPrimBoolean:
1028 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001029 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001030 break;
1031 case Primitive::kPrimByte:
1032 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001033 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1035 break;
1036 case Primitive::kPrimChar:
1037 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001038 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001039 break;
1040 case Primitive::kPrimShort:
1041 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001042 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1044 break;
1045 case Primitive::kPrimInt:
1046 case Primitive::kPrimNot:
1047 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001048 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001049 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001050 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001051 break;
1052 case Primitive::kPrimFloat:
1053 case Primitive::kPrimDouble: {
1054 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001055 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001056
1057 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1058 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001059 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 __ Fmov(FPRegister(dst), temp);
1061 break;
1062 }
1063 case Primitive::kPrimVoid:
1064 LOG(FATAL) << "Unreachable type " << type;
1065 }
1066}
1067
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001068void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001069 CPURegister src,
1070 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001071 switch (type) {
1072 case Primitive::kPrimBoolean:
1073 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001074 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001075 break;
1076 case Primitive::kPrimChar:
1077 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001078 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001079 break;
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimNot:
1082 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001083 case Primitive::kPrimFloat:
1084 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001085 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001086 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001087 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001088 case Primitive::kPrimVoid:
1089 LOG(FATAL) << "Unreachable type " << type;
1090 }
1091}
1092
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001093void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1094 CPURegister src,
1095 const MemOperand& dst) {
1096 UseScratchRegisterScope temps(GetVIXLAssembler());
1097 Register temp_base = temps.AcquireX();
1098
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099 DCHECK(!dst.IsPreIndex());
1100 DCHECK(!dst.IsPostIndex());
1101
1102 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001103 Operand op = OperandFromMemOperand(dst);
1104 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001105 MemOperand base = MemOperand(temp_base);
1106 switch (type) {
1107 case Primitive::kPrimBoolean:
1108 case Primitive::kPrimByte:
1109 __ Stlrb(Register(src), base);
1110 break;
1111 case Primitive::kPrimChar:
1112 case Primitive::kPrimShort:
1113 __ Stlrh(Register(src), base);
1114 break;
1115 case Primitive::kPrimInt:
1116 case Primitive::kPrimNot:
1117 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001118 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001119 __ Stlr(Register(src), base);
1120 break;
1121 case Primitive::kPrimFloat:
1122 case Primitive::kPrimDouble: {
1123 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001124 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125
1126 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1127 __ Fmov(temp, FPRegister(src));
1128 __ Stlr(temp, base);
1129 break;
1130 }
1131 case Primitive::kPrimVoid:
1132 LOG(FATAL) << "Unreachable type " << type;
1133 }
1134}
1135
Calin Juravle175dc732015-08-25 15:42:32 +01001136void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1137 HInstruction* instruction,
1138 uint32_t dex_pc,
1139 SlowPathCode* slow_path) {
1140 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1141 instruction,
1142 dex_pc,
1143 slow_path);
1144}
1145
Alexandre Rames67555f72014-11-18 10:55:16 +00001146void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1147 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001148 uint32_t dex_pc,
1149 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001150 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001151 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001152 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1153 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001154 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001155}
1156
1157void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1158 vixl::Register class_reg) {
1159 UseScratchRegisterScope temps(GetVIXLAssembler());
1160 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001161 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001162 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001163
Serban Constantinescu02164b32014-11-13 14:05:07 +00001164 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001165 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001166 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1167 __ Add(temp, class_reg, status_offset);
1168 __ Ldar(temp, HeapOperand(temp));
1169 __ Cmp(temp, mirror::Class::kStatusInitialized);
1170 __ B(lt, slow_path->GetEntryLabel());
1171 } else {
1172 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1173 __ Cmp(temp, mirror::Class::kStatusInitialized);
1174 __ B(lt, slow_path->GetEntryLabel());
1175 __ Dmb(InnerShareable, BarrierReads);
1176 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001177 __ Bind(slow_path->GetExitLabel());
1178}
Alexandre Rames5319def2014-10-23 10:03:10 +01001179
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001180void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1181 BarrierType type = BarrierAll;
1182
1183 switch (kind) {
1184 case MemBarrierKind::kAnyAny:
1185 case MemBarrierKind::kAnyStore: {
1186 type = BarrierAll;
1187 break;
1188 }
1189 case MemBarrierKind::kLoadAny: {
1190 type = BarrierReads;
1191 break;
1192 }
1193 case MemBarrierKind::kStoreStore: {
1194 type = BarrierWrites;
1195 break;
1196 }
1197 default:
1198 LOG(FATAL) << "Unexpected memory barrier " << kind;
1199 }
1200 __ Dmb(InnerShareable, type);
1201}
1202
Serban Constantinescu02164b32014-11-13 14:05:07 +00001203void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1204 HBasicBlock* successor) {
1205 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001206 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1207 if (slow_path == nullptr) {
1208 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1209 instruction->SetSlowPath(slow_path);
1210 codegen_->AddSlowPath(slow_path);
1211 if (successor != nullptr) {
1212 DCHECK(successor->IsLoopHeader());
1213 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1214 }
1215 } else {
1216 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1217 }
1218
Serban Constantinescu02164b32014-11-13 14:05:07 +00001219 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1220 Register temp = temps.AcquireW();
1221
1222 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1223 if (successor == nullptr) {
1224 __ Cbnz(temp, slow_path->GetEntryLabel());
1225 __ Bind(slow_path->GetReturnLabel());
1226 } else {
1227 __ Cbz(temp, codegen_->GetLabelOf(successor));
1228 __ B(slow_path->GetEntryLabel());
1229 // slow_path will return to GetLabelOf(successor).
1230 }
1231}
1232
Alexandre Rames5319def2014-10-23 10:03:10 +01001233InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1234 CodeGeneratorARM64* codegen)
1235 : HGraphVisitor(graph),
1236 assembler_(codegen->GetAssembler()),
1237 codegen_(codegen) {}
1238
1239#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001241
1242#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1243
1244enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001245 // Using a base helps identify when we hit such breakpoints.
1246 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001247#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1248 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1249#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1250};
1251
1252#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1253 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001254 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001255 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1256 } \
1257 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1259 locations->SetOut(Location::Any()); \
1260 }
1261 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1262#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1263
1264#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001265#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001266
Alexandre Rames67555f72014-11-18 10:55:16 +00001267void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001268 DCHECK_EQ(instr->InputCount(), 2U);
1269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1270 Primitive::Type type = instr->GetResultType();
1271 switch (type) {
1272 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001273 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001274 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001275 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001277 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001278
1279 case Primitive::kPrimFloat:
1280 case Primitive::kPrimDouble:
1281 locations->SetInAt(0, Location::RequiresFpuRegister());
1282 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001284 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001285
Alexandre Rames5319def2014-10-23 10:03:10 +01001286 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001287 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001288 }
1289}
1290
Alexandre Rames09a99962015-04-15 11:47:56 +01001291void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1292 LocationSummary* locations =
1293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1294 locations->SetInAt(0, Location::RequiresRegister());
1295 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1296 locations->SetOut(Location::RequiresFpuRegister());
1297 } else {
1298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1299 }
1300}
1301
1302void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1303 const FieldInfo& field_info) {
1304 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001305 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001306 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001307
1308 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1309 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1310
1311 if (field_info.IsVolatile()) {
1312 if (use_acquire_release) {
1313 // NB: LoadAcquire will record the pc info if needed.
1314 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1315 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001316 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001317 codegen_->MaybeRecordImplicitNullCheck(instruction);
1318 // For IRIW sequential consistency kLoadAny is not sufficient.
1319 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1320 }
1321 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001322 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001323 codegen_->MaybeRecordImplicitNullCheck(instruction);
1324 }
Roland Levillain4d027112015-07-01 15:41:14 +01001325
1326 if (field_type == Primitive::kPrimNot) {
1327 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1328 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001329}
1330
1331void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1332 LocationSummary* locations =
1333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1334 locations->SetInAt(0, Location::RequiresRegister());
1335 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1336 locations->SetInAt(1, Location::RequiresFpuRegister());
1337 } else {
1338 locations->SetInAt(1, Location::RequiresRegister());
1339 }
1340}
1341
1342void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001343 const FieldInfo& field_info,
1344 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001345 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001346 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001347
1348 Register obj = InputRegisterAt(instruction, 0);
1349 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001350 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001351 Offset offset = field_info.GetFieldOffset();
1352 Primitive::Type field_type = field_info.GetFieldType();
1353 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1354
Roland Levillain4d027112015-07-01 15:41:14 +01001355 {
1356 // We use a block to end the scratch scope before the write barrier, thus
1357 // freeing the temporary registers so they can be used in `MarkGCCard`.
1358 UseScratchRegisterScope temps(GetVIXLAssembler());
1359
1360 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1361 DCHECK(value.IsW());
1362 Register temp = temps.AcquireW();
1363 __ Mov(temp, value.W());
1364 GetAssembler()->PoisonHeapReference(temp.W());
1365 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001366 }
Roland Levillain4d027112015-07-01 15:41:14 +01001367
1368 if (field_info.IsVolatile()) {
1369 if (use_acquire_release) {
1370 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1371 codegen_->MaybeRecordImplicitNullCheck(instruction);
1372 } else {
1373 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1374 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1375 codegen_->MaybeRecordImplicitNullCheck(instruction);
1376 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1377 }
1378 } else {
1379 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1380 codegen_->MaybeRecordImplicitNullCheck(instruction);
1381 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001382 }
1383
1384 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001385 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001386 }
1387}
1388
Alexandre Rames67555f72014-11-18 10:55:16 +00001389void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001391
1392 switch (type) {
1393 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001394 case Primitive::kPrimLong: {
1395 Register dst = OutputRegister(instr);
1396 Register lhs = InputRegisterAt(instr, 0);
1397 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001398 if (instr->IsAdd()) {
1399 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001400 } else if (instr->IsAnd()) {
1401 __ And(dst, lhs, rhs);
1402 } else if (instr->IsOr()) {
1403 __ Orr(dst, lhs, rhs);
1404 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001405 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001406 } else {
1407 DCHECK(instr->IsXor());
1408 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001409 }
1410 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001411 }
1412 case Primitive::kPrimFloat:
1413 case Primitive::kPrimDouble: {
1414 FPRegister dst = OutputFPRegister(instr);
1415 FPRegister lhs = InputFPRegisterAt(instr, 0);
1416 FPRegister rhs = InputFPRegisterAt(instr, 1);
1417 if (instr->IsAdd()) {
1418 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001419 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001420 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001421 } else {
1422 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001423 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001424 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001425 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001426 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001427 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001428 }
1429}
1430
Serban Constantinescu02164b32014-11-13 14:05:07 +00001431void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1432 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1433
1434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1435 Primitive::Type type = instr->GetResultType();
1436 switch (type) {
1437 case Primitive::kPrimInt:
1438 case Primitive::kPrimLong: {
1439 locations->SetInAt(0, Location::RequiresRegister());
1440 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1441 locations->SetOut(Location::RequiresRegister());
1442 break;
1443 }
1444 default:
1445 LOG(FATAL) << "Unexpected shift type " << type;
1446 }
1447}
1448
1449void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1450 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1451
1452 Primitive::Type type = instr->GetType();
1453 switch (type) {
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimLong: {
1456 Register dst = OutputRegister(instr);
1457 Register lhs = InputRegisterAt(instr, 0);
1458 Operand rhs = InputOperandAt(instr, 1);
1459 if (rhs.IsImmediate()) {
1460 uint32_t shift_value = (type == Primitive::kPrimInt)
1461 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1462 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1463 if (instr->IsShl()) {
1464 __ Lsl(dst, lhs, shift_value);
1465 } else if (instr->IsShr()) {
1466 __ Asr(dst, lhs, shift_value);
1467 } else {
1468 __ Lsr(dst, lhs, shift_value);
1469 }
1470 } else {
1471 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1472
1473 if (instr->IsShl()) {
1474 __ Lsl(dst, lhs, rhs_reg);
1475 } else if (instr->IsShr()) {
1476 __ Asr(dst, lhs, rhs_reg);
1477 } else {
1478 __ Lsr(dst, lhs, rhs_reg);
1479 }
1480 }
1481 break;
1482 }
1483 default:
1484 LOG(FATAL) << "Unexpected shift operation type " << type;
1485 }
1486}
1487
Alexandre Rames5319def2014-10-23 10:03:10 +01001488void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001489 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001490}
1491
1492void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001493 HandleBinaryOp(instruction);
1494}
1495
1496void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1497 HandleBinaryOp(instruction);
1498}
1499
1500void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1501 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001502}
1503
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001504void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1505 LocationSummary* locations =
1506 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001509 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1510 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1511 } else {
1512 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1513 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001514}
1515
1516void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1517 LocationSummary* locations = instruction->GetLocations();
1518 Primitive::Type type = instruction->GetType();
1519 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001520 Location index = locations->InAt(1);
1521 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001522 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001523 MacroAssembler* masm = GetVIXLAssembler();
1524 UseScratchRegisterScope temps(masm);
1525 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001526
1527 if (index.IsConstant()) {
1528 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001529 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001530 } else {
1531 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001532 __ Add(temp, obj, offset);
1533 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001534 }
1535
Alexandre Rames67555f72014-11-18 10:55:16 +00001536 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001537 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001538
1539 if (type == Primitive::kPrimNot) {
1540 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1541 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001542}
1543
Alexandre Rames5319def2014-10-23 10:03:10 +01001544void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1546 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001548}
1549
1550void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001551 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001552 __ Ldr(OutputRegister(instruction),
1553 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001554 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001555}
1556
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001558 if (instruction->NeedsTypeCheck()) {
1559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561 InvokeRuntimeCallingConvention calling_convention;
1562 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1563 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1564 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1565 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001566 LocationSummary* locations =
1567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 locations->SetInAt(0, Location::RequiresRegister());
1569 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001570 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1571 locations->SetInAt(2, Location::RequiresFpuRegister());
1572 } else {
1573 locations->SetInAt(2, Location::RequiresRegister());
1574 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001575 }
1576}
1577
1578void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1579 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001580 LocationSummary* locations = instruction->GetLocations();
1581 bool needs_runtime_call = locations->WillCall();
1582
1583 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001584 // Note: if heap poisoning is enabled, pAputObject takes cares
1585 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001586 codegen_->InvokeRuntime(
1587 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001588 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001590 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001592 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 Location index = locations->InAt(1);
1594 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001595 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001596 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001597 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001598 {
1599 // We use a block to end the scratch scope before the write barrier, thus
1600 // freeing the temporary registers so they can be used in `MarkGCCard`.
1601 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001602
Roland Levillain4d027112015-07-01 15:41:14 +01001603 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1604 DCHECK(value.IsW());
1605 Register temp = temps.AcquireW();
1606 __ Mov(temp, value.W());
1607 GetAssembler()->PoisonHeapReference(temp.W());
1608 source = temp;
1609 }
1610
Alexandre Rames97833a02015-04-16 15:07:12 +01001611 if (index.IsConstant()) {
1612 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1613 destination = HeapOperand(obj, offset);
1614 } else {
1615 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001616 __ Add(temp, obj, offset);
1617 destination = HeapOperand(temp,
1618 XRegisterFrom(index),
1619 LSL,
1620 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001621 }
1622
Roland Levillain4d027112015-07-01 15:41:14 +01001623 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001624 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001625 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001626 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001627 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001628 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629 }
1630}
1631
Alexandre Rames67555f72014-11-18 10:55:16 +00001632void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001633 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1634 ? LocationSummary::kCallOnSlowPath
1635 : LocationSummary::kNoCall;
1636 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001637 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001638 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001639 if (instruction->HasUses()) {
1640 locations->SetOut(Location::SameAsFirstInput());
1641 }
1642}
1643
1644void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001645 BoundsCheckSlowPathARM64* slow_path =
1646 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001647 codegen_->AddSlowPath(slow_path);
1648
1649 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1650 __ B(slow_path->GetEntryLabel(), hs);
1651}
1652
Alexandre Rames67555f72014-11-18 10:55:16 +00001653void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1654 LocationSummary* locations =
1655 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1656 locations->SetInAt(0, Location::RequiresRegister());
1657 if (check->HasUses()) {
1658 locations->SetOut(Location::SameAsFirstInput());
1659 }
1660}
1661
1662void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1663 // We assume the class is not null.
1664 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1665 check->GetLoadClass(), check, check->GetDexPc(), true);
1666 codegen_->AddSlowPath(slow_path);
1667 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1668}
1669
Roland Levillain7f63c522015-07-13 15:54:55 +00001670static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1671 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1672 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1673}
1674
Serban Constantinescu02164b32014-11-13 14:05:07 +00001675void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001676 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001677 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1678 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001679 switch (in_type) {
1680 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001681 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001682 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1684 break;
1685 }
1686 case Primitive::kPrimFloat:
1687 case Primitive::kPrimDouble: {
1688 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001689 locations->SetInAt(1,
1690 IsFloatingPointZeroConstant(compare->InputAt(1))
1691 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1692 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001693 locations->SetOut(Location::RequiresRegister());
1694 break;
1695 }
1696 default:
1697 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1698 }
1699}
1700
1701void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1702 Primitive::Type in_type = compare->InputAt(0)->GetType();
1703
1704 // 0 if: left == right
1705 // 1 if: left > right
1706 // -1 if: left < right
1707 switch (in_type) {
1708 case Primitive::kPrimLong: {
1709 Register result = OutputRegister(compare);
1710 Register left = InputRegisterAt(compare, 0);
1711 Operand right = InputOperandAt(compare, 1);
1712
1713 __ Cmp(left, right);
1714 __ Cset(result, ne);
1715 __ Cneg(result, result, lt);
1716 break;
1717 }
1718 case Primitive::kPrimFloat:
1719 case Primitive::kPrimDouble: {
1720 Register result = OutputRegister(compare);
1721 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001722 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001723 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1724 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001725 __ Fcmp(left, 0.0);
1726 } else {
1727 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1728 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001729 if (compare->IsGtBias()) {
1730 __ Cset(result, ne);
1731 } else {
1732 __ Csetm(result, ne);
1733 }
1734 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001735 break;
1736 }
1737 default:
1738 LOG(FATAL) << "Unimplemented compare type " << in_type;
1739 }
1740}
1741
1742void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001744
1745 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1746 locations->SetInAt(0, Location::RequiresFpuRegister());
1747 locations->SetInAt(1,
1748 IsFloatingPointZeroConstant(instruction->InputAt(1))
1749 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1750 : Location::RequiresFpuRegister());
1751 } else {
1752 // Integer cases.
1753 locations->SetInAt(0, Location::RequiresRegister());
1754 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1755 }
1756
Alexandre Rames5319def2014-10-23 10:03:10 +01001757 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001759 }
1760}
1761
1762void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1763 if (!instruction->NeedsMaterialization()) {
1764 return;
1765 }
1766
1767 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001768 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001769 IfCondition if_cond = instruction->GetCondition();
1770 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001771
Roland Levillain7f63c522015-07-13 15:54:55 +00001772 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1773 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1774 if (locations->InAt(1).IsConstant()) {
1775 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1776 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1777 __ Fcmp(lhs, 0.0);
1778 } else {
1779 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1780 }
1781 __ Cset(res, arm64_cond);
1782 if (instruction->IsFPConditionTrueIfNaN()) {
1783 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1784 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1785 } else if (instruction->IsFPConditionFalseIfNaN()) {
1786 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1787 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1788 }
1789 } else {
1790 // Integer cases.
1791 Register lhs = InputRegisterAt(instruction, 0);
1792 Operand rhs = InputOperandAt(instruction, 1);
1793 __ Cmp(lhs, rhs);
1794 __ Cset(res, arm64_cond);
1795 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001796}
1797
1798#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1799 M(Equal) \
1800 M(NotEqual) \
1801 M(LessThan) \
1802 M(LessThanOrEqual) \
1803 M(GreaterThan) \
1804 M(GreaterThanOrEqual)
1805#define DEFINE_CONDITION_VISITORS(Name) \
1806void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1807void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1808FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001809#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001810#undef FOR_EACH_CONDITION_INSTRUCTION
1811
Zheng Xuc6667102015-05-15 16:08:45 +08001812void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1813 DCHECK(instruction->IsDiv() || instruction->IsRem());
1814
1815 LocationSummary* locations = instruction->GetLocations();
1816 Location second = locations->InAt(1);
1817 DCHECK(second.IsConstant());
1818
1819 Register out = OutputRegister(instruction);
1820 Register dividend = InputRegisterAt(instruction, 0);
1821 int64_t imm = Int64FromConstant(second.GetConstant());
1822 DCHECK(imm == 1 || imm == -1);
1823
1824 if (instruction->IsRem()) {
1825 __ Mov(out, 0);
1826 } else {
1827 if (imm == 1) {
1828 __ Mov(out, dividend);
1829 } else {
1830 __ Neg(out, dividend);
1831 }
1832 }
1833}
1834
1835void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1836 DCHECK(instruction->IsDiv() || instruction->IsRem());
1837
1838 LocationSummary* locations = instruction->GetLocations();
1839 Location second = locations->InAt(1);
1840 DCHECK(second.IsConstant());
1841
1842 Register out = OutputRegister(instruction);
1843 Register dividend = InputRegisterAt(instruction, 0);
1844 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001845 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001846 DCHECK(IsPowerOfTwo(abs_imm));
1847 int ctz_imm = CTZ(abs_imm);
1848
1849 UseScratchRegisterScope temps(GetVIXLAssembler());
1850 Register temp = temps.AcquireSameSizeAs(out);
1851
1852 if (instruction->IsDiv()) {
1853 __ Add(temp, dividend, abs_imm - 1);
1854 __ Cmp(dividend, 0);
1855 __ Csel(out, temp, dividend, lt);
1856 if (imm > 0) {
1857 __ Asr(out, out, ctz_imm);
1858 } else {
1859 __ Neg(out, Operand(out, ASR, ctz_imm));
1860 }
1861 } else {
1862 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1863 __ Asr(temp, dividend, bits - 1);
1864 __ Lsr(temp, temp, bits - ctz_imm);
1865 __ Add(out, dividend, temp);
1866 __ And(out, out, abs_imm - 1);
1867 __ Sub(out, out, temp);
1868 }
1869}
1870
1871void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1872 DCHECK(instruction->IsDiv() || instruction->IsRem());
1873
1874 LocationSummary* locations = instruction->GetLocations();
1875 Location second = locations->InAt(1);
1876 DCHECK(second.IsConstant());
1877
1878 Register out = OutputRegister(instruction);
1879 Register dividend = InputRegisterAt(instruction, 0);
1880 int64_t imm = Int64FromConstant(second.GetConstant());
1881
1882 Primitive::Type type = instruction->GetResultType();
1883 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1884
1885 int64_t magic;
1886 int shift;
1887 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1888
1889 UseScratchRegisterScope temps(GetVIXLAssembler());
1890 Register temp = temps.AcquireSameSizeAs(out);
1891
1892 // temp = get_high(dividend * magic)
1893 __ Mov(temp, magic);
1894 if (type == Primitive::kPrimLong) {
1895 __ Smulh(temp, dividend, temp);
1896 } else {
1897 __ Smull(temp.X(), dividend, temp);
1898 __ Lsr(temp.X(), temp.X(), 32);
1899 }
1900
1901 if (imm > 0 && magic < 0) {
1902 __ Add(temp, temp, dividend);
1903 } else if (imm < 0 && magic > 0) {
1904 __ Sub(temp, temp, dividend);
1905 }
1906
1907 if (shift != 0) {
1908 __ Asr(temp, temp, shift);
1909 }
1910
1911 if (instruction->IsDiv()) {
1912 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1913 } else {
1914 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1915 // TODO: Strength reduction for msub.
1916 Register temp_imm = temps.AcquireSameSizeAs(out);
1917 __ Mov(temp_imm, imm);
1918 __ Msub(out, temp, temp_imm, dividend);
1919 }
1920}
1921
1922void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1923 DCHECK(instruction->IsDiv() || instruction->IsRem());
1924 Primitive::Type type = instruction->GetResultType();
1925 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1926
1927 LocationSummary* locations = instruction->GetLocations();
1928 Register out = OutputRegister(instruction);
1929 Location second = locations->InAt(1);
1930
1931 if (second.IsConstant()) {
1932 int64_t imm = Int64FromConstant(second.GetConstant());
1933
1934 if (imm == 0) {
1935 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1936 } else if (imm == 1 || imm == -1) {
1937 DivRemOneOrMinusOne(instruction);
1938 } else if (IsPowerOfTwo(std::abs(imm))) {
1939 DivRemByPowerOfTwo(instruction);
1940 } else {
1941 DCHECK(imm <= -2 || imm >= 2);
1942 GenerateDivRemWithAnyConstant(instruction);
1943 }
1944 } else {
1945 Register dividend = InputRegisterAt(instruction, 0);
1946 Register divisor = InputRegisterAt(instruction, 1);
1947 if (instruction->IsDiv()) {
1948 __ Sdiv(out, dividend, divisor);
1949 } else {
1950 UseScratchRegisterScope temps(GetVIXLAssembler());
1951 Register temp = temps.AcquireSameSizeAs(out);
1952 __ Sdiv(temp, dividend, divisor);
1953 __ Msub(out, temp, divisor, dividend);
1954 }
1955 }
1956}
1957
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001958void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1959 LocationSummary* locations =
1960 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1961 switch (div->GetResultType()) {
1962 case Primitive::kPrimInt:
1963 case Primitive::kPrimLong:
1964 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001965 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1967 break;
1968
1969 case Primitive::kPrimFloat:
1970 case Primitive::kPrimDouble:
1971 locations->SetInAt(0, Location::RequiresFpuRegister());
1972 locations->SetInAt(1, Location::RequiresFpuRegister());
1973 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1974 break;
1975
1976 default:
1977 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1978 }
1979}
1980
1981void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1982 Primitive::Type type = div->GetResultType();
1983 switch (type) {
1984 case Primitive::kPrimInt:
1985 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001986 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001987 break;
1988
1989 case Primitive::kPrimFloat:
1990 case Primitive::kPrimDouble:
1991 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1992 break;
1993
1994 default:
1995 LOG(FATAL) << "Unexpected div type " << type;
1996 }
1997}
1998
Alexandre Rames67555f72014-11-18 10:55:16 +00001999void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002000 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2001 ? LocationSummary::kCallOnSlowPath
2002 : LocationSummary::kNoCall;
2003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002004 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2005 if (instruction->HasUses()) {
2006 locations->SetOut(Location::SameAsFirstInput());
2007 }
2008}
2009
2010void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2011 SlowPathCodeARM64* slow_path =
2012 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2013 codegen_->AddSlowPath(slow_path);
2014 Location value = instruction->GetLocations()->InAt(0);
2015
Alexandre Rames3e69f162014-12-10 10:36:50 +00002016 Primitive::Type type = instruction->GetType();
2017
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002018 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2019 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002020 return;
2021 }
2022
Alexandre Rames67555f72014-11-18 10:55:16 +00002023 if (value.IsConstant()) {
2024 int64_t divisor = Int64ConstantFrom(value);
2025 if (divisor == 0) {
2026 __ B(slow_path->GetEntryLabel());
2027 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002028 // A division by a non-null constant is valid. We don't need to perform
2029 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002030 }
2031 } else {
2032 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2033 }
2034}
2035
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002036void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2037 LocationSummary* locations =
2038 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2039 locations->SetOut(Location::ConstantLocation(constant));
2040}
2041
2042void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2043 UNUSED(constant);
2044 // Will be generated at use site.
2045}
2046
Alexandre Rames5319def2014-10-23 10:03:10 +01002047void LocationsBuilderARM64::VisitExit(HExit* exit) {
2048 exit->SetLocations(nullptr);
2049}
2050
2051void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002052 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002053}
2054
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002055void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2056 LocationSummary* locations =
2057 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2058 locations->SetOut(Location::ConstantLocation(constant));
2059}
2060
2061void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2062 UNUSED(constant);
2063 // Will be generated at use site.
2064}
2065
David Brazdilfc6a86a2015-06-26 10:33:45 +00002066void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002067 DCHECK(!successor->IsExitBlock());
2068 HBasicBlock* block = got->GetBlock();
2069 HInstruction* previous = got->GetPrevious();
2070 HLoopInformation* info = block->GetLoopInformation();
2071
David Brazdil46e2a392015-03-16 17:31:52 +00002072 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002073 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2074 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2075 return;
2076 }
2077 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2078 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2079 }
2080 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002081 __ B(codegen_->GetLabelOf(successor));
2082 }
2083}
2084
David Brazdilfc6a86a2015-06-26 10:33:45 +00002085void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2086 got->SetLocations(nullptr);
2087}
2088
2089void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2090 HandleGoto(got, got->GetSuccessor());
2091}
2092
2093void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2094 try_boundary->SetLocations(nullptr);
2095}
2096
2097void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2098 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2099 if (!successor->IsExitBlock()) {
2100 HandleGoto(try_boundary, successor);
2101 }
2102}
2103
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002104void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2105 vixl::Label* true_target,
2106 vixl::Label* false_target,
2107 vixl::Label* always_true_target) {
2108 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002109 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002110
Serban Constantinescu02164b32014-11-13 14:05:07 +00002111 if (cond->IsIntConstant()) {
2112 int32_t cond_value = cond->AsIntConstant()->GetValue();
2113 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002114 if (always_true_target != nullptr) {
2115 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002116 }
2117 return;
2118 } else {
2119 DCHECK_EQ(cond_value, 0);
2120 }
2121 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002122 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002123 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002124 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002125 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002126 } else {
2127 // The condition instruction has not been materialized, use its inputs as
2128 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002129 Primitive::Type type =
2130 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2131
2132 if (Primitive::IsFloatingPointType(type)) {
2133 // FP compares don't like null false_targets.
2134 if (false_target == nullptr) {
2135 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002136 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002137 FPRegister lhs = InputFPRegisterAt(condition, 0);
2138 if (condition->GetLocations()->InAt(1).IsConstant()) {
2139 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2140 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2141 __ Fcmp(lhs, 0.0);
2142 } else {
2143 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2144 }
2145 if (condition->IsFPConditionTrueIfNaN()) {
2146 __ B(vs, true_target); // VS for unordered.
2147 } else if (condition->IsFPConditionFalseIfNaN()) {
2148 __ B(vs, false_target); // VS for unordered.
2149 }
2150 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002151 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002152 // Integer cases.
2153 Register lhs = InputRegisterAt(condition, 0);
2154 Operand rhs = InputOperandAt(condition, 1);
2155 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2156 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2157 switch (arm64_cond) {
2158 case eq:
2159 __ Cbz(lhs, true_target);
2160 break;
2161 case ne:
2162 __ Cbnz(lhs, true_target);
2163 break;
2164 case lt:
2165 // Test the sign bit and branch accordingly.
2166 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2167 break;
2168 case ge:
2169 // Test the sign bit and branch accordingly.
2170 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2171 break;
2172 default:
2173 // Without the `static_cast` the compiler throws an error for
2174 // `-Werror=sign-promo`.
2175 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2176 }
2177 } else {
2178 __ Cmp(lhs, rhs);
2179 __ B(arm64_cond, true_target);
2180 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002181 }
2182 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002183 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002184 __ B(false_target);
2185 }
2186}
2187
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002188void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2190 HInstruction* cond = if_instr->InputAt(0);
2191 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2192 locations->SetInAt(0, Location::RequiresRegister());
2193 }
2194}
2195
2196void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2197 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2198 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2199 vixl::Label* always_true_target = true_target;
2200 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2201 if_instr->IfTrueSuccessor())) {
2202 always_true_target = nullptr;
2203 }
2204 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2205 if_instr->IfFalseSuccessor())) {
2206 false_target = nullptr;
2207 }
2208 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2209}
2210
2211void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2212 LocationSummary* locations = new (GetGraph()->GetArena())
2213 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2214 HInstruction* cond = deoptimize->InputAt(0);
2215 DCHECK(cond->IsCondition());
2216 if (cond->AsCondition()->NeedsMaterialization()) {
2217 locations->SetInAt(0, Location::RequiresRegister());
2218 }
2219}
2220
2221void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2222 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2223 DeoptimizationSlowPathARM64(deoptimize);
2224 codegen_->AddSlowPath(slow_path);
2225 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2226 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2227}
2228
Alexandre Rames5319def2014-10-23 10:03:10 +01002229void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002230 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002231}
2232
2233void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002234 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002235}
2236
2237void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002238 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002239}
2240
2241void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002242 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002243}
2244
Alexandre Rames67555f72014-11-18 10:55:16 +00002245void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002246 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2247 switch (instruction->GetTypeCheckKind()) {
2248 case TypeCheckKind::kExactCheck:
2249 case TypeCheckKind::kAbstractClassCheck:
2250 case TypeCheckKind::kClassHierarchyCheck:
2251 case TypeCheckKind::kArrayObjectCheck:
2252 call_kind = LocationSummary::kNoCall;
2253 break;
2254 case TypeCheckKind::kInterfaceCheck:
2255 call_kind = LocationSummary::kCall;
2256 break;
2257 case TypeCheckKind::kArrayCheck:
2258 call_kind = LocationSummary::kCallOnSlowPath;
2259 break;
2260 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002262 if (call_kind != LocationSummary::kCall) {
2263 locations->SetInAt(0, Location::RequiresRegister());
2264 locations->SetInAt(1, Location::RequiresRegister());
2265 // The out register is used as a temporary, so it overlaps with the inputs.
2266 // Note that TypeCheckSlowPathARM64 uses this register too.
2267 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2268 } else {
2269 InvokeRuntimeCallingConvention calling_convention;
2270 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2271 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2272 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2273 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002274}
2275
2276void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2277 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002278 Register obj = InputRegisterAt(instruction, 0);
2279 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002280 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002281 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2282 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2283 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2284 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002285
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002286 vixl::Label done, zero;
2287 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002288
2289 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002290 // Avoid null check if we know `obj` is not null.
2291 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002292 __ Cbz(obj, &zero);
2293 }
2294
2295 // In case of an interface check, we put the object class into the object register.
2296 // This is safe, as the register is caller-save, and the object must be in another
2297 // register if it survives the runtime call.
2298 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2299 ? obj
2300 : out;
2301 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2302 GetAssembler()->MaybeUnpoisonHeapReference(target);
2303
2304 switch (instruction->GetTypeCheckKind()) {
2305 case TypeCheckKind::kExactCheck: {
2306 __ Cmp(out, cls);
2307 __ Cset(out, eq);
2308 if (zero.IsLinked()) {
2309 __ B(&done);
2310 }
2311 break;
2312 }
2313 case TypeCheckKind::kAbstractClassCheck: {
2314 // If the class is abstract, we eagerly fetch the super class of the
2315 // object to avoid doing a comparison we know will fail.
2316 vixl::Label loop, success;
2317 __ Bind(&loop);
2318 __ Ldr(out, HeapOperand(out, super_offset));
2319 GetAssembler()->MaybeUnpoisonHeapReference(out);
2320 // If `out` is null, we use it for the result, and jump to `done`.
2321 __ Cbz(out, &done);
2322 __ Cmp(out, cls);
2323 __ B(ne, &loop);
2324 __ Mov(out, 1);
2325 if (zero.IsLinked()) {
2326 __ B(&done);
2327 }
2328 break;
2329 }
2330 case TypeCheckKind::kClassHierarchyCheck: {
2331 // Walk over the class hierarchy to find a match.
2332 vixl::Label loop, success;
2333 __ Bind(&loop);
2334 __ Cmp(out, cls);
2335 __ B(eq, &success);
2336 __ Ldr(out, HeapOperand(out, super_offset));
2337 GetAssembler()->MaybeUnpoisonHeapReference(out);
2338 __ Cbnz(out, &loop);
2339 // If `out` is null, we use it for the result, and jump to `done`.
2340 __ B(&done);
2341 __ Bind(&success);
2342 __ Mov(out, 1);
2343 if (zero.IsLinked()) {
2344 __ B(&done);
2345 }
2346 break;
2347 }
2348 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002349 // Do an exact check.
2350 vixl::Label exact_check;
2351 __ Cmp(out, cls);
2352 __ B(eq, &exact_check);
2353 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002354 __ Ldr(out, HeapOperand(out, component_offset));
2355 GetAssembler()->MaybeUnpoisonHeapReference(out);
2356 // If `out` is null, we use it for the result, and jump to `done`.
2357 __ Cbz(out, &done);
2358 __ Ldrh(out, HeapOperand(out, primitive_offset));
2359 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2360 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002361 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002362 __ Mov(out, 1);
2363 __ B(&done);
2364 break;
2365 }
2366 case TypeCheckKind::kArrayCheck: {
2367 __ Cmp(out, cls);
2368 DCHECK(locations->OnlyCallsOnSlowPath());
2369 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2370 instruction, /* is_fatal */ false);
2371 codegen_->AddSlowPath(slow_path);
2372 __ B(ne, slow_path->GetEntryLabel());
2373 __ Mov(out, 1);
2374 if (zero.IsLinked()) {
2375 __ B(&done);
2376 }
2377 break;
2378 }
2379
2380 case TypeCheckKind::kInterfaceCheck:
2381 default: {
2382 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2383 instruction,
2384 instruction->GetDexPc(),
2385 nullptr);
2386 if (zero.IsLinked()) {
2387 __ B(&done);
2388 }
2389 break;
2390 }
2391 }
2392
2393 if (zero.IsLinked()) {
2394 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002395 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002396 }
2397
2398 if (done.IsLinked()) {
2399 __ Bind(&done);
2400 }
2401
2402 if (slow_path != nullptr) {
2403 __ Bind(slow_path->GetExitLabel());
2404 }
2405}
2406
2407void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2408 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2409 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2410
2411 switch (instruction->GetTypeCheckKind()) {
2412 case TypeCheckKind::kExactCheck:
2413 case TypeCheckKind::kAbstractClassCheck:
2414 case TypeCheckKind::kClassHierarchyCheck:
2415 case TypeCheckKind::kArrayObjectCheck:
2416 call_kind = throws_into_catch
2417 ? LocationSummary::kCallOnSlowPath
2418 : LocationSummary::kNoCall;
2419 break;
2420 case TypeCheckKind::kInterfaceCheck:
2421 call_kind = LocationSummary::kCall;
2422 break;
2423 case TypeCheckKind::kArrayCheck:
2424 call_kind = LocationSummary::kCallOnSlowPath;
2425 break;
2426 }
2427
2428 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2429 instruction, call_kind);
2430 if (call_kind != LocationSummary::kCall) {
2431 locations->SetInAt(0, Location::RequiresRegister());
2432 locations->SetInAt(1, Location::RequiresRegister());
2433 // Note that TypeCheckSlowPathARM64 uses this register too.
2434 locations->AddTemp(Location::RequiresRegister());
2435 } else {
2436 InvokeRuntimeCallingConvention calling_convention;
2437 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2438 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2439 }
2440}
2441
2442void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2443 LocationSummary* locations = instruction->GetLocations();
2444 Register obj = InputRegisterAt(instruction, 0);
2445 Register cls = InputRegisterAt(instruction, 1);
2446 Register temp;
2447 if (!locations->WillCall()) {
2448 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2449 }
2450
2451 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2452 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2453 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2454 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2455 SlowPathCodeARM64* slow_path = nullptr;
2456
2457 if (!locations->WillCall()) {
2458 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2459 instruction, !locations->CanCall());
2460 codegen_->AddSlowPath(slow_path);
2461 }
2462
2463 vixl::Label done;
2464 // Avoid null check if we know obj is not null.
2465 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002466 __ Cbz(obj, &done);
2467 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002469 if (locations->WillCall()) {
2470 __ Ldr(obj, HeapOperand(obj, class_offset));
2471 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002472 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002473 __ Ldr(temp, HeapOperand(obj, class_offset));
2474 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002475 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002476
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002477 switch (instruction->GetTypeCheckKind()) {
2478 case TypeCheckKind::kExactCheck:
2479 case TypeCheckKind::kArrayCheck: {
2480 __ Cmp(temp, cls);
2481 // Jump to slow path for throwing the exception or doing a
2482 // more involved array check.
2483 __ B(ne, slow_path->GetEntryLabel());
2484 break;
2485 }
2486 case TypeCheckKind::kAbstractClassCheck: {
2487 // If the class is abstract, we eagerly fetch the super class of the
2488 // object to avoid doing a comparison we know will fail.
2489 vixl::Label loop;
2490 __ Bind(&loop);
2491 __ Ldr(temp, HeapOperand(temp, super_offset));
2492 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2493 // Jump to the slow path to throw the exception.
2494 __ Cbz(temp, slow_path->GetEntryLabel());
2495 __ Cmp(temp, cls);
2496 __ B(ne, &loop);
2497 break;
2498 }
2499 case TypeCheckKind::kClassHierarchyCheck: {
2500 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002501 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002502 __ Bind(&loop);
2503 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002504 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002505 __ Ldr(temp, HeapOperand(temp, super_offset));
2506 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2507 __ Cbnz(temp, &loop);
2508 // Jump to the slow path to throw the exception.
2509 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002510 break;
2511 }
2512 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002513 // Do an exact check.
2514 __ Cmp(temp, cls);
2515 __ B(eq, &done);
2516 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002517 __ Ldr(temp, HeapOperand(temp, component_offset));
2518 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2519 __ Cbz(temp, slow_path->GetEntryLabel());
2520 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2521 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2522 __ Cbnz(temp, slow_path->GetEntryLabel());
2523 break;
2524 }
2525 case TypeCheckKind::kInterfaceCheck:
2526 default:
2527 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2528 instruction,
2529 instruction->GetDexPc(),
2530 nullptr);
2531 break;
2532 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002533 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002534
2535 if (slow_path != nullptr) {
2536 __ Bind(slow_path->GetExitLabel());
2537 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002538}
2539
Alexandre Rames5319def2014-10-23 10:03:10 +01002540void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2542 locations->SetOut(Location::ConstantLocation(constant));
2543}
2544
2545void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2546 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002547 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002548}
2549
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002550void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2552 locations->SetOut(Location::ConstantLocation(constant));
2553}
2554
2555void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2556 // Will be generated at use site.
2557 UNUSED(constant);
2558}
2559
Calin Juravle175dc732015-08-25 15:42:32 +01002560void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2561 // The trampoline uses the same calling convention as dex calling conventions,
2562 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2563 // the method_idx.
2564 HandleInvoke(invoke);
2565}
2566
2567void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2568 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2569}
2570
Alexandre Rames5319def2014-10-23 10:03:10 +01002571void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002572 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002573 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002574}
2575
Alexandre Rames67555f72014-11-18 10:55:16 +00002576void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2577 HandleInvoke(invoke);
2578}
2579
2580void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2581 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002582 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2583 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2584 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002585 Location receiver = invoke->GetLocations()->InAt(0);
2586 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002587 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002588
2589 // The register ip1 is required to be used for the hidden argument in
2590 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002591 MacroAssembler* masm = GetVIXLAssembler();
2592 UseScratchRegisterScope scratch_scope(masm);
2593 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002594 scratch_scope.Exclude(ip1);
2595 __ Mov(ip1, invoke->GetDexMethodIndex());
2596
2597 // temp = object->GetClass();
2598 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002599 __ Ldr(temp.W(), StackOperandFrom(receiver));
2600 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002601 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002602 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002603 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002604 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002605 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002606 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002607 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002608 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002609 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002610 // lr();
2611 __ Blr(lr);
2612 DCHECK(!codegen_->IsLeafMethod());
2613 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2614}
2615
2616void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002617 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2618 if (intrinsic.TryDispatch(invoke)) {
2619 return;
2620 }
2621
Alexandre Rames67555f72014-11-18 10:55:16 +00002622 HandleInvoke(invoke);
2623}
2624
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002625void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002626 // When we do not run baseline, explicit clinit checks triggered by static
2627 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2628 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002629
Andreas Gampe878d58c2015-01-15 23:24:00 -08002630 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2631 if (intrinsic.TryDispatch(invoke)) {
2632 return;
2633 }
2634
Alexandre Rames67555f72014-11-18 10:55:16 +00002635 HandleInvoke(invoke);
2636}
2637
Andreas Gampe878d58c2015-01-15 23:24:00 -08002638static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2639 if (invoke->GetLocations()->Intrinsified()) {
2640 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2641 intrinsic.Dispatch(invoke);
2642 return true;
2643 }
2644 return false;
2645}
2646
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002647void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002648 // For better instruction scheduling we load the direct code pointer before the method pointer.
2649 bool direct_code_loaded = false;
2650 switch (invoke->GetCodePtrLocation()) {
2651 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2652 // LR = code address from literal pool with link-time patch.
2653 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2654 direct_code_loaded = true;
2655 break;
2656 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2657 // LR = invoke->GetDirectCodePtr();
2658 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2659 direct_code_loaded = true;
2660 break;
2661 default:
2662 break;
2663 }
2664
Andreas Gampe878d58c2015-01-15 23:24:00 -08002665 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002666 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2667 switch (invoke->GetMethodLoadKind()) {
2668 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2669 // temp = thread->string_init_entrypoint
2670 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2671 break;
2672 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2673 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2674 break;
2675 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2676 // Load method address from literal pool.
2677 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2678 break;
2679 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2680 // Load method address from literal pool with a link-time patch.
2681 __ Ldr(XRegisterFrom(temp).X(),
2682 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2683 break;
2684 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2685 // Add ADRP with its PC-relative DexCache access patch.
2686 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2687 invoke->GetDexCacheArrayOffset());
2688 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2689 {
2690 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2691 __ adrp(XRegisterFrom(temp).X(), 0);
2692 }
2693 __ Bind(pc_insn_label); // Bind after ADRP.
2694 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2695 // Add LDR with its PC-relative DexCache access patch.
2696 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2697 invoke->GetDexCacheArrayOffset());
2698 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2699 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2700 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2701 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002702 }
Vladimir Marko58155012015-08-19 12:49:41 +00002703 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2704 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2705 Register reg = XRegisterFrom(temp);
2706 Register method_reg;
2707 if (current_method.IsRegister()) {
2708 method_reg = XRegisterFrom(current_method);
2709 } else {
2710 DCHECK(invoke->GetLocations()->Intrinsified());
2711 DCHECK(!current_method.IsValid());
2712 method_reg = reg;
2713 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2714 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002715
Vladimir Marko58155012015-08-19 12:49:41 +00002716 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002717 __ Ldr(reg.X(),
2718 MemOperand(method_reg.X(),
2719 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002720 // temp = temp[index_in_cache];
2721 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2722 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2723 break;
2724 }
2725 }
2726
2727 switch (invoke->GetCodePtrLocation()) {
2728 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2729 __ Bl(&frame_entry_label_);
2730 break;
2731 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2732 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2733 vixl::Label* label = &relative_call_patches_.back().label;
2734 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2735 __ Bind(label); // Bind after BL.
2736 break;
2737 }
2738 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2739 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2740 // LR prepared above for better instruction scheduling.
2741 DCHECK(direct_code_loaded);
2742 // lr()
2743 __ Blr(lr);
2744 break;
2745 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2746 // LR = callee_method->entry_point_from_quick_compiled_code_;
2747 __ Ldr(lr, MemOperand(
2748 XRegisterFrom(callee_method).X(),
2749 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2750 // lr()
2751 __ Blr(lr);
2752 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002753 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002754
Andreas Gampe878d58c2015-01-15 23:24:00 -08002755 DCHECK(!IsLeafMethod());
2756}
2757
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002758void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2759 LocationSummary* locations = invoke->GetLocations();
2760 Location receiver = locations->InAt(0);
2761 Register temp = XRegisterFrom(temp_in);
2762 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2763 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2764 Offset class_offset = mirror::Object::ClassOffset();
2765 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2766
2767 BlockPoolsScope block_pools(GetVIXLAssembler());
2768
2769 DCHECK(receiver.IsRegister());
2770 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2771 MaybeRecordImplicitNullCheck(invoke);
2772 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2773 // temp = temp->GetMethodAt(method_offset);
2774 __ Ldr(temp, MemOperand(temp, method_offset));
2775 // lr = temp->GetEntryPoint();
2776 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2777 // lr();
2778 __ Blr(lr);
2779}
2780
Vladimir Marko58155012015-08-19 12:49:41 +00002781void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2782 DCHECK(linker_patches->empty());
2783 size_t size =
2784 method_patches_.size() +
2785 call_patches_.size() +
2786 relative_call_patches_.size() +
2787 pc_rel_dex_cache_patches_.size();
2788 linker_patches->reserve(size);
2789 for (const auto& entry : method_patches_) {
2790 const MethodReference& target_method = entry.first;
2791 vixl::Literal<uint64_t>* literal = entry.second;
2792 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2793 target_method.dex_file,
2794 target_method.dex_method_index));
2795 }
2796 for (const auto& entry : call_patches_) {
2797 const MethodReference& target_method = entry.first;
2798 vixl::Literal<uint64_t>* literal = entry.second;
2799 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2800 target_method.dex_file,
2801 target_method.dex_method_index));
2802 }
2803 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2804 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2805 info.target_method.dex_file,
2806 info.target_method.dex_method_index));
2807 }
2808 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2809 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2810 &info.target_dex_file,
2811 info.pc_insn_label->location() - 4u,
2812 info.element_offset));
2813 }
2814}
2815
2816vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2817 // Look up the literal for value.
2818 auto lb = uint64_literals_.lower_bound(value);
2819 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2820 return lb->second;
2821 }
2822 // We don't have a literal for this value, insert a new one.
2823 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2824 uint64_literals_.PutBefore(lb, value, literal);
2825 return literal;
2826}
2827
2828vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2829 MethodReference target_method,
2830 MethodToLiteralMap* map) {
2831 // Look up the literal for target_method.
2832 auto lb = map->lower_bound(target_method);
2833 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2834 return lb->second;
2835 }
2836 // We don't have a literal for this method yet, insert a new one.
2837 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2838 map->PutBefore(lb, target_method, literal);
2839 return literal;
2840}
2841
2842vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2843 MethodReference target_method) {
2844 return DeduplicateMethodLiteral(target_method, &method_patches_);
2845}
2846
2847vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2848 MethodReference target_method) {
2849 return DeduplicateMethodLiteral(target_method, &call_patches_);
2850}
2851
2852
Andreas Gampe878d58c2015-01-15 23:24:00 -08002853void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002854 // When we do not run baseline, explicit clinit checks triggered by static
2855 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2856 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002857
Andreas Gampe878d58c2015-01-15 23:24:00 -08002858 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2859 return;
2860 }
2861
Alexandre Ramesd921d642015-04-16 15:07:16 +01002862 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002863 LocationSummary* locations = invoke->GetLocations();
2864 codegen_->GenerateStaticOrDirectCall(
2865 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002866 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002867}
2868
2869void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002870 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2871 return;
2872 }
2873
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002874 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002875 DCHECK(!codegen_->IsLeafMethod());
2876 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2877}
2878
Alexandre Rames67555f72014-11-18 10:55:16 +00002879void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2880 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2881 : LocationSummary::kNoCall;
2882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002883 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002884 locations->SetOut(Location::RequiresRegister());
2885}
2886
2887void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2888 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002889 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002890 if (cls->IsReferrersClass()) {
2891 DCHECK(!cls->CanCallRuntime());
2892 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002893 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002894 } else {
2895 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002896 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
2897 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
2898 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2899 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002900
2901 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2902 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2903 codegen_->AddSlowPath(slow_path);
2904 __ Cbz(out, slow_path->GetEntryLabel());
2905 if (cls->MustGenerateClinitCheck()) {
2906 GenerateClassInitializationCheck(slow_path, out);
2907 } else {
2908 __ Bind(slow_path->GetExitLabel());
2909 }
2910 }
2911}
2912
David Brazdilcb1c0552015-08-04 16:22:25 +01002913static MemOperand GetExceptionTlsAddress() {
2914 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2915}
2916
Alexandre Rames67555f72014-11-18 10:55:16 +00002917void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2918 LocationSummary* locations =
2919 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2920 locations->SetOut(Location::RequiresRegister());
2921}
2922
2923void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002924 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2925}
2926
2927void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2928 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2929}
2930
2931void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2932 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002933}
2934
Alexandre Rames5319def2014-10-23 10:03:10 +01002935void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2936 load->SetLocations(nullptr);
2937}
2938
2939void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2940 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002941 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002942}
2943
Alexandre Rames67555f72014-11-18 10:55:16 +00002944void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2945 LocationSummary* locations =
2946 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002947 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002948 locations->SetOut(Location::RequiresRegister());
2949}
2950
2951void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2952 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2953 codegen_->AddSlowPath(slow_path);
2954
2955 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002956 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002957 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002958 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
2959 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2960 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002961 __ Cbz(out, slow_path->GetEntryLabel());
2962 __ Bind(slow_path->GetExitLabel());
2963}
2964
Alexandre Rames5319def2014-10-23 10:03:10 +01002965void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2966 local->SetLocations(nullptr);
2967}
2968
2969void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2970 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2971}
2972
2973void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2975 locations->SetOut(Location::ConstantLocation(constant));
2976}
2977
2978void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2979 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002980 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002981}
2982
Alexandre Rames67555f72014-11-18 10:55:16 +00002983void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2984 LocationSummary* locations =
2985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2986 InvokeRuntimeCallingConvention calling_convention;
2987 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2988}
2989
2990void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2991 codegen_->InvokeRuntime(instruction->IsEnter()
2992 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2993 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002994 instruction->GetDexPc(),
2995 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002996 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002997}
2998
Alexandre Rames42d641b2014-10-27 14:00:51 +00002999void LocationsBuilderARM64::VisitMul(HMul* mul) {
3000 LocationSummary* locations =
3001 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3002 switch (mul->GetResultType()) {
3003 case Primitive::kPrimInt:
3004 case Primitive::kPrimLong:
3005 locations->SetInAt(0, Location::RequiresRegister());
3006 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003008 break;
3009
3010 case Primitive::kPrimFloat:
3011 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003012 locations->SetInAt(0, Location::RequiresFpuRegister());
3013 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003014 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003015 break;
3016
3017 default:
3018 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3019 }
3020}
3021
3022void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3023 switch (mul->GetResultType()) {
3024 case Primitive::kPrimInt:
3025 case Primitive::kPrimLong:
3026 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3027 break;
3028
3029 case Primitive::kPrimFloat:
3030 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003031 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003032 break;
3033
3034 default:
3035 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3036 }
3037}
3038
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003039void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3040 LocationSummary* locations =
3041 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3042 switch (neg->GetResultType()) {
3043 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003044 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003045 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003047 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003048
3049 case Primitive::kPrimFloat:
3050 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003051 locations->SetInAt(0, Location::RequiresFpuRegister());
3052 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003053 break;
3054
3055 default:
3056 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3057 }
3058}
3059
3060void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3061 switch (neg->GetResultType()) {
3062 case Primitive::kPrimInt:
3063 case Primitive::kPrimLong:
3064 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3065 break;
3066
3067 case Primitive::kPrimFloat:
3068 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003069 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003070 break;
3071
3072 default:
3073 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3074 }
3075}
3076
3077void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3080 InvokeRuntimeCallingConvention calling_convention;
3081 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003082 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003083 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003084 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003085 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003086 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003087}
3088
3089void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3090 LocationSummary* locations = instruction->GetLocations();
3091 InvokeRuntimeCallingConvention calling_convention;
3092 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3093 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003094 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003095 // Note: if heap poisoning is enabled, the entry point takes cares
3096 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003097 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3098 instruction,
3099 instruction->GetDexPc(),
3100 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003101 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003102}
3103
Alexandre Rames5319def2014-10-23 10:03:10 +01003104void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3105 LocationSummary* locations =
3106 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3107 InvokeRuntimeCallingConvention calling_convention;
3108 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003109 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003110 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003111 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003112}
3113
3114void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3115 LocationSummary* locations = instruction->GetLocations();
3116 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3117 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003118 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003119 // Note: if heap poisoning is enabled, the entry point takes cares
3120 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003121 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3122 instruction,
3123 instruction->GetDexPc(),
3124 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003125 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003126}
3127
3128void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003130 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003132}
3133
3134void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003135 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003136 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003137 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003138 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003139 break;
3140
3141 default:
3142 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3143 }
3144}
3145
David Brazdil66d126e2015-04-03 16:02:44 +01003146void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3148 locations->SetInAt(0, Location::RequiresRegister());
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3150}
3151
3152void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003153 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3154}
3155
Alexandre Rames5319def2014-10-23 10:03:10 +01003156void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003157 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3158 ? LocationSummary::kCallOnSlowPath
3159 : LocationSummary::kNoCall;
3160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003161 locations->SetInAt(0, Location::RequiresRegister());
3162 if (instruction->HasUses()) {
3163 locations->SetOut(Location::SameAsFirstInput());
3164 }
3165}
3166
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003167void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003168 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3169 return;
3170 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003171
Alexandre Ramesd921d642015-04-16 15:07:16 +01003172 BlockPoolsScope block_pools(GetVIXLAssembler());
3173 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003174 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3175 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3176}
3177
3178void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003179 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3180 codegen_->AddSlowPath(slow_path);
3181
3182 LocationSummary* locations = instruction->GetLocations();
3183 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003184
3185 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003186}
3187
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003188void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003189 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003190 GenerateImplicitNullCheck(instruction);
3191 } else {
3192 GenerateExplicitNullCheck(instruction);
3193 }
3194}
3195
Alexandre Rames67555f72014-11-18 10:55:16 +00003196void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3197 HandleBinaryOp(instruction);
3198}
3199
3200void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3201 HandleBinaryOp(instruction);
3202}
3203
Alexandre Rames3e69f162014-12-10 10:36:50 +00003204void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3205 LOG(FATAL) << "Unreachable";
3206}
3207
3208void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3209 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3210}
3211
Alexandre Rames5319def2014-10-23 10:03:10 +01003212void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3214 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3215 if (location.IsStackSlot()) {
3216 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3217 } else if (location.IsDoubleStackSlot()) {
3218 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3219 }
3220 locations->SetOut(location);
3221}
3222
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003223void InstructionCodeGeneratorARM64::VisitParameterValue(
3224 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003225 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003226}
3227
3228void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3229 LocationSummary* locations =
3230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003231 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003232}
3233
3234void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3235 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3236 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003237}
3238
3239void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3241 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3242 locations->SetInAt(i, Location::Any());
3243 }
3244 locations->SetOut(Location::Any());
3245}
3246
3247void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003248 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003249 LOG(FATAL) << "Unreachable";
3250}
3251
Serban Constantinescu02164b32014-11-13 14:05:07 +00003252void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003253 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003254 LocationSummary::CallKind call_kind =
3255 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3257
3258 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003259 case Primitive::kPrimInt:
3260 case Primitive::kPrimLong:
3261 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003262 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3264 break;
3265
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003266 case Primitive::kPrimFloat:
3267 case Primitive::kPrimDouble: {
3268 InvokeRuntimeCallingConvention calling_convention;
3269 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3270 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3271 locations->SetOut(calling_convention.GetReturnLocation(type));
3272
3273 break;
3274 }
3275
Serban Constantinescu02164b32014-11-13 14:05:07 +00003276 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003277 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003278 }
3279}
3280
3281void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3282 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003283
Serban Constantinescu02164b32014-11-13 14:05:07 +00003284 switch (type) {
3285 case Primitive::kPrimInt:
3286 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003287 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003288 break;
3289 }
3290
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003291 case Primitive::kPrimFloat:
3292 case Primitive::kPrimDouble: {
3293 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3294 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003295 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003296 break;
3297 }
3298
Serban Constantinescu02164b32014-11-13 14:05:07 +00003299 default:
3300 LOG(FATAL) << "Unexpected rem type " << type;
3301 }
3302}
3303
Calin Juravle27df7582015-04-17 19:12:31 +01003304void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3305 memory_barrier->SetLocations(nullptr);
3306}
3307
3308void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3309 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3310}
3311
Alexandre Rames5319def2014-10-23 10:03:10 +01003312void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3314 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003315 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003316}
3317
3318void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003319 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003320 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003321}
3322
3323void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3324 instruction->SetLocations(nullptr);
3325}
3326
3327void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003328 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003329 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003330}
3331
Serban Constantinescu02164b32014-11-13 14:05:07 +00003332void LocationsBuilderARM64::VisitShl(HShl* shl) {
3333 HandleShift(shl);
3334}
3335
3336void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3337 HandleShift(shl);
3338}
3339
3340void LocationsBuilderARM64::VisitShr(HShr* shr) {
3341 HandleShift(shr);
3342}
3343
3344void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3345 HandleShift(shr);
3346}
3347
Alexandre Rames5319def2014-10-23 10:03:10 +01003348void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3350 Primitive::Type field_type = store->InputAt(1)->GetType();
3351 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003352 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003353 case Primitive::kPrimBoolean:
3354 case Primitive::kPrimByte:
3355 case Primitive::kPrimChar:
3356 case Primitive::kPrimShort:
3357 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003358 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003359 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3360 break;
3361
3362 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003363 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003364 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3365 break;
3366
3367 default:
3368 LOG(FATAL) << "Unimplemented local type " << field_type;
3369 }
3370}
3371
3372void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003373 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003374}
3375
3376void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003377 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003378}
3379
3380void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003381 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003382}
3383
Alexandre Rames67555f72014-11-18 10:55:16 +00003384void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003385 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003386}
3387
3388void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003389 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003390}
3391
3392void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003393 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003394}
3395
Alexandre Rames67555f72014-11-18 10:55:16 +00003396void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003397 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003398}
3399
3400void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3402}
3403
3404void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003405 HBasicBlock* block = instruction->GetBlock();
3406 if (block->GetLoopInformation() != nullptr) {
3407 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3408 // The back edge will generate the suspend check.
3409 return;
3410 }
3411 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3412 // The goto will generate the suspend check.
3413 return;
3414 }
3415 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003416}
3417
3418void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3419 temp->SetLocations(nullptr);
3420}
3421
3422void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3423 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003424 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003425}
3426
Alexandre Rames67555f72014-11-18 10:55:16 +00003427void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3428 LocationSummary* locations =
3429 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3430 InvokeRuntimeCallingConvention calling_convention;
3431 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3432}
3433
3434void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3435 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003436 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003437 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003438}
3439
3440void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3441 LocationSummary* locations =
3442 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3443 Primitive::Type input_type = conversion->GetInputType();
3444 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003445 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003446 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3447 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3448 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3449 }
3450
Alexandre Rames542361f2015-01-29 16:57:31 +00003451 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003452 locations->SetInAt(0, Location::RequiresFpuRegister());
3453 } else {
3454 locations->SetInAt(0, Location::RequiresRegister());
3455 }
3456
Alexandre Rames542361f2015-01-29 16:57:31 +00003457 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003458 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3459 } else {
3460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3461 }
3462}
3463
3464void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3465 Primitive::Type result_type = conversion->GetResultType();
3466 Primitive::Type input_type = conversion->GetInputType();
3467
3468 DCHECK_NE(input_type, result_type);
3469
Alexandre Rames542361f2015-01-29 16:57:31 +00003470 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003471 int result_size = Primitive::ComponentSize(result_type);
3472 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003473 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003474 Register output = OutputRegister(conversion);
3475 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003476 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3477 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003478 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3479 // 'int' values are used directly as W registers, discarding the top
3480 // bits, so we don't need to sign-extend and can just perform a move.
3481 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3482 // top 32 bits of the target register. We theoretically could leave those
3483 // bits unchanged, but we would have to make sure that no code uses a
3484 // 32bit input value as a 64bit value assuming that the top 32 bits are
3485 // zero.
3486 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003487 } else if ((result_type == Primitive::kPrimChar) ||
3488 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3489 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003490 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003491 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003492 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003493 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003494 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003495 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003496 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3497 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003498 } else if (Primitive::IsFloatingPointType(result_type) &&
3499 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003500 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3501 } else {
3502 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3503 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003504 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003505}
Alexandre Rames67555f72014-11-18 10:55:16 +00003506
Serban Constantinescu02164b32014-11-13 14:05:07 +00003507void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3508 HandleShift(ushr);
3509}
3510
3511void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3512 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003513}
3514
3515void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3516 HandleBinaryOp(instruction);
3517}
3518
3519void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3520 HandleBinaryOp(instruction);
3521}
3522
Calin Juravleb1498f62015-02-16 13:13:29 +00003523void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3524 // Nothing to do, this should be removed during prepare for register allocator.
3525 UNUSED(instruction);
3526 LOG(FATAL) << "Unreachable";
3527}
3528
3529void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3530 // Nothing to do, this should be removed during prepare for register allocator.
3531 UNUSED(instruction);
3532 LOG(FATAL) << "Unreachable";
3533}
3534
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003535void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3536 DCHECK(codegen_->IsBaseline());
3537 LocationSummary* locations =
3538 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3539 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3540}
3541
3542void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3543 DCHECK(codegen_->IsBaseline());
3544 // Will be generated at use site.
3545}
3546
Mark Mendellfe57faa2015-09-18 09:26:15 -04003547// Simple implementation of packed switch - generate cascaded compare/jumps.
3548void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3549 LocationSummary* locations =
3550 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3551 locations->SetInAt(0, Location::RequiresRegister());
3552}
3553
3554void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3555 int32_t lower_bound = switch_instr->GetStartValue();
3556 int32_t num_entries = switch_instr->GetNumEntries();
3557 Register value_reg = InputRegisterAt(switch_instr, 0);
3558 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3559
3560 // Create a series of compare/jumps.
3561 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3562 for (int32_t i = 0; i < num_entries; i++) {
3563 int32_t case_value = lower_bound + i;
3564 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3565 if (case_value == 0) {
3566 __ Cbz(value_reg, succ);
3567 } else {
3568 __ Cmp(value_reg, vixl::Operand(case_value));
3569 __ B(eq, succ);
3570 }
3571 }
3572
3573 // And the default for any other value.
3574 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3575 __ B(codegen_->GetLabelOf(default_block));
3576 }
3577}
3578
Alexandre Rames67555f72014-11-18 10:55:16 +00003579#undef __
3580#undef QUICK_ENTRY_POINT
3581
Alexandre Rames5319def2014-10-23 10:03:10 +01003582} // namespace arm64
3583} // namespace art