blob: c06b39ba17218e9b2d6283a8eec8abc76813a0aa [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 Geoffray76b1e172015-05-27 17:18:33 +0100659 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700660 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100661 } else if (locations != nullptr && locations->Out().Equals(location)) {
662 return;
663 } else if (instruction->IsIntConstant()
664 || instruction->IsLongConstant()
665 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000666 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100667 if (location.IsRegister()) {
668 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000669 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100670 (instruction->IsLongConstant() && dst.Is64Bits()));
671 __ Mov(dst, value);
672 } else {
673 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000674 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000675 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
676 ? temps.AcquireW()
677 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100678 __ Mov(temp, value);
679 __ Str(temp, StackOperandFrom(location));
680 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000681 } else if (instruction->IsTemporary()) {
682 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000683 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100684 } else if (instruction->IsLoadLocal()) {
685 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000686 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000687 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000688 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000689 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100690 }
691
692 } else {
693 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000694 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100695 }
696}
697
Alexandre Rames5319def2014-10-23 10:03:10 +0100698Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
699 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000700
Alexandre Rames5319def2014-10-23 10:03:10 +0100701 switch (type) {
702 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000703 case Primitive::kPrimInt:
704 case Primitive::kPrimFloat:
705 return Location::StackSlot(GetStackSlot(load->GetLocal()));
706
707 case Primitive::kPrimLong:
708 case Primitive::kPrimDouble:
709 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
710
Alexandre Rames5319def2014-10-23 10:03:10 +0100711 case Primitive::kPrimBoolean:
712 case Primitive::kPrimByte:
713 case Primitive::kPrimChar:
714 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100715 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100716 LOG(FATAL) << "Unexpected type " << type;
717 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000718
Alexandre Rames5319def2014-10-23 10:03:10 +0100719 LOG(FATAL) << "Unreachable";
720 return Location::NoLocation();
721}
722
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100723void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000724 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100725 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000726 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100727 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100728 if (value_can_be_null) {
729 __ Cbz(value, &done);
730 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100731 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
732 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000733 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100734 if (value_can_be_null) {
735 __ Bind(&done);
736 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100737}
738
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000739void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
740 // Blocked core registers:
741 // lr : Runtime reserved.
742 // tr : Runtime reserved.
743 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
744 // ip1 : VIXL core temp.
745 // ip0 : VIXL core temp.
746 //
747 // Blocked fp registers:
748 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100749 CPURegList reserved_core_registers = vixl_reserved_core_registers;
750 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100751 while (!reserved_core_registers.IsEmpty()) {
752 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
753 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000754
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000755 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800756 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000757 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
758 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000759
760 if (is_baseline) {
761 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
762 while (!reserved_core_baseline_registers.IsEmpty()) {
763 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
764 }
765
766 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
767 while (!reserved_fp_baseline_registers.IsEmpty()) {
768 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
769 }
770 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100771}
772
773Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
774 if (type == Primitive::kPrimVoid) {
775 LOG(FATAL) << "Unreachable type " << type;
776 }
777
Alexandre Rames542361f2015-01-29 16:57:31 +0000778 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000779 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
780 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100781 return Location::FpuRegisterLocation(reg);
782 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000783 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
784 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100785 return Location::RegisterLocation(reg);
786 }
787}
788
Alexandre Rames3e69f162014-12-10 10:36:50 +0000789size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
790 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
791 __ Str(reg, MemOperand(sp, stack_index));
792 return kArm64WordSize;
793}
794
795size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
796 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
797 __ Ldr(reg, MemOperand(sp, stack_index));
798 return kArm64WordSize;
799}
800
801size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
802 FPRegister reg = FPRegister(reg_id, kDRegSize);
803 __ Str(reg, MemOperand(sp, stack_index));
804 return kArm64WordSize;
805}
806
807size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
808 FPRegister reg = FPRegister(reg_id, kDRegSize);
809 __ Ldr(reg, MemOperand(sp, stack_index));
810 return kArm64WordSize;
811}
812
Alexandre Rames5319def2014-10-23 10:03:10 +0100813void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100814 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100815}
816
817void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100818 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100819}
820
Alexandre Rames67555f72014-11-18 10:55:16 +0000821void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000822 if (constant->IsIntConstant()) {
823 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
824 } else if (constant->IsLongConstant()) {
825 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
826 } else if (constant->IsNullConstant()) {
827 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000828 } else if (constant->IsFloatConstant()) {
829 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
830 } else {
831 DCHECK(constant->IsDoubleConstant());
832 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
833 }
834}
835
Alexandre Rames3e69f162014-12-10 10:36:50 +0000836
837static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
838 DCHECK(constant.IsConstant());
839 HConstant* cst = constant.GetConstant();
840 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000841 // Null is mapped to a core W register, which we associate with kPrimInt.
842 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000843 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
844 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
845 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
846}
847
848void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000849 if (source.Equals(destination)) {
850 return;
851 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000852
853 // A valid move can always be inferred from the destination and source
854 // locations. When moving from and to a register, the argument type can be
855 // used to generate 32bit instead of 64bit moves. In debug mode we also
856 // checks the coherency of the locations and the type.
857 bool unspecified_type = (type == Primitive::kPrimVoid);
858
859 if (destination.IsRegister() || destination.IsFpuRegister()) {
860 if (unspecified_type) {
861 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
862 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000863 (src_cst != nullptr && (src_cst->IsIntConstant()
864 || src_cst->IsFloatConstant()
865 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000866 // For stack slots and 32bit constants, a 64bit type is appropriate.
867 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000868 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000869 // If the source is a double stack slot or a 64bit constant, a 64bit
870 // type is appropriate. Else the source is a register, and since the
871 // type has not been specified, we chose a 64bit type to force a 64bit
872 // move.
873 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000874 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000875 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000876 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
877 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 CPURegister dst = CPURegisterFrom(destination, type);
879 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
880 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
881 __ Ldr(dst, StackOperandFrom(source));
882 } else if (source.IsConstant()) {
883 DCHECK(CoherentConstantAndType(source, type));
884 MoveConstant(dst, source.GetConstant());
885 } else {
886 if (destination.IsRegister()) {
887 __ Mov(Register(dst), RegisterFrom(source, type));
888 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800889 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
891 }
892 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000893 } else { // The destination is not a register. It must be a stack slot.
894 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
895 if (source.IsRegister() || source.IsFpuRegister()) {
896 if (unspecified_type) {
897 if (source.IsRegister()) {
898 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
899 } else {
900 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
901 }
902 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
904 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000905 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
906 } else if (source.IsConstant()) {
907 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
908 UseScratchRegisterScope temps(GetVIXLAssembler());
909 HConstant* src_cst = source.GetConstant();
910 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000911 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000912 temp = temps.AcquireW();
913 } else if (src_cst->IsLongConstant()) {
914 temp = temps.AcquireX();
915 } else if (src_cst->IsFloatConstant()) {
916 temp = temps.AcquireS();
917 } else {
918 DCHECK(src_cst->IsDoubleConstant());
919 temp = temps.AcquireD();
920 }
921 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000922 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000923 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000924 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000925 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000926 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000927 // There is generally less pressure on FP registers.
928 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000929 __ Ldr(temp, StackOperandFrom(source));
930 __ Str(temp, StackOperandFrom(destination));
931 }
932 }
933}
934
935void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000936 CPURegister dst,
937 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000938 switch (type) {
939 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000940 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000941 break;
942 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000943 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000944 break;
945 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000946 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000947 break;
948 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000949 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000950 break;
951 case Primitive::kPrimInt:
952 case Primitive::kPrimNot:
953 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000954 case Primitive::kPrimFloat:
955 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000956 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000957 __ Ldr(dst, src);
958 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000959 case Primitive::kPrimVoid:
960 LOG(FATAL) << "Unreachable type " << type;
961 }
962}
963
Calin Juravle77520bc2015-01-12 18:45:46 +0000964void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000965 CPURegister dst,
966 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100967 MacroAssembler* masm = GetVIXLAssembler();
968 BlockPoolsScope block_pools(masm);
969 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000970 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000971 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000972
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000973 DCHECK(!src.IsPreIndex());
974 DCHECK(!src.IsPostIndex());
975
976 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800977 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000978 MemOperand base = MemOperand(temp_base);
979 switch (type) {
980 case Primitive::kPrimBoolean:
981 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000982 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000983 break;
984 case Primitive::kPrimByte:
985 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000986 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000987 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
988 break;
989 case Primitive::kPrimChar:
990 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000991 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000992 break;
993 case Primitive::kPrimShort:
994 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000995 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000996 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
997 break;
998 case Primitive::kPrimInt:
999 case Primitive::kPrimNot:
1000 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001001 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001002 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001003 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 break;
1005 case Primitive::kPrimFloat:
1006 case Primitive::kPrimDouble: {
1007 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001008 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009
1010 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1011 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001012 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001013 __ Fmov(FPRegister(dst), temp);
1014 break;
1015 }
1016 case Primitive::kPrimVoid:
1017 LOG(FATAL) << "Unreachable type " << type;
1018 }
1019}
1020
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001021void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001022 CPURegister src,
1023 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001024 switch (type) {
1025 case Primitive::kPrimBoolean:
1026 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001027 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001028 break;
1029 case Primitive::kPrimChar:
1030 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001031 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001032 break;
1033 case Primitive::kPrimInt:
1034 case Primitive::kPrimNot:
1035 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001036 case Primitive::kPrimFloat:
1037 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001038 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001039 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001040 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001041 case Primitive::kPrimVoid:
1042 LOG(FATAL) << "Unreachable type " << type;
1043 }
1044}
1045
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001046void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1047 CPURegister src,
1048 const MemOperand& dst) {
1049 UseScratchRegisterScope temps(GetVIXLAssembler());
1050 Register temp_base = temps.AcquireX();
1051
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 DCHECK(!dst.IsPreIndex());
1053 DCHECK(!dst.IsPostIndex());
1054
1055 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001056 Operand op = OperandFromMemOperand(dst);
1057 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001058 MemOperand base = MemOperand(temp_base);
1059 switch (type) {
1060 case Primitive::kPrimBoolean:
1061 case Primitive::kPrimByte:
1062 __ Stlrb(Register(src), base);
1063 break;
1064 case Primitive::kPrimChar:
1065 case Primitive::kPrimShort:
1066 __ Stlrh(Register(src), base);
1067 break;
1068 case Primitive::kPrimInt:
1069 case Primitive::kPrimNot:
1070 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001071 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001072 __ Stlr(Register(src), base);
1073 break;
1074 case Primitive::kPrimFloat:
1075 case Primitive::kPrimDouble: {
1076 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001077 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001078
1079 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1080 __ Fmov(temp, FPRegister(src));
1081 __ Stlr(temp, base);
1082 break;
1083 }
1084 case Primitive::kPrimVoid:
1085 LOG(FATAL) << "Unreachable type " << type;
1086 }
1087}
1088
Alexandre Rames67555f72014-11-18 10:55:16 +00001089void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1090 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001091 uint32_t dex_pc,
1092 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001093 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001094 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1095 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001096 RecordPcInfo(instruction, dex_pc, slow_path);
1097 DCHECK(instruction->IsSuspendCheck()
1098 || instruction->IsBoundsCheck()
1099 || instruction->IsNullCheck()
1100 || instruction->IsDivZeroCheck()
1101 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001102}
1103
1104void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1105 vixl::Register class_reg) {
1106 UseScratchRegisterScope temps(GetVIXLAssembler());
1107 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001108 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001109 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001110
Serban Constantinescu02164b32014-11-13 14:05:07 +00001111 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001112 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001113 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1114 __ Add(temp, class_reg, status_offset);
1115 __ Ldar(temp, HeapOperand(temp));
1116 __ Cmp(temp, mirror::Class::kStatusInitialized);
1117 __ B(lt, slow_path->GetEntryLabel());
1118 } else {
1119 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1120 __ Cmp(temp, mirror::Class::kStatusInitialized);
1121 __ B(lt, slow_path->GetEntryLabel());
1122 __ Dmb(InnerShareable, BarrierReads);
1123 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001124 __ Bind(slow_path->GetExitLabel());
1125}
Alexandre Rames5319def2014-10-23 10:03:10 +01001126
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001127void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1128 BarrierType type = BarrierAll;
1129
1130 switch (kind) {
1131 case MemBarrierKind::kAnyAny:
1132 case MemBarrierKind::kAnyStore: {
1133 type = BarrierAll;
1134 break;
1135 }
1136 case MemBarrierKind::kLoadAny: {
1137 type = BarrierReads;
1138 break;
1139 }
1140 case MemBarrierKind::kStoreStore: {
1141 type = BarrierWrites;
1142 break;
1143 }
1144 default:
1145 LOG(FATAL) << "Unexpected memory barrier " << kind;
1146 }
1147 __ Dmb(InnerShareable, type);
1148}
1149
Serban Constantinescu02164b32014-11-13 14:05:07 +00001150void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1151 HBasicBlock* successor) {
1152 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001153 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1154 if (slow_path == nullptr) {
1155 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1156 instruction->SetSlowPath(slow_path);
1157 codegen_->AddSlowPath(slow_path);
1158 if (successor != nullptr) {
1159 DCHECK(successor->IsLoopHeader());
1160 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1161 }
1162 } else {
1163 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1164 }
1165
Serban Constantinescu02164b32014-11-13 14:05:07 +00001166 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1167 Register temp = temps.AcquireW();
1168
1169 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1170 if (successor == nullptr) {
1171 __ Cbnz(temp, slow_path->GetEntryLabel());
1172 __ Bind(slow_path->GetReturnLabel());
1173 } else {
1174 __ Cbz(temp, codegen_->GetLabelOf(successor));
1175 __ B(slow_path->GetEntryLabel());
1176 // slow_path will return to GetLabelOf(successor).
1177 }
1178}
1179
Alexandre Rames5319def2014-10-23 10:03:10 +01001180InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1181 CodeGeneratorARM64* codegen)
1182 : HGraphVisitor(graph),
1183 assembler_(codegen->GetAssembler()),
1184 codegen_(codegen) {}
1185
1186#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001187 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001188
1189#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1190
1191enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001192 // Using a base helps identify when we hit such breakpoints.
1193 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001194#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1195 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1196#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1197};
1198
1199#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1200 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001201 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001202 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1203 } \
1204 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1205 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1206 locations->SetOut(Location::Any()); \
1207 }
1208 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1209#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1210
1211#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001212#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001213
Alexandre Rames67555f72014-11-18 10:55:16 +00001214void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001215 DCHECK_EQ(instr->InputCount(), 2U);
1216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1217 Primitive::Type type = instr->GetResultType();
1218 switch (type) {
1219 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001220 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001221 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001222 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001224 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001225
1226 case Primitive::kPrimFloat:
1227 case Primitive::kPrimDouble:
1228 locations->SetInAt(0, Location::RequiresFpuRegister());
1229 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001230 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001231 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001232
Alexandre Rames5319def2014-10-23 10:03:10 +01001233 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001234 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001235 }
1236}
1237
Alexandre Rames09a99962015-04-15 11:47:56 +01001238void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1239 LocationSummary* locations =
1240 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1241 locations->SetInAt(0, Location::RequiresRegister());
1242 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1243 locations->SetOut(Location::RequiresFpuRegister());
1244 } else {
1245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1246 }
1247}
1248
1249void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1250 const FieldInfo& field_info) {
1251 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001252 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001253 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001254
1255 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1256 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1257
1258 if (field_info.IsVolatile()) {
1259 if (use_acquire_release) {
1260 // NB: LoadAcquire will record the pc info if needed.
1261 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1262 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001263 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001264 codegen_->MaybeRecordImplicitNullCheck(instruction);
1265 // For IRIW sequential consistency kLoadAny is not sufficient.
1266 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1267 }
1268 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001269 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001270 codegen_->MaybeRecordImplicitNullCheck(instruction);
1271 }
Roland Levillain4d027112015-07-01 15:41:14 +01001272
1273 if (field_type == Primitive::kPrimNot) {
1274 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1275 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001276}
1277
1278void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1279 LocationSummary* locations =
1280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1281 locations->SetInAt(0, Location::RequiresRegister());
1282 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1283 locations->SetInAt(1, Location::RequiresFpuRegister());
1284 } else {
1285 locations->SetInAt(1, Location::RequiresRegister());
1286 }
1287}
1288
1289void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001290 const FieldInfo& field_info,
1291 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001292 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001293 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001294
1295 Register obj = InputRegisterAt(instruction, 0);
1296 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001297 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001298 Offset offset = field_info.GetFieldOffset();
1299 Primitive::Type field_type = field_info.GetFieldType();
1300 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1301
Roland Levillain4d027112015-07-01 15:41:14 +01001302 {
1303 // We use a block to end the scratch scope before the write barrier, thus
1304 // freeing the temporary registers so they can be used in `MarkGCCard`.
1305 UseScratchRegisterScope temps(GetVIXLAssembler());
1306
1307 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1308 DCHECK(value.IsW());
1309 Register temp = temps.AcquireW();
1310 __ Mov(temp, value.W());
1311 GetAssembler()->PoisonHeapReference(temp.W());
1312 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001313 }
Roland Levillain4d027112015-07-01 15:41:14 +01001314
1315 if (field_info.IsVolatile()) {
1316 if (use_acquire_release) {
1317 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1318 codegen_->MaybeRecordImplicitNullCheck(instruction);
1319 } else {
1320 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1321 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1322 codegen_->MaybeRecordImplicitNullCheck(instruction);
1323 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1324 }
1325 } else {
1326 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1327 codegen_->MaybeRecordImplicitNullCheck(instruction);
1328 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001329 }
1330
1331 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001332 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001333 }
1334}
1335
Alexandre Rames67555f72014-11-18 10:55:16 +00001336void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001337 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001338
1339 switch (type) {
1340 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001341 case Primitive::kPrimLong: {
1342 Register dst = OutputRegister(instr);
1343 Register lhs = InputRegisterAt(instr, 0);
1344 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 if (instr->IsAdd()) {
1346 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001347 } else if (instr->IsAnd()) {
1348 __ And(dst, lhs, rhs);
1349 } else if (instr->IsOr()) {
1350 __ Orr(dst, lhs, rhs);
1351 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001352 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001353 } else {
1354 DCHECK(instr->IsXor());
1355 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 }
1357 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001358 }
1359 case Primitive::kPrimFloat:
1360 case Primitive::kPrimDouble: {
1361 FPRegister dst = OutputFPRegister(instr);
1362 FPRegister lhs = InputFPRegisterAt(instr, 0);
1363 FPRegister rhs = InputFPRegisterAt(instr, 1);
1364 if (instr->IsAdd()) {
1365 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001366 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001367 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001368 } else {
1369 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001370 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001371 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001372 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001373 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001374 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001375 }
1376}
1377
Serban Constantinescu02164b32014-11-13 14:05:07 +00001378void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1379 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1380
1381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1382 Primitive::Type type = instr->GetResultType();
1383 switch (type) {
1384 case Primitive::kPrimInt:
1385 case Primitive::kPrimLong: {
1386 locations->SetInAt(0, Location::RequiresRegister());
1387 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1388 locations->SetOut(Location::RequiresRegister());
1389 break;
1390 }
1391 default:
1392 LOG(FATAL) << "Unexpected shift type " << type;
1393 }
1394}
1395
1396void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1397 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1398
1399 Primitive::Type type = instr->GetType();
1400 switch (type) {
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimLong: {
1403 Register dst = OutputRegister(instr);
1404 Register lhs = InputRegisterAt(instr, 0);
1405 Operand rhs = InputOperandAt(instr, 1);
1406 if (rhs.IsImmediate()) {
1407 uint32_t shift_value = (type == Primitive::kPrimInt)
1408 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1409 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1410 if (instr->IsShl()) {
1411 __ Lsl(dst, lhs, shift_value);
1412 } else if (instr->IsShr()) {
1413 __ Asr(dst, lhs, shift_value);
1414 } else {
1415 __ Lsr(dst, lhs, shift_value);
1416 }
1417 } else {
1418 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1419
1420 if (instr->IsShl()) {
1421 __ Lsl(dst, lhs, rhs_reg);
1422 } else if (instr->IsShr()) {
1423 __ Asr(dst, lhs, rhs_reg);
1424 } else {
1425 __ Lsr(dst, lhs, rhs_reg);
1426 }
1427 }
1428 break;
1429 }
1430 default:
1431 LOG(FATAL) << "Unexpected shift operation type " << type;
1432 }
1433}
1434
Alexandre Rames5319def2014-10-23 10:03:10 +01001435void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001436 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001437}
1438
1439void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001440 HandleBinaryOp(instruction);
1441}
1442
1443void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1444 HandleBinaryOp(instruction);
1445}
1446
1447void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1448 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001449}
1450
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001451void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1452 LocationSummary* locations =
1453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001456 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1457 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1458 } else {
1459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1460 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001461}
1462
1463void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1464 LocationSummary* locations = instruction->GetLocations();
1465 Primitive::Type type = instruction->GetType();
1466 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001467 Location index = locations->InAt(1);
1468 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001469 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001470 MacroAssembler* masm = GetVIXLAssembler();
1471 UseScratchRegisterScope temps(masm);
1472 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001473
1474 if (index.IsConstant()) {
1475 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001476 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001477 } else {
1478 Register temp = temps.AcquireSameSizeAs(obj);
1479 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1480 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001481 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001482 }
1483
Alexandre Rames67555f72014-11-18 10:55:16 +00001484 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001485 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001486
1487 if (type == Primitive::kPrimNot) {
1488 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1489 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001490}
1491
Alexandre Rames5319def2014-10-23 10:03:10 +01001492void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1494 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001496}
1497
1498void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001499 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001500 __ Ldr(OutputRegister(instruction),
1501 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001502 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001503}
1504
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001505void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001506 if (instruction->NeedsTypeCheck()) {
1507 LocationSummary* locations =
1508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001509 InvokeRuntimeCallingConvention calling_convention;
1510 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1511 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1512 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1513 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001516 locations->SetInAt(0, Location::RequiresRegister());
1517 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001518 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1519 locations->SetInAt(2, Location::RequiresFpuRegister());
1520 } else {
1521 locations->SetInAt(2, Location::RequiresRegister());
1522 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001523 }
1524}
1525
1526void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1527 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001528 LocationSummary* locations = instruction->GetLocations();
1529 bool needs_runtime_call = locations->WillCall();
1530
1531 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001532 // Note: if heap poisoning is enabled, pAputObject takes cares
1533 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001534 codegen_->InvokeRuntime(
1535 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001536 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001537 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001538 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001539 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001540 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001541 Location index = locations->InAt(1);
1542 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001544 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001545 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001546 {
1547 // We use a block to end the scratch scope before the write barrier, thus
1548 // freeing the temporary registers so they can be used in `MarkGCCard`.
1549 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001550
Roland Levillain4d027112015-07-01 15:41:14 +01001551 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1552 DCHECK(value.IsW());
1553 Register temp = temps.AcquireW();
1554 __ Mov(temp, value.W());
1555 GetAssembler()->PoisonHeapReference(temp.W());
1556 source = temp;
1557 }
1558
Alexandre Rames97833a02015-04-16 15:07:12 +01001559 if (index.IsConstant()) {
1560 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1561 destination = HeapOperand(obj, offset);
1562 } else {
1563 Register temp = temps.AcquireSameSizeAs(obj);
1564 Register index_reg = InputRegisterAt(instruction, 1);
1565 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1566 destination = HeapOperand(temp, offset);
1567 }
1568
Roland Levillain4d027112015-07-01 15:41:14 +01001569 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001570 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001571 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001572 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001573 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001574 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001575 }
1576}
1577
Alexandre Rames67555f72014-11-18 10:55:16 +00001578void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1579 LocationSummary* locations =
1580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1581 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001582 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001583 if (instruction->HasUses()) {
1584 locations->SetOut(Location::SameAsFirstInput());
1585 }
1586}
1587
1588void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001589 LocationSummary* locations = instruction->GetLocations();
1590 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1591 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 codegen_->AddSlowPath(slow_path);
1593
1594 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1595 __ B(slow_path->GetEntryLabel(), hs);
1596}
1597
1598void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1600 instruction, LocationSummary::kCallOnSlowPath);
1601 locations->SetInAt(0, Location::RequiresRegister());
1602 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001603 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001604}
1605
1606void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001607 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001608 Register obj = InputRegisterAt(instruction, 0);;
1609 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001610 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001611
Alexandre Rames3e69f162014-12-10 10:36:50 +00001612 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1613 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001614 codegen_->AddSlowPath(slow_path);
1615
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001616 // Avoid null check if we know obj is not null.
1617 if (instruction->MustDoNullCheck()) {
1618 __ Cbz(obj, slow_path->GetExitLabel());
1619 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001620 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001621 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001622 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001623 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001624 // The checkcast succeeds if the classes are equal (fast path).
1625 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001626 __ B(ne, slow_path->GetEntryLabel());
1627 __ Bind(slow_path->GetExitLabel());
1628}
1629
1630void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1631 LocationSummary* locations =
1632 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1633 locations->SetInAt(0, Location::RequiresRegister());
1634 if (check->HasUses()) {
1635 locations->SetOut(Location::SameAsFirstInput());
1636 }
1637}
1638
1639void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1640 // We assume the class is not null.
1641 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1642 check->GetLoadClass(), check, check->GetDexPc(), true);
1643 codegen_->AddSlowPath(slow_path);
1644 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1645}
1646
Roland Levillain7f63c522015-07-13 15:54:55 +00001647static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1648 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1649 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1650}
1651
Serban Constantinescu02164b32014-11-13 14:05:07 +00001652void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001654 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1655 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001656 switch (in_type) {
1657 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001658 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001659 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1661 break;
1662 }
1663 case Primitive::kPrimFloat:
1664 case Primitive::kPrimDouble: {
1665 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001666 locations->SetInAt(1,
1667 IsFloatingPointZeroConstant(compare->InputAt(1))
1668 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1669 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001670 locations->SetOut(Location::RequiresRegister());
1671 break;
1672 }
1673 default:
1674 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1675 }
1676}
1677
1678void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1679 Primitive::Type in_type = compare->InputAt(0)->GetType();
1680
1681 // 0 if: left == right
1682 // 1 if: left > right
1683 // -1 if: left < right
1684 switch (in_type) {
1685 case Primitive::kPrimLong: {
1686 Register result = OutputRegister(compare);
1687 Register left = InputRegisterAt(compare, 0);
1688 Operand right = InputOperandAt(compare, 1);
1689
1690 __ Cmp(left, right);
1691 __ Cset(result, ne);
1692 __ Cneg(result, result, lt);
1693 break;
1694 }
1695 case Primitive::kPrimFloat:
1696 case Primitive::kPrimDouble: {
1697 Register result = OutputRegister(compare);
1698 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001699 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001700 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1701 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001702 __ Fcmp(left, 0.0);
1703 } else {
1704 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1705 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001706 if (compare->IsGtBias()) {
1707 __ Cset(result, ne);
1708 } else {
1709 __ Csetm(result, ne);
1710 }
1711 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001712 break;
1713 }
1714 default:
1715 LOG(FATAL) << "Unimplemented compare type " << in_type;
1716 }
1717}
1718
1719void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001721
1722 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1723 locations->SetInAt(0, Location::RequiresFpuRegister());
1724 locations->SetInAt(1,
1725 IsFloatingPointZeroConstant(instruction->InputAt(1))
1726 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1727 : Location::RequiresFpuRegister());
1728 } else {
1729 // Integer cases.
1730 locations->SetInAt(0, Location::RequiresRegister());
1731 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1732 }
1733
Alexandre Rames5319def2014-10-23 10:03:10 +01001734 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001736 }
1737}
1738
1739void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1740 if (!instruction->NeedsMaterialization()) {
1741 return;
1742 }
1743
1744 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001745 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001746 IfCondition if_cond = instruction->GetCondition();
1747 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001748
Roland Levillain7f63c522015-07-13 15:54:55 +00001749 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1750 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1751 if (locations->InAt(1).IsConstant()) {
1752 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1753 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1754 __ Fcmp(lhs, 0.0);
1755 } else {
1756 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1757 }
1758 __ Cset(res, arm64_cond);
1759 if (instruction->IsFPConditionTrueIfNaN()) {
1760 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1761 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1762 } else if (instruction->IsFPConditionFalseIfNaN()) {
1763 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1764 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1765 }
1766 } else {
1767 // Integer cases.
1768 Register lhs = InputRegisterAt(instruction, 0);
1769 Operand rhs = InputOperandAt(instruction, 1);
1770 __ Cmp(lhs, rhs);
1771 __ Cset(res, arm64_cond);
1772 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001773}
1774
1775#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1776 M(Equal) \
1777 M(NotEqual) \
1778 M(LessThan) \
1779 M(LessThanOrEqual) \
1780 M(GreaterThan) \
1781 M(GreaterThanOrEqual)
1782#define DEFINE_CONDITION_VISITORS(Name) \
1783void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1784void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1785FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001786#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001787#undef FOR_EACH_CONDITION_INSTRUCTION
1788
Zheng Xuc6667102015-05-15 16:08:45 +08001789void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1790 DCHECK(instruction->IsDiv() || instruction->IsRem());
1791
1792 LocationSummary* locations = instruction->GetLocations();
1793 Location second = locations->InAt(1);
1794 DCHECK(second.IsConstant());
1795
1796 Register out = OutputRegister(instruction);
1797 Register dividend = InputRegisterAt(instruction, 0);
1798 int64_t imm = Int64FromConstant(second.GetConstant());
1799 DCHECK(imm == 1 || imm == -1);
1800
1801 if (instruction->IsRem()) {
1802 __ Mov(out, 0);
1803 } else {
1804 if (imm == 1) {
1805 __ Mov(out, dividend);
1806 } else {
1807 __ Neg(out, dividend);
1808 }
1809 }
1810}
1811
1812void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1813 DCHECK(instruction->IsDiv() || instruction->IsRem());
1814
1815 LocationSummary* locations = instruction->GetLocations();
1816 Location second = locations->InAt(1);
1817 DCHECK(second.IsConstant());
1818
1819 Register out = OutputRegister(instruction);
1820 Register dividend = InputRegisterAt(instruction, 0);
1821 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001822 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001823 DCHECK(IsPowerOfTwo(abs_imm));
1824 int ctz_imm = CTZ(abs_imm);
1825
1826 UseScratchRegisterScope temps(GetVIXLAssembler());
1827 Register temp = temps.AcquireSameSizeAs(out);
1828
1829 if (instruction->IsDiv()) {
1830 __ Add(temp, dividend, abs_imm - 1);
1831 __ Cmp(dividend, 0);
1832 __ Csel(out, temp, dividend, lt);
1833 if (imm > 0) {
1834 __ Asr(out, out, ctz_imm);
1835 } else {
1836 __ Neg(out, Operand(out, ASR, ctz_imm));
1837 }
1838 } else {
1839 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1840 __ Asr(temp, dividend, bits - 1);
1841 __ Lsr(temp, temp, bits - ctz_imm);
1842 __ Add(out, dividend, temp);
1843 __ And(out, out, abs_imm - 1);
1844 __ Sub(out, out, temp);
1845 }
1846}
1847
1848void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1849 DCHECK(instruction->IsDiv() || instruction->IsRem());
1850
1851 LocationSummary* locations = instruction->GetLocations();
1852 Location second = locations->InAt(1);
1853 DCHECK(second.IsConstant());
1854
1855 Register out = OutputRegister(instruction);
1856 Register dividend = InputRegisterAt(instruction, 0);
1857 int64_t imm = Int64FromConstant(second.GetConstant());
1858
1859 Primitive::Type type = instruction->GetResultType();
1860 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1861
1862 int64_t magic;
1863 int shift;
1864 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1865
1866 UseScratchRegisterScope temps(GetVIXLAssembler());
1867 Register temp = temps.AcquireSameSizeAs(out);
1868
1869 // temp = get_high(dividend * magic)
1870 __ Mov(temp, magic);
1871 if (type == Primitive::kPrimLong) {
1872 __ Smulh(temp, dividend, temp);
1873 } else {
1874 __ Smull(temp.X(), dividend, temp);
1875 __ Lsr(temp.X(), temp.X(), 32);
1876 }
1877
1878 if (imm > 0 && magic < 0) {
1879 __ Add(temp, temp, dividend);
1880 } else if (imm < 0 && magic > 0) {
1881 __ Sub(temp, temp, dividend);
1882 }
1883
1884 if (shift != 0) {
1885 __ Asr(temp, temp, shift);
1886 }
1887
1888 if (instruction->IsDiv()) {
1889 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1890 } else {
1891 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1892 // TODO: Strength reduction for msub.
1893 Register temp_imm = temps.AcquireSameSizeAs(out);
1894 __ Mov(temp_imm, imm);
1895 __ Msub(out, temp, temp_imm, dividend);
1896 }
1897}
1898
1899void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1900 DCHECK(instruction->IsDiv() || instruction->IsRem());
1901 Primitive::Type type = instruction->GetResultType();
1902 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1903
1904 LocationSummary* locations = instruction->GetLocations();
1905 Register out = OutputRegister(instruction);
1906 Location second = locations->InAt(1);
1907
1908 if (second.IsConstant()) {
1909 int64_t imm = Int64FromConstant(second.GetConstant());
1910
1911 if (imm == 0) {
1912 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1913 } else if (imm == 1 || imm == -1) {
1914 DivRemOneOrMinusOne(instruction);
1915 } else if (IsPowerOfTwo(std::abs(imm))) {
1916 DivRemByPowerOfTwo(instruction);
1917 } else {
1918 DCHECK(imm <= -2 || imm >= 2);
1919 GenerateDivRemWithAnyConstant(instruction);
1920 }
1921 } else {
1922 Register dividend = InputRegisterAt(instruction, 0);
1923 Register divisor = InputRegisterAt(instruction, 1);
1924 if (instruction->IsDiv()) {
1925 __ Sdiv(out, dividend, divisor);
1926 } else {
1927 UseScratchRegisterScope temps(GetVIXLAssembler());
1928 Register temp = temps.AcquireSameSizeAs(out);
1929 __ Sdiv(temp, dividend, divisor);
1930 __ Msub(out, temp, divisor, dividend);
1931 }
1932 }
1933}
1934
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001935void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1938 switch (div->GetResultType()) {
1939 case Primitive::kPrimInt:
1940 case Primitive::kPrimLong:
1941 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001942 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1944 break;
1945
1946 case Primitive::kPrimFloat:
1947 case Primitive::kPrimDouble:
1948 locations->SetInAt(0, Location::RequiresFpuRegister());
1949 locations->SetInAt(1, Location::RequiresFpuRegister());
1950 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1951 break;
1952
1953 default:
1954 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1955 }
1956}
1957
1958void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1959 Primitive::Type type = div->GetResultType();
1960 switch (type) {
1961 case Primitive::kPrimInt:
1962 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001963 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001964 break;
1965
1966 case Primitive::kPrimFloat:
1967 case Primitive::kPrimDouble:
1968 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected div type " << type;
1973 }
1974}
1975
Alexandre Rames67555f72014-11-18 10:55:16 +00001976void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1977 LocationSummary* locations =
1978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1979 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1980 if (instruction->HasUses()) {
1981 locations->SetOut(Location::SameAsFirstInput());
1982 }
1983}
1984
1985void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1986 SlowPathCodeARM64* slow_path =
1987 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1988 codegen_->AddSlowPath(slow_path);
1989 Location value = instruction->GetLocations()->InAt(0);
1990
Alexandre Rames3e69f162014-12-10 10:36:50 +00001991 Primitive::Type type = instruction->GetType();
1992
1993 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1994 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1995 return;
1996 }
1997
Alexandre Rames67555f72014-11-18 10:55:16 +00001998 if (value.IsConstant()) {
1999 int64_t divisor = Int64ConstantFrom(value);
2000 if (divisor == 0) {
2001 __ B(slow_path->GetEntryLabel());
2002 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002003 // A division by a non-null constant is valid. We don't need to perform
2004 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002005 }
2006 } else {
2007 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2008 }
2009}
2010
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002011void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2012 LocationSummary* locations =
2013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2014 locations->SetOut(Location::ConstantLocation(constant));
2015}
2016
2017void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2018 UNUSED(constant);
2019 // Will be generated at use site.
2020}
2021
Alexandre Rames5319def2014-10-23 10:03:10 +01002022void LocationsBuilderARM64::VisitExit(HExit* exit) {
2023 exit->SetLocations(nullptr);
2024}
2025
2026void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002027 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002028}
2029
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002030void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2033 locations->SetOut(Location::ConstantLocation(constant));
2034}
2035
2036void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2037 UNUSED(constant);
2038 // Will be generated at use site.
2039}
2040
David Brazdilfc6a86a2015-06-26 10:33:45 +00002041void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002042 DCHECK(!successor->IsExitBlock());
2043 HBasicBlock* block = got->GetBlock();
2044 HInstruction* previous = got->GetPrevious();
2045 HLoopInformation* info = block->GetLoopInformation();
2046
David Brazdil46e2a392015-03-16 17:31:52 +00002047 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002048 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2049 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2050 return;
2051 }
2052 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2053 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2054 }
2055 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002056 __ B(codegen_->GetLabelOf(successor));
2057 }
2058}
2059
David Brazdilfc6a86a2015-06-26 10:33:45 +00002060void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2061 got->SetLocations(nullptr);
2062}
2063
2064void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2065 HandleGoto(got, got->GetSuccessor());
2066}
2067
2068void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2069 try_boundary->SetLocations(nullptr);
2070}
2071
2072void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2073 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2074 if (!successor->IsExitBlock()) {
2075 HandleGoto(try_boundary, successor);
2076 }
2077}
2078
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002079void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2080 vixl::Label* true_target,
2081 vixl::Label* false_target,
2082 vixl::Label* always_true_target) {
2083 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002084 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002085
Serban Constantinescu02164b32014-11-13 14:05:07 +00002086 if (cond->IsIntConstant()) {
2087 int32_t cond_value = cond->AsIntConstant()->GetValue();
2088 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002089 if (always_true_target != nullptr) {
2090 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002091 }
2092 return;
2093 } else {
2094 DCHECK_EQ(cond_value, 0);
2095 }
2096 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002097 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002098 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002099 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002100 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002101 } else {
2102 // The condition instruction has not been materialized, use its inputs as
2103 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002104 Primitive::Type type =
2105 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2106
2107 if (Primitive::IsFloatingPointType(type)) {
2108 // FP compares don't like null false_targets.
2109 if (false_target == nullptr) {
2110 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002111 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002112 FPRegister lhs = InputFPRegisterAt(condition, 0);
2113 if (condition->GetLocations()->InAt(1).IsConstant()) {
2114 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2115 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2116 __ Fcmp(lhs, 0.0);
2117 } else {
2118 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2119 }
2120 if (condition->IsFPConditionTrueIfNaN()) {
2121 __ B(vs, true_target); // VS for unordered.
2122 } else if (condition->IsFPConditionFalseIfNaN()) {
2123 __ B(vs, false_target); // VS for unordered.
2124 }
2125 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002126 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002127 // Integer cases.
2128 Register lhs = InputRegisterAt(condition, 0);
2129 Operand rhs = InputOperandAt(condition, 1);
2130 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2131 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2132 switch (arm64_cond) {
2133 case eq:
2134 __ Cbz(lhs, true_target);
2135 break;
2136 case ne:
2137 __ Cbnz(lhs, true_target);
2138 break;
2139 case lt:
2140 // Test the sign bit and branch accordingly.
2141 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2142 break;
2143 case ge:
2144 // Test the sign bit and branch accordingly.
2145 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2146 break;
2147 default:
2148 // Without the `static_cast` the compiler throws an error for
2149 // `-Werror=sign-promo`.
2150 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2151 }
2152 } else {
2153 __ Cmp(lhs, rhs);
2154 __ B(arm64_cond, true_target);
2155 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002156 }
2157 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002158 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002159 __ B(false_target);
2160 }
2161}
2162
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002163void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2165 HInstruction* cond = if_instr->InputAt(0);
2166 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2167 locations->SetInAt(0, Location::RequiresRegister());
2168 }
2169}
2170
2171void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2172 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2173 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2174 vixl::Label* always_true_target = true_target;
2175 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2176 if_instr->IfTrueSuccessor())) {
2177 always_true_target = nullptr;
2178 }
2179 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2180 if_instr->IfFalseSuccessor())) {
2181 false_target = nullptr;
2182 }
2183 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2184}
2185
2186void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2187 LocationSummary* locations = new (GetGraph()->GetArena())
2188 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2189 HInstruction* cond = deoptimize->InputAt(0);
2190 DCHECK(cond->IsCondition());
2191 if (cond->AsCondition()->NeedsMaterialization()) {
2192 locations->SetInAt(0, Location::RequiresRegister());
2193 }
2194}
2195
2196void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2197 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2198 DeoptimizationSlowPathARM64(deoptimize);
2199 codegen_->AddSlowPath(slow_path);
2200 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2201 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2202}
2203
Alexandre Rames5319def2014-10-23 10:03:10 +01002204void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002205 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002206}
2207
2208void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002209 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002210}
2211
2212void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002213 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002214}
2215
2216void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002217 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002218}
2219
Alexandre Rames67555f72014-11-18 10:55:16 +00002220void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2221 LocationSummary::CallKind call_kind =
2222 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2224 locations->SetInAt(0, Location::RequiresRegister());
2225 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002226 // The output does overlap inputs.
2227 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002228}
2229
2230void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2231 LocationSummary* locations = instruction->GetLocations();
2232 Register obj = InputRegisterAt(instruction, 0);;
2233 Register cls = InputRegisterAt(instruction, 1);;
2234 Register out = OutputRegister(instruction);
2235
2236 vixl::Label done;
2237
2238 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002239 // Avoid null check if we know `obj` is not null.
2240 if (instruction->MustDoNullCheck()) {
2241 __ Mov(out, 0);
2242 __ Cbz(obj, &done);
2243 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002244
2245 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002246 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002247 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002248 __ Cmp(out, cls);
2249 if (instruction->IsClassFinal()) {
2250 // Classes must be equal for the instanceof to succeed.
2251 __ Cset(out, eq);
2252 } else {
2253 // If the classes are not equal, we go into a slow path.
2254 DCHECK(locations->OnlyCallsOnSlowPath());
2255 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002256 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2257 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002258 codegen_->AddSlowPath(slow_path);
2259 __ B(ne, slow_path->GetEntryLabel());
2260 __ Mov(out, 1);
2261 __ Bind(slow_path->GetExitLabel());
2262 }
2263
2264 __ Bind(&done);
2265}
2266
Alexandre Rames5319def2014-10-23 10:03:10 +01002267void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2268 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2269 locations->SetOut(Location::ConstantLocation(constant));
2270}
2271
2272void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2273 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002274 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002275}
2276
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002277void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2279 locations->SetOut(Location::ConstantLocation(constant));
2280}
2281
2282void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2283 // Will be generated at use site.
2284 UNUSED(constant);
2285}
2286
Alexandre Rames5319def2014-10-23 10:03:10 +01002287void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002288 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002289 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002290}
2291
Alexandre Rames67555f72014-11-18 10:55:16 +00002292void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2293 HandleInvoke(invoke);
2294}
2295
2296void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2297 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002298 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2299 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2300 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002301 Location receiver = invoke->GetLocations()->InAt(0);
2302 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002303 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002304
2305 // The register ip1 is required to be used for the hidden argument in
2306 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002307 MacroAssembler* masm = GetVIXLAssembler();
2308 UseScratchRegisterScope scratch_scope(masm);
2309 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002310 scratch_scope.Exclude(ip1);
2311 __ Mov(ip1, invoke->GetDexMethodIndex());
2312
2313 // temp = object->GetClass();
2314 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315 __ Ldr(temp.W(), StackOperandFrom(receiver));
2316 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002317 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002318 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002319 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002320 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002321 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002322 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002324 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002325 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002326 // lr();
2327 __ Blr(lr);
2328 DCHECK(!codegen_->IsLeafMethod());
2329 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2330}
2331
2332void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002333 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2334 if (intrinsic.TryDispatch(invoke)) {
2335 return;
2336 }
2337
Alexandre Rames67555f72014-11-18 10:55:16 +00002338 HandleInvoke(invoke);
2339}
2340
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002341void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002342 // When we do not run baseline, explicit clinit checks triggered by static
2343 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2344 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002345
Andreas Gampe878d58c2015-01-15 23:24:00 -08002346 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2347 if (intrinsic.TryDispatch(invoke)) {
2348 return;
2349 }
2350
Alexandre Rames67555f72014-11-18 10:55:16 +00002351 HandleInvoke(invoke);
2352}
2353
Andreas Gampe878d58c2015-01-15 23:24:00 -08002354static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2355 if (invoke->GetLocations()->Intrinsified()) {
2356 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2357 intrinsic.Dispatch(invoke);
2358 return true;
2359 }
2360 return false;
2361}
2362
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002363void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002364 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002365 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002366
2367 // TODO: Implement all kinds of calls:
2368 // 1) boot -> boot
2369 // 2) app -> boot
2370 // 3) app -> app
2371 //
2372 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2373
Jeff Hao848f70a2014-01-15 13:49:50 -08002374 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002375 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002376 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002377 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002378 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002379 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002380 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002381 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002382 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002383 } else if (invoke->IsRecursive()) {
2384 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002385 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002386 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002387 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002388 Register method_reg;
2389 if (current_method.IsRegister()) {
2390 method_reg = XRegisterFrom(current_method);
2391 } else {
2392 DCHECK(invoke->GetLocations()->Intrinsified());
2393 DCHECK(!current_method.IsValid());
2394 method_reg = reg;
2395 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2396 }
2397
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002398 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002399 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002400 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2401 // temp = temp[index_in_cache];
2402 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2403 // lr = temp->entry_point_from_quick_compiled_code_;
2404 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2405 kArm64WordSize).Int32Value()));
2406 // lr();
2407 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002408 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002409
Andreas Gampe878d58c2015-01-15 23:24:00 -08002410 DCHECK(!IsLeafMethod());
2411}
2412
2413void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002414 // When we do not run baseline, explicit clinit checks triggered by static
2415 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2416 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002417
Andreas Gampe878d58c2015-01-15 23:24:00 -08002418 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2419 return;
2420 }
2421
Alexandre Ramesd921d642015-04-16 15:07:16 +01002422 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002423 LocationSummary* locations = invoke->GetLocations();
2424 codegen_->GenerateStaticOrDirectCall(
2425 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002426 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002427}
2428
2429void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002430 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2431 return;
2432 }
2433
Alexandre Rames5319def2014-10-23 10:03:10 +01002434 LocationSummary* locations = invoke->GetLocations();
2435 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002436 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2437 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2438 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002439 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002440 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002441
Alexandre Ramesd921d642015-04-16 15:07:16 +01002442 BlockPoolsScope block_pools(GetVIXLAssembler());
2443
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002444 DCHECK(receiver.IsRegister());
2445 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002446 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002447 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002448 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002449 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002450 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002451 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002452 // lr();
2453 __ Blr(lr);
2454 DCHECK(!codegen_->IsLeafMethod());
2455 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2456}
2457
Alexandre Rames67555f72014-11-18 10:55:16 +00002458void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2459 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2460 : LocationSummary::kNoCall;
2461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002462 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002463 locations->SetOut(Location::RequiresRegister());
2464}
2465
2466void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2467 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002468 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002469 if (cls->IsReferrersClass()) {
2470 DCHECK(!cls->CanCallRuntime());
2471 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002472 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002473 } else {
2474 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002475 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002476 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002477 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002478
2479 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2480 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2481 codegen_->AddSlowPath(slow_path);
2482 __ Cbz(out, slow_path->GetEntryLabel());
2483 if (cls->MustGenerateClinitCheck()) {
2484 GenerateClassInitializationCheck(slow_path, out);
2485 } else {
2486 __ Bind(slow_path->GetExitLabel());
2487 }
2488 }
2489}
2490
2491void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2492 LocationSummary* locations =
2493 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2494 locations->SetOut(Location::RequiresRegister());
2495}
2496
2497void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2498 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2499 __ Ldr(OutputRegister(instruction), exception);
2500 __ Str(wzr, exception);
2501}
2502
Alexandre Rames5319def2014-10-23 10:03:10 +01002503void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2504 load->SetLocations(nullptr);
2505}
2506
2507void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2508 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002509 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002510}
2511
Alexandre Rames67555f72014-11-18 10:55:16 +00002512void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2513 LocationSummary* locations =
2514 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002515 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002516 locations->SetOut(Location::RequiresRegister());
2517}
2518
2519void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2520 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2521 codegen_->AddSlowPath(slow_path);
2522
2523 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002524 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002525 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002526 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002527 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002528 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002529 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002530 __ Cbz(out, slow_path->GetEntryLabel());
2531 __ Bind(slow_path->GetExitLabel());
2532}
2533
Alexandre Rames5319def2014-10-23 10:03:10 +01002534void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2535 local->SetLocations(nullptr);
2536}
2537
2538void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2539 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2540}
2541
2542void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2544 locations->SetOut(Location::ConstantLocation(constant));
2545}
2546
2547void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2548 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002549 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002550}
2551
Alexandre Rames67555f72014-11-18 10:55:16 +00002552void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2553 LocationSummary* locations =
2554 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2555 InvokeRuntimeCallingConvention calling_convention;
2556 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2557}
2558
2559void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2560 codegen_->InvokeRuntime(instruction->IsEnter()
2561 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2562 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002563 instruction->GetDexPc(),
2564 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002565 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002566}
2567
Alexandre Rames42d641b2014-10-27 14:00:51 +00002568void LocationsBuilderARM64::VisitMul(HMul* mul) {
2569 LocationSummary* locations =
2570 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2571 switch (mul->GetResultType()) {
2572 case Primitive::kPrimInt:
2573 case Primitive::kPrimLong:
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002577 break;
2578
2579 case Primitive::kPrimFloat:
2580 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002581 locations->SetInAt(0, Location::RequiresFpuRegister());
2582 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002583 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002584 break;
2585
2586 default:
2587 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2588 }
2589}
2590
2591void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2592 switch (mul->GetResultType()) {
2593 case Primitive::kPrimInt:
2594 case Primitive::kPrimLong:
2595 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2596 break;
2597
2598 case Primitive::kPrimFloat:
2599 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002600 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002601 break;
2602
2603 default:
2604 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2605 }
2606}
2607
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002608void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2609 LocationSummary* locations =
2610 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2611 switch (neg->GetResultType()) {
2612 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002613 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002614 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002615 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002616 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002617
2618 case Primitive::kPrimFloat:
2619 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002620 locations->SetInAt(0, Location::RequiresFpuRegister());
2621 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002622 break;
2623
2624 default:
2625 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2626 }
2627}
2628
2629void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2630 switch (neg->GetResultType()) {
2631 case Primitive::kPrimInt:
2632 case Primitive::kPrimLong:
2633 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2634 break;
2635
2636 case Primitive::kPrimFloat:
2637 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002638 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002639 break;
2640
2641 default:
2642 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2643 }
2644}
2645
2646void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2649 InvokeRuntimeCallingConvention calling_convention;
2650 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002651 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002652 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002653 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002654 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002655 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002656}
2657
2658void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2659 LocationSummary* locations = instruction->GetLocations();
2660 InvokeRuntimeCallingConvention calling_convention;
2661 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2662 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002663 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002664 // Note: if heap poisoning is enabled, the entry point takes cares
2665 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002666 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002667 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2668 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002669 instruction->GetDexPc(),
2670 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002671 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002672}
2673
Alexandre Rames5319def2014-10-23 10:03:10 +01002674void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2675 LocationSummary* locations =
2676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2677 InvokeRuntimeCallingConvention calling_convention;
2678 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002679 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002680 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002681 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002682}
2683
2684void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2685 LocationSummary* locations = instruction->GetLocations();
2686 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2687 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002688 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002689 // Note: if heap poisoning is enabled, the entry point takes cares
2690 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002691 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002692 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2693 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002694 instruction->GetDexPc(),
2695 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002696 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002697}
2698
2699void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002701 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002702 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002703}
2704
2705void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002706 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002707 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002708 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002709 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002710 break;
2711
2712 default:
2713 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2714 }
2715}
2716
David Brazdil66d126e2015-04-03 16:02:44 +01002717void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2719 locations->SetInAt(0, Location::RequiresRegister());
2720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2721}
2722
2723void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002724 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2725}
2726
Alexandre Rames5319def2014-10-23 10:03:10 +01002727void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2728 LocationSummary* locations =
2729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2730 locations->SetInAt(0, Location::RequiresRegister());
2731 if (instruction->HasUses()) {
2732 locations->SetOut(Location::SameAsFirstInput());
2733 }
2734}
2735
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002736void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002737 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2738 return;
2739 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002740
Alexandre Ramesd921d642015-04-16 15:07:16 +01002741 BlockPoolsScope block_pools(GetVIXLAssembler());
2742 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002743 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2744 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2745}
2746
2747void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002748 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2749 codegen_->AddSlowPath(slow_path);
2750
2751 LocationSummary* locations = instruction->GetLocations();
2752 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002753
2754 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002755}
2756
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002757void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2758 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2759 GenerateImplicitNullCheck(instruction);
2760 } else {
2761 GenerateExplicitNullCheck(instruction);
2762 }
2763}
2764
Alexandre Rames67555f72014-11-18 10:55:16 +00002765void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2766 HandleBinaryOp(instruction);
2767}
2768
2769void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2770 HandleBinaryOp(instruction);
2771}
2772
Alexandre Rames3e69f162014-12-10 10:36:50 +00002773void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2774 LOG(FATAL) << "Unreachable";
2775}
2776
2777void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2778 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2779}
2780
Alexandre Rames5319def2014-10-23 10:03:10 +01002781void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2783 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2784 if (location.IsStackSlot()) {
2785 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2786 } else if (location.IsDoubleStackSlot()) {
2787 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2788 }
2789 locations->SetOut(location);
2790}
2791
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002792void InstructionCodeGeneratorARM64::VisitParameterValue(
2793 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002794 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002795}
2796
2797void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002800 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002801}
2802
2803void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2804 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2805 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002806}
2807
2808void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2809 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2810 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2811 locations->SetInAt(i, Location::Any());
2812 }
2813 locations->SetOut(Location::Any());
2814}
2815
2816void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002817 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002818 LOG(FATAL) << "Unreachable";
2819}
2820
Serban Constantinescu02164b32014-11-13 14:05:07 +00002821void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002822 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002823 LocationSummary::CallKind call_kind =
2824 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2826
2827 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002828 case Primitive::kPrimInt:
2829 case Primitive::kPrimLong:
2830 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002831 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2833 break;
2834
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002835 case Primitive::kPrimFloat:
2836 case Primitive::kPrimDouble: {
2837 InvokeRuntimeCallingConvention calling_convention;
2838 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2839 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2840 locations->SetOut(calling_convention.GetReturnLocation(type));
2841
2842 break;
2843 }
2844
Serban Constantinescu02164b32014-11-13 14:05:07 +00002845 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002846 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002847 }
2848}
2849
2850void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2851 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002852
Serban Constantinescu02164b32014-11-13 14:05:07 +00002853 switch (type) {
2854 case Primitive::kPrimInt:
2855 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002856 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002857 break;
2858 }
2859
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002860 case Primitive::kPrimFloat:
2861 case Primitive::kPrimDouble: {
2862 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2863 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002864 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002865 break;
2866 }
2867
Serban Constantinescu02164b32014-11-13 14:05:07 +00002868 default:
2869 LOG(FATAL) << "Unexpected rem type " << type;
2870 }
2871}
2872
Calin Juravle27df7582015-04-17 19:12:31 +01002873void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2874 memory_barrier->SetLocations(nullptr);
2875}
2876
2877void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2878 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2879}
2880
Alexandre Rames5319def2014-10-23 10:03:10 +01002881void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2883 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002884 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002885}
2886
2887void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002888 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002889 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002890}
2891
2892void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2893 instruction->SetLocations(nullptr);
2894}
2895
2896void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002897 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002898 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002899}
2900
Serban Constantinescu02164b32014-11-13 14:05:07 +00002901void LocationsBuilderARM64::VisitShl(HShl* shl) {
2902 HandleShift(shl);
2903}
2904
2905void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2906 HandleShift(shl);
2907}
2908
2909void LocationsBuilderARM64::VisitShr(HShr* shr) {
2910 HandleShift(shr);
2911}
2912
2913void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2914 HandleShift(shr);
2915}
2916
Alexandre Rames5319def2014-10-23 10:03:10 +01002917void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2919 Primitive::Type field_type = store->InputAt(1)->GetType();
2920 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002921 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002922 case Primitive::kPrimBoolean:
2923 case Primitive::kPrimByte:
2924 case Primitive::kPrimChar:
2925 case Primitive::kPrimShort:
2926 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002927 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002928 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2929 break;
2930
2931 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002932 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002933 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2934 break;
2935
2936 default:
2937 LOG(FATAL) << "Unimplemented local type " << field_type;
2938 }
2939}
2940
2941void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002942 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002943}
2944
2945void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002946 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002947}
2948
2949void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002950 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002951}
2952
Alexandre Rames67555f72014-11-18 10:55:16 +00002953void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002954 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002955}
2956
2957void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002958 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002959}
2960
2961void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002962 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002963}
2964
Alexandre Rames67555f72014-11-18 10:55:16 +00002965void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002966 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002967}
2968
2969void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2970 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2971}
2972
2973void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002974 HBasicBlock* block = instruction->GetBlock();
2975 if (block->GetLoopInformation() != nullptr) {
2976 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2977 // The back edge will generate the suspend check.
2978 return;
2979 }
2980 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2981 // The goto will generate the suspend check.
2982 return;
2983 }
2984 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002985}
2986
2987void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2988 temp->SetLocations(nullptr);
2989}
2990
2991void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2992 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002993 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002994}
2995
Alexandre Rames67555f72014-11-18 10:55:16 +00002996void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2997 LocationSummary* locations =
2998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2999 InvokeRuntimeCallingConvention calling_convention;
3000 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3001}
3002
3003void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3004 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003005 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003006 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003007}
3008
3009void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3010 LocationSummary* locations =
3011 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3012 Primitive::Type input_type = conversion->GetInputType();
3013 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003014 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003015 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3016 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3017 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3018 }
3019
Alexandre Rames542361f2015-01-29 16:57:31 +00003020 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003021 locations->SetInAt(0, Location::RequiresFpuRegister());
3022 } else {
3023 locations->SetInAt(0, Location::RequiresRegister());
3024 }
3025
Alexandre Rames542361f2015-01-29 16:57:31 +00003026 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003027 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3028 } else {
3029 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3030 }
3031}
3032
3033void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3034 Primitive::Type result_type = conversion->GetResultType();
3035 Primitive::Type input_type = conversion->GetInputType();
3036
3037 DCHECK_NE(input_type, result_type);
3038
Alexandre Rames542361f2015-01-29 16:57:31 +00003039 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003040 int result_size = Primitive::ComponentSize(result_type);
3041 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003042 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003043 Register output = OutputRegister(conversion);
3044 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003045 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3046 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3047 } else if ((result_type == Primitive::kPrimChar) ||
3048 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3049 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003050 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003051 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003052 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003053 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003054 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003055 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003056 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3057 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003058 } else if (Primitive::IsFloatingPointType(result_type) &&
3059 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003060 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3061 } else {
3062 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3063 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003064 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003065}
Alexandre Rames67555f72014-11-18 10:55:16 +00003066
Serban Constantinescu02164b32014-11-13 14:05:07 +00003067void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3068 HandleShift(ushr);
3069}
3070
3071void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3072 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003073}
3074
3075void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3076 HandleBinaryOp(instruction);
3077}
3078
3079void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3080 HandleBinaryOp(instruction);
3081}
3082
Calin Juravleb1498f62015-02-16 13:13:29 +00003083void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3084 // Nothing to do, this should be removed during prepare for register allocator.
3085 UNUSED(instruction);
3086 LOG(FATAL) << "Unreachable";
3087}
3088
3089void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3090 // Nothing to do, this should be removed during prepare for register allocator.
3091 UNUSED(instruction);
3092 LOG(FATAL) << "Unreachable";
3093}
3094
Alexandre Rames67555f72014-11-18 10:55:16 +00003095#undef __
3096#undef QUICK_ENTRY_POINT
3097
Alexandre Rames5319def2014-10-23 10:03:10 +01003098} // namespace arm64
3099} // namespace art