blob: bbde7e8efdabe5f5b502e5bada0aeb5e5c7fc593 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080022#include "common_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000193 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
194 Location index_location,
195 Location length_location)
196 : instruction_(instruction),
197 index_location_(index_location),
198 length_location_(length_location) {}
199
Alexandre Rames5319def2014-10-23 10:03:10 +0100200
Alexandre Rames67555f72014-11-18 10:55:16 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000202 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100203 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100208 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames9931f312015-06-19 14:47:01 +0100215 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
216
Alexandre Rames5319def2014-10-23 10:03:10 +0100217 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000218 HBoundsCheck* const instruction_;
219 const Location index_location_;
220 const Location length_location_;
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
223};
224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
226 public:
227 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
231 __ Bind(GetEntryLabel());
232 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000233 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800234 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000235 }
236
Alexandre Rames9931f312015-06-19 14:47:01 +0100237 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
238
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 private:
240 HDivZeroCheck* const instruction_;
241 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
242};
243
244class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
245 public:
246 LoadClassSlowPathARM64(HLoadClass* cls,
247 HInstruction* at,
248 uint32_t dex_pc,
249 bool do_clinit)
250 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
251 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
252 }
253
254 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
255 LocationSummary* locations = at_->GetLocations();
256 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
257
258 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000259 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000260
261 InvokeRuntimeCallingConvention calling_convention;
262 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
264 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000265 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800266 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100267 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800268 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100269 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800270 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000271
272 // Move the class to the desired location.
273 Location out = locations->Out();
274 if (out.IsValid()) {
275 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
276 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000277 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000278 }
279
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000281 __ B(GetExitLabel());
282 }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
285
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 private:
287 // The class this slow path will load.
288 HLoadClass* const cls_;
289
290 // The instruction where this slow path is happening.
291 // (Might be the load class or an initialization check).
292 HInstruction* const at_;
293
294 // The dex PC of `at_`.
295 const uint32_t dex_pc_;
296
297 // Whether to initialize the class.
298 const bool do_clinit_;
299
300 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
301};
302
303class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
304 public:
305 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
306
307 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
308 LocationSummary* locations = instruction_->GetLocations();
309 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
310 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
311
312 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314
315 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800316 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000318 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100319 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000321 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000323 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324 __ B(GetExitLabel());
325 }
326
Alexandre Rames9931f312015-06-19 14:47:01 +0100327 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
328
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 private:
330 HLoadString* const instruction_;
331
332 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
333};
334
Alexandre Rames5319def2014-10-23 10:03:10 +0100335class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
336 public:
337 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
340 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100341 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000342 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000343 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800344 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100345 }
346
Alexandre Rames9931f312015-06-19 14:47:01 +0100347 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
348
Alexandre Rames5319def2014-10-23 10:03:10 +0100349 private:
350 HNullCheck* const instruction_;
351
352 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
353};
354
355class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
356 public:
357 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
358 HBasicBlock* successor)
359 : instruction_(instruction), successor_(successor) {}
360
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
362 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100363 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000364 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000366 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800367 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000368 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000369 if (successor_ == nullptr) {
370 __ B(GetReturnLabel());
371 } else {
372 __ B(arm64_codegen->GetLabelOf(successor_));
373 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 }
375
376 vixl::Label* GetReturnLabel() {
377 DCHECK(successor_ == nullptr);
378 return &return_label_;
379 }
380
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100381 HBasicBlock* GetSuccessor() const {
382 return successor_;
383 }
384
Alexandre Rames9931f312015-06-19 14:47:01 +0100385 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
386
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 private:
388 HSuspendCheck* const instruction_;
389 // If not null, the block to branch to after the suspend check.
390 HBasicBlock* const successor_;
391
392 // If `successor_` is null, the label to branch to after the suspend check.
393 vixl::Label return_label_;
394
395 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
396};
397
Alexandre Rames67555f72014-11-18 10:55:16 +0000398class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
399 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000400 TypeCheckSlowPathARM64(HInstruction* instruction,
401 Location class_to_check,
402 Location object_class,
403 uint32_t dex_pc)
404 : instruction_(instruction),
405 class_to_check_(class_to_check),
406 object_class_(object_class),
407 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000408
409 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000410 LocationSummary* locations = instruction_->GetLocations();
411 DCHECK(instruction_->IsCheckCast()
412 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
413 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
414
Alexandre Rames67555f72014-11-18 10:55:16 +0000415 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000416 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000417
418 // We're moving two locations to locations that could overlap, so we need a parallel
419 // move resolver.
420 InvokeRuntimeCallingConvention calling_convention;
421 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100422 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
423 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000424
425 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000426 arm64_codegen->InvokeRuntime(
427 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000428 Primitive::Type ret_type = instruction_->GetType();
429 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
430 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800431 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
432 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433 } else {
434 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000435 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800436 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000437 }
438
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000439 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000440 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 }
442
Alexandre Rames9931f312015-06-19 14:47:01 +0100443 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
444
Alexandre Rames67555f72014-11-18 10:55:16 +0000445 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446 HInstruction* const instruction_;
447 const Location class_to_check_;
448 const Location object_class_;
449 uint32_t dex_pc_;
450
Alexandre Rames67555f72014-11-18 10:55:16 +0000451 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
452};
453
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700454class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
455 public:
456 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
457 : instruction_(instruction) {}
458
459 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
460 __ Bind(GetEntryLabel());
461 SaveLiveRegisters(codegen, instruction_->GetLocations());
462 DCHECK(instruction_->IsDeoptimize());
463 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
464 uint32_t dex_pc = deoptimize->GetDexPc();
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
466 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
467 }
468
Alexandre Rames9931f312015-06-19 14:47:01 +0100469 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
470
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700471 private:
472 HInstruction* const instruction_;
473 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
474};
475
Alexandre Rames5319def2014-10-23 10:03:10 +0100476#undef __
477
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100478Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100479 Location next_location;
480 if (type == Primitive::kPrimVoid) {
481 LOG(FATAL) << "Unreachable type " << type;
482 }
483
Alexandre Rames542361f2015-01-29 16:57:31 +0000484 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100485 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
486 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000487 } else if (!Primitive::IsFloatingPointType(type) &&
488 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000489 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
490 } else {
491 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000492 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
493 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 }
495
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000496 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000497 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100498 return next_location;
499}
500
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100501Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100502 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100503}
504
Serban Constantinescu579885a2015-02-22 20:51:33 +0000505CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
506 const Arm64InstructionSetFeatures& isa_features,
507 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100508 : CodeGenerator(graph,
509 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000510 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000511 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000512 callee_saved_core_registers.list(),
513 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000514 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 block_labels_(nullptr),
516 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000517 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000518 move_resolver_(graph->GetArena(), this),
519 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000520 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000521 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000522}
Alexandre Rames5319def2014-10-23 10:03:10 +0100523
Alexandre Rames67555f72014-11-18 10:55:16 +0000524#undef __
525#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100526
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000527void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
528 // Ensure we emit the literal pool.
529 __ FinalizeCode();
530 CodeGenerator::Finalize(allocator);
531}
532
Zheng Xuad4450e2015-04-17 18:48:56 +0800533void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
534 // Note: There are 6 kinds of moves:
535 // 1. constant -> GPR/FPR (non-cycle)
536 // 2. constant -> stack (non-cycle)
537 // 3. GPR/FPR -> GPR/FPR
538 // 4. GPR/FPR -> stack
539 // 5. stack -> GPR/FPR
540 // 6. stack -> stack (non-cycle)
541 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
542 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
543 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
544 // dependency.
545 vixl_temps_.Open(GetVIXLAssembler());
546}
547
548void ParallelMoveResolverARM64::FinishEmitNativeCode() {
549 vixl_temps_.Close();
550}
551
552Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
553 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
554 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
555 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
556 Location scratch = GetScratchLocation(kind);
557 if (!scratch.Equals(Location::NoLocation())) {
558 return scratch;
559 }
560 // Allocate from VIXL temp registers.
561 if (kind == Location::kRegister) {
562 scratch = LocationFrom(vixl_temps_.AcquireX());
563 } else {
564 DCHECK(kind == Location::kFpuRegister);
565 scratch = LocationFrom(vixl_temps_.AcquireD());
566 }
567 AddScratchLocation(scratch);
568 return scratch;
569}
570
571void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
572 if (loc.IsRegister()) {
573 vixl_temps_.Release(XRegisterFrom(loc));
574 } else {
575 DCHECK(loc.IsFpuRegister());
576 vixl_temps_.Release(DRegisterFrom(loc));
577 }
578 RemoveScratchLocation(loc);
579}
580
Alexandre Rames3e69f162014-12-10 10:36:50 +0000581void ParallelMoveResolverARM64::EmitMove(size_t index) {
582 MoveOperands* move = moves_.Get(index);
583 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
584}
585
Alexandre Rames5319def2014-10-23 10:03:10 +0100586void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100587 MacroAssembler* masm = GetVIXLAssembler();
588 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000589 __ Bind(&frame_entry_label_);
590
Serban Constantinescu02164b32014-11-13 14:05:07 +0000591 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
592 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100593 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000594 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000595 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000596 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000597 __ Ldr(wzr, MemOperand(temp, 0));
598 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000599 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100600
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000601 if (!HasEmptyFrame()) {
602 int frame_size = GetFrameSize();
603 // Stack layout:
604 // sp[frame_size - 8] : lr.
605 // ... : other preserved core registers.
606 // ... : other preserved fp registers.
607 // ... : reserved frame space.
608 // sp[0] : current method.
609 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100610 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800611 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
612 frame_size - GetCoreSpillSize());
613 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
614 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000615 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100616}
617
618void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100619 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100620 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000621 if (!HasEmptyFrame()) {
622 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800623 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
624 frame_size - FrameEntrySpillSize());
625 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
626 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000627 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100628 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000629 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100630 __ Ret();
631 GetAssembler()->cfi().RestoreState();
632 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100633}
634
Zheng Xuda403092015-04-24 17:35:39 +0800635vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
636 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
637 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
638 core_spill_mask_);
639}
640
641vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
642 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
643 GetNumberOfFloatingPointRegisters()));
644 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
645 fpu_spill_mask_);
646}
647
Alexandre Rames5319def2014-10-23 10:03:10 +0100648void CodeGeneratorARM64::Bind(HBasicBlock* block) {
649 __ Bind(GetLabelOf(block));
650}
651
Alexandre Rames5319def2014-10-23 10:03:10 +0100652void CodeGeneratorARM64::Move(HInstruction* instruction,
653 Location location,
654 HInstruction* move_for) {
655 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100656 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000657 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100658
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100659 if (instruction->IsFakeString()) {
660 // The fake string is an alias for null.
661 DCHECK(IsBaseline());
662 instruction = locations->Out().GetConstant();
663 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
664 }
665
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100666 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700667 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100668 } else if (locations != nullptr && locations->Out().Equals(location)) {
669 return;
670 } else if (instruction->IsIntConstant()
671 || instruction->IsLongConstant()
672 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000673 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100674 if (location.IsRegister()) {
675 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000676 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100677 (instruction->IsLongConstant() && dst.Is64Bits()));
678 __ Mov(dst, value);
679 } else {
680 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000681 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000682 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
683 ? temps.AcquireW()
684 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100685 __ Mov(temp, value);
686 __ Str(temp, StackOperandFrom(location));
687 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000688 } else if (instruction->IsTemporary()) {
689 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000690 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 } else if (instruction->IsLoadLocal()) {
692 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000693 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000694 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000695 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000696 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100697 }
698
699 } else {
700 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000701 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100702 }
703}
704
Alexandre Rames5319def2014-10-23 10:03:10 +0100705Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
706 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000707
Alexandre Rames5319def2014-10-23 10:03:10 +0100708 switch (type) {
709 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000710 case Primitive::kPrimInt:
711 case Primitive::kPrimFloat:
712 return Location::StackSlot(GetStackSlot(load->GetLocal()));
713
714 case Primitive::kPrimLong:
715 case Primitive::kPrimDouble:
716 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
717
Alexandre Rames5319def2014-10-23 10:03:10 +0100718 case Primitive::kPrimBoolean:
719 case Primitive::kPrimByte:
720 case Primitive::kPrimChar:
721 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100722 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100723 LOG(FATAL) << "Unexpected type " << type;
724 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000725
Alexandre Rames5319def2014-10-23 10:03:10 +0100726 LOG(FATAL) << "Unreachable";
727 return Location::NoLocation();
728}
729
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100730void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000731 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000733 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100734 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100735 if (value_can_be_null) {
736 __ Cbz(value, &done);
737 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100738 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
739 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000740 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100741 if (value_can_be_null) {
742 __ Bind(&done);
743 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100744}
745
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000746void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
747 // Blocked core registers:
748 // lr : Runtime reserved.
749 // tr : Runtime reserved.
750 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
751 // ip1 : VIXL core temp.
752 // ip0 : VIXL core temp.
753 //
754 // Blocked fp registers:
755 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100756 CPURegList reserved_core_registers = vixl_reserved_core_registers;
757 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100758 while (!reserved_core_registers.IsEmpty()) {
759 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
760 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000761
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000762 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800763 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000764 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
765 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000766
767 if (is_baseline) {
768 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
769 while (!reserved_core_baseline_registers.IsEmpty()) {
770 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
771 }
772
773 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
774 while (!reserved_fp_baseline_registers.IsEmpty()) {
775 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
776 }
777 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100778}
779
780Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
781 if (type == Primitive::kPrimVoid) {
782 LOG(FATAL) << "Unreachable type " << type;
783 }
784
Alexandre Rames542361f2015-01-29 16:57:31 +0000785 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000786 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
787 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100788 return Location::FpuRegisterLocation(reg);
789 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000790 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
791 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100792 return Location::RegisterLocation(reg);
793 }
794}
795
Alexandre Rames3e69f162014-12-10 10:36:50 +0000796size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
797 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
798 __ Str(reg, MemOperand(sp, stack_index));
799 return kArm64WordSize;
800}
801
802size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
803 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
804 __ Ldr(reg, MemOperand(sp, stack_index));
805 return kArm64WordSize;
806}
807
808size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
809 FPRegister reg = FPRegister(reg_id, kDRegSize);
810 __ Str(reg, MemOperand(sp, stack_index));
811 return kArm64WordSize;
812}
813
814size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
815 FPRegister reg = FPRegister(reg_id, kDRegSize);
816 __ Ldr(reg, MemOperand(sp, stack_index));
817 return kArm64WordSize;
818}
819
Alexandre Rames5319def2014-10-23 10:03:10 +0100820void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100821 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100822}
823
824void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100825 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100826}
827
Alexandre Rames67555f72014-11-18 10:55:16 +0000828void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000829 if (constant->IsIntConstant()) {
830 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
831 } else if (constant->IsLongConstant()) {
832 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
833 } else if (constant->IsNullConstant()) {
834 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000835 } else if (constant->IsFloatConstant()) {
836 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
837 } else {
838 DCHECK(constant->IsDoubleConstant());
839 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
840 }
841}
842
Alexandre Rames3e69f162014-12-10 10:36:50 +0000843
844static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
845 DCHECK(constant.IsConstant());
846 HConstant* cst = constant.GetConstant();
847 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 // Null is mapped to a core W register, which we associate with kPrimInt.
849 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000850 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
851 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
852 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
853}
854
855void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000856 if (source.Equals(destination)) {
857 return;
858 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000859
860 // A valid move can always be inferred from the destination and source
861 // locations. When moving from and to a register, the argument type can be
862 // used to generate 32bit instead of 64bit moves. In debug mode we also
863 // checks the coherency of the locations and the type.
864 bool unspecified_type = (type == Primitive::kPrimVoid);
865
866 if (destination.IsRegister() || destination.IsFpuRegister()) {
867 if (unspecified_type) {
868 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
869 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000870 (src_cst != nullptr && (src_cst->IsIntConstant()
871 || src_cst->IsFloatConstant()
872 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000873 // For stack slots and 32bit constants, a 64bit type is appropriate.
874 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000875 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000876 // If the source is a double stack slot or a 64bit constant, a 64bit
877 // type is appropriate. Else the source is a register, and since the
878 // type has not been specified, we chose a 64bit type to force a 64bit
879 // move.
880 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000881 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000882 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
884 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000885 CPURegister dst = CPURegisterFrom(destination, type);
886 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
887 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
888 __ Ldr(dst, StackOperandFrom(source));
889 } else if (source.IsConstant()) {
890 DCHECK(CoherentConstantAndType(source, type));
891 MoveConstant(dst, source.GetConstant());
892 } else {
893 if (destination.IsRegister()) {
894 __ Mov(Register(dst), RegisterFrom(source, type));
895 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800896 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000897 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
898 }
899 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000900 } else { // The destination is not a register. It must be a stack slot.
901 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
902 if (source.IsRegister() || source.IsFpuRegister()) {
903 if (unspecified_type) {
904 if (source.IsRegister()) {
905 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
906 } else {
907 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
908 }
909 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000910 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
911 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000912 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
913 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100914 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000915 UseScratchRegisterScope temps(GetVIXLAssembler());
916 HConstant* src_cst = source.GetConstant();
917 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000918 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000919 temp = temps.AcquireW();
920 } else if (src_cst->IsLongConstant()) {
921 temp = temps.AcquireX();
922 } else if (src_cst->IsFloatConstant()) {
923 temp = temps.AcquireS();
924 } else {
925 DCHECK(src_cst->IsDoubleConstant());
926 temp = temps.AcquireD();
927 }
928 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000929 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000930 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000931 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000932 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000933 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000934 // There is generally less pressure on FP registers.
935 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000936 __ Ldr(temp, StackOperandFrom(source));
937 __ Str(temp, StackOperandFrom(destination));
938 }
939 }
940}
941
942void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000943 CPURegister dst,
944 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000945 switch (type) {
946 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000947 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000948 break;
949 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000950 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000951 break;
952 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000953 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000954 break;
955 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000956 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000957 break;
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot:
960 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000961 case Primitive::kPrimFloat:
962 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000963 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000964 __ Ldr(dst, src);
965 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000966 case Primitive::kPrimVoid:
967 LOG(FATAL) << "Unreachable type " << type;
968 }
969}
970
Calin Juravle77520bc2015-01-12 18:45:46 +0000971void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000972 CPURegister dst,
973 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100974 MacroAssembler* masm = GetVIXLAssembler();
975 BlockPoolsScope block_pools(masm);
976 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000978 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000979
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000980 DCHECK(!src.IsPreIndex());
981 DCHECK(!src.IsPostIndex());
982
983 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800984 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000985 MemOperand base = MemOperand(temp_base);
986 switch (type) {
987 case Primitive::kPrimBoolean:
988 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000989 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990 break;
991 case Primitive::kPrimByte:
992 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000993 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
995 break;
996 case Primitive::kPrimChar:
997 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000998 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 break;
1000 case Primitive::kPrimShort:
1001 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001002 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001003 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1004 break;
1005 case Primitive::kPrimInt:
1006 case Primitive::kPrimNot:
1007 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001008 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001010 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001011 break;
1012 case Primitive::kPrimFloat:
1013 case Primitive::kPrimDouble: {
1014 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001015 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016
1017 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1018 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001019 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001020 __ Fmov(FPRegister(dst), temp);
1021 break;
1022 }
1023 case Primitive::kPrimVoid:
1024 LOG(FATAL) << "Unreachable type " << type;
1025 }
1026}
1027
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001028void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001029 CPURegister src,
1030 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001031 switch (type) {
1032 case Primitive::kPrimBoolean:
1033 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001035 break;
1036 case Primitive::kPrimChar:
1037 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001038 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001039 break;
1040 case Primitive::kPrimInt:
1041 case Primitive::kPrimNot:
1042 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001043 case Primitive::kPrimFloat:
1044 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001045 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001046 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001047 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001048 case Primitive::kPrimVoid:
1049 LOG(FATAL) << "Unreachable type " << type;
1050 }
1051}
1052
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001053void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1054 CPURegister src,
1055 const MemOperand& dst) {
1056 UseScratchRegisterScope temps(GetVIXLAssembler());
1057 Register temp_base = temps.AcquireX();
1058
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001059 DCHECK(!dst.IsPreIndex());
1060 DCHECK(!dst.IsPostIndex());
1061
1062 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001063 Operand op = OperandFromMemOperand(dst);
1064 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001065 MemOperand base = MemOperand(temp_base);
1066 switch (type) {
1067 case Primitive::kPrimBoolean:
1068 case Primitive::kPrimByte:
1069 __ Stlrb(Register(src), base);
1070 break;
1071 case Primitive::kPrimChar:
1072 case Primitive::kPrimShort:
1073 __ Stlrh(Register(src), base);
1074 break;
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot:
1077 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001078 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001079 __ Stlr(Register(src), base);
1080 break;
1081 case Primitive::kPrimFloat:
1082 case Primitive::kPrimDouble: {
1083 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001084 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001085
1086 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1087 __ Fmov(temp, FPRegister(src));
1088 __ Stlr(temp, base);
1089 break;
1090 }
1091 case Primitive::kPrimVoid:
1092 LOG(FATAL) << "Unreachable type " << type;
1093 }
1094}
1095
Alexandre Rames67555f72014-11-18 10:55:16 +00001096void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1097 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001098 uint32_t dex_pc,
1099 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001100 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1102 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001103 RecordPcInfo(instruction, dex_pc, slow_path);
1104 DCHECK(instruction->IsSuspendCheck()
1105 || instruction->IsBoundsCheck()
1106 || instruction->IsNullCheck()
1107 || instruction->IsDivZeroCheck()
1108 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001109}
1110
1111void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1112 vixl::Register class_reg) {
1113 UseScratchRegisterScope temps(GetVIXLAssembler());
1114 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001115 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001116 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001117
Serban Constantinescu02164b32014-11-13 14:05:07 +00001118 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001119 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001120 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1121 __ Add(temp, class_reg, status_offset);
1122 __ Ldar(temp, HeapOperand(temp));
1123 __ Cmp(temp, mirror::Class::kStatusInitialized);
1124 __ B(lt, slow_path->GetEntryLabel());
1125 } else {
1126 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1127 __ Cmp(temp, mirror::Class::kStatusInitialized);
1128 __ B(lt, slow_path->GetEntryLabel());
1129 __ Dmb(InnerShareable, BarrierReads);
1130 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001131 __ Bind(slow_path->GetExitLabel());
1132}
Alexandre Rames5319def2014-10-23 10:03:10 +01001133
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001134void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1135 BarrierType type = BarrierAll;
1136
1137 switch (kind) {
1138 case MemBarrierKind::kAnyAny:
1139 case MemBarrierKind::kAnyStore: {
1140 type = BarrierAll;
1141 break;
1142 }
1143 case MemBarrierKind::kLoadAny: {
1144 type = BarrierReads;
1145 break;
1146 }
1147 case MemBarrierKind::kStoreStore: {
1148 type = BarrierWrites;
1149 break;
1150 }
1151 default:
1152 LOG(FATAL) << "Unexpected memory barrier " << kind;
1153 }
1154 __ Dmb(InnerShareable, type);
1155}
1156
Serban Constantinescu02164b32014-11-13 14:05:07 +00001157void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1158 HBasicBlock* successor) {
1159 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001160 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1161 if (slow_path == nullptr) {
1162 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1163 instruction->SetSlowPath(slow_path);
1164 codegen_->AddSlowPath(slow_path);
1165 if (successor != nullptr) {
1166 DCHECK(successor->IsLoopHeader());
1167 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1168 }
1169 } else {
1170 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1171 }
1172
Serban Constantinescu02164b32014-11-13 14:05:07 +00001173 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1174 Register temp = temps.AcquireW();
1175
1176 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1177 if (successor == nullptr) {
1178 __ Cbnz(temp, slow_path->GetEntryLabel());
1179 __ Bind(slow_path->GetReturnLabel());
1180 } else {
1181 __ Cbz(temp, codegen_->GetLabelOf(successor));
1182 __ B(slow_path->GetEntryLabel());
1183 // slow_path will return to GetLabelOf(successor).
1184 }
1185}
1186
Alexandre Rames5319def2014-10-23 10:03:10 +01001187InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1188 CodeGeneratorARM64* codegen)
1189 : HGraphVisitor(graph),
1190 assembler_(codegen->GetAssembler()),
1191 codegen_(codegen) {}
1192
1193#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001194 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001195
1196#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1197
1198enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001199 // Using a base helps identify when we hit such breakpoints.
1200 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001201#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1202 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1203#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1204};
1205
1206#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1207 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001208 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001209 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1210 } \
1211 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1213 locations->SetOut(Location::Any()); \
1214 }
1215 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1216#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1217
1218#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001219#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001220
Alexandre Rames67555f72014-11-18 10:55:16 +00001221void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001222 DCHECK_EQ(instr->InputCount(), 2U);
1223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1224 Primitive::Type type = instr->GetResultType();
1225 switch (type) {
1226 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001227 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001228 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001229 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001231 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001232
1233 case Primitive::kPrimFloat:
1234 case Primitive::kPrimDouble:
1235 locations->SetInAt(0, Location::RequiresFpuRegister());
1236 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001237 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001238 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001239
Alexandre Rames5319def2014-10-23 10:03:10 +01001240 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001241 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001242 }
1243}
1244
Alexandre Rames09a99962015-04-15 11:47:56 +01001245void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1246 LocationSummary* locations =
1247 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1248 locations->SetInAt(0, Location::RequiresRegister());
1249 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1250 locations->SetOut(Location::RequiresFpuRegister());
1251 } else {
1252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1253 }
1254}
1255
1256void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1257 const FieldInfo& field_info) {
1258 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001259 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001260 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001261
1262 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1263 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1264
1265 if (field_info.IsVolatile()) {
1266 if (use_acquire_release) {
1267 // NB: LoadAcquire will record the pc info if needed.
1268 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1269 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001270 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001271 codegen_->MaybeRecordImplicitNullCheck(instruction);
1272 // For IRIW sequential consistency kLoadAny is not sufficient.
1273 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1274 }
1275 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001276 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001277 codegen_->MaybeRecordImplicitNullCheck(instruction);
1278 }
Roland Levillain4d027112015-07-01 15:41:14 +01001279
1280 if (field_type == Primitive::kPrimNot) {
1281 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1282 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001283}
1284
1285void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1286 LocationSummary* locations =
1287 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1288 locations->SetInAt(0, Location::RequiresRegister());
1289 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1290 locations->SetInAt(1, Location::RequiresFpuRegister());
1291 } else {
1292 locations->SetInAt(1, Location::RequiresRegister());
1293 }
1294}
1295
1296void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001297 const FieldInfo& field_info,
1298 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001299 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001300 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001301
1302 Register obj = InputRegisterAt(instruction, 0);
1303 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001304 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001305 Offset offset = field_info.GetFieldOffset();
1306 Primitive::Type field_type = field_info.GetFieldType();
1307 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1308
Roland Levillain4d027112015-07-01 15:41:14 +01001309 {
1310 // We use a block to end the scratch scope before the write barrier, thus
1311 // freeing the temporary registers so they can be used in `MarkGCCard`.
1312 UseScratchRegisterScope temps(GetVIXLAssembler());
1313
1314 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1315 DCHECK(value.IsW());
1316 Register temp = temps.AcquireW();
1317 __ Mov(temp, value.W());
1318 GetAssembler()->PoisonHeapReference(temp.W());
1319 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001320 }
Roland Levillain4d027112015-07-01 15:41:14 +01001321
1322 if (field_info.IsVolatile()) {
1323 if (use_acquire_release) {
1324 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1325 codegen_->MaybeRecordImplicitNullCheck(instruction);
1326 } else {
1327 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1328 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1329 codegen_->MaybeRecordImplicitNullCheck(instruction);
1330 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1331 }
1332 } else {
1333 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1334 codegen_->MaybeRecordImplicitNullCheck(instruction);
1335 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001336 }
1337
1338 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001339 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001340 }
1341}
1342
Alexandre Rames67555f72014-11-18 10:55:16 +00001343void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001344 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001345
1346 switch (type) {
1347 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001348 case Primitive::kPrimLong: {
1349 Register dst = OutputRegister(instr);
1350 Register lhs = InputRegisterAt(instr, 0);
1351 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 if (instr->IsAdd()) {
1353 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001354 } else if (instr->IsAnd()) {
1355 __ And(dst, lhs, rhs);
1356 } else if (instr->IsOr()) {
1357 __ Orr(dst, lhs, rhs);
1358 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001359 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001360 } else {
1361 DCHECK(instr->IsXor());
1362 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001363 }
1364 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001365 }
1366 case Primitive::kPrimFloat:
1367 case Primitive::kPrimDouble: {
1368 FPRegister dst = OutputFPRegister(instr);
1369 FPRegister lhs = InputFPRegisterAt(instr, 0);
1370 FPRegister rhs = InputFPRegisterAt(instr, 1);
1371 if (instr->IsAdd()) {
1372 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001373 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001374 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001375 } else {
1376 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001377 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001378 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001379 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001381 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001382 }
1383}
1384
Serban Constantinescu02164b32014-11-13 14:05:07 +00001385void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1386 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1387
1388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1389 Primitive::Type type = instr->GetResultType();
1390 switch (type) {
1391 case Primitive::kPrimInt:
1392 case Primitive::kPrimLong: {
1393 locations->SetInAt(0, Location::RequiresRegister());
1394 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1395 locations->SetOut(Location::RequiresRegister());
1396 break;
1397 }
1398 default:
1399 LOG(FATAL) << "Unexpected shift type " << type;
1400 }
1401}
1402
1403void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1404 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1405
1406 Primitive::Type type = instr->GetType();
1407 switch (type) {
1408 case Primitive::kPrimInt:
1409 case Primitive::kPrimLong: {
1410 Register dst = OutputRegister(instr);
1411 Register lhs = InputRegisterAt(instr, 0);
1412 Operand rhs = InputOperandAt(instr, 1);
1413 if (rhs.IsImmediate()) {
1414 uint32_t shift_value = (type == Primitive::kPrimInt)
1415 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1416 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1417 if (instr->IsShl()) {
1418 __ Lsl(dst, lhs, shift_value);
1419 } else if (instr->IsShr()) {
1420 __ Asr(dst, lhs, shift_value);
1421 } else {
1422 __ Lsr(dst, lhs, shift_value);
1423 }
1424 } else {
1425 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1426
1427 if (instr->IsShl()) {
1428 __ Lsl(dst, lhs, rhs_reg);
1429 } else if (instr->IsShr()) {
1430 __ Asr(dst, lhs, rhs_reg);
1431 } else {
1432 __ Lsr(dst, lhs, rhs_reg);
1433 }
1434 }
1435 break;
1436 }
1437 default:
1438 LOG(FATAL) << "Unexpected shift operation type " << type;
1439 }
1440}
1441
Alexandre Rames5319def2014-10-23 10:03:10 +01001442void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001443 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001444}
1445
1446void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001447 HandleBinaryOp(instruction);
1448}
1449
1450void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1451 HandleBinaryOp(instruction);
1452}
1453
1454void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1455 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001456}
1457
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001458void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1459 LocationSummary* locations =
1460 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1461 locations->SetInAt(0, Location::RequiresRegister());
1462 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001463 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1464 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1465 } else {
1466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1467 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001468}
1469
1470void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1471 LocationSummary* locations = instruction->GetLocations();
1472 Primitive::Type type = instruction->GetType();
1473 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001474 Location index = locations->InAt(1);
1475 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001476 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001477 MacroAssembler* masm = GetVIXLAssembler();
1478 UseScratchRegisterScope temps(masm);
1479 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001480
1481 if (index.IsConstant()) {
1482 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001483 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001484 } else {
1485 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001486 __ Add(temp, obj, offset);
1487 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001488 }
1489
Alexandre Rames67555f72014-11-18 10:55:16 +00001490 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001491 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001492
1493 if (type == Primitive::kPrimNot) {
1494 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1495 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001496}
1497
Alexandre Rames5319def2014-10-23 10:03:10 +01001498void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1500 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001501 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001502}
1503
1504void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001505 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001506 __ Ldr(OutputRegister(instruction),
1507 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001508 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001509}
1510
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001511void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001512 if (instruction->NeedsTypeCheck()) {
1513 LocationSummary* locations =
1514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001515 InvokeRuntimeCallingConvention calling_convention;
1516 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1517 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1518 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1519 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001520 LocationSummary* locations =
1521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001524 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1525 locations->SetInAt(2, Location::RequiresFpuRegister());
1526 } else {
1527 locations->SetInAt(2, Location::RequiresRegister());
1528 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001529 }
1530}
1531
1532void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1533 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001534 LocationSummary* locations = instruction->GetLocations();
1535 bool needs_runtime_call = locations->WillCall();
1536
1537 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001538 // Note: if heap poisoning is enabled, pAputObject takes cares
1539 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001540 codegen_->InvokeRuntime(
1541 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001542 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001543 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001544 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001545 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001546 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001547 Location index = locations->InAt(1);
1548 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001549 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001550 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001551 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001552 {
1553 // We use a block to end the scratch scope before the write barrier, thus
1554 // freeing the temporary registers so they can be used in `MarkGCCard`.
1555 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001556
Roland Levillain4d027112015-07-01 15:41:14 +01001557 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1558 DCHECK(value.IsW());
1559 Register temp = temps.AcquireW();
1560 __ Mov(temp, value.W());
1561 GetAssembler()->PoisonHeapReference(temp.W());
1562 source = temp;
1563 }
1564
Alexandre Rames97833a02015-04-16 15:07:12 +01001565 if (index.IsConstant()) {
1566 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1567 destination = HeapOperand(obj, offset);
1568 } else {
1569 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001570 __ Add(temp, obj, offset);
1571 destination = HeapOperand(temp,
1572 XRegisterFrom(index),
1573 LSL,
1574 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001575 }
1576
Roland Levillain4d027112015-07-01 15:41:14 +01001577 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001578 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001579 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001580 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001581 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001582 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001583 }
1584}
1585
Alexandre Rames67555f72014-11-18 10:55:16 +00001586void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1587 LocationSummary* locations =
1588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1589 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001590 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001591 if (instruction->HasUses()) {
1592 locations->SetOut(Location::SameAsFirstInput());
1593 }
1594}
1595
1596void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001597 LocationSummary* locations = instruction->GetLocations();
1598 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1599 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001600 codegen_->AddSlowPath(slow_path);
1601
1602 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1603 __ B(slow_path->GetEntryLabel(), hs);
1604}
1605
1606void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1608 instruction, LocationSummary::kCallOnSlowPath);
1609 locations->SetInAt(0, Location::RequiresRegister());
1610 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001611 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001612}
1613
1614void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001615 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001616 Register obj = InputRegisterAt(instruction, 0);;
1617 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001618 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001619
Alexandre Rames3e69f162014-12-10 10:36:50 +00001620 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1621 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001622 codegen_->AddSlowPath(slow_path);
1623
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001624 // Avoid null check if we know obj is not null.
1625 if (instruction->MustDoNullCheck()) {
1626 __ Cbz(obj, slow_path->GetExitLabel());
1627 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001628 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001629 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001630 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001631 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001632 // The checkcast succeeds if the classes are equal (fast path).
1633 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001634 __ B(ne, slow_path->GetEntryLabel());
1635 __ Bind(slow_path->GetExitLabel());
1636}
1637
1638void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1639 LocationSummary* locations =
1640 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1641 locations->SetInAt(0, Location::RequiresRegister());
1642 if (check->HasUses()) {
1643 locations->SetOut(Location::SameAsFirstInput());
1644 }
1645}
1646
1647void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1648 // We assume the class is not null.
1649 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1650 check->GetLoadClass(), check, check->GetDexPc(), true);
1651 codegen_->AddSlowPath(slow_path);
1652 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1653}
1654
Roland Levillain7f63c522015-07-13 15:54:55 +00001655static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1656 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1657 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1658}
1659
Serban Constantinescu02164b32014-11-13 14:05:07 +00001660void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001661 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001662 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1663 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001664 switch (in_type) {
1665 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001666 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001667 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001668 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1669 break;
1670 }
1671 case Primitive::kPrimFloat:
1672 case Primitive::kPrimDouble: {
1673 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001674 locations->SetInAt(1,
1675 IsFloatingPointZeroConstant(compare->InputAt(1))
1676 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1677 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001678 locations->SetOut(Location::RequiresRegister());
1679 break;
1680 }
1681 default:
1682 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1683 }
1684}
1685
1686void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1687 Primitive::Type in_type = compare->InputAt(0)->GetType();
1688
1689 // 0 if: left == right
1690 // 1 if: left > right
1691 // -1 if: left < right
1692 switch (in_type) {
1693 case Primitive::kPrimLong: {
1694 Register result = OutputRegister(compare);
1695 Register left = InputRegisterAt(compare, 0);
1696 Operand right = InputOperandAt(compare, 1);
1697
1698 __ Cmp(left, right);
1699 __ Cset(result, ne);
1700 __ Cneg(result, result, lt);
1701 break;
1702 }
1703 case Primitive::kPrimFloat:
1704 case Primitive::kPrimDouble: {
1705 Register result = OutputRegister(compare);
1706 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001707 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001708 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1709 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001710 __ Fcmp(left, 0.0);
1711 } else {
1712 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1713 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001714 if (compare->IsGtBias()) {
1715 __ Cset(result, ne);
1716 } else {
1717 __ Csetm(result, ne);
1718 }
1719 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001720 break;
1721 }
1722 default:
1723 LOG(FATAL) << "Unimplemented compare type " << in_type;
1724 }
1725}
1726
1727void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001729
1730 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1731 locations->SetInAt(0, Location::RequiresFpuRegister());
1732 locations->SetInAt(1,
1733 IsFloatingPointZeroConstant(instruction->InputAt(1))
1734 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1735 : Location::RequiresFpuRegister());
1736 } else {
1737 // Integer cases.
1738 locations->SetInAt(0, Location::RequiresRegister());
1739 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1740 }
1741
Alexandre Rames5319def2014-10-23 10:03:10 +01001742 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001743 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001744 }
1745}
1746
1747void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1748 if (!instruction->NeedsMaterialization()) {
1749 return;
1750 }
1751
1752 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001753 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001754 IfCondition if_cond = instruction->GetCondition();
1755 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001756
Roland Levillain7f63c522015-07-13 15:54:55 +00001757 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1758 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1759 if (locations->InAt(1).IsConstant()) {
1760 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1761 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1762 __ Fcmp(lhs, 0.0);
1763 } else {
1764 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1765 }
1766 __ Cset(res, arm64_cond);
1767 if (instruction->IsFPConditionTrueIfNaN()) {
1768 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1769 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1770 } else if (instruction->IsFPConditionFalseIfNaN()) {
1771 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1772 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1773 }
1774 } else {
1775 // Integer cases.
1776 Register lhs = InputRegisterAt(instruction, 0);
1777 Operand rhs = InputOperandAt(instruction, 1);
1778 __ Cmp(lhs, rhs);
1779 __ Cset(res, arm64_cond);
1780 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001781}
1782
1783#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1784 M(Equal) \
1785 M(NotEqual) \
1786 M(LessThan) \
1787 M(LessThanOrEqual) \
1788 M(GreaterThan) \
1789 M(GreaterThanOrEqual)
1790#define DEFINE_CONDITION_VISITORS(Name) \
1791void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1792void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1793FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001794#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001795#undef FOR_EACH_CONDITION_INSTRUCTION
1796
Zheng Xuc6667102015-05-15 16:08:45 +08001797void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1798 DCHECK(instruction->IsDiv() || instruction->IsRem());
1799
1800 LocationSummary* locations = instruction->GetLocations();
1801 Location second = locations->InAt(1);
1802 DCHECK(second.IsConstant());
1803
1804 Register out = OutputRegister(instruction);
1805 Register dividend = InputRegisterAt(instruction, 0);
1806 int64_t imm = Int64FromConstant(second.GetConstant());
1807 DCHECK(imm == 1 || imm == -1);
1808
1809 if (instruction->IsRem()) {
1810 __ Mov(out, 0);
1811 } else {
1812 if (imm == 1) {
1813 __ Mov(out, dividend);
1814 } else {
1815 __ Neg(out, dividend);
1816 }
1817 }
1818}
1819
1820void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1821 DCHECK(instruction->IsDiv() || instruction->IsRem());
1822
1823 LocationSummary* locations = instruction->GetLocations();
1824 Location second = locations->InAt(1);
1825 DCHECK(second.IsConstant());
1826
1827 Register out = OutputRegister(instruction);
1828 Register dividend = InputRegisterAt(instruction, 0);
1829 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001830 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001831 DCHECK(IsPowerOfTwo(abs_imm));
1832 int ctz_imm = CTZ(abs_imm);
1833
1834 UseScratchRegisterScope temps(GetVIXLAssembler());
1835 Register temp = temps.AcquireSameSizeAs(out);
1836
1837 if (instruction->IsDiv()) {
1838 __ Add(temp, dividend, abs_imm - 1);
1839 __ Cmp(dividend, 0);
1840 __ Csel(out, temp, dividend, lt);
1841 if (imm > 0) {
1842 __ Asr(out, out, ctz_imm);
1843 } else {
1844 __ Neg(out, Operand(out, ASR, ctz_imm));
1845 }
1846 } else {
1847 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1848 __ Asr(temp, dividend, bits - 1);
1849 __ Lsr(temp, temp, bits - ctz_imm);
1850 __ Add(out, dividend, temp);
1851 __ And(out, out, abs_imm - 1);
1852 __ Sub(out, out, temp);
1853 }
1854}
1855
1856void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1857 DCHECK(instruction->IsDiv() || instruction->IsRem());
1858
1859 LocationSummary* locations = instruction->GetLocations();
1860 Location second = locations->InAt(1);
1861 DCHECK(second.IsConstant());
1862
1863 Register out = OutputRegister(instruction);
1864 Register dividend = InputRegisterAt(instruction, 0);
1865 int64_t imm = Int64FromConstant(second.GetConstant());
1866
1867 Primitive::Type type = instruction->GetResultType();
1868 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1869
1870 int64_t magic;
1871 int shift;
1872 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1873
1874 UseScratchRegisterScope temps(GetVIXLAssembler());
1875 Register temp = temps.AcquireSameSizeAs(out);
1876
1877 // temp = get_high(dividend * magic)
1878 __ Mov(temp, magic);
1879 if (type == Primitive::kPrimLong) {
1880 __ Smulh(temp, dividend, temp);
1881 } else {
1882 __ Smull(temp.X(), dividend, temp);
1883 __ Lsr(temp.X(), temp.X(), 32);
1884 }
1885
1886 if (imm > 0 && magic < 0) {
1887 __ Add(temp, temp, dividend);
1888 } else if (imm < 0 && magic > 0) {
1889 __ Sub(temp, temp, dividend);
1890 }
1891
1892 if (shift != 0) {
1893 __ Asr(temp, temp, shift);
1894 }
1895
1896 if (instruction->IsDiv()) {
1897 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1898 } else {
1899 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1900 // TODO: Strength reduction for msub.
1901 Register temp_imm = temps.AcquireSameSizeAs(out);
1902 __ Mov(temp_imm, imm);
1903 __ Msub(out, temp, temp_imm, dividend);
1904 }
1905}
1906
1907void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1908 DCHECK(instruction->IsDiv() || instruction->IsRem());
1909 Primitive::Type type = instruction->GetResultType();
1910 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1911
1912 LocationSummary* locations = instruction->GetLocations();
1913 Register out = OutputRegister(instruction);
1914 Location second = locations->InAt(1);
1915
1916 if (second.IsConstant()) {
1917 int64_t imm = Int64FromConstant(second.GetConstant());
1918
1919 if (imm == 0) {
1920 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1921 } else if (imm == 1 || imm == -1) {
1922 DivRemOneOrMinusOne(instruction);
1923 } else if (IsPowerOfTwo(std::abs(imm))) {
1924 DivRemByPowerOfTwo(instruction);
1925 } else {
1926 DCHECK(imm <= -2 || imm >= 2);
1927 GenerateDivRemWithAnyConstant(instruction);
1928 }
1929 } else {
1930 Register dividend = InputRegisterAt(instruction, 0);
1931 Register divisor = InputRegisterAt(instruction, 1);
1932 if (instruction->IsDiv()) {
1933 __ Sdiv(out, dividend, divisor);
1934 } else {
1935 UseScratchRegisterScope temps(GetVIXLAssembler());
1936 Register temp = temps.AcquireSameSizeAs(out);
1937 __ Sdiv(temp, dividend, divisor);
1938 __ Msub(out, temp, divisor, dividend);
1939 }
1940 }
1941}
1942
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001943void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1946 switch (div->GetResultType()) {
1947 case Primitive::kPrimInt:
1948 case Primitive::kPrimLong:
1949 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001950 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1952 break;
1953
1954 case Primitive::kPrimFloat:
1955 case Primitive::kPrimDouble:
1956 locations->SetInAt(0, Location::RequiresFpuRegister());
1957 locations->SetInAt(1, Location::RequiresFpuRegister());
1958 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1959 break;
1960
1961 default:
1962 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1963 }
1964}
1965
1966void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1967 Primitive::Type type = div->GetResultType();
1968 switch (type) {
1969 case Primitive::kPrimInt:
1970 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001971 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001972 break;
1973
1974 case Primitive::kPrimFloat:
1975 case Primitive::kPrimDouble:
1976 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1977 break;
1978
1979 default:
1980 LOG(FATAL) << "Unexpected div type " << type;
1981 }
1982}
1983
Alexandre Rames67555f72014-11-18 10:55:16 +00001984void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1985 LocationSummary* locations =
1986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1987 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1988 if (instruction->HasUses()) {
1989 locations->SetOut(Location::SameAsFirstInput());
1990 }
1991}
1992
1993void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1994 SlowPathCodeARM64* slow_path =
1995 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1996 codegen_->AddSlowPath(slow_path);
1997 Location value = instruction->GetLocations()->InAt(0);
1998
Alexandre Rames3e69f162014-12-10 10:36:50 +00001999 Primitive::Type type = instruction->GetType();
2000
2001 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
2002 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
2003 return;
2004 }
2005
Alexandre Rames67555f72014-11-18 10:55:16 +00002006 if (value.IsConstant()) {
2007 int64_t divisor = Int64ConstantFrom(value);
2008 if (divisor == 0) {
2009 __ B(slow_path->GetEntryLabel());
2010 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002011 // A division by a non-null constant is valid. We don't need to perform
2012 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002013 }
2014 } else {
2015 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2016 }
2017}
2018
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002019void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2022 locations->SetOut(Location::ConstantLocation(constant));
2023}
2024
2025void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2026 UNUSED(constant);
2027 // Will be generated at use site.
2028}
2029
Alexandre Rames5319def2014-10-23 10:03:10 +01002030void LocationsBuilderARM64::VisitExit(HExit* exit) {
2031 exit->SetLocations(nullptr);
2032}
2033
2034void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002035 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002036}
2037
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002038void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2039 LocationSummary* locations =
2040 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2041 locations->SetOut(Location::ConstantLocation(constant));
2042}
2043
2044void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2045 UNUSED(constant);
2046 // Will be generated at use site.
2047}
2048
David Brazdilfc6a86a2015-06-26 10:33:45 +00002049void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002050 DCHECK(!successor->IsExitBlock());
2051 HBasicBlock* block = got->GetBlock();
2052 HInstruction* previous = got->GetPrevious();
2053 HLoopInformation* info = block->GetLoopInformation();
2054
David Brazdil46e2a392015-03-16 17:31:52 +00002055 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002056 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2057 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2058 return;
2059 }
2060 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2061 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2062 }
2063 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002064 __ B(codegen_->GetLabelOf(successor));
2065 }
2066}
2067
David Brazdilfc6a86a2015-06-26 10:33:45 +00002068void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2069 got->SetLocations(nullptr);
2070}
2071
2072void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2073 HandleGoto(got, got->GetSuccessor());
2074}
2075
2076void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2077 try_boundary->SetLocations(nullptr);
2078}
2079
2080void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2081 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2082 if (!successor->IsExitBlock()) {
2083 HandleGoto(try_boundary, successor);
2084 }
2085}
2086
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002087void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2088 vixl::Label* true_target,
2089 vixl::Label* false_target,
2090 vixl::Label* always_true_target) {
2091 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002092 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002093
Serban Constantinescu02164b32014-11-13 14:05:07 +00002094 if (cond->IsIntConstant()) {
2095 int32_t cond_value = cond->AsIntConstant()->GetValue();
2096 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002097 if (always_true_target != nullptr) {
2098 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002099 }
2100 return;
2101 } else {
2102 DCHECK_EQ(cond_value, 0);
2103 }
2104 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002105 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002106 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002107 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002108 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002109 } else {
2110 // The condition instruction has not been materialized, use its inputs as
2111 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002112 Primitive::Type type =
2113 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2114
2115 if (Primitive::IsFloatingPointType(type)) {
2116 // FP compares don't like null false_targets.
2117 if (false_target == nullptr) {
2118 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002119 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002120 FPRegister lhs = InputFPRegisterAt(condition, 0);
2121 if (condition->GetLocations()->InAt(1).IsConstant()) {
2122 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2123 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2124 __ Fcmp(lhs, 0.0);
2125 } else {
2126 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2127 }
2128 if (condition->IsFPConditionTrueIfNaN()) {
2129 __ B(vs, true_target); // VS for unordered.
2130 } else if (condition->IsFPConditionFalseIfNaN()) {
2131 __ B(vs, false_target); // VS for unordered.
2132 }
2133 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002134 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002135 // Integer cases.
2136 Register lhs = InputRegisterAt(condition, 0);
2137 Operand rhs = InputOperandAt(condition, 1);
2138 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2139 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2140 switch (arm64_cond) {
2141 case eq:
2142 __ Cbz(lhs, true_target);
2143 break;
2144 case ne:
2145 __ Cbnz(lhs, true_target);
2146 break;
2147 case lt:
2148 // Test the sign bit and branch accordingly.
2149 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2150 break;
2151 case ge:
2152 // Test the sign bit and branch accordingly.
2153 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2154 break;
2155 default:
2156 // Without the `static_cast` the compiler throws an error for
2157 // `-Werror=sign-promo`.
2158 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2159 }
2160 } else {
2161 __ Cmp(lhs, rhs);
2162 __ B(arm64_cond, true_target);
2163 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002164 }
2165 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002166 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002167 __ B(false_target);
2168 }
2169}
2170
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002171void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2173 HInstruction* cond = if_instr->InputAt(0);
2174 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2175 locations->SetInAt(0, Location::RequiresRegister());
2176 }
2177}
2178
2179void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2180 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2181 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2182 vixl::Label* always_true_target = true_target;
2183 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2184 if_instr->IfTrueSuccessor())) {
2185 always_true_target = nullptr;
2186 }
2187 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2188 if_instr->IfFalseSuccessor())) {
2189 false_target = nullptr;
2190 }
2191 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2192}
2193
2194void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2195 LocationSummary* locations = new (GetGraph()->GetArena())
2196 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2197 HInstruction* cond = deoptimize->InputAt(0);
2198 DCHECK(cond->IsCondition());
2199 if (cond->AsCondition()->NeedsMaterialization()) {
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 }
2202}
2203
2204void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2205 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2206 DeoptimizationSlowPathARM64(deoptimize);
2207 codegen_->AddSlowPath(slow_path);
2208 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2209 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2210}
2211
Alexandre Rames5319def2014-10-23 10:03:10 +01002212void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002213 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002214}
2215
2216void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002217 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002218}
2219
2220void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002221 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002222}
2223
2224void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002225 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002226}
2227
Alexandre Rames67555f72014-11-18 10:55:16 +00002228void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2229 LocationSummary::CallKind call_kind =
2230 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2231 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2232 locations->SetInAt(0, Location::RequiresRegister());
2233 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002234 // The output does overlap inputs.
2235 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002236}
2237
2238void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2239 LocationSummary* locations = instruction->GetLocations();
2240 Register obj = InputRegisterAt(instruction, 0);;
2241 Register cls = InputRegisterAt(instruction, 1);;
2242 Register out = OutputRegister(instruction);
2243
2244 vixl::Label done;
2245
2246 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002247 // Avoid null check if we know `obj` is not null.
2248 if (instruction->MustDoNullCheck()) {
2249 __ Mov(out, 0);
2250 __ Cbz(obj, &done);
2251 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002252
2253 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002254 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002255 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002256 __ Cmp(out, cls);
2257 if (instruction->IsClassFinal()) {
2258 // Classes must be equal for the instanceof to succeed.
2259 __ Cset(out, eq);
2260 } else {
2261 // If the classes are not equal, we go into a slow path.
2262 DCHECK(locations->OnlyCallsOnSlowPath());
2263 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002264 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2265 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002266 codegen_->AddSlowPath(slow_path);
2267 __ B(ne, slow_path->GetEntryLabel());
2268 __ Mov(out, 1);
2269 __ Bind(slow_path->GetExitLabel());
2270 }
2271
2272 __ Bind(&done);
2273}
2274
Alexandre Rames5319def2014-10-23 10:03:10 +01002275void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2277 locations->SetOut(Location::ConstantLocation(constant));
2278}
2279
2280void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2281 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002282 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002283}
2284
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002285void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2286 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2287 locations->SetOut(Location::ConstantLocation(constant));
2288}
2289
2290void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2291 // Will be generated at use site.
2292 UNUSED(constant);
2293}
2294
Alexandre Rames5319def2014-10-23 10:03:10 +01002295void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002296 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002297 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002298}
2299
Alexandre Rames67555f72014-11-18 10:55:16 +00002300void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2301 HandleInvoke(invoke);
2302}
2303
2304void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2305 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002306 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2307 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2308 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002309 Location receiver = invoke->GetLocations()->InAt(0);
2310 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002311 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002312
2313 // The register ip1 is required to be used for the hidden argument in
2314 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002315 MacroAssembler* masm = GetVIXLAssembler();
2316 UseScratchRegisterScope scratch_scope(masm);
2317 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002318 scratch_scope.Exclude(ip1);
2319 __ Mov(ip1, invoke->GetDexMethodIndex());
2320
2321 // temp = object->GetClass();
2322 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323 __ Ldr(temp.W(), StackOperandFrom(receiver));
2324 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002325 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002327 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002328 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002329 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002330 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002331 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002332 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002334 // lr();
2335 __ Blr(lr);
2336 DCHECK(!codegen_->IsLeafMethod());
2337 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2338}
2339
2340void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002341 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2342 if (intrinsic.TryDispatch(invoke)) {
2343 return;
2344 }
2345
Alexandre Rames67555f72014-11-18 10:55:16 +00002346 HandleInvoke(invoke);
2347}
2348
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002349void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002350 // When we do not run baseline, explicit clinit checks triggered by static
2351 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2352 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002353
Andreas Gampe878d58c2015-01-15 23:24:00 -08002354 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2355 if (intrinsic.TryDispatch(invoke)) {
2356 return;
2357 }
2358
Alexandre Rames67555f72014-11-18 10:55:16 +00002359 HandleInvoke(invoke);
2360}
2361
Andreas Gampe878d58c2015-01-15 23:24:00 -08002362static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2363 if (invoke->GetLocations()->Intrinsified()) {
2364 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2365 intrinsic.Dispatch(invoke);
2366 return true;
2367 }
2368 return false;
2369}
2370
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002371void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002372 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002373 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002374
2375 // TODO: Implement all kinds of calls:
2376 // 1) boot -> boot
2377 // 2) app -> boot
2378 // 3) app -> app
2379 //
2380 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2381
Jeff Hao848f70a2014-01-15 13:49:50 -08002382 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002383 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002384 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002385 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002386 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002387 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002388 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002389 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002390 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002391 } else if (invoke->IsRecursive()) {
2392 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002393 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002394 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002395 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002396 Register method_reg;
2397 if (current_method.IsRegister()) {
2398 method_reg = XRegisterFrom(current_method);
2399 } else {
2400 DCHECK(invoke->GetLocations()->Intrinsified());
2401 DCHECK(!current_method.IsValid());
2402 method_reg = reg;
2403 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2404 }
2405
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002406 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002407 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002408 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2409 // temp = temp[index_in_cache];
2410 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2411 // lr = temp->entry_point_from_quick_compiled_code_;
2412 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2413 kArm64WordSize).Int32Value()));
2414 // lr();
2415 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002416 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002417
Andreas Gampe878d58c2015-01-15 23:24:00 -08002418 DCHECK(!IsLeafMethod());
2419}
2420
2421void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002422 // When we do not run baseline, explicit clinit checks triggered by static
2423 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2424 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002425
Andreas Gampe878d58c2015-01-15 23:24:00 -08002426 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2427 return;
2428 }
2429
Alexandre Ramesd921d642015-04-16 15:07:16 +01002430 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002431 LocationSummary* locations = invoke->GetLocations();
2432 codegen_->GenerateStaticOrDirectCall(
2433 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002434 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002435}
2436
2437void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002438 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2439 return;
2440 }
2441
Alexandre Rames5319def2014-10-23 10:03:10 +01002442 LocationSummary* locations = invoke->GetLocations();
2443 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002444 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2445 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2446 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002447 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002448 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002449
Alexandre Ramesd921d642015-04-16 15:07:16 +01002450 BlockPoolsScope block_pools(GetVIXLAssembler());
2451
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002452 DCHECK(receiver.IsRegister());
2453 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002454 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002455 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002456 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002457 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002458 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002459 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 // lr();
2461 __ Blr(lr);
2462 DCHECK(!codegen_->IsLeafMethod());
2463 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2464}
2465
Alexandre Rames67555f72014-11-18 10:55:16 +00002466void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2467 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2468 : LocationSummary::kNoCall;
2469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002470 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002471 locations->SetOut(Location::RequiresRegister());
2472}
2473
2474void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2475 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002476 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002477 if (cls->IsReferrersClass()) {
2478 DCHECK(!cls->CanCallRuntime());
2479 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002480 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002481 } else {
2482 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002483 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002484 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002485 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002486
2487 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2488 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2489 codegen_->AddSlowPath(slow_path);
2490 __ Cbz(out, slow_path->GetEntryLabel());
2491 if (cls->MustGenerateClinitCheck()) {
2492 GenerateClassInitializationCheck(slow_path, out);
2493 } else {
2494 __ Bind(slow_path->GetExitLabel());
2495 }
2496 }
2497}
2498
David Brazdilcb1c0552015-08-04 16:22:25 +01002499static MemOperand GetExceptionTlsAddress() {
2500 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2501}
2502
Alexandre Rames67555f72014-11-18 10:55:16 +00002503void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2504 LocationSummary* locations =
2505 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2506 locations->SetOut(Location::RequiresRegister());
2507}
2508
2509void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002510 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2511}
2512
2513void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2514 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2515}
2516
2517void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2518 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002519}
2520
Alexandre Rames5319def2014-10-23 10:03:10 +01002521void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2522 load->SetLocations(nullptr);
2523}
2524
2525void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2526 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002527 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
Alexandre Rames67555f72014-11-18 10:55:16 +00002530void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2531 LocationSummary* locations =
2532 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002533 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002534 locations->SetOut(Location::RequiresRegister());
2535}
2536
2537void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2538 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2539 codegen_->AddSlowPath(slow_path);
2540
2541 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002542 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002543 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002544 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002545 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002546 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002547 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002548 __ Cbz(out, slow_path->GetEntryLabel());
2549 __ Bind(slow_path->GetExitLabel());
2550}
2551
Alexandre Rames5319def2014-10-23 10:03:10 +01002552void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2553 local->SetLocations(nullptr);
2554}
2555
2556void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2557 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2558}
2559
2560void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2562 locations->SetOut(Location::ConstantLocation(constant));
2563}
2564
2565void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2566 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002567 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002568}
2569
Alexandre Rames67555f72014-11-18 10:55:16 +00002570void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2571 LocationSummary* locations =
2572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2573 InvokeRuntimeCallingConvention calling_convention;
2574 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2575}
2576
2577void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2578 codegen_->InvokeRuntime(instruction->IsEnter()
2579 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2580 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002581 instruction->GetDexPc(),
2582 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002583 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002584}
2585
Alexandre Rames42d641b2014-10-27 14:00:51 +00002586void LocationsBuilderARM64::VisitMul(HMul* mul) {
2587 LocationSummary* locations =
2588 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2589 switch (mul->GetResultType()) {
2590 case Primitive::kPrimInt:
2591 case Primitive::kPrimLong:
2592 locations->SetInAt(0, Location::RequiresRegister());
2593 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002595 break;
2596
2597 case Primitive::kPrimFloat:
2598 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002599 locations->SetInAt(0, Location::RequiresFpuRegister());
2600 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002601 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002602 break;
2603
2604 default:
2605 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2606 }
2607}
2608
2609void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2610 switch (mul->GetResultType()) {
2611 case Primitive::kPrimInt:
2612 case Primitive::kPrimLong:
2613 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2614 break;
2615
2616 case Primitive::kPrimFloat:
2617 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002618 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2623 }
2624}
2625
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002626void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2627 LocationSummary* locations =
2628 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2629 switch (neg->GetResultType()) {
2630 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002631 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002632 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002634 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002635
2636 case Primitive::kPrimFloat:
2637 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002638 locations->SetInAt(0, Location::RequiresFpuRegister());
2639 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002640 break;
2641
2642 default:
2643 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2644 }
2645}
2646
2647void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2648 switch (neg->GetResultType()) {
2649 case Primitive::kPrimInt:
2650 case Primitive::kPrimLong:
2651 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2652 break;
2653
2654 case Primitive::kPrimFloat:
2655 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002656 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2661 }
2662}
2663
2664void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2665 LocationSummary* locations =
2666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2667 InvokeRuntimeCallingConvention calling_convention;
2668 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002669 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002670 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002671 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002672 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002673 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002674}
2675
2676void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2677 LocationSummary* locations = instruction->GetLocations();
2678 InvokeRuntimeCallingConvention calling_convention;
2679 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2680 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002681 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002682 // Note: if heap poisoning is enabled, the entry point takes cares
2683 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002684 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002685 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2686 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002687 instruction->GetDexPc(),
2688 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002689 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002690}
2691
Alexandre Rames5319def2014-10-23 10:03:10 +01002692void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2693 LocationSummary* locations =
2694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2695 InvokeRuntimeCallingConvention calling_convention;
2696 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002697 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002698 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002699 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002700}
2701
2702void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2703 LocationSummary* locations = instruction->GetLocations();
2704 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2705 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002706 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002707 // Note: if heap poisoning is enabled, the entry point takes cares
2708 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002709 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002710 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2711 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002712 instruction->GetDexPc(),
2713 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002714 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002715}
2716
2717void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002719 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002721}
2722
2723void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002724 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002725 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002726 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002727 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002728 break;
2729
2730 default:
2731 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2732 }
2733}
2734
David Brazdil66d126e2015-04-03 16:02:44 +01002735void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2736 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2737 locations->SetInAt(0, Location::RequiresRegister());
2738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2739}
2740
2741void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002742 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2743}
2744
Alexandre Rames5319def2014-10-23 10:03:10 +01002745void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2746 LocationSummary* locations =
2747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2748 locations->SetInAt(0, Location::RequiresRegister());
2749 if (instruction->HasUses()) {
2750 locations->SetOut(Location::SameAsFirstInput());
2751 }
2752}
2753
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002754void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002755 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2756 return;
2757 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002758
Alexandre Ramesd921d642015-04-16 15:07:16 +01002759 BlockPoolsScope block_pools(GetVIXLAssembler());
2760 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002761 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2762 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2763}
2764
2765void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002766 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2767 codegen_->AddSlowPath(slow_path);
2768
2769 LocationSummary* locations = instruction->GetLocations();
2770 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002771
2772 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002773}
2774
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002775void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2776 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2777 GenerateImplicitNullCheck(instruction);
2778 } else {
2779 GenerateExplicitNullCheck(instruction);
2780 }
2781}
2782
Alexandre Rames67555f72014-11-18 10:55:16 +00002783void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2784 HandleBinaryOp(instruction);
2785}
2786
2787void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2788 HandleBinaryOp(instruction);
2789}
2790
Alexandre Rames3e69f162014-12-10 10:36:50 +00002791void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2792 LOG(FATAL) << "Unreachable";
2793}
2794
2795void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2796 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2797}
2798
Alexandre Rames5319def2014-10-23 10:03:10 +01002799void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2801 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2802 if (location.IsStackSlot()) {
2803 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2804 } else if (location.IsDoubleStackSlot()) {
2805 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2806 }
2807 locations->SetOut(location);
2808}
2809
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002810void InstructionCodeGeneratorARM64::VisitParameterValue(
2811 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002812 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002813}
2814
2815void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2816 LocationSummary* locations =
2817 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002818 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002819}
2820
2821void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2822 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2823 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002824}
2825
2826void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2828 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2829 locations->SetInAt(i, Location::Any());
2830 }
2831 locations->SetOut(Location::Any());
2832}
2833
2834void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002835 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002836 LOG(FATAL) << "Unreachable";
2837}
2838
Serban Constantinescu02164b32014-11-13 14:05:07 +00002839void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002840 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002841 LocationSummary::CallKind call_kind =
2842 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002843 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2844
2845 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002846 case Primitive::kPrimInt:
2847 case Primitive::kPrimLong:
2848 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002849 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2851 break;
2852
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002853 case Primitive::kPrimFloat:
2854 case Primitive::kPrimDouble: {
2855 InvokeRuntimeCallingConvention calling_convention;
2856 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2857 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2858 locations->SetOut(calling_convention.GetReturnLocation(type));
2859
2860 break;
2861 }
2862
Serban Constantinescu02164b32014-11-13 14:05:07 +00002863 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002864 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002865 }
2866}
2867
2868void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2869 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002870
Serban Constantinescu02164b32014-11-13 14:05:07 +00002871 switch (type) {
2872 case Primitive::kPrimInt:
2873 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002874 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002875 break;
2876 }
2877
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002878 case Primitive::kPrimFloat:
2879 case Primitive::kPrimDouble: {
2880 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2881 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002882 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002883 break;
2884 }
2885
Serban Constantinescu02164b32014-11-13 14:05:07 +00002886 default:
2887 LOG(FATAL) << "Unexpected rem type " << type;
2888 }
2889}
2890
Calin Juravle27df7582015-04-17 19:12:31 +01002891void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2892 memory_barrier->SetLocations(nullptr);
2893}
2894
2895void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2896 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2897}
2898
Alexandre Rames5319def2014-10-23 10:03:10 +01002899void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2901 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002902 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002903}
2904
2905void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002906 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002907 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002908}
2909
2910void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2911 instruction->SetLocations(nullptr);
2912}
2913
2914void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002915 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002916 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002917}
2918
Serban Constantinescu02164b32014-11-13 14:05:07 +00002919void LocationsBuilderARM64::VisitShl(HShl* shl) {
2920 HandleShift(shl);
2921}
2922
2923void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2924 HandleShift(shl);
2925}
2926
2927void LocationsBuilderARM64::VisitShr(HShr* shr) {
2928 HandleShift(shr);
2929}
2930
2931void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2932 HandleShift(shr);
2933}
2934
Alexandre Rames5319def2014-10-23 10:03:10 +01002935void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2936 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2937 Primitive::Type field_type = store->InputAt(1)->GetType();
2938 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002939 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002940 case Primitive::kPrimBoolean:
2941 case Primitive::kPrimByte:
2942 case Primitive::kPrimChar:
2943 case Primitive::kPrimShort:
2944 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002945 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002946 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2947 break;
2948
2949 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002950 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002951 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2952 break;
2953
2954 default:
2955 LOG(FATAL) << "Unimplemented local type " << field_type;
2956 }
2957}
2958
2959void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002960 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002961}
2962
2963void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002964 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002965}
2966
2967void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002968 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002969}
2970
Alexandre Rames67555f72014-11-18 10:55:16 +00002971void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002972 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002973}
2974
2975void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002976 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002977}
2978
2979void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002980 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002981}
2982
Alexandre Rames67555f72014-11-18 10:55:16 +00002983void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002984 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002985}
2986
2987void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2989}
2990
2991void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002992 HBasicBlock* block = instruction->GetBlock();
2993 if (block->GetLoopInformation() != nullptr) {
2994 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2995 // The back edge will generate the suspend check.
2996 return;
2997 }
2998 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2999 // The goto will generate the suspend check.
3000 return;
3001 }
3002 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003003}
3004
3005void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3006 temp->SetLocations(nullptr);
3007}
3008
3009void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3010 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003011 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003012}
3013
Alexandre Rames67555f72014-11-18 10:55:16 +00003014void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3015 LocationSummary* locations =
3016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3017 InvokeRuntimeCallingConvention calling_convention;
3018 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3019}
3020
3021void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3022 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003023 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003024 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003025}
3026
3027void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3028 LocationSummary* locations =
3029 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3030 Primitive::Type input_type = conversion->GetInputType();
3031 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003032 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003033 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3034 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3035 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3036 }
3037
Alexandre Rames542361f2015-01-29 16:57:31 +00003038 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003039 locations->SetInAt(0, Location::RequiresFpuRegister());
3040 } else {
3041 locations->SetInAt(0, Location::RequiresRegister());
3042 }
3043
Alexandre Rames542361f2015-01-29 16:57:31 +00003044 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003045 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3046 } else {
3047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3048 }
3049}
3050
3051void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3052 Primitive::Type result_type = conversion->GetResultType();
3053 Primitive::Type input_type = conversion->GetInputType();
3054
3055 DCHECK_NE(input_type, result_type);
3056
Alexandre Rames542361f2015-01-29 16:57:31 +00003057 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003058 int result_size = Primitive::ComponentSize(result_type);
3059 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003060 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003061 Register output = OutputRegister(conversion);
3062 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003063 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3064 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3065 } else if ((result_type == Primitive::kPrimChar) ||
3066 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3067 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003068 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003069 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003070 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003071 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003072 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003073 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003074 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3075 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003076 } else if (Primitive::IsFloatingPointType(result_type) &&
3077 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003078 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3079 } else {
3080 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3081 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003082 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003083}
Alexandre Rames67555f72014-11-18 10:55:16 +00003084
Serban Constantinescu02164b32014-11-13 14:05:07 +00003085void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3086 HandleShift(ushr);
3087}
3088
3089void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3090 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003091}
3092
3093void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3094 HandleBinaryOp(instruction);
3095}
3096
3097void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3098 HandleBinaryOp(instruction);
3099}
3100
Calin Juravleb1498f62015-02-16 13:13:29 +00003101void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3102 // Nothing to do, this should be removed during prepare for register allocator.
3103 UNUSED(instruction);
3104 LOG(FATAL) << "Unreachable";
3105}
3106
3107void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3108 // Nothing to do, this should be removed during prepare for register allocator.
3109 UNUSED(instruction);
3110 LOG(FATAL) << "Unreachable";
3111}
3112
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003113void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3114 DCHECK(codegen_->IsBaseline());
3115 LocationSummary* locations =
3116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3117 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3118}
3119
3120void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3121 DCHECK(codegen_->IsBaseline());
3122 // Will be generated at use site.
3123}
3124
Alexandre Rames67555f72014-11-18 10:55:16 +00003125#undef __
3126#undef QUICK_ENTRY_POINT
3127
Alexandre Rames5319def2014-10-23 10:03:10 +01003128} // namespace arm64
3129} // namespace art