blob: 87f502bbcdf518e204ea7b064c9c59980088db56 [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)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100480 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700481
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
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100499class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
500 public:
501 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
502
503 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
504 LocationSummary* locations = instruction_->GetLocations();
505 __ Bind(GetEntryLabel());
506 SaveLiveRegisters(codegen, locations);
507
508 InvokeRuntimeCallingConvention calling_convention;
509 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
510 parallel_move.AddMove(
511 locations->InAt(0),
512 LocationFrom(calling_convention.GetRegisterAt(0)),
513 Primitive::kPrimNot,
514 nullptr);
515 parallel_move.AddMove(
516 locations->InAt(1),
517 LocationFrom(calling_convention.GetRegisterAt(1)),
518 Primitive::kPrimInt,
519 nullptr);
520 parallel_move.AddMove(
521 locations->InAt(2),
522 LocationFrom(calling_convention.GetRegisterAt(2)),
523 Primitive::kPrimNot,
524 nullptr);
525 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
526
527 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
528 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
529 instruction_,
530 instruction_->GetDexPc(),
531 this);
532 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
533 RestoreLiveRegisters(codegen, locations);
534 __ B(GetExitLabel());
535 }
536
537 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
538
539 private:
540 HInstruction* const instruction_;
541
542 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
543};
544
Alexandre Rames5319def2014-10-23 10:03:10 +0100545#undef __
546
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100547Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100548 Location next_location;
549 if (type == Primitive::kPrimVoid) {
550 LOG(FATAL) << "Unreachable type " << type;
551 }
552
Alexandre Rames542361f2015-01-29 16:57:31 +0000553 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100554 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
555 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000556 } else if (!Primitive::IsFloatingPointType(type) &&
557 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000558 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
559 } else {
560 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000561 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
562 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 }
564
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000565 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000566 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100567 return next_location;
568}
569
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100570Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100571 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100572}
573
Serban Constantinescu579885a2015-02-22 20:51:33 +0000574CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
575 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100576 const CompilerOptions& compiler_options,
577 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100578 : CodeGenerator(graph,
579 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000580 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000581 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000582 callee_saved_core_registers.list(),
583 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100584 compiler_options,
585 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100586 block_labels_(nullptr),
587 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000588 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000589 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000590 isa_features_(isa_features),
591 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
592 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
593 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
594 relative_call_patches_(graph->GetArena()->Adapter()),
595 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000596 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000597 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000598}
Alexandre Rames5319def2014-10-23 10:03:10 +0100599
Alexandre Rames67555f72014-11-18 10:55:16 +0000600#undef __
601#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100602
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000603void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
604 // Ensure we emit the literal pool.
605 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000606
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000607 CodeGenerator::Finalize(allocator);
608}
609
Zheng Xuad4450e2015-04-17 18:48:56 +0800610void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
611 // Note: There are 6 kinds of moves:
612 // 1. constant -> GPR/FPR (non-cycle)
613 // 2. constant -> stack (non-cycle)
614 // 3. GPR/FPR -> GPR/FPR
615 // 4. GPR/FPR -> stack
616 // 5. stack -> GPR/FPR
617 // 6. stack -> stack (non-cycle)
618 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
619 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
620 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
621 // dependency.
622 vixl_temps_.Open(GetVIXLAssembler());
623}
624
625void ParallelMoveResolverARM64::FinishEmitNativeCode() {
626 vixl_temps_.Close();
627}
628
629Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
630 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
631 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
632 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
633 Location scratch = GetScratchLocation(kind);
634 if (!scratch.Equals(Location::NoLocation())) {
635 return scratch;
636 }
637 // Allocate from VIXL temp registers.
638 if (kind == Location::kRegister) {
639 scratch = LocationFrom(vixl_temps_.AcquireX());
640 } else {
641 DCHECK(kind == Location::kFpuRegister);
642 scratch = LocationFrom(vixl_temps_.AcquireD());
643 }
644 AddScratchLocation(scratch);
645 return scratch;
646}
647
648void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
649 if (loc.IsRegister()) {
650 vixl_temps_.Release(XRegisterFrom(loc));
651 } else {
652 DCHECK(loc.IsFpuRegister());
653 vixl_temps_.Release(DRegisterFrom(loc));
654 }
655 RemoveScratchLocation(loc);
656}
657
Alexandre Rames3e69f162014-12-10 10:36:50 +0000658void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100659 DCHECK_LT(index, moves_.size());
660 MoveOperands* move = moves_[index];
Alexandre Rames3e69f162014-12-10 10:36:50 +0000661 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
662}
663
Alexandre Rames5319def2014-10-23 10:03:10 +0100664void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100665 MacroAssembler* masm = GetVIXLAssembler();
666 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000667 __ Bind(&frame_entry_label_);
668
Serban Constantinescu02164b32014-11-13 14:05:07 +0000669 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
670 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100671 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000672 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000673 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000674 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000675 __ Ldr(wzr, MemOperand(temp, 0));
676 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000677 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100678
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000679 if (!HasEmptyFrame()) {
680 int frame_size = GetFrameSize();
681 // Stack layout:
682 // sp[frame_size - 8] : lr.
683 // ... : other preserved core registers.
684 // ... : other preserved fp registers.
685 // ... : reserved frame space.
686 // sp[0] : current method.
687 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100688 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800689 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
690 frame_size - GetCoreSpillSize());
691 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
692 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000693 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100694}
695
696void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100697 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100698 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000699 if (!HasEmptyFrame()) {
700 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800701 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
702 frame_size - FrameEntrySpillSize());
703 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
704 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000705 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100706 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000707 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100708 __ Ret();
709 GetAssembler()->cfi().RestoreState();
710 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100711}
712
Zheng Xuda403092015-04-24 17:35:39 +0800713vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
714 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
715 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
716 core_spill_mask_);
717}
718
719vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
720 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
721 GetNumberOfFloatingPointRegisters()));
722 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
723 fpu_spill_mask_);
724}
725
Alexandre Rames5319def2014-10-23 10:03:10 +0100726void CodeGeneratorARM64::Bind(HBasicBlock* block) {
727 __ Bind(GetLabelOf(block));
728}
729
Alexandre Rames5319def2014-10-23 10:03:10 +0100730void CodeGeneratorARM64::Move(HInstruction* instruction,
731 Location location,
732 HInstruction* move_for) {
733 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100734 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000735 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100736
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100737 if (instruction->IsFakeString()) {
738 // The fake string is an alias for null.
739 DCHECK(IsBaseline());
740 instruction = locations->Out().GetConstant();
741 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
742 }
743
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100744 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700745 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100746 } else if (locations != nullptr && locations->Out().Equals(location)) {
747 return;
748 } else if (instruction->IsIntConstant()
749 || instruction->IsLongConstant()
750 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000751 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100752 if (location.IsRegister()) {
753 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000754 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100755 (instruction->IsLongConstant() && dst.Is64Bits()));
756 __ Mov(dst, value);
757 } else {
758 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000759 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000760 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
761 ? temps.AcquireW()
762 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100763 __ Mov(temp, value);
764 __ Str(temp, StackOperandFrom(location));
765 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000766 } else if (instruction->IsTemporary()) {
767 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000768 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100769 } else if (instruction->IsLoadLocal()) {
770 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000771 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000772 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000773 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000774 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100775 }
776
777 } else {
778 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000779 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100780 }
781}
782
Calin Juravle175dc732015-08-25 15:42:32 +0100783void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
784 DCHECK(location.IsRegister());
785 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
786}
787
Alexandre Rames5319def2014-10-23 10:03:10 +0100788Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
789 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000790
Alexandre Rames5319def2014-10-23 10:03:10 +0100791 switch (type) {
792 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000793 case Primitive::kPrimInt:
794 case Primitive::kPrimFloat:
795 return Location::StackSlot(GetStackSlot(load->GetLocal()));
796
797 case Primitive::kPrimLong:
798 case Primitive::kPrimDouble:
799 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
800
Alexandre Rames5319def2014-10-23 10:03:10 +0100801 case Primitive::kPrimBoolean:
802 case Primitive::kPrimByte:
803 case Primitive::kPrimChar:
804 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100805 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100806 LOG(FATAL) << "Unexpected type " << type;
807 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000808
Alexandre Rames5319def2014-10-23 10:03:10 +0100809 LOG(FATAL) << "Unreachable";
810 return Location::NoLocation();
811}
812
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100813void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000814 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100815 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000816 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100817 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100818 if (value_can_be_null) {
819 __ Cbz(value, &done);
820 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100821 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
822 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000823 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100824 if (value_can_be_null) {
825 __ Bind(&done);
826 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100827}
828
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000829void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
830 // Blocked core registers:
831 // lr : Runtime reserved.
832 // tr : Runtime reserved.
833 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
834 // ip1 : VIXL core temp.
835 // ip0 : VIXL core temp.
836 //
837 // Blocked fp registers:
838 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100839 CPURegList reserved_core_registers = vixl_reserved_core_registers;
840 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100841 while (!reserved_core_registers.IsEmpty()) {
842 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
843 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000844
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000845 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800846 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000847 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
848 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000849
850 if (is_baseline) {
851 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
852 while (!reserved_core_baseline_registers.IsEmpty()) {
853 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
854 }
855
856 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
857 while (!reserved_fp_baseline_registers.IsEmpty()) {
858 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
859 }
860 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100861}
862
863Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
864 if (type == Primitive::kPrimVoid) {
865 LOG(FATAL) << "Unreachable type " << type;
866 }
867
Alexandre Rames542361f2015-01-29 16:57:31 +0000868 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000869 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
870 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100871 return Location::FpuRegisterLocation(reg);
872 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000873 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
874 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100875 return Location::RegisterLocation(reg);
876 }
877}
878
Alexandre Rames3e69f162014-12-10 10:36:50 +0000879size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
880 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
881 __ Str(reg, MemOperand(sp, stack_index));
882 return kArm64WordSize;
883}
884
885size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
886 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
887 __ Ldr(reg, MemOperand(sp, stack_index));
888 return kArm64WordSize;
889}
890
891size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
892 FPRegister reg = FPRegister(reg_id, kDRegSize);
893 __ Str(reg, MemOperand(sp, stack_index));
894 return kArm64WordSize;
895}
896
897size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
898 FPRegister reg = FPRegister(reg_id, kDRegSize);
899 __ Ldr(reg, MemOperand(sp, stack_index));
900 return kArm64WordSize;
901}
902
Alexandre Rames5319def2014-10-23 10:03:10 +0100903void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100904 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100905}
906
907void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100908 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100909}
910
Alexandre Rames67555f72014-11-18 10:55:16 +0000911void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000912 if (constant->IsIntConstant()) {
913 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
914 } else if (constant->IsLongConstant()) {
915 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
916 } else if (constant->IsNullConstant()) {
917 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000918 } else if (constant->IsFloatConstant()) {
919 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
920 } else {
921 DCHECK(constant->IsDoubleConstant());
922 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
923 }
924}
925
Alexandre Rames3e69f162014-12-10 10:36:50 +0000926
927static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
928 DCHECK(constant.IsConstant());
929 HConstant* cst = constant.GetConstant();
930 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000931 // Null is mapped to a core W register, which we associate with kPrimInt.
932 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000933 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
934 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
935 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
936}
937
938void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000939 if (source.Equals(destination)) {
940 return;
941 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000942
943 // A valid move can always be inferred from the destination and source
944 // locations. When moving from and to a register, the argument type can be
945 // used to generate 32bit instead of 64bit moves. In debug mode we also
946 // checks the coherency of the locations and the type.
947 bool unspecified_type = (type == Primitive::kPrimVoid);
948
949 if (destination.IsRegister() || destination.IsFpuRegister()) {
950 if (unspecified_type) {
951 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
952 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000953 (src_cst != nullptr && (src_cst->IsIntConstant()
954 || src_cst->IsFloatConstant()
955 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000956 // For stack slots and 32bit constants, a 64bit type is appropriate.
957 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000958 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000959 // If the source is a double stack slot or a 64bit constant, a 64bit
960 // type is appropriate. Else the source is a register, and since the
961 // type has not been specified, we chose a 64bit type to force a 64bit
962 // move.
963 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000964 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000965 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000966 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
967 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000968 CPURegister dst = CPURegisterFrom(destination, type);
969 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
970 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
971 __ Ldr(dst, StackOperandFrom(source));
972 } else if (source.IsConstant()) {
973 DCHECK(CoherentConstantAndType(source, type));
974 MoveConstant(dst, source.GetConstant());
975 } else {
976 if (destination.IsRegister()) {
977 __ Mov(Register(dst), RegisterFrom(source, type));
978 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800979 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000980 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
981 }
982 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000983 } else { // The destination is not a register. It must be a stack slot.
984 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
985 if (source.IsRegister() || source.IsFpuRegister()) {
986 if (unspecified_type) {
987 if (source.IsRegister()) {
988 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
989 } else {
990 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
991 }
992 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000993 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
994 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000995 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
996 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100997 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000998 UseScratchRegisterScope temps(GetVIXLAssembler());
999 HConstant* src_cst = source.GetConstant();
1000 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001001 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001002 temp = temps.AcquireW();
1003 } else if (src_cst->IsLongConstant()) {
1004 temp = temps.AcquireX();
1005 } else if (src_cst->IsFloatConstant()) {
1006 temp = temps.AcquireS();
1007 } else {
1008 DCHECK(src_cst->IsDoubleConstant());
1009 temp = temps.AcquireD();
1010 }
1011 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001012 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001013 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001014 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001015 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001016 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001017 // There is generally less pressure on FP registers.
1018 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001019 __ Ldr(temp, StackOperandFrom(source));
1020 __ Str(temp, StackOperandFrom(destination));
1021 }
1022 }
1023}
1024
1025void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001026 CPURegister dst,
1027 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001028 switch (type) {
1029 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001030 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001031 break;
1032 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001033 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001034 break;
1035 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001036 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001037 break;
1038 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001039 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001040 break;
1041 case Primitive::kPrimInt:
1042 case Primitive::kPrimNot:
1043 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001044 case Primitive::kPrimFloat:
1045 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001046 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001047 __ Ldr(dst, src);
1048 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001049 case Primitive::kPrimVoid:
1050 LOG(FATAL) << "Unreachable type " << type;
1051 }
1052}
1053
Calin Juravle77520bc2015-01-12 18:45:46 +00001054void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001055 CPURegister dst,
1056 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001057 MacroAssembler* masm = GetVIXLAssembler();
1058 BlockPoolsScope block_pools(masm);
1059 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001061 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001062
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001063 DCHECK(!src.IsPreIndex());
1064 DCHECK(!src.IsPostIndex());
1065
1066 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001067 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001068 MemOperand base = MemOperand(temp_base);
1069 switch (type) {
1070 case Primitive::kPrimBoolean:
1071 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001072 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001073 break;
1074 case Primitive::kPrimByte:
1075 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001076 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001077 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1078 break;
1079 case Primitive::kPrimChar:
1080 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001081 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001082 break;
1083 case Primitive::kPrimShort:
1084 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001085 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001086 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1087 break;
1088 case Primitive::kPrimInt:
1089 case Primitive::kPrimNot:
1090 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001091 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001092 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001093 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001094 break;
1095 case Primitive::kPrimFloat:
1096 case Primitive::kPrimDouble: {
1097 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001098 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099
1100 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1101 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001102 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001103 __ Fmov(FPRegister(dst), temp);
1104 break;
1105 }
1106 case Primitive::kPrimVoid:
1107 LOG(FATAL) << "Unreachable type " << type;
1108 }
1109}
1110
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001111void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001112 CPURegister src,
1113 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001114 switch (type) {
1115 case Primitive::kPrimBoolean:
1116 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001117 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001118 break;
1119 case Primitive::kPrimChar:
1120 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001121 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001122 break;
1123 case Primitive::kPrimInt:
1124 case Primitive::kPrimNot:
1125 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001126 case Primitive::kPrimFloat:
1127 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001128 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001129 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001130 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001131 case Primitive::kPrimVoid:
1132 LOG(FATAL) << "Unreachable type " << type;
1133 }
1134}
1135
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001136void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1137 CPURegister src,
1138 const MemOperand& dst) {
1139 UseScratchRegisterScope temps(GetVIXLAssembler());
1140 Register temp_base = temps.AcquireX();
1141
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001142 DCHECK(!dst.IsPreIndex());
1143 DCHECK(!dst.IsPostIndex());
1144
1145 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001146 Operand op = OperandFromMemOperand(dst);
1147 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001148 MemOperand base = MemOperand(temp_base);
1149 switch (type) {
1150 case Primitive::kPrimBoolean:
1151 case Primitive::kPrimByte:
1152 __ Stlrb(Register(src), base);
1153 break;
1154 case Primitive::kPrimChar:
1155 case Primitive::kPrimShort:
1156 __ Stlrh(Register(src), base);
1157 break;
1158 case Primitive::kPrimInt:
1159 case Primitive::kPrimNot:
1160 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001161 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001162 __ Stlr(Register(src), base);
1163 break;
1164 case Primitive::kPrimFloat:
1165 case Primitive::kPrimDouble: {
1166 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001167 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001168
1169 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1170 __ Fmov(temp, FPRegister(src));
1171 __ Stlr(temp, base);
1172 break;
1173 }
1174 case Primitive::kPrimVoid:
1175 LOG(FATAL) << "Unreachable type " << type;
1176 }
1177}
1178
Calin Juravle175dc732015-08-25 15:42:32 +01001179void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1180 HInstruction* instruction,
1181 uint32_t dex_pc,
1182 SlowPathCode* slow_path) {
1183 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1184 instruction,
1185 dex_pc,
1186 slow_path);
1187}
1188
Alexandre Rames67555f72014-11-18 10:55:16 +00001189void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1190 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001191 uint32_t dex_pc,
1192 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001193 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001194 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001195 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1196 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001197 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001198}
1199
1200void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1201 vixl::Register class_reg) {
1202 UseScratchRegisterScope temps(GetVIXLAssembler());
1203 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001204 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001205 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001206
Serban Constantinescu02164b32014-11-13 14:05:07 +00001207 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001208 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001209 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1210 __ Add(temp, class_reg, status_offset);
1211 __ Ldar(temp, HeapOperand(temp));
1212 __ Cmp(temp, mirror::Class::kStatusInitialized);
1213 __ B(lt, slow_path->GetEntryLabel());
1214 } else {
1215 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1216 __ Cmp(temp, mirror::Class::kStatusInitialized);
1217 __ B(lt, slow_path->GetEntryLabel());
1218 __ Dmb(InnerShareable, BarrierReads);
1219 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001220 __ Bind(slow_path->GetExitLabel());
1221}
Alexandre Rames5319def2014-10-23 10:03:10 +01001222
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001223void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1224 BarrierType type = BarrierAll;
1225
1226 switch (kind) {
1227 case MemBarrierKind::kAnyAny:
1228 case MemBarrierKind::kAnyStore: {
1229 type = BarrierAll;
1230 break;
1231 }
1232 case MemBarrierKind::kLoadAny: {
1233 type = BarrierReads;
1234 break;
1235 }
1236 case MemBarrierKind::kStoreStore: {
1237 type = BarrierWrites;
1238 break;
1239 }
1240 default:
1241 LOG(FATAL) << "Unexpected memory barrier " << kind;
1242 }
1243 __ Dmb(InnerShareable, type);
1244}
1245
Serban Constantinescu02164b32014-11-13 14:05:07 +00001246void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1247 HBasicBlock* successor) {
1248 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001249 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1250 if (slow_path == nullptr) {
1251 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1252 instruction->SetSlowPath(slow_path);
1253 codegen_->AddSlowPath(slow_path);
1254 if (successor != nullptr) {
1255 DCHECK(successor->IsLoopHeader());
1256 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1257 }
1258 } else {
1259 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1260 }
1261
Serban Constantinescu02164b32014-11-13 14:05:07 +00001262 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1263 Register temp = temps.AcquireW();
1264
1265 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1266 if (successor == nullptr) {
1267 __ Cbnz(temp, slow_path->GetEntryLabel());
1268 __ Bind(slow_path->GetReturnLabel());
1269 } else {
1270 __ Cbz(temp, codegen_->GetLabelOf(successor));
1271 __ B(slow_path->GetEntryLabel());
1272 // slow_path will return to GetLabelOf(successor).
1273 }
1274}
1275
Alexandre Rames5319def2014-10-23 10:03:10 +01001276InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1277 CodeGeneratorARM64* codegen)
1278 : HGraphVisitor(graph),
1279 assembler_(codegen->GetAssembler()),
1280 codegen_(codegen) {}
1281
1282#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001283 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001284
1285#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1286
1287enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001288 // Using a base helps identify when we hit such breakpoints.
1289 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001290#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1291 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1292#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1293};
1294
1295#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1296 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001297 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001298 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1299 } \
1300 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1301 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1302 locations->SetOut(Location::Any()); \
1303 }
1304 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1305#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1306
1307#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001308#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001309
Alexandre Rames67555f72014-11-18 10:55:16 +00001310void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001311 DCHECK_EQ(instr->InputCount(), 2U);
1312 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1313 Primitive::Type type = instr->GetResultType();
1314 switch (type) {
1315 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001316 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001317 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001318 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001320 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001321
1322 case Primitive::kPrimFloat:
1323 case Primitive::kPrimDouble:
1324 locations->SetInAt(0, Location::RequiresFpuRegister());
1325 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001326 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001327 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001328
Alexandre Rames5319def2014-10-23 10:03:10 +01001329 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001330 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001331 }
1332}
1333
Alexandre Rames09a99962015-04-15 11:47:56 +01001334void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1335 LocationSummary* locations =
1336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1337 locations->SetInAt(0, Location::RequiresRegister());
1338 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1339 locations->SetOut(Location::RequiresFpuRegister());
1340 } else {
1341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1342 }
1343}
1344
1345void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1346 const FieldInfo& field_info) {
1347 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001348 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001349 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001350
1351 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1352 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1353
1354 if (field_info.IsVolatile()) {
1355 if (use_acquire_release) {
1356 // NB: LoadAcquire will record the pc info if needed.
1357 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1358 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001359 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001360 codegen_->MaybeRecordImplicitNullCheck(instruction);
1361 // For IRIW sequential consistency kLoadAny is not sufficient.
1362 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1363 }
1364 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001365 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001366 codegen_->MaybeRecordImplicitNullCheck(instruction);
1367 }
Roland Levillain4d027112015-07-01 15:41:14 +01001368
1369 if (field_type == Primitive::kPrimNot) {
1370 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1371 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001372}
1373
1374void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1375 LocationSummary* locations =
1376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1377 locations->SetInAt(0, Location::RequiresRegister());
1378 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1379 locations->SetInAt(1, Location::RequiresFpuRegister());
1380 } else {
1381 locations->SetInAt(1, Location::RequiresRegister());
1382 }
1383}
1384
1385void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001386 const FieldInfo& field_info,
1387 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001388 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001389 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001390
1391 Register obj = InputRegisterAt(instruction, 0);
1392 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001393 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001394 Offset offset = field_info.GetFieldOffset();
1395 Primitive::Type field_type = field_info.GetFieldType();
1396 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1397
Roland Levillain4d027112015-07-01 15:41:14 +01001398 {
1399 // We use a block to end the scratch scope before the write barrier, thus
1400 // freeing the temporary registers so they can be used in `MarkGCCard`.
1401 UseScratchRegisterScope temps(GetVIXLAssembler());
1402
1403 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1404 DCHECK(value.IsW());
1405 Register temp = temps.AcquireW();
1406 __ Mov(temp, value.W());
1407 GetAssembler()->PoisonHeapReference(temp.W());
1408 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001409 }
Roland Levillain4d027112015-07-01 15:41:14 +01001410
1411 if (field_info.IsVolatile()) {
1412 if (use_acquire_release) {
1413 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1414 codegen_->MaybeRecordImplicitNullCheck(instruction);
1415 } else {
1416 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1417 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1418 codegen_->MaybeRecordImplicitNullCheck(instruction);
1419 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1420 }
1421 } else {
1422 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1423 codegen_->MaybeRecordImplicitNullCheck(instruction);
1424 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001425 }
1426
1427 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001428 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001429 }
1430}
1431
Alexandre Rames67555f72014-11-18 10:55:16 +00001432void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001433 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001434
1435 switch (type) {
1436 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001437 case Primitive::kPrimLong: {
1438 Register dst = OutputRegister(instr);
1439 Register lhs = InputRegisterAt(instr, 0);
1440 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001441 if (instr->IsAdd()) {
1442 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001443 } else if (instr->IsAnd()) {
1444 __ And(dst, lhs, rhs);
1445 } else if (instr->IsOr()) {
1446 __ Orr(dst, lhs, rhs);
1447 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001448 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001449 } else {
1450 DCHECK(instr->IsXor());
1451 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001452 }
1453 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001454 }
1455 case Primitive::kPrimFloat:
1456 case Primitive::kPrimDouble: {
1457 FPRegister dst = OutputFPRegister(instr);
1458 FPRegister lhs = InputFPRegisterAt(instr, 0);
1459 FPRegister rhs = InputFPRegisterAt(instr, 1);
1460 if (instr->IsAdd()) {
1461 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001462 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001463 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001464 } else {
1465 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001466 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001467 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001468 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001469 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001470 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001471 }
1472}
1473
Serban Constantinescu02164b32014-11-13 14:05:07 +00001474void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1475 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1476
1477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1478 Primitive::Type type = instr->GetResultType();
1479 switch (type) {
1480 case Primitive::kPrimInt:
1481 case Primitive::kPrimLong: {
1482 locations->SetInAt(0, Location::RequiresRegister());
1483 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1484 locations->SetOut(Location::RequiresRegister());
1485 break;
1486 }
1487 default:
1488 LOG(FATAL) << "Unexpected shift type " << type;
1489 }
1490}
1491
1492void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1493 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1494
1495 Primitive::Type type = instr->GetType();
1496 switch (type) {
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimLong: {
1499 Register dst = OutputRegister(instr);
1500 Register lhs = InputRegisterAt(instr, 0);
1501 Operand rhs = InputOperandAt(instr, 1);
1502 if (rhs.IsImmediate()) {
1503 uint32_t shift_value = (type == Primitive::kPrimInt)
1504 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1505 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1506 if (instr->IsShl()) {
1507 __ Lsl(dst, lhs, shift_value);
1508 } else if (instr->IsShr()) {
1509 __ Asr(dst, lhs, shift_value);
1510 } else {
1511 __ Lsr(dst, lhs, shift_value);
1512 }
1513 } else {
1514 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1515
1516 if (instr->IsShl()) {
1517 __ Lsl(dst, lhs, rhs_reg);
1518 } else if (instr->IsShr()) {
1519 __ Asr(dst, lhs, rhs_reg);
1520 } else {
1521 __ Lsr(dst, lhs, rhs_reg);
1522 }
1523 }
1524 break;
1525 }
1526 default:
1527 LOG(FATAL) << "Unexpected shift operation type " << type;
1528 }
1529}
1530
Alexandre Rames5319def2014-10-23 10:03:10 +01001531void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001532 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001533}
1534
1535void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001536 HandleBinaryOp(instruction);
1537}
1538
1539void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1540 HandleBinaryOp(instruction);
1541}
1542
1543void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1544 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001545}
1546
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001547void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1548 LocationSummary* locations =
1549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1550 locations->SetInAt(0, Location::RequiresRegister());
1551 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001552 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1553 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1554 } else {
1555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1556 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557}
1558
1559void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1560 LocationSummary* locations = instruction->GetLocations();
1561 Primitive::Type type = instruction->GetType();
1562 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001563 Location index = locations->InAt(1);
1564 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001565 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001566 MacroAssembler* masm = GetVIXLAssembler();
1567 UseScratchRegisterScope temps(masm);
1568 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001569
1570 if (index.IsConstant()) {
1571 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001572 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001573 } else {
1574 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001575 __ Add(temp, obj, offset);
1576 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577 }
1578
Alexandre Rames67555f72014-11-18 10:55:16 +00001579 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001580 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001581
1582 if (type == Primitive::kPrimNot) {
1583 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1584 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001585}
1586
Alexandre Rames5319def2014-10-23 10:03:10 +01001587void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1589 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001591}
1592
1593void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001594 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001595 __ Ldr(OutputRegister(instruction),
1596 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001597 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001598}
1599
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001600void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1602 instruction,
1603 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1604 locations->SetInAt(0, Location::RequiresRegister());
1605 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1606 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1607 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001608 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001609 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001610 }
1611}
1612
1613void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1614 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001615 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001616 bool may_need_runtime_call = locations->CanCall();
1617 bool needs_write_barrier =
1618 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001619
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001620 Register array = InputRegisterAt(instruction, 0);
1621 CPURegister value = InputCPURegisterAt(instruction, 2);
1622 CPURegister source = value;
1623 Location index = locations->InAt(1);
1624 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1625 MemOperand destination = HeapOperand(array);
1626 MacroAssembler* masm = GetVIXLAssembler();
1627 BlockPoolsScope block_pools(masm);
1628
1629 if (!needs_write_barrier) {
1630 DCHECK(!may_need_runtime_call);
1631 if (index.IsConstant()) {
1632 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1633 destination = HeapOperand(array, offset);
1634 } else {
1635 UseScratchRegisterScope temps(masm);
1636 Register temp = temps.AcquireSameSizeAs(array);
1637 __ Add(temp, array, offset);
1638 destination = HeapOperand(temp,
1639 XRegisterFrom(index),
1640 LSL,
1641 Primitive::ComponentSizeShift(value_type));
1642 }
1643 codegen_->Store(value_type, value, destination);
1644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001645 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001646 DCHECK(needs_write_barrier);
1647 vixl::Label done;
1648 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001649 {
1650 // We use a block to end the scratch scope before the write barrier, thus
1651 // freeing the temporary registers so they can be used in `MarkGCCard`.
1652 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001653 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001654 if (index.IsConstant()) {
1655 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001656 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001657 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001658 destination = HeapOperand(temp,
1659 XRegisterFrom(index),
1660 LSL,
1661 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001662 }
1663
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1665 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1666 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1667
1668 if (may_need_runtime_call) {
1669 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1670 codegen_->AddSlowPath(slow_path);
1671 if (instruction->GetValueCanBeNull()) {
1672 vixl::Label non_zero;
1673 __ Cbnz(Register(value), &non_zero);
1674 if (!index.IsConstant()) {
1675 __ Add(temp, array, offset);
1676 }
1677 __ Str(wzr, destination);
1678 codegen_->MaybeRecordImplicitNullCheck(instruction);
1679 __ B(&done);
1680 __ Bind(&non_zero);
1681 }
1682
1683 Register temp2 = temps.AcquireSameSizeAs(array);
1684 __ Ldr(temp, HeapOperand(array, class_offset));
1685 codegen_->MaybeRecordImplicitNullCheck(instruction);
1686 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1687 __ Ldr(temp, HeapOperand(temp, component_offset));
1688 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1689 // No need to poison/unpoison, we're comparing two poisoned references.
1690 __ Cmp(temp, temp2);
1691 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1692 vixl::Label do_put;
1693 __ B(eq, &do_put);
1694 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1695 __ Ldr(temp, HeapOperand(temp, super_offset));
1696 // No need to unpoison, we're comparing against null.
1697 __ Cbnz(temp, slow_path->GetEntryLabel());
1698 __ Bind(&do_put);
1699 } else {
1700 __ B(ne, slow_path->GetEntryLabel());
1701 }
1702 }
1703
1704 if (kPoisonHeapReferences) {
1705 DCHECK(value.IsW());
1706 __ Mov(temp, value.W());
1707 GetAssembler()->PoisonHeapReference(temp);
1708 source = temp;
1709 }
1710
1711 if (!index.IsConstant()) {
1712 __ Add(temp, array, offset);
1713 }
1714 __ Str(value, destination);
1715
1716 if (!may_need_runtime_call) {
1717 codegen_->MaybeRecordImplicitNullCheck(instruction);
1718 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001719 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001720
1721 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1722
1723 if (done.IsLinked()) {
1724 __ Bind(&done);
1725 }
1726
1727 if (slow_path != nullptr) {
1728 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001729 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001730 }
1731}
1732
Alexandre Rames67555f72014-11-18 10:55:16 +00001733void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001734 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1735 ? LocationSummary::kCallOnSlowPath
1736 : LocationSummary::kNoCall;
1737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001738 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001739 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 if (instruction->HasUses()) {
1741 locations->SetOut(Location::SameAsFirstInput());
1742 }
1743}
1744
1745void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001746 BoundsCheckSlowPathARM64* slow_path =
1747 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 codegen_->AddSlowPath(slow_path);
1749
1750 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1751 __ B(slow_path->GetEntryLabel(), hs);
1752}
1753
Alexandre Rames67555f72014-11-18 10:55:16 +00001754void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1755 LocationSummary* locations =
1756 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1757 locations->SetInAt(0, Location::RequiresRegister());
1758 if (check->HasUses()) {
1759 locations->SetOut(Location::SameAsFirstInput());
1760 }
1761}
1762
1763void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1764 // We assume the class is not null.
1765 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1766 check->GetLoadClass(), check, check->GetDexPc(), true);
1767 codegen_->AddSlowPath(slow_path);
1768 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1769}
1770
Roland Levillain7f63c522015-07-13 15:54:55 +00001771static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1772 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1773 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1774}
1775
Serban Constantinescu02164b32014-11-13 14:05:07 +00001776void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001777 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001778 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1779 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001780 switch (in_type) {
1781 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001782 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001783 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001784 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1785 break;
1786 }
1787 case Primitive::kPrimFloat:
1788 case Primitive::kPrimDouble: {
1789 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001790 locations->SetInAt(1,
1791 IsFloatingPointZeroConstant(compare->InputAt(1))
1792 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1793 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001794 locations->SetOut(Location::RequiresRegister());
1795 break;
1796 }
1797 default:
1798 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1799 }
1800}
1801
1802void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1803 Primitive::Type in_type = compare->InputAt(0)->GetType();
1804
1805 // 0 if: left == right
1806 // 1 if: left > right
1807 // -1 if: left < right
1808 switch (in_type) {
1809 case Primitive::kPrimLong: {
1810 Register result = OutputRegister(compare);
1811 Register left = InputRegisterAt(compare, 0);
1812 Operand right = InputOperandAt(compare, 1);
1813
1814 __ Cmp(left, right);
1815 __ Cset(result, ne);
1816 __ Cneg(result, result, lt);
1817 break;
1818 }
1819 case Primitive::kPrimFloat:
1820 case Primitive::kPrimDouble: {
1821 Register result = OutputRegister(compare);
1822 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001823 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001824 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1825 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001826 __ Fcmp(left, 0.0);
1827 } else {
1828 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1829 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001830 if (compare->IsGtBias()) {
1831 __ Cset(result, ne);
1832 } else {
1833 __ Csetm(result, ne);
1834 }
1835 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001836 break;
1837 }
1838 default:
1839 LOG(FATAL) << "Unimplemented compare type " << in_type;
1840 }
1841}
1842
1843void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001845
1846 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1847 locations->SetInAt(0, Location::RequiresFpuRegister());
1848 locations->SetInAt(1,
1849 IsFloatingPointZeroConstant(instruction->InputAt(1))
1850 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1851 : Location::RequiresFpuRegister());
1852 } else {
1853 // Integer cases.
1854 locations->SetInAt(0, Location::RequiresRegister());
1855 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1856 }
1857
Alexandre Rames5319def2014-10-23 10:03:10 +01001858 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001860 }
1861}
1862
1863void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1864 if (!instruction->NeedsMaterialization()) {
1865 return;
1866 }
1867
1868 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001869 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001870 IfCondition if_cond = instruction->GetCondition();
1871 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001872
Roland Levillain7f63c522015-07-13 15:54:55 +00001873 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1874 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1875 if (locations->InAt(1).IsConstant()) {
1876 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1877 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1878 __ Fcmp(lhs, 0.0);
1879 } else {
1880 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1881 }
1882 __ Cset(res, arm64_cond);
1883 if (instruction->IsFPConditionTrueIfNaN()) {
1884 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1885 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1886 } else if (instruction->IsFPConditionFalseIfNaN()) {
1887 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1888 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1889 }
1890 } else {
1891 // Integer cases.
1892 Register lhs = InputRegisterAt(instruction, 0);
1893 Operand rhs = InputOperandAt(instruction, 1);
1894 __ Cmp(lhs, rhs);
1895 __ Cset(res, arm64_cond);
1896 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001897}
1898
1899#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1900 M(Equal) \
1901 M(NotEqual) \
1902 M(LessThan) \
1903 M(LessThanOrEqual) \
1904 M(GreaterThan) \
1905 M(GreaterThanOrEqual)
1906#define DEFINE_CONDITION_VISITORS(Name) \
1907void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1908void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1909FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001910#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001911#undef FOR_EACH_CONDITION_INSTRUCTION
1912
Zheng Xuc6667102015-05-15 16:08:45 +08001913void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1914 DCHECK(instruction->IsDiv() || instruction->IsRem());
1915
1916 LocationSummary* locations = instruction->GetLocations();
1917 Location second = locations->InAt(1);
1918 DCHECK(second.IsConstant());
1919
1920 Register out = OutputRegister(instruction);
1921 Register dividend = InputRegisterAt(instruction, 0);
1922 int64_t imm = Int64FromConstant(second.GetConstant());
1923 DCHECK(imm == 1 || imm == -1);
1924
1925 if (instruction->IsRem()) {
1926 __ Mov(out, 0);
1927 } else {
1928 if (imm == 1) {
1929 __ Mov(out, dividend);
1930 } else {
1931 __ Neg(out, dividend);
1932 }
1933 }
1934}
1935
1936void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1937 DCHECK(instruction->IsDiv() || instruction->IsRem());
1938
1939 LocationSummary* locations = instruction->GetLocations();
1940 Location second = locations->InAt(1);
1941 DCHECK(second.IsConstant());
1942
1943 Register out = OutputRegister(instruction);
1944 Register dividend = InputRegisterAt(instruction, 0);
1945 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001946 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001947 DCHECK(IsPowerOfTwo(abs_imm));
1948 int ctz_imm = CTZ(abs_imm);
1949
1950 UseScratchRegisterScope temps(GetVIXLAssembler());
1951 Register temp = temps.AcquireSameSizeAs(out);
1952
1953 if (instruction->IsDiv()) {
1954 __ Add(temp, dividend, abs_imm - 1);
1955 __ Cmp(dividend, 0);
1956 __ Csel(out, temp, dividend, lt);
1957 if (imm > 0) {
1958 __ Asr(out, out, ctz_imm);
1959 } else {
1960 __ Neg(out, Operand(out, ASR, ctz_imm));
1961 }
1962 } else {
1963 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1964 __ Asr(temp, dividend, bits - 1);
1965 __ Lsr(temp, temp, bits - ctz_imm);
1966 __ Add(out, dividend, temp);
1967 __ And(out, out, abs_imm - 1);
1968 __ Sub(out, out, temp);
1969 }
1970}
1971
1972void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1973 DCHECK(instruction->IsDiv() || instruction->IsRem());
1974
1975 LocationSummary* locations = instruction->GetLocations();
1976 Location second = locations->InAt(1);
1977 DCHECK(second.IsConstant());
1978
1979 Register out = OutputRegister(instruction);
1980 Register dividend = InputRegisterAt(instruction, 0);
1981 int64_t imm = Int64FromConstant(second.GetConstant());
1982
1983 Primitive::Type type = instruction->GetResultType();
1984 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1985
1986 int64_t magic;
1987 int shift;
1988 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1989
1990 UseScratchRegisterScope temps(GetVIXLAssembler());
1991 Register temp = temps.AcquireSameSizeAs(out);
1992
1993 // temp = get_high(dividend * magic)
1994 __ Mov(temp, magic);
1995 if (type == Primitive::kPrimLong) {
1996 __ Smulh(temp, dividend, temp);
1997 } else {
1998 __ Smull(temp.X(), dividend, temp);
1999 __ Lsr(temp.X(), temp.X(), 32);
2000 }
2001
2002 if (imm > 0 && magic < 0) {
2003 __ Add(temp, temp, dividend);
2004 } else if (imm < 0 && magic > 0) {
2005 __ Sub(temp, temp, dividend);
2006 }
2007
2008 if (shift != 0) {
2009 __ Asr(temp, temp, shift);
2010 }
2011
2012 if (instruction->IsDiv()) {
2013 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2014 } else {
2015 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2016 // TODO: Strength reduction for msub.
2017 Register temp_imm = temps.AcquireSameSizeAs(out);
2018 __ Mov(temp_imm, imm);
2019 __ Msub(out, temp, temp_imm, dividend);
2020 }
2021}
2022
2023void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2024 DCHECK(instruction->IsDiv() || instruction->IsRem());
2025 Primitive::Type type = instruction->GetResultType();
2026 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2027
2028 LocationSummary* locations = instruction->GetLocations();
2029 Register out = OutputRegister(instruction);
2030 Location second = locations->InAt(1);
2031
2032 if (second.IsConstant()) {
2033 int64_t imm = Int64FromConstant(second.GetConstant());
2034
2035 if (imm == 0) {
2036 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2037 } else if (imm == 1 || imm == -1) {
2038 DivRemOneOrMinusOne(instruction);
2039 } else if (IsPowerOfTwo(std::abs(imm))) {
2040 DivRemByPowerOfTwo(instruction);
2041 } else {
2042 DCHECK(imm <= -2 || imm >= 2);
2043 GenerateDivRemWithAnyConstant(instruction);
2044 }
2045 } else {
2046 Register dividend = InputRegisterAt(instruction, 0);
2047 Register divisor = InputRegisterAt(instruction, 1);
2048 if (instruction->IsDiv()) {
2049 __ Sdiv(out, dividend, divisor);
2050 } else {
2051 UseScratchRegisterScope temps(GetVIXLAssembler());
2052 Register temp = temps.AcquireSameSizeAs(out);
2053 __ Sdiv(temp, dividend, divisor);
2054 __ Msub(out, temp, divisor, dividend);
2055 }
2056 }
2057}
2058
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002059void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2060 LocationSummary* locations =
2061 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2062 switch (div->GetResultType()) {
2063 case Primitive::kPrimInt:
2064 case Primitive::kPrimLong:
2065 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002066 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2068 break;
2069
2070 case Primitive::kPrimFloat:
2071 case Primitive::kPrimDouble:
2072 locations->SetInAt(0, Location::RequiresFpuRegister());
2073 locations->SetInAt(1, Location::RequiresFpuRegister());
2074 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2075 break;
2076
2077 default:
2078 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2079 }
2080}
2081
2082void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2083 Primitive::Type type = div->GetResultType();
2084 switch (type) {
2085 case Primitive::kPrimInt:
2086 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002087 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002088 break;
2089
2090 case Primitive::kPrimFloat:
2091 case Primitive::kPrimDouble:
2092 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2093 break;
2094
2095 default:
2096 LOG(FATAL) << "Unexpected div type " << type;
2097 }
2098}
2099
Alexandre Rames67555f72014-11-18 10:55:16 +00002100void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002101 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2102 ? LocationSummary::kCallOnSlowPath
2103 : LocationSummary::kNoCall;
2104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002105 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2106 if (instruction->HasUses()) {
2107 locations->SetOut(Location::SameAsFirstInput());
2108 }
2109}
2110
2111void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2112 SlowPathCodeARM64* slow_path =
2113 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2114 codegen_->AddSlowPath(slow_path);
2115 Location value = instruction->GetLocations()->InAt(0);
2116
Alexandre Rames3e69f162014-12-10 10:36:50 +00002117 Primitive::Type type = instruction->GetType();
2118
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002119 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2120 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002121 return;
2122 }
2123
Alexandre Rames67555f72014-11-18 10:55:16 +00002124 if (value.IsConstant()) {
2125 int64_t divisor = Int64ConstantFrom(value);
2126 if (divisor == 0) {
2127 __ B(slow_path->GetEntryLabel());
2128 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002129 // A division by a non-null constant is valid. We don't need to perform
2130 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002131 }
2132 } else {
2133 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2134 }
2135}
2136
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002137void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2138 LocationSummary* locations =
2139 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2140 locations->SetOut(Location::ConstantLocation(constant));
2141}
2142
2143void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2144 UNUSED(constant);
2145 // Will be generated at use site.
2146}
2147
Alexandre Rames5319def2014-10-23 10:03:10 +01002148void LocationsBuilderARM64::VisitExit(HExit* exit) {
2149 exit->SetLocations(nullptr);
2150}
2151
2152void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002153 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002154}
2155
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002156void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2157 LocationSummary* locations =
2158 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2159 locations->SetOut(Location::ConstantLocation(constant));
2160}
2161
2162void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2163 UNUSED(constant);
2164 // Will be generated at use site.
2165}
2166
David Brazdilfc6a86a2015-06-26 10:33:45 +00002167void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002168 DCHECK(!successor->IsExitBlock());
2169 HBasicBlock* block = got->GetBlock();
2170 HInstruction* previous = got->GetPrevious();
2171 HLoopInformation* info = block->GetLoopInformation();
2172
David Brazdil46e2a392015-03-16 17:31:52 +00002173 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002174 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2175 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2176 return;
2177 }
2178 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2179 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2180 }
2181 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002182 __ B(codegen_->GetLabelOf(successor));
2183 }
2184}
2185
David Brazdilfc6a86a2015-06-26 10:33:45 +00002186void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2187 got->SetLocations(nullptr);
2188}
2189
2190void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2191 HandleGoto(got, got->GetSuccessor());
2192}
2193
2194void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2195 try_boundary->SetLocations(nullptr);
2196}
2197
2198void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2199 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2200 if (!successor->IsExitBlock()) {
2201 HandleGoto(try_boundary, successor);
2202 }
2203}
2204
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002205void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2206 vixl::Label* true_target,
2207 vixl::Label* false_target,
2208 vixl::Label* always_true_target) {
2209 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002210 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002211
Serban Constantinescu02164b32014-11-13 14:05:07 +00002212 if (cond->IsIntConstant()) {
2213 int32_t cond_value = cond->AsIntConstant()->GetValue();
2214 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002215 if (always_true_target != nullptr) {
2216 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002217 }
2218 return;
2219 } else {
2220 DCHECK_EQ(cond_value, 0);
2221 }
2222 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002223 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002224 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002225 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002226 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002227 } else {
2228 // The condition instruction has not been materialized, use its inputs as
2229 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002230 Primitive::Type type =
2231 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2232
2233 if (Primitive::IsFloatingPointType(type)) {
2234 // FP compares don't like null false_targets.
2235 if (false_target == nullptr) {
2236 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002237 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002238 FPRegister lhs = InputFPRegisterAt(condition, 0);
2239 if (condition->GetLocations()->InAt(1).IsConstant()) {
2240 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2241 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2242 __ Fcmp(lhs, 0.0);
2243 } else {
2244 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2245 }
2246 if (condition->IsFPConditionTrueIfNaN()) {
2247 __ B(vs, true_target); // VS for unordered.
2248 } else if (condition->IsFPConditionFalseIfNaN()) {
2249 __ B(vs, false_target); // VS for unordered.
2250 }
2251 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002252 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002253 // Integer cases.
2254 Register lhs = InputRegisterAt(condition, 0);
2255 Operand rhs = InputOperandAt(condition, 1);
2256 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2257 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2258 switch (arm64_cond) {
2259 case eq:
2260 __ Cbz(lhs, true_target);
2261 break;
2262 case ne:
2263 __ Cbnz(lhs, true_target);
2264 break;
2265 case lt:
2266 // Test the sign bit and branch accordingly.
2267 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2268 break;
2269 case ge:
2270 // Test the sign bit and branch accordingly.
2271 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2272 break;
2273 default:
2274 // Without the `static_cast` the compiler throws an error for
2275 // `-Werror=sign-promo`.
2276 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2277 }
2278 } else {
2279 __ Cmp(lhs, rhs);
2280 __ B(arm64_cond, true_target);
2281 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002282 }
2283 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002284 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002285 __ B(false_target);
2286 }
2287}
2288
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002289void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2291 HInstruction* cond = if_instr->InputAt(0);
2292 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2293 locations->SetInAt(0, Location::RequiresRegister());
2294 }
2295}
2296
2297void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2298 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2299 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2300 vixl::Label* always_true_target = true_target;
2301 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2302 if_instr->IfTrueSuccessor())) {
2303 always_true_target = nullptr;
2304 }
2305 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2306 if_instr->IfFalseSuccessor())) {
2307 false_target = nullptr;
2308 }
2309 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2310}
2311
2312void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2313 LocationSummary* locations = new (GetGraph()->GetArena())
2314 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2315 HInstruction* cond = deoptimize->InputAt(0);
2316 DCHECK(cond->IsCondition());
2317 if (cond->AsCondition()->NeedsMaterialization()) {
2318 locations->SetInAt(0, Location::RequiresRegister());
2319 }
2320}
2321
2322void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2323 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2324 DeoptimizationSlowPathARM64(deoptimize);
2325 codegen_->AddSlowPath(slow_path);
2326 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2327 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2328}
2329
Alexandre Rames5319def2014-10-23 10:03:10 +01002330void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002331 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002332}
2333
2334void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002335 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002336}
2337
2338void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002339 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002340}
2341
2342void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002343 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002344}
2345
Alexandre Rames67555f72014-11-18 10:55:16 +00002346void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002347 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2348 switch (instruction->GetTypeCheckKind()) {
2349 case TypeCheckKind::kExactCheck:
2350 case TypeCheckKind::kAbstractClassCheck:
2351 case TypeCheckKind::kClassHierarchyCheck:
2352 case TypeCheckKind::kArrayObjectCheck:
2353 call_kind = LocationSummary::kNoCall;
2354 break;
2355 case TypeCheckKind::kInterfaceCheck:
2356 call_kind = LocationSummary::kCall;
2357 break;
2358 case TypeCheckKind::kArrayCheck:
2359 call_kind = LocationSummary::kCallOnSlowPath;
2360 break;
2361 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002363 if (call_kind != LocationSummary::kCall) {
2364 locations->SetInAt(0, Location::RequiresRegister());
2365 locations->SetInAt(1, Location::RequiresRegister());
2366 // The out register is used as a temporary, so it overlaps with the inputs.
2367 // Note that TypeCheckSlowPathARM64 uses this register too.
2368 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2369 } else {
2370 InvokeRuntimeCallingConvention calling_convention;
2371 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2372 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2373 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2374 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002375}
2376
2377void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2378 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002379 Register obj = InputRegisterAt(instruction, 0);
2380 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002381 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002382 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2383 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2384 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2385 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002387 vixl::Label done, zero;
2388 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002389
2390 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002391 // Avoid null check if we know `obj` is not null.
2392 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002393 __ Cbz(obj, &zero);
2394 }
2395
2396 // In case of an interface check, we put the object class into the object register.
2397 // This is safe, as the register is caller-save, and the object must be in another
2398 // register if it survives the runtime call.
2399 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2400 ? obj
2401 : out;
2402 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2403 GetAssembler()->MaybeUnpoisonHeapReference(target);
2404
2405 switch (instruction->GetTypeCheckKind()) {
2406 case TypeCheckKind::kExactCheck: {
2407 __ Cmp(out, cls);
2408 __ Cset(out, eq);
2409 if (zero.IsLinked()) {
2410 __ B(&done);
2411 }
2412 break;
2413 }
2414 case TypeCheckKind::kAbstractClassCheck: {
2415 // If the class is abstract, we eagerly fetch the super class of the
2416 // object to avoid doing a comparison we know will fail.
2417 vixl::Label loop, success;
2418 __ Bind(&loop);
2419 __ Ldr(out, HeapOperand(out, super_offset));
2420 GetAssembler()->MaybeUnpoisonHeapReference(out);
2421 // If `out` is null, we use it for the result, and jump to `done`.
2422 __ Cbz(out, &done);
2423 __ Cmp(out, cls);
2424 __ B(ne, &loop);
2425 __ Mov(out, 1);
2426 if (zero.IsLinked()) {
2427 __ B(&done);
2428 }
2429 break;
2430 }
2431 case TypeCheckKind::kClassHierarchyCheck: {
2432 // Walk over the class hierarchy to find a match.
2433 vixl::Label loop, success;
2434 __ Bind(&loop);
2435 __ Cmp(out, cls);
2436 __ B(eq, &success);
2437 __ Ldr(out, HeapOperand(out, super_offset));
2438 GetAssembler()->MaybeUnpoisonHeapReference(out);
2439 __ Cbnz(out, &loop);
2440 // If `out` is null, we use it for the result, and jump to `done`.
2441 __ B(&done);
2442 __ Bind(&success);
2443 __ Mov(out, 1);
2444 if (zero.IsLinked()) {
2445 __ B(&done);
2446 }
2447 break;
2448 }
2449 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002450 // Do an exact check.
2451 vixl::Label exact_check;
2452 __ Cmp(out, cls);
2453 __ B(eq, &exact_check);
2454 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002455 __ Ldr(out, HeapOperand(out, component_offset));
2456 GetAssembler()->MaybeUnpoisonHeapReference(out);
2457 // If `out` is null, we use it for the result, and jump to `done`.
2458 __ Cbz(out, &done);
2459 __ Ldrh(out, HeapOperand(out, primitive_offset));
2460 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2461 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002462 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002463 __ Mov(out, 1);
2464 __ B(&done);
2465 break;
2466 }
2467 case TypeCheckKind::kArrayCheck: {
2468 __ Cmp(out, cls);
2469 DCHECK(locations->OnlyCallsOnSlowPath());
2470 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2471 instruction, /* is_fatal */ false);
2472 codegen_->AddSlowPath(slow_path);
2473 __ B(ne, slow_path->GetEntryLabel());
2474 __ Mov(out, 1);
2475 if (zero.IsLinked()) {
2476 __ B(&done);
2477 }
2478 break;
2479 }
2480
2481 case TypeCheckKind::kInterfaceCheck:
2482 default: {
2483 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2484 instruction,
2485 instruction->GetDexPc(),
2486 nullptr);
2487 if (zero.IsLinked()) {
2488 __ B(&done);
2489 }
2490 break;
2491 }
2492 }
2493
2494 if (zero.IsLinked()) {
2495 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002496 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002497 }
2498
2499 if (done.IsLinked()) {
2500 __ Bind(&done);
2501 }
2502
2503 if (slow_path != nullptr) {
2504 __ Bind(slow_path->GetExitLabel());
2505 }
2506}
2507
2508void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2509 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2510 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2511
2512 switch (instruction->GetTypeCheckKind()) {
2513 case TypeCheckKind::kExactCheck:
2514 case TypeCheckKind::kAbstractClassCheck:
2515 case TypeCheckKind::kClassHierarchyCheck:
2516 case TypeCheckKind::kArrayObjectCheck:
2517 call_kind = throws_into_catch
2518 ? LocationSummary::kCallOnSlowPath
2519 : LocationSummary::kNoCall;
2520 break;
2521 case TypeCheckKind::kInterfaceCheck:
2522 call_kind = LocationSummary::kCall;
2523 break;
2524 case TypeCheckKind::kArrayCheck:
2525 call_kind = LocationSummary::kCallOnSlowPath;
2526 break;
2527 }
2528
2529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2530 instruction, call_kind);
2531 if (call_kind != LocationSummary::kCall) {
2532 locations->SetInAt(0, Location::RequiresRegister());
2533 locations->SetInAt(1, Location::RequiresRegister());
2534 // Note that TypeCheckSlowPathARM64 uses this register too.
2535 locations->AddTemp(Location::RequiresRegister());
2536 } else {
2537 InvokeRuntimeCallingConvention calling_convention;
2538 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2539 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2540 }
2541}
2542
2543void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2544 LocationSummary* locations = instruction->GetLocations();
2545 Register obj = InputRegisterAt(instruction, 0);
2546 Register cls = InputRegisterAt(instruction, 1);
2547 Register temp;
2548 if (!locations->WillCall()) {
2549 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2550 }
2551
2552 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2553 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2554 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2555 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2556 SlowPathCodeARM64* slow_path = nullptr;
2557
2558 if (!locations->WillCall()) {
2559 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2560 instruction, !locations->CanCall());
2561 codegen_->AddSlowPath(slow_path);
2562 }
2563
2564 vixl::Label done;
2565 // Avoid null check if we know obj is not null.
2566 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002567 __ Cbz(obj, &done);
2568 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002569
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002570 if (locations->WillCall()) {
2571 __ Ldr(obj, HeapOperand(obj, class_offset));
2572 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002573 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002574 __ Ldr(temp, HeapOperand(obj, class_offset));
2575 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002576 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002578 switch (instruction->GetTypeCheckKind()) {
2579 case TypeCheckKind::kExactCheck:
2580 case TypeCheckKind::kArrayCheck: {
2581 __ Cmp(temp, cls);
2582 // Jump to slow path for throwing the exception or doing a
2583 // more involved array check.
2584 __ B(ne, slow_path->GetEntryLabel());
2585 break;
2586 }
2587 case TypeCheckKind::kAbstractClassCheck: {
2588 // If the class is abstract, we eagerly fetch the super class of the
2589 // object to avoid doing a comparison we know will fail.
2590 vixl::Label loop;
2591 __ Bind(&loop);
2592 __ Ldr(temp, HeapOperand(temp, super_offset));
2593 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2594 // Jump to the slow path to throw the exception.
2595 __ Cbz(temp, slow_path->GetEntryLabel());
2596 __ Cmp(temp, cls);
2597 __ B(ne, &loop);
2598 break;
2599 }
2600 case TypeCheckKind::kClassHierarchyCheck: {
2601 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002602 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002603 __ Bind(&loop);
2604 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002605 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002606 __ Ldr(temp, HeapOperand(temp, super_offset));
2607 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2608 __ Cbnz(temp, &loop);
2609 // Jump to the slow path to throw the exception.
2610 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002611 break;
2612 }
2613 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002614 // Do an exact check.
2615 __ Cmp(temp, cls);
2616 __ B(eq, &done);
2617 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002618 __ Ldr(temp, HeapOperand(temp, component_offset));
2619 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2620 __ Cbz(temp, slow_path->GetEntryLabel());
2621 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2622 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2623 __ Cbnz(temp, slow_path->GetEntryLabel());
2624 break;
2625 }
2626 case TypeCheckKind::kInterfaceCheck:
2627 default:
2628 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2629 instruction,
2630 instruction->GetDexPc(),
2631 nullptr);
2632 break;
2633 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002634 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002635
2636 if (slow_path != nullptr) {
2637 __ Bind(slow_path->GetExitLabel());
2638 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002639}
2640
Alexandre Rames5319def2014-10-23 10:03:10 +01002641void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2643 locations->SetOut(Location::ConstantLocation(constant));
2644}
2645
2646void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2647 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002648 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002649}
2650
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002651void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2653 locations->SetOut(Location::ConstantLocation(constant));
2654}
2655
2656void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2657 // Will be generated at use site.
2658 UNUSED(constant);
2659}
2660
Calin Juravle175dc732015-08-25 15:42:32 +01002661void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2662 // The trampoline uses the same calling convention as dex calling conventions,
2663 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2664 // the method_idx.
2665 HandleInvoke(invoke);
2666}
2667
2668void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2669 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2670}
2671
Alexandre Rames5319def2014-10-23 10:03:10 +01002672void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002673 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002674 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002675}
2676
Alexandre Rames67555f72014-11-18 10:55:16 +00002677void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2678 HandleInvoke(invoke);
2679}
2680
2681void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2682 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002683 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2684 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2685 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002686 Location receiver = invoke->GetLocations()->InAt(0);
2687 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002688 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002689
2690 // The register ip1 is required to be used for the hidden argument in
2691 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002692 MacroAssembler* masm = GetVIXLAssembler();
2693 UseScratchRegisterScope scratch_scope(masm);
2694 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002695 scratch_scope.Exclude(ip1);
2696 __ Mov(ip1, invoke->GetDexMethodIndex());
2697
2698 // temp = object->GetClass();
2699 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002700 __ Ldr(temp.W(), StackOperandFrom(receiver));
2701 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002702 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002703 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002704 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002705 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002706 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002707 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002708 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002709 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002710 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002711 // lr();
2712 __ Blr(lr);
2713 DCHECK(!codegen_->IsLeafMethod());
2714 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2715}
2716
2717void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002718 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2719 if (intrinsic.TryDispatch(invoke)) {
2720 return;
2721 }
2722
Alexandre Rames67555f72014-11-18 10:55:16 +00002723 HandleInvoke(invoke);
2724}
2725
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002726void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002727 // When we do not run baseline, explicit clinit checks triggered by static
2728 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2729 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002730
Andreas Gampe878d58c2015-01-15 23:24:00 -08002731 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2732 if (intrinsic.TryDispatch(invoke)) {
2733 return;
2734 }
2735
Alexandre Rames67555f72014-11-18 10:55:16 +00002736 HandleInvoke(invoke);
2737}
2738
Andreas Gampe878d58c2015-01-15 23:24:00 -08002739static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2740 if (invoke->GetLocations()->Intrinsified()) {
2741 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2742 intrinsic.Dispatch(invoke);
2743 return true;
2744 }
2745 return false;
2746}
2747
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002748void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002749 // For better instruction scheduling we load the direct code pointer before the method pointer.
2750 bool direct_code_loaded = false;
2751 switch (invoke->GetCodePtrLocation()) {
2752 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2753 // LR = code address from literal pool with link-time patch.
2754 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2755 direct_code_loaded = true;
2756 break;
2757 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2758 // LR = invoke->GetDirectCodePtr();
2759 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2760 direct_code_loaded = true;
2761 break;
2762 default:
2763 break;
2764 }
2765
Andreas Gampe878d58c2015-01-15 23:24:00 -08002766 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002767 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2768 switch (invoke->GetMethodLoadKind()) {
2769 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2770 // temp = thread->string_init_entrypoint
2771 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2772 break;
2773 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2774 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2775 break;
2776 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2777 // Load method address from literal pool.
2778 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2779 break;
2780 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2781 // Load method address from literal pool with a link-time patch.
2782 __ Ldr(XRegisterFrom(temp).X(),
2783 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2784 break;
2785 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2786 // Add ADRP with its PC-relative DexCache access patch.
2787 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2788 invoke->GetDexCacheArrayOffset());
2789 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2790 {
2791 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2792 __ adrp(XRegisterFrom(temp).X(), 0);
2793 }
2794 __ Bind(pc_insn_label); // Bind after ADRP.
2795 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2796 // Add LDR with its PC-relative DexCache access patch.
2797 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2798 invoke->GetDexCacheArrayOffset());
2799 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2800 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2801 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2802 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002803 }
Vladimir Marko58155012015-08-19 12:49:41 +00002804 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2805 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2806 Register reg = XRegisterFrom(temp);
2807 Register method_reg;
2808 if (current_method.IsRegister()) {
2809 method_reg = XRegisterFrom(current_method);
2810 } else {
2811 DCHECK(invoke->GetLocations()->Intrinsified());
2812 DCHECK(!current_method.IsValid());
2813 method_reg = reg;
2814 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2815 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002816
Vladimir Marko58155012015-08-19 12:49:41 +00002817 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002818 __ Ldr(reg.X(),
2819 MemOperand(method_reg.X(),
2820 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002821 // temp = temp[index_in_cache];
2822 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2823 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2824 break;
2825 }
2826 }
2827
2828 switch (invoke->GetCodePtrLocation()) {
2829 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2830 __ Bl(&frame_entry_label_);
2831 break;
2832 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2833 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2834 vixl::Label* label = &relative_call_patches_.back().label;
2835 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2836 __ Bind(label); // Bind after BL.
2837 break;
2838 }
2839 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2840 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2841 // LR prepared above for better instruction scheduling.
2842 DCHECK(direct_code_loaded);
2843 // lr()
2844 __ Blr(lr);
2845 break;
2846 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2847 // LR = callee_method->entry_point_from_quick_compiled_code_;
2848 __ Ldr(lr, MemOperand(
2849 XRegisterFrom(callee_method).X(),
2850 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2851 // lr()
2852 __ Blr(lr);
2853 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002854 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002855
Andreas Gampe878d58c2015-01-15 23:24:00 -08002856 DCHECK(!IsLeafMethod());
2857}
2858
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002859void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2860 LocationSummary* locations = invoke->GetLocations();
2861 Location receiver = locations->InAt(0);
2862 Register temp = XRegisterFrom(temp_in);
2863 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2864 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2865 Offset class_offset = mirror::Object::ClassOffset();
2866 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2867
2868 BlockPoolsScope block_pools(GetVIXLAssembler());
2869
2870 DCHECK(receiver.IsRegister());
2871 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2872 MaybeRecordImplicitNullCheck(invoke);
2873 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2874 // temp = temp->GetMethodAt(method_offset);
2875 __ Ldr(temp, MemOperand(temp, method_offset));
2876 // lr = temp->GetEntryPoint();
2877 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2878 // lr();
2879 __ Blr(lr);
2880}
2881
Vladimir Marko58155012015-08-19 12:49:41 +00002882void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2883 DCHECK(linker_patches->empty());
2884 size_t size =
2885 method_patches_.size() +
2886 call_patches_.size() +
2887 relative_call_patches_.size() +
2888 pc_rel_dex_cache_patches_.size();
2889 linker_patches->reserve(size);
2890 for (const auto& entry : method_patches_) {
2891 const MethodReference& target_method = entry.first;
2892 vixl::Literal<uint64_t>* literal = entry.second;
2893 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2894 target_method.dex_file,
2895 target_method.dex_method_index));
2896 }
2897 for (const auto& entry : call_patches_) {
2898 const MethodReference& target_method = entry.first;
2899 vixl::Literal<uint64_t>* literal = entry.second;
2900 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2901 target_method.dex_file,
2902 target_method.dex_method_index));
2903 }
2904 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2905 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2906 info.target_method.dex_file,
2907 info.target_method.dex_method_index));
2908 }
2909 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2910 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2911 &info.target_dex_file,
2912 info.pc_insn_label->location() - 4u,
2913 info.element_offset));
2914 }
2915}
2916
2917vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2918 // Look up the literal for value.
2919 auto lb = uint64_literals_.lower_bound(value);
2920 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2921 return lb->second;
2922 }
2923 // We don't have a literal for this value, insert a new one.
2924 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2925 uint64_literals_.PutBefore(lb, value, literal);
2926 return literal;
2927}
2928
2929vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2930 MethodReference target_method,
2931 MethodToLiteralMap* map) {
2932 // Look up the literal for target_method.
2933 auto lb = map->lower_bound(target_method);
2934 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2935 return lb->second;
2936 }
2937 // We don't have a literal for this method yet, insert a new one.
2938 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2939 map->PutBefore(lb, target_method, literal);
2940 return literal;
2941}
2942
2943vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2944 MethodReference target_method) {
2945 return DeduplicateMethodLiteral(target_method, &method_patches_);
2946}
2947
2948vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2949 MethodReference target_method) {
2950 return DeduplicateMethodLiteral(target_method, &call_patches_);
2951}
2952
2953
Andreas Gampe878d58c2015-01-15 23:24:00 -08002954void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002955 // When we do not run baseline, explicit clinit checks triggered by static
2956 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2957 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002958
Andreas Gampe878d58c2015-01-15 23:24:00 -08002959 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2960 return;
2961 }
2962
Alexandre Ramesd921d642015-04-16 15:07:16 +01002963 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002964 LocationSummary* locations = invoke->GetLocations();
2965 codegen_->GenerateStaticOrDirectCall(
2966 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002967 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002968}
2969
2970void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002971 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2972 return;
2973 }
2974
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002975 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002976 DCHECK(!codegen_->IsLeafMethod());
2977 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2978}
2979
Alexandre Rames67555f72014-11-18 10:55:16 +00002980void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2981 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2982 : LocationSummary::kNoCall;
2983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002984 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002985 locations->SetOut(Location::RequiresRegister());
2986}
2987
2988void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2989 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002990 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002991 if (cls->IsReferrersClass()) {
2992 DCHECK(!cls->CanCallRuntime());
2993 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002994 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002995 } else {
2996 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002997 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
2998 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
2999 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3000 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003001
3002 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3003 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3004 codegen_->AddSlowPath(slow_path);
3005 __ Cbz(out, slow_path->GetEntryLabel());
3006 if (cls->MustGenerateClinitCheck()) {
3007 GenerateClassInitializationCheck(slow_path, out);
3008 } else {
3009 __ Bind(slow_path->GetExitLabel());
3010 }
3011 }
3012}
3013
David Brazdilcb1c0552015-08-04 16:22:25 +01003014static MemOperand GetExceptionTlsAddress() {
3015 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3016}
3017
Alexandre Rames67555f72014-11-18 10:55:16 +00003018void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3019 LocationSummary* locations =
3020 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3021 locations->SetOut(Location::RequiresRegister());
3022}
3023
3024void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003025 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3026}
3027
3028void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3029 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3030}
3031
3032void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3033 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003034}
3035
Alexandre Rames5319def2014-10-23 10:03:10 +01003036void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3037 load->SetLocations(nullptr);
3038}
3039
3040void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3041 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003042 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003043}
3044
Alexandre Rames67555f72014-11-18 10:55:16 +00003045void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3046 LocationSummary* locations =
3047 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003048 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003049 locations->SetOut(Location::RequiresRegister());
3050}
3051
3052void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3053 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3054 codegen_->AddSlowPath(slow_path);
3055
3056 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003057 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003058 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003059 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3060 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3061 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003062 __ Cbz(out, slow_path->GetEntryLabel());
3063 __ Bind(slow_path->GetExitLabel());
3064}
3065
Alexandre Rames5319def2014-10-23 10:03:10 +01003066void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3067 local->SetLocations(nullptr);
3068}
3069
3070void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3071 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3072}
3073
3074void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3076 locations->SetOut(Location::ConstantLocation(constant));
3077}
3078
3079void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3080 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003081 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003082}
3083
Alexandre Rames67555f72014-11-18 10:55:16 +00003084void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3087 InvokeRuntimeCallingConvention calling_convention;
3088 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3089}
3090
3091void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3092 codegen_->InvokeRuntime(instruction->IsEnter()
3093 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3094 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003095 instruction->GetDexPc(),
3096 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003097 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003098}
3099
Alexandre Rames42d641b2014-10-27 14:00:51 +00003100void LocationsBuilderARM64::VisitMul(HMul* mul) {
3101 LocationSummary* locations =
3102 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3103 switch (mul->GetResultType()) {
3104 case Primitive::kPrimInt:
3105 case Primitive::kPrimLong:
3106 locations->SetInAt(0, Location::RequiresRegister());
3107 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003109 break;
3110
3111 case Primitive::kPrimFloat:
3112 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003113 locations->SetInAt(0, Location::RequiresFpuRegister());
3114 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003115 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003116 break;
3117
3118 default:
3119 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3120 }
3121}
3122
3123void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3124 switch (mul->GetResultType()) {
3125 case Primitive::kPrimInt:
3126 case Primitive::kPrimLong:
3127 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3128 break;
3129
3130 case Primitive::kPrimFloat:
3131 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003132 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003133 break;
3134
3135 default:
3136 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3137 }
3138}
3139
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003140void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3141 LocationSummary* locations =
3142 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3143 switch (neg->GetResultType()) {
3144 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003145 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003146 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003147 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003148 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003149
3150 case Primitive::kPrimFloat:
3151 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003152 locations->SetInAt(0, Location::RequiresFpuRegister());
3153 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003154 break;
3155
3156 default:
3157 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3158 }
3159}
3160
3161void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3162 switch (neg->GetResultType()) {
3163 case Primitive::kPrimInt:
3164 case Primitive::kPrimLong:
3165 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3166 break;
3167
3168 case Primitive::kPrimFloat:
3169 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003170 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003171 break;
3172
3173 default:
3174 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3175 }
3176}
3177
3178void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3179 LocationSummary* locations =
3180 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3181 InvokeRuntimeCallingConvention calling_convention;
3182 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003183 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003184 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003185 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003186 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003187 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003188}
3189
3190void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3191 LocationSummary* locations = instruction->GetLocations();
3192 InvokeRuntimeCallingConvention calling_convention;
3193 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3194 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003195 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003196 // Note: if heap poisoning is enabled, the entry point takes cares
3197 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003198 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3199 instruction,
3200 instruction->GetDexPc(),
3201 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003202 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003203}
3204
Alexandre Rames5319def2014-10-23 10:03:10 +01003205void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3206 LocationSummary* locations =
3207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3208 InvokeRuntimeCallingConvention calling_convention;
3209 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003210 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003211 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003212 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003213}
3214
3215void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3216 LocationSummary* locations = instruction->GetLocations();
3217 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3218 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003219 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003220 // Note: if heap poisoning is enabled, the entry point takes cares
3221 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003222 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3223 instruction,
3224 instruction->GetDexPc(),
3225 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003226 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003227}
3228
3229void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003231 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003233}
3234
3235void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003236 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003237 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003238 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003239 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003240 break;
3241
3242 default:
3243 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3244 }
3245}
3246
David Brazdil66d126e2015-04-03 16:02:44 +01003247void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3248 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3249 locations->SetInAt(0, Location::RequiresRegister());
3250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3251}
3252
3253void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003254 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3255}
3256
Alexandre Rames5319def2014-10-23 10:03:10 +01003257void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003258 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3259 ? LocationSummary::kCallOnSlowPath
3260 : LocationSummary::kNoCall;
3261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003262 locations->SetInAt(0, Location::RequiresRegister());
3263 if (instruction->HasUses()) {
3264 locations->SetOut(Location::SameAsFirstInput());
3265 }
3266}
3267
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003268void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003269 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3270 return;
3271 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003272
Alexandre Ramesd921d642015-04-16 15:07:16 +01003273 BlockPoolsScope block_pools(GetVIXLAssembler());
3274 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003275 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3276 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3277}
3278
3279void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003280 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3281 codegen_->AddSlowPath(slow_path);
3282
3283 LocationSummary* locations = instruction->GetLocations();
3284 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003285
3286 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003287}
3288
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003289void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003290 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003291 GenerateImplicitNullCheck(instruction);
3292 } else {
3293 GenerateExplicitNullCheck(instruction);
3294 }
3295}
3296
Alexandre Rames67555f72014-11-18 10:55:16 +00003297void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3298 HandleBinaryOp(instruction);
3299}
3300
3301void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3302 HandleBinaryOp(instruction);
3303}
3304
Alexandre Rames3e69f162014-12-10 10:36:50 +00003305void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3306 LOG(FATAL) << "Unreachable";
3307}
3308
3309void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3310 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3311}
3312
Alexandre Rames5319def2014-10-23 10:03:10 +01003313void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3315 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3316 if (location.IsStackSlot()) {
3317 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3318 } else if (location.IsDoubleStackSlot()) {
3319 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3320 }
3321 locations->SetOut(location);
3322}
3323
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003324void InstructionCodeGeneratorARM64::VisitParameterValue(
3325 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003326 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003327}
3328
3329void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3330 LocationSummary* locations =
3331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003332 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003333}
3334
3335void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3336 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3337 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003338}
3339
3340void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3341 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3342 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3343 locations->SetInAt(i, Location::Any());
3344 }
3345 locations->SetOut(Location::Any());
3346}
3347
3348void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003349 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003350 LOG(FATAL) << "Unreachable";
3351}
3352
Serban Constantinescu02164b32014-11-13 14:05:07 +00003353void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003354 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003355 LocationSummary::CallKind call_kind =
3356 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3358
3359 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003360 case Primitive::kPrimInt:
3361 case Primitive::kPrimLong:
3362 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003363 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3365 break;
3366
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003367 case Primitive::kPrimFloat:
3368 case Primitive::kPrimDouble: {
3369 InvokeRuntimeCallingConvention calling_convention;
3370 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3371 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3372 locations->SetOut(calling_convention.GetReturnLocation(type));
3373
3374 break;
3375 }
3376
Serban Constantinescu02164b32014-11-13 14:05:07 +00003377 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003378 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003379 }
3380}
3381
3382void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3383 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003384
Serban Constantinescu02164b32014-11-13 14:05:07 +00003385 switch (type) {
3386 case Primitive::kPrimInt:
3387 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003388 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003389 break;
3390 }
3391
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003392 case Primitive::kPrimFloat:
3393 case Primitive::kPrimDouble: {
3394 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3395 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003396 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003397 break;
3398 }
3399
Serban Constantinescu02164b32014-11-13 14:05:07 +00003400 default:
3401 LOG(FATAL) << "Unexpected rem type " << type;
3402 }
3403}
3404
Calin Juravle27df7582015-04-17 19:12:31 +01003405void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3406 memory_barrier->SetLocations(nullptr);
3407}
3408
3409void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3410 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3411}
3412
Alexandre Rames5319def2014-10-23 10:03:10 +01003413void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3415 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003416 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003417}
3418
3419void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003420 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003421 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003422}
3423
3424void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3425 instruction->SetLocations(nullptr);
3426}
3427
3428void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003429 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003430 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003431}
3432
Serban Constantinescu02164b32014-11-13 14:05:07 +00003433void LocationsBuilderARM64::VisitShl(HShl* shl) {
3434 HandleShift(shl);
3435}
3436
3437void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3438 HandleShift(shl);
3439}
3440
3441void LocationsBuilderARM64::VisitShr(HShr* shr) {
3442 HandleShift(shr);
3443}
3444
3445void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3446 HandleShift(shr);
3447}
3448
Alexandre Rames5319def2014-10-23 10:03:10 +01003449void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3451 Primitive::Type field_type = store->InputAt(1)->GetType();
3452 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003453 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003454 case Primitive::kPrimBoolean:
3455 case Primitive::kPrimByte:
3456 case Primitive::kPrimChar:
3457 case Primitive::kPrimShort:
3458 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003459 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003460 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3461 break;
3462
3463 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003464 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003465 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3466 break;
3467
3468 default:
3469 LOG(FATAL) << "Unimplemented local type " << field_type;
3470 }
3471}
3472
3473void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003474 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003475}
3476
3477void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003478 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003479}
3480
3481void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003482 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003483}
3484
Alexandre Rames67555f72014-11-18 10:55:16 +00003485void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003486 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003487}
3488
3489void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003490 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003491}
3492
3493void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003494 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003495}
3496
Alexandre Rames67555f72014-11-18 10:55:16 +00003497void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003498 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003499}
3500
3501void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3502 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3503}
3504
3505void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003506 HBasicBlock* block = instruction->GetBlock();
3507 if (block->GetLoopInformation() != nullptr) {
3508 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3509 // The back edge will generate the suspend check.
3510 return;
3511 }
3512 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3513 // The goto will generate the suspend check.
3514 return;
3515 }
3516 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003517}
3518
3519void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3520 temp->SetLocations(nullptr);
3521}
3522
3523void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3524 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003525 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003526}
3527
Alexandre Rames67555f72014-11-18 10:55:16 +00003528void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3529 LocationSummary* locations =
3530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3531 InvokeRuntimeCallingConvention calling_convention;
3532 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3533}
3534
3535void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3536 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003537 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003538 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003539}
3540
3541void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3544 Primitive::Type input_type = conversion->GetInputType();
3545 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003546 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003547 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3548 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3549 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3550 }
3551
Alexandre Rames542361f2015-01-29 16:57:31 +00003552 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003553 locations->SetInAt(0, Location::RequiresFpuRegister());
3554 } else {
3555 locations->SetInAt(0, Location::RequiresRegister());
3556 }
3557
Alexandre Rames542361f2015-01-29 16:57:31 +00003558 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3560 } else {
3561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3562 }
3563}
3564
3565void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3566 Primitive::Type result_type = conversion->GetResultType();
3567 Primitive::Type input_type = conversion->GetInputType();
3568
3569 DCHECK_NE(input_type, result_type);
3570
Alexandre Rames542361f2015-01-29 16:57:31 +00003571 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003572 int result_size = Primitive::ComponentSize(result_type);
3573 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003574 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003575 Register output = OutputRegister(conversion);
3576 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003577 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3578 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003579 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3580 // 'int' values are used directly as W registers, discarding the top
3581 // bits, so we don't need to sign-extend and can just perform a move.
3582 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3583 // top 32 bits of the target register. We theoretically could leave those
3584 // bits unchanged, but we would have to make sure that no code uses a
3585 // 32bit input value as a 64bit value assuming that the top 32 bits are
3586 // zero.
3587 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003588 } else if ((result_type == Primitive::kPrimChar) ||
3589 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3590 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003591 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003592 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003593 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003594 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003595 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003596 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003597 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3598 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003599 } else if (Primitive::IsFloatingPointType(result_type) &&
3600 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003601 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3602 } else {
3603 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3604 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003605 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003606}
Alexandre Rames67555f72014-11-18 10:55:16 +00003607
Serban Constantinescu02164b32014-11-13 14:05:07 +00003608void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3609 HandleShift(ushr);
3610}
3611
3612void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3613 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003614}
3615
3616void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3617 HandleBinaryOp(instruction);
3618}
3619
3620void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3621 HandleBinaryOp(instruction);
3622}
3623
Calin Juravleb1498f62015-02-16 13:13:29 +00003624void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3625 // Nothing to do, this should be removed during prepare for register allocator.
3626 UNUSED(instruction);
3627 LOG(FATAL) << "Unreachable";
3628}
3629
3630void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3631 // Nothing to do, this should be removed during prepare for register allocator.
3632 UNUSED(instruction);
3633 LOG(FATAL) << "Unreachable";
3634}
3635
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003636void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3637 DCHECK(codegen_->IsBaseline());
3638 LocationSummary* locations =
3639 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3640 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3641}
3642
3643void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3644 DCHECK(codegen_->IsBaseline());
3645 // Will be generated at use site.
3646}
3647
Mark Mendellfe57faa2015-09-18 09:26:15 -04003648// Simple implementation of packed switch - generate cascaded compare/jumps.
3649void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3650 LocationSummary* locations =
3651 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3652 locations->SetInAt(0, Location::RequiresRegister());
3653}
3654
3655void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3656 int32_t lower_bound = switch_instr->GetStartValue();
3657 int32_t num_entries = switch_instr->GetNumEntries();
3658 Register value_reg = InputRegisterAt(switch_instr, 0);
3659 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3660
3661 // Create a series of compare/jumps.
3662 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3663 for (int32_t i = 0; i < num_entries; i++) {
3664 int32_t case_value = lower_bound + i;
3665 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3666 if (case_value == 0) {
3667 __ Cbz(value_reg, succ);
3668 } else {
3669 __ Cmp(value_reg, vixl::Operand(case_value));
3670 __ B(eq, succ);
3671 }
3672 }
3673
3674 // And the default for any other value.
3675 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3676 __ B(codegen_->GetLabelOf(default_block));
3677 }
3678}
3679
Alexandre Rames67555f72014-11-18 10:55:16 +00003680#undef __
3681#undef QUICK_ENTRY_POINT
3682
Alexandre Rames5319def2014-10-23 10:03:10 +01003683} // namespace arm64
3684} // namespace art