blob: 689a35a166a6a79096478172ed34957a1c55b57b [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;
80 default:
81 LOG(FATAL) << "Unknown if condition";
82 }
83 return nv; // Unreachable.
84}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
87 DCHECK_NE(return_type, Primitive::kPrimVoid);
88 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
89 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
90 // but we use the exact registers for clarity.
91 if (return_type == Primitive::kPrimFloat) {
92 return LocationFrom(s0);
93 } else if (return_type == Primitive::kPrimDouble) {
94 return LocationFrom(d0);
95 } else if (return_type == Primitive::kPrimLong) {
96 return LocationFrom(x0);
97 } 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
215 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000216 HBoundsCheck* const instruction_;
217 const Location index_location_;
218 const Location length_location_;
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
221};
222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
224 public:
225 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
226
227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
228 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
229 __ Bind(GetEntryLabel());
230 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000231 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800232 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000233 }
234
235 private:
236 HDivZeroCheck* const instruction_;
237 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
238};
239
240class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
241 public:
242 LoadClassSlowPathARM64(HLoadClass* cls,
243 HInstruction* at,
244 uint32_t dex_pc,
245 bool do_clinit)
246 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
247 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
248 }
249
250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
251 LocationSummary* locations = at_->GetLocations();
252 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
253
254 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000255 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000256
257 InvokeRuntimeCallingConvention calling_convention;
258 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000259 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
260 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000261 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100263 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800264 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100265 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800266 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000267
268 // Move the class to the desired location.
269 Location out = locations->Out();
270 if (out.IsValid()) {
271 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
272 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000273 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 }
275
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 __ B(GetExitLabel());
278 }
279
280 private:
281 // The class this slow path will load.
282 HLoadClass* const cls_;
283
284 // The instruction where this slow path is happening.
285 // (Might be the load class or an initialization check).
286 HInstruction* const at_;
287
288 // The dex PC of `at_`.
289 const uint32_t dex_pc_;
290
291 // Whether to initialize the class.
292 const bool do_clinit_;
293
294 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
295};
296
297class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
298 public:
299 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
300
301 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
302 LocationSummary* locations = instruction_->GetLocations();
303 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
304 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
305
306 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000307 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000308
309 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800310 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000312 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100313 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000315 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 __ B(GetExitLabel());
319 }
320
321 private:
322 HLoadString* const instruction_;
323
324 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
325};
326
Alexandre Rames5319def2014-10-23 10:03:10 +0100327class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
328 public:
329 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
330
Alexandre Rames67555f72014-11-18 10:55:16 +0000331 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
332 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100333 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000335 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800336 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100337 }
338
339 private:
340 HNullCheck* const instruction_;
341
342 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
343};
344
345class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
346 public:
347 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
348 HBasicBlock* successor)
349 : instruction_(instruction), successor_(successor) {}
350
Alexandre Rames67555f72014-11-18 10:55:16 +0000351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100353 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000354 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000356 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800357 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000358 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 if (successor_ == nullptr) {
360 __ B(GetReturnLabel());
361 } else {
362 __ B(arm64_codegen->GetLabelOf(successor_));
363 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100364 }
365
366 vixl::Label* GetReturnLabel() {
367 DCHECK(successor_ == nullptr);
368 return &return_label_;
369 }
370
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100371 HBasicBlock* GetSuccessor() const {
372 return successor_;
373 }
374
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 private:
376 HSuspendCheck* const instruction_;
377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 vixl::Label return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
384};
385
Alexandre Rames67555f72014-11-18 10:55:16 +0000386class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
387 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000388 TypeCheckSlowPathARM64(HInstruction* instruction,
389 Location class_to_check,
390 Location object_class,
391 uint32_t dex_pc)
392 : instruction_(instruction),
393 class_to_check_(class_to_check),
394 object_class_(object_class),
395 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000398 LocationSummary* locations = instruction_->GetLocations();
399 DCHECK(instruction_->IsCheckCast()
400 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
401 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
402
Alexandre Rames67555f72014-11-18 10:55:16 +0000403 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000404 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000405
406 // We're moving two locations to locations that could overlap, so we need a parallel
407 // move resolver.
408 InvokeRuntimeCallingConvention calling_convention;
409 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100410 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
411 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000412
413 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000414 arm64_codegen->InvokeRuntime(
415 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000416 Primitive::Type ret_type = instruction_->GetType();
417 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
418 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800419 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
420 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000421 } else {
422 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000423 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800424 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000425 }
426
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000427 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000428 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000429 }
430
431 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000432 HInstruction* const instruction_;
433 const Location class_to_check_;
434 const Location object_class_;
435 uint32_t dex_pc_;
436
Alexandre Rames67555f72014-11-18 10:55:16 +0000437 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
438};
439
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700440class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
441 public:
442 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
443 : instruction_(instruction) {}
444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
446 __ Bind(GetEntryLabel());
447 SaveLiveRegisters(codegen, instruction_->GetLocations());
448 DCHECK(instruction_->IsDeoptimize());
449 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
450 uint32_t dex_pc = deoptimize->GetDexPc();
451 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
452 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
453 }
454
455 private:
456 HInstruction* const instruction_;
457 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
458};
459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460#undef __
461
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100462Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100463 Location next_location;
464 if (type == Primitive::kPrimVoid) {
465 LOG(FATAL) << "Unreachable type " << type;
466 }
467
Alexandre Rames542361f2015-01-29 16:57:31 +0000468 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100469 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
470 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000471 } else if (!Primitive::IsFloatingPointType(type) &&
472 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000473 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
474 } else {
475 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000476 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
477 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100478 }
479
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000480 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000481 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 return next_location;
483}
484
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100485Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
486 return LocationFrom(x0);
487}
488
Serban Constantinescu579885a2015-02-22 20:51:33 +0000489CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
490 const Arm64InstructionSetFeatures& isa_features,
491 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100492 : CodeGenerator(graph,
493 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000494 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000495 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000496 callee_saved_core_registers.list(),
497 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000498 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100499 block_labels_(nullptr),
500 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000502 move_resolver_(graph->GetArena(), this),
503 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000504 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000505 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000506}
Alexandre Rames5319def2014-10-23 10:03:10 +0100507
Alexandre Rames67555f72014-11-18 10:55:16 +0000508#undef __
509#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100510
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000511void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
512 // Ensure we emit the literal pool.
513 __ FinalizeCode();
514 CodeGenerator::Finalize(allocator);
515}
516
Zheng Xuad4450e2015-04-17 18:48:56 +0800517void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
518 // Note: There are 6 kinds of moves:
519 // 1. constant -> GPR/FPR (non-cycle)
520 // 2. constant -> stack (non-cycle)
521 // 3. GPR/FPR -> GPR/FPR
522 // 4. GPR/FPR -> stack
523 // 5. stack -> GPR/FPR
524 // 6. stack -> stack (non-cycle)
525 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
526 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
527 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
528 // dependency.
529 vixl_temps_.Open(GetVIXLAssembler());
530}
531
532void ParallelMoveResolverARM64::FinishEmitNativeCode() {
533 vixl_temps_.Close();
534}
535
536Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
537 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
538 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
539 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
540 Location scratch = GetScratchLocation(kind);
541 if (!scratch.Equals(Location::NoLocation())) {
542 return scratch;
543 }
544 // Allocate from VIXL temp registers.
545 if (kind == Location::kRegister) {
546 scratch = LocationFrom(vixl_temps_.AcquireX());
547 } else {
548 DCHECK(kind == Location::kFpuRegister);
549 scratch = LocationFrom(vixl_temps_.AcquireD());
550 }
551 AddScratchLocation(scratch);
552 return scratch;
553}
554
555void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
556 if (loc.IsRegister()) {
557 vixl_temps_.Release(XRegisterFrom(loc));
558 } else {
559 DCHECK(loc.IsFpuRegister());
560 vixl_temps_.Release(DRegisterFrom(loc));
561 }
562 RemoveScratchLocation(loc);
563}
564
Alexandre Rames3e69f162014-12-10 10:36:50 +0000565void ParallelMoveResolverARM64::EmitMove(size_t index) {
566 MoveOperands* move = moves_.Get(index);
567 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
568}
569
Alexandre Rames5319def2014-10-23 10:03:10 +0100570void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100571 MacroAssembler* masm = GetVIXLAssembler();
572 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000573 __ Bind(&frame_entry_label_);
574
Serban Constantinescu02164b32014-11-13 14:05:07 +0000575 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
576 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100577 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000578 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000579 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000580 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000581 __ Ldr(wzr, MemOperand(temp, 0));
582 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000583 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100584
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000585 if (!HasEmptyFrame()) {
586 int frame_size = GetFrameSize();
587 // Stack layout:
588 // sp[frame_size - 8] : lr.
589 // ... : other preserved core registers.
590 // ... : other preserved fp registers.
591 // ... : reserved frame space.
592 // sp[0] : current method.
593 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100594 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800595 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
596 frame_size - GetCoreSpillSize());
597 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
598 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000599 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100600}
601
602void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100603 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100604 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000605 if (!HasEmptyFrame()) {
606 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800607 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
608 frame_size - FrameEntrySpillSize());
609 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
610 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000611 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100612 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000613 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100614 __ Ret();
615 GetAssembler()->cfi().RestoreState();
616 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100617}
618
Zheng Xuda403092015-04-24 17:35:39 +0800619vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
620 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
621 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
622 core_spill_mask_);
623}
624
625vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
626 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
627 GetNumberOfFloatingPointRegisters()));
628 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
629 fpu_spill_mask_);
630}
631
Alexandre Rames5319def2014-10-23 10:03:10 +0100632void CodeGeneratorARM64::Bind(HBasicBlock* block) {
633 __ Bind(GetLabelOf(block));
634}
635
Alexandre Rames5319def2014-10-23 10:03:10 +0100636void CodeGeneratorARM64::Move(HInstruction* instruction,
637 Location location,
638 HInstruction* move_for) {
639 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100640 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000641 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100642
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100643 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700644 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100645 } else if (locations != nullptr && locations->Out().Equals(location)) {
646 return;
647 } else if (instruction->IsIntConstant()
648 || instruction->IsLongConstant()
649 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000650 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100651 if (location.IsRegister()) {
652 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000653 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100654 (instruction->IsLongConstant() && dst.Is64Bits()));
655 __ Mov(dst, value);
656 } else {
657 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000658 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000659 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
660 ? temps.AcquireW()
661 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100662 __ Mov(temp, value);
663 __ Str(temp, StackOperandFrom(location));
664 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000665 } else if (instruction->IsTemporary()) {
666 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000667 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100668 } else if (instruction->IsLoadLocal()) {
669 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000670 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000671 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000672 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000673 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100674 }
675
676 } else {
677 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000678 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100679 }
680}
681
Alexandre Rames5319def2014-10-23 10:03:10 +0100682Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
683 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000684
Alexandre Rames5319def2014-10-23 10:03:10 +0100685 switch (type) {
686 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000687 case Primitive::kPrimInt:
688 case Primitive::kPrimFloat:
689 return Location::StackSlot(GetStackSlot(load->GetLocal()));
690
691 case Primitive::kPrimLong:
692 case Primitive::kPrimDouble:
693 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
694
Alexandre Rames5319def2014-10-23 10:03:10 +0100695 case Primitive::kPrimBoolean:
696 case Primitive::kPrimByte:
697 case Primitive::kPrimChar:
698 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100699 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100700 LOG(FATAL) << "Unexpected type " << type;
701 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000702
Alexandre Rames5319def2014-10-23 10:03:10 +0100703 LOG(FATAL) << "Unreachable";
704 return Location::NoLocation();
705}
706
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100707void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000708 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100709 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000710 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100711 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100712 if (value_can_be_null) {
713 __ Cbz(value, &done);
714 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100715 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
716 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000717 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100718 if (value_can_be_null) {
719 __ Bind(&done);
720 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100721}
722
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000723void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
724 // Blocked core registers:
725 // lr : Runtime reserved.
726 // tr : Runtime reserved.
727 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
728 // ip1 : VIXL core temp.
729 // ip0 : VIXL core temp.
730 //
731 // Blocked fp registers:
732 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100733 CPURegList reserved_core_registers = vixl_reserved_core_registers;
734 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100735 while (!reserved_core_registers.IsEmpty()) {
736 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
737 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000738
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000739 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800740 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000741 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
742 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000743
744 if (is_baseline) {
745 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
746 while (!reserved_core_baseline_registers.IsEmpty()) {
747 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
748 }
749
750 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
751 while (!reserved_fp_baseline_registers.IsEmpty()) {
752 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
753 }
754 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100755}
756
757Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
758 if (type == Primitive::kPrimVoid) {
759 LOG(FATAL) << "Unreachable type " << type;
760 }
761
Alexandre Rames542361f2015-01-29 16:57:31 +0000762 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000763 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
764 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100765 return Location::FpuRegisterLocation(reg);
766 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000767 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
768 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100769 return Location::RegisterLocation(reg);
770 }
771}
772
Alexandre Rames3e69f162014-12-10 10:36:50 +0000773size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
774 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
775 __ Str(reg, MemOperand(sp, stack_index));
776 return kArm64WordSize;
777}
778
779size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
780 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
781 __ Ldr(reg, MemOperand(sp, stack_index));
782 return kArm64WordSize;
783}
784
785size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
786 FPRegister reg = FPRegister(reg_id, kDRegSize);
787 __ Str(reg, MemOperand(sp, stack_index));
788 return kArm64WordSize;
789}
790
791size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
792 FPRegister reg = FPRegister(reg_id, kDRegSize);
793 __ Ldr(reg, MemOperand(sp, stack_index));
794 return kArm64WordSize;
795}
796
Alexandre Rames5319def2014-10-23 10:03:10 +0100797void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100798 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100799}
800
801void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100802 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100803}
804
Alexandre Rames67555f72014-11-18 10:55:16 +0000805void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000806 if (constant->IsIntConstant()) {
807 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
808 } else if (constant->IsLongConstant()) {
809 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
810 } else if (constant->IsNullConstant()) {
811 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000812 } else if (constant->IsFloatConstant()) {
813 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
814 } else {
815 DCHECK(constant->IsDoubleConstant());
816 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
817 }
818}
819
Alexandre Rames3e69f162014-12-10 10:36:50 +0000820
821static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
822 DCHECK(constant.IsConstant());
823 HConstant* cst = constant.GetConstant();
824 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000825 // Null is mapped to a core W register, which we associate with kPrimInt.
826 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000827 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
828 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
829 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
830}
831
832void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000833 if (source.Equals(destination)) {
834 return;
835 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000836
837 // A valid move can always be inferred from the destination and source
838 // locations. When moving from and to a register, the argument type can be
839 // used to generate 32bit instead of 64bit moves. In debug mode we also
840 // checks the coherency of the locations and the type.
841 bool unspecified_type = (type == Primitive::kPrimVoid);
842
843 if (destination.IsRegister() || destination.IsFpuRegister()) {
844 if (unspecified_type) {
845 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
846 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000847 (src_cst != nullptr && (src_cst->IsIntConstant()
848 || src_cst->IsFloatConstant()
849 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000850 // For stack slots and 32bit constants, a 64bit type is appropriate.
851 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000852 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000853 // If the source is a double stack slot or a 64bit constant, a 64bit
854 // type is appropriate. Else the source is a register, and since the
855 // type has not been specified, we chose a 64bit type to force a 64bit
856 // move.
857 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000858 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000859 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000860 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
861 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000862 CPURegister dst = CPURegisterFrom(destination, type);
863 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
864 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
865 __ Ldr(dst, StackOperandFrom(source));
866 } else if (source.IsConstant()) {
867 DCHECK(CoherentConstantAndType(source, type));
868 MoveConstant(dst, source.GetConstant());
869 } else {
870 if (destination.IsRegister()) {
871 __ Mov(Register(dst), RegisterFrom(source, type));
872 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800873 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000874 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
875 }
876 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000877 } else { // The destination is not a register. It must be a stack slot.
878 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
879 if (source.IsRegister() || source.IsFpuRegister()) {
880 if (unspecified_type) {
881 if (source.IsRegister()) {
882 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
883 } else {
884 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
885 }
886 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000887 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
888 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000889 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
890 } else if (source.IsConstant()) {
891 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
892 UseScratchRegisterScope temps(GetVIXLAssembler());
893 HConstant* src_cst = source.GetConstant();
894 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000895 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000896 temp = temps.AcquireW();
897 } else if (src_cst->IsLongConstant()) {
898 temp = temps.AcquireX();
899 } else if (src_cst->IsFloatConstant()) {
900 temp = temps.AcquireS();
901 } else {
902 DCHECK(src_cst->IsDoubleConstant());
903 temp = temps.AcquireD();
904 }
905 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000906 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000907 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000908 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000909 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000910 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000911 // There is generally less pressure on FP registers.
912 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000913 __ Ldr(temp, StackOperandFrom(source));
914 __ Str(temp, StackOperandFrom(destination));
915 }
916 }
917}
918
919void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000920 CPURegister dst,
921 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000922 switch (type) {
923 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000924 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000925 break;
926 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000927 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000928 break;
929 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000930 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000931 break;
932 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000933 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000934 break;
935 case Primitive::kPrimInt:
936 case Primitive::kPrimNot:
937 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000938 case Primitive::kPrimFloat:
939 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000940 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000941 __ Ldr(dst, src);
942 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000943 case Primitive::kPrimVoid:
944 LOG(FATAL) << "Unreachable type " << type;
945 }
946}
947
Calin Juravle77520bc2015-01-12 18:45:46 +0000948void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000949 CPURegister dst,
950 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100951 MacroAssembler* masm = GetVIXLAssembler();
952 BlockPoolsScope block_pools(masm);
953 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000954 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000955 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000956
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957 DCHECK(!src.IsPreIndex());
958 DCHECK(!src.IsPostIndex());
959
960 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800961 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000962 MemOperand base = MemOperand(temp_base);
963 switch (type) {
964 case Primitive::kPrimBoolean:
965 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000966 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000967 break;
968 case Primitive::kPrimByte:
969 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000970 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000971 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
972 break;
973 case Primitive::kPrimChar:
974 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000975 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000976 break;
977 case Primitive::kPrimShort:
978 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000979 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000980 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
981 break;
982 case Primitive::kPrimInt:
983 case Primitive::kPrimNot:
984 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000985 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000986 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000987 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000988 break;
989 case Primitive::kPrimFloat:
990 case Primitive::kPrimDouble: {
991 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000992 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000993
994 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
995 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000996 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000997 __ Fmov(FPRegister(dst), temp);
998 break;
999 }
1000 case Primitive::kPrimVoid:
1001 LOG(FATAL) << "Unreachable type " << type;
1002 }
1003}
1004
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001005void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001006 CPURegister src,
1007 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001008 switch (type) {
1009 case Primitive::kPrimBoolean:
1010 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001011 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001012 break;
1013 case Primitive::kPrimChar:
1014 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001015 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001016 break;
1017 case Primitive::kPrimInt:
1018 case Primitive::kPrimNot:
1019 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001020 case Primitive::kPrimFloat:
1021 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001022 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001023 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001024 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001025 case Primitive::kPrimVoid:
1026 LOG(FATAL) << "Unreachable type " << type;
1027 }
1028}
1029
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001030void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1031 CPURegister src,
1032 const MemOperand& dst) {
1033 UseScratchRegisterScope temps(GetVIXLAssembler());
1034 Register temp_base = temps.AcquireX();
1035
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001036 DCHECK(!dst.IsPreIndex());
1037 DCHECK(!dst.IsPostIndex());
1038
1039 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001040 Operand op = OperandFromMemOperand(dst);
1041 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001042 MemOperand base = MemOperand(temp_base);
1043 switch (type) {
1044 case Primitive::kPrimBoolean:
1045 case Primitive::kPrimByte:
1046 __ Stlrb(Register(src), base);
1047 break;
1048 case Primitive::kPrimChar:
1049 case Primitive::kPrimShort:
1050 __ Stlrh(Register(src), base);
1051 break;
1052 case Primitive::kPrimInt:
1053 case Primitive::kPrimNot:
1054 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001055 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001056 __ Stlr(Register(src), base);
1057 break;
1058 case Primitive::kPrimFloat:
1059 case Primitive::kPrimDouble: {
1060 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001061 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001062
1063 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1064 __ Fmov(temp, FPRegister(src));
1065 __ Stlr(temp, base);
1066 break;
1067 }
1068 case Primitive::kPrimVoid:
1069 LOG(FATAL) << "Unreachable type " << type;
1070 }
1071}
1072
Alexandre Rames67555f72014-11-18 10:55:16 +00001073void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001074 DCHECK(RequiresCurrentMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075 CHECK(current_method.IsX());
Alexandre Rames67555f72014-11-18 10:55:16 +00001076 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
1077}
1078
1079void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1080 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001081 uint32_t dex_pc,
1082 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001083 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001084 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1085 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001086 RecordPcInfo(instruction, dex_pc, slow_path);
1087 DCHECK(instruction->IsSuspendCheck()
1088 || instruction->IsBoundsCheck()
1089 || instruction->IsNullCheck()
1090 || instruction->IsDivZeroCheck()
1091 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001092}
1093
1094void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1095 vixl::Register class_reg) {
1096 UseScratchRegisterScope temps(GetVIXLAssembler());
1097 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001098 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001099 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001100
Serban Constantinescu02164b32014-11-13 14:05:07 +00001101 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001102 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001103 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1104 __ Add(temp, class_reg, status_offset);
1105 __ Ldar(temp, HeapOperand(temp));
1106 __ Cmp(temp, mirror::Class::kStatusInitialized);
1107 __ B(lt, slow_path->GetEntryLabel());
1108 } else {
1109 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1110 __ Cmp(temp, mirror::Class::kStatusInitialized);
1111 __ B(lt, slow_path->GetEntryLabel());
1112 __ Dmb(InnerShareable, BarrierReads);
1113 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001114 __ Bind(slow_path->GetExitLabel());
1115}
Alexandre Rames5319def2014-10-23 10:03:10 +01001116
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001117void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1118 BarrierType type = BarrierAll;
1119
1120 switch (kind) {
1121 case MemBarrierKind::kAnyAny:
1122 case MemBarrierKind::kAnyStore: {
1123 type = BarrierAll;
1124 break;
1125 }
1126 case MemBarrierKind::kLoadAny: {
1127 type = BarrierReads;
1128 break;
1129 }
1130 case MemBarrierKind::kStoreStore: {
1131 type = BarrierWrites;
1132 break;
1133 }
1134 default:
1135 LOG(FATAL) << "Unexpected memory barrier " << kind;
1136 }
1137 __ Dmb(InnerShareable, type);
1138}
1139
Serban Constantinescu02164b32014-11-13 14:05:07 +00001140void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1141 HBasicBlock* successor) {
1142 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001143 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1144 if (slow_path == nullptr) {
1145 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1146 instruction->SetSlowPath(slow_path);
1147 codegen_->AddSlowPath(slow_path);
1148 if (successor != nullptr) {
1149 DCHECK(successor->IsLoopHeader());
1150 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1151 }
1152 } else {
1153 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1154 }
1155
Serban Constantinescu02164b32014-11-13 14:05:07 +00001156 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1157 Register temp = temps.AcquireW();
1158
1159 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1160 if (successor == nullptr) {
1161 __ Cbnz(temp, slow_path->GetEntryLabel());
1162 __ Bind(slow_path->GetReturnLabel());
1163 } else {
1164 __ Cbz(temp, codegen_->GetLabelOf(successor));
1165 __ B(slow_path->GetEntryLabel());
1166 // slow_path will return to GetLabelOf(successor).
1167 }
1168}
1169
Alexandre Rames5319def2014-10-23 10:03:10 +01001170InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1171 CodeGeneratorARM64* codegen)
1172 : HGraphVisitor(graph),
1173 assembler_(codegen->GetAssembler()),
1174 codegen_(codegen) {}
1175
1176#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001177 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001178
1179#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1180
1181enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001182 // Using a base helps identify when we hit such breakpoints.
1183 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001184#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1185 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1186#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1187};
1188
1189#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1190 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001191 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001192 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1193 } \
1194 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1196 locations->SetOut(Location::Any()); \
1197 }
1198 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1199#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1200
1201#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001202#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001203
Alexandre Rames67555f72014-11-18 10:55:16 +00001204void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001205 DCHECK_EQ(instr->InputCount(), 2U);
1206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1207 Primitive::Type type = instr->GetResultType();
1208 switch (type) {
1209 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001210 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001211 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001212 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001214 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001215
1216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
1218 locations->SetInAt(0, Location::RequiresFpuRegister());
1219 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001220 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001221 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001222
Alexandre Rames5319def2014-10-23 10:03:10 +01001223 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001224 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001225 }
1226}
1227
Alexandre Rames09a99962015-04-15 11:47:56 +01001228void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1229 LocationSummary* locations =
1230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1231 locations->SetInAt(0, Location::RequiresRegister());
1232 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1233 locations->SetOut(Location::RequiresFpuRegister());
1234 } else {
1235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1236 }
1237}
1238
1239void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1240 const FieldInfo& field_info) {
1241 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001242 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001243
1244 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1245 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1246
1247 if (field_info.IsVolatile()) {
1248 if (use_acquire_release) {
1249 // NB: LoadAcquire will record the pc info if needed.
1250 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1251 } else {
1252 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1253 codegen_->MaybeRecordImplicitNullCheck(instruction);
1254 // For IRIW sequential consistency kLoadAny is not sufficient.
1255 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1256 }
1257 } else {
1258 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1259 codegen_->MaybeRecordImplicitNullCheck(instruction);
1260 }
1261}
1262
1263void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1264 LocationSummary* locations =
1265 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1266 locations->SetInAt(0, Location::RequiresRegister());
1267 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1268 locations->SetInAt(1, Location::RequiresFpuRegister());
1269 } else {
1270 locations->SetInAt(1, Location::RequiresRegister());
1271 }
1272}
1273
1274void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001275 const FieldInfo& field_info,
1276 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001277 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001278 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001279
1280 Register obj = InputRegisterAt(instruction, 0);
1281 CPURegister value = InputCPURegisterAt(instruction, 1);
1282 Offset offset = field_info.GetFieldOffset();
1283 Primitive::Type field_type = field_info.GetFieldType();
1284 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1285
1286 if (field_info.IsVolatile()) {
1287 if (use_acquire_release) {
1288 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1289 codegen_->MaybeRecordImplicitNullCheck(instruction);
1290 } else {
1291 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1292 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1293 codegen_->MaybeRecordImplicitNullCheck(instruction);
1294 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1295 }
1296 } else {
1297 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1298 codegen_->MaybeRecordImplicitNullCheck(instruction);
1299 }
1300
1301 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001302 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001303 }
1304}
1305
Alexandre Rames67555f72014-11-18 10:55:16 +00001306void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001307 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001308
1309 switch (type) {
1310 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001311 case Primitive::kPrimLong: {
1312 Register dst = OutputRegister(instr);
1313 Register lhs = InputRegisterAt(instr, 0);
1314 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001315 if (instr->IsAdd()) {
1316 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001317 } else if (instr->IsAnd()) {
1318 __ And(dst, lhs, rhs);
1319 } else if (instr->IsOr()) {
1320 __ Orr(dst, lhs, rhs);
1321 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001322 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001323 } else {
1324 DCHECK(instr->IsXor());
1325 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001326 }
1327 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001328 }
1329 case Primitive::kPrimFloat:
1330 case Primitive::kPrimDouble: {
1331 FPRegister dst = OutputFPRegister(instr);
1332 FPRegister lhs = InputFPRegisterAt(instr, 0);
1333 FPRegister rhs = InputFPRegisterAt(instr, 1);
1334 if (instr->IsAdd()) {
1335 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001336 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001337 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001338 } else {
1339 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001340 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001341 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001342 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001343 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 }
1346}
1347
Serban Constantinescu02164b32014-11-13 14:05:07 +00001348void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1349 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1350
1351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1352 Primitive::Type type = instr->GetResultType();
1353 switch (type) {
1354 case Primitive::kPrimInt:
1355 case Primitive::kPrimLong: {
1356 locations->SetInAt(0, Location::RequiresRegister());
1357 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1358 locations->SetOut(Location::RequiresRegister());
1359 break;
1360 }
1361 default:
1362 LOG(FATAL) << "Unexpected shift type " << type;
1363 }
1364}
1365
1366void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1367 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1368
1369 Primitive::Type type = instr->GetType();
1370 switch (type) {
1371 case Primitive::kPrimInt:
1372 case Primitive::kPrimLong: {
1373 Register dst = OutputRegister(instr);
1374 Register lhs = InputRegisterAt(instr, 0);
1375 Operand rhs = InputOperandAt(instr, 1);
1376 if (rhs.IsImmediate()) {
1377 uint32_t shift_value = (type == Primitive::kPrimInt)
1378 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1379 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1380 if (instr->IsShl()) {
1381 __ Lsl(dst, lhs, shift_value);
1382 } else if (instr->IsShr()) {
1383 __ Asr(dst, lhs, shift_value);
1384 } else {
1385 __ Lsr(dst, lhs, shift_value);
1386 }
1387 } else {
1388 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1389
1390 if (instr->IsShl()) {
1391 __ Lsl(dst, lhs, rhs_reg);
1392 } else if (instr->IsShr()) {
1393 __ Asr(dst, lhs, rhs_reg);
1394 } else {
1395 __ Lsr(dst, lhs, rhs_reg);
1396 }
1397 }
1398 break;
1399 }
1400 default:
1401 LOG(FATAL) << "Unexpected shift operation type " << type;
1402 }
1403}
1404
Alexandre Rames5319def2014-10-23 10:03:10 +01001405void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001406 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001407}
1408
1409void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001410 HandleBinaryOp(instruction);
1411}
1412
1413void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1414 HandleBinaryOp(instruction);
1415}
1416
1417void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1418 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001419}
1420
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1424 locations->SetInAt(0, Location::RequiresRegister());
1425 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001426 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1427 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1428 } else {
1429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1430 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001431}
1432
1433void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1434 LocationSummary* locations = instruction->GetLocations();
1435 Primitive::Type type = instruction->GetType();
1436 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001437 Location index = locations->InAt(1);
1438 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001439 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001440 MacroAssembler* masm = GetVIXLAssembler();
1441 UseScratchRegisterScope temps(masm);
1442 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001443
1444 if (index.IsConstant()) {
1445 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001446 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001447 } else {
1448 Register temp = temps.AcquireSameSizeAs(obj);
1449 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1450 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001451 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001452 }
1453
Alexandre Rames67555f72014-11-18 10:55:16 +00001454 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001455 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001456}
1457
Alexandre Rames5319def2014-10-23 10:03:10 +01001458void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1460 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001462}
1463
1464void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001465 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001466 __ Ldr(OutputRegister(instruction),
1467 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001468 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001469}
1470
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001471void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001472 if (instruction->NeedsTypeCheck()) {
1473 LocationSummary* locations =
1474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001475 InvokeRuntimeCallingConvention calling_convention;
1476 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1477 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1478 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1479 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001480 LocationSummary* locations =
1481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001482 locations->SetInAt(0, Location::RequiresRegister());
1483 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001484 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1485 locations->SetInAt(2, Location::RequiresFpuRegister());
1486 } else {
1487 locations->SetInAt(2, Location::RequiresRegister());
1488 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001489 }
1490}
1491
1492void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1493 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001494 LocationSummary* locations = instruction->GetLocations();
1495 bool needs_runtime_call = locations->WillCall();
1496
1497 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001498 codegen_->InvokeRuntime(
1499 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001500 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001501 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001502 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001503 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001504 Location index = locations->InAt(1);
1505 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001506 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001507 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001508 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001509 {
1510 // We use a block to end the scratch scope before the write barrier, thus
1511 // freeing the temporary registers so they can be used in `MarkGCCard`.
1512 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001513
Alexandre Rames97833a02015-04-16 15:07:12 +01001514 if (index.IsConstant()) {
1515 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1516 destination = HeapOperand(obj, offset);
1517 } else {
1518 Register temp = temps.AcquireSameSizeAs(obj);
1519 Register index_reg = InputRegisterAt(instruction, 1);
1520 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1521 destination = HeapOperand(temp, offset);
1522 }
1523
1524 codegen_->Store(value_type, value, destination);
1525 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001526 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001527 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001528 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001529 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001530 }
1531}
1532
Alexandre Rames67555f72014-11-18 10:55:16 +00001533void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1534 LocationSummary* locations =
1535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1536 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001537 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001538 if (instruction->HasUses()) {
1539 locations->SetOut(Location::SameAsFirstInput());
1540 }
1541}
1542
1543void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001544 LocationSummary* locations = instruction->GetLocations();
1545 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1546 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001547 codegen_->AddSlowPath(slow_path);
1548
1549 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1550 __ B(slow_path->GetEntryLabel(), hs);
1551}
1552
1553void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1555 instruction, LocationSummary::kCallOnSlowPath);
1556 locations->SetInAt(0, Location::RequiresRegister());
1557 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001559}
1560
1561void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001562 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001563 Register obj = InputRegisterAt(instruction, 0);;
1564 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001565 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001566
Alexandre Rames3e69f162014-12-10 10:36:50 +00001567 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1568 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 codegen_->AddSlowPath(slow_path);
1570
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001571 // Avoid null check if we know obj is not null.
1572 if (instruction->MustDoNullCheck()) {
1573 __ Cbz(obj, slow_path->GetExitLabel());
1574 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001575 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001576 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1577 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001578 __ B(ne, slow_path->GetEntryLabel());
1579 __ Bind(slow_path->GetExitLabel());
1580}
1581
1582void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1583 LocationSummary* locations =
1584 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1585 locations->SetInAt(0, Location::RequiresRegister());
1586 if (check->HasUses()) {
1587 locations->SetOut(Location::SameAsFirstInput());
1588 }
1589}
1590
1591void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1592 // We assume the class is not null.
1593 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1594 check->GetLoadClass(), check, check->GetDexPc(), true);
1595 codegen_->AddSlowPath(slow_path);
1596 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1597}
1598
Serban Constantinescu02164b32014-11-13 14:05:07 +00001599void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001601 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1602 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001603 switch (in_type) {
1604 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001605 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001606 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1608 break;
1609 }
1610 case Primitive::kPrimFloat:
1611 case Primitive::kPrimDouble: {
1612 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001613 HInstruction* right = compare->InputAt(1);
1614 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1615 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1616 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1617 } else {
1618 locations->SetInAt(1, Location::RequiresFpuRegister());
1619 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001620 locations->SetOut(Location::RequiresRegister());
1621 break;
1622 }
1623 default:
1624 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1625 }
1626}
1627
1628void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1629 Primitive::Type in_type = compare->InputAt(0)->GetType();
1630
1631 // 0 if: left == right
1632 // 1 if: left > right
1633 // -1 if: left < right
1634 switch (in_type) {
1635 case Primitive::kPrimLong: {
1636 Register result = OutputRegister(compare);
1637 Register left = InputRegisterAt(compare, 0);
1638 Operand right = InputOperandAt(compare, 1);
1639
1640 __ Cmp(left, right);
1641 __ Cset(result, ne);
1642 __ Cneg(result, result, lt);
1643 break;
1644 }
1645 case Primitive::kPrimFloat:
1646 case Primitive::kPrimDouble: {
1647 Register result = OutputRegister(compare);
1648 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001649 if (compare->GetLocations()->InAt(1).IsConstant()) {
1650 if (kIsDebugBuild) {
1651 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1652 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1653 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1654 }
1655 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1656 __ Fcmp(left, 0.0);
1657 } else {
1658 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1659 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001660 if (compare->IsGtBias()) {
1661 __ Cset(result, ne);
1662 } else {
1663 __ Csetm(result, ne);
1664 }
1665 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001666 break;
1667 }
1668 default:
1669 LOG(FATAL) << "Unimplemented compare type " << in_type;
1670 }
1671}
1672
1673void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1675 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001676 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001677 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001679 }
1680}
1681
1682void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1683 if (!instruction->NeedsMaterialization()) {
1684 return;
1685 }
1686
1687 LocationSummary* locations = instruction->GetLocations();
1688 Register lhs = InputRegisterAt(instruction, 0);
1689 Operand rhs = InputOperandAt(instruction, 1);
1690 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1691 Condition cond = ARM64Condition(instruction->GetCondition());
1692
1693 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001694 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001695}
1696
1697#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1698 M(Equal) \
1699 M(NotEqual) \
1700 M(LessThan) \
1701 M(LessThanOrEqual) \
1702 M(GreaterThan) \
1703 M(GreaterThanOrEqual)
1704#define DEFINE_CONDITION_VISITORS(Name) \
1705void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1706void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1707FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001708#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001709#undef FOR_EACH_CONDITION_INSTRUCTION
1710
Zheng Xuc6667102015-05-15 16:08:45 +08001711void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1712 DCHECK(instruction->IsDiv() || instruction->IsRem());
1713
1714 LocationSummary* locations = instruction->GetLocations();
1715 Location second = locations->InAt(1);
1716 DCHECK(second.IsConstant());
1717
1718 Register out = OutputRegister(instruction);
1719 Register dividend = InputRegisterAt(instruction, 0);
1720 int64_t imm = Int64FromConstant(second.GetConstant());
1721 DCHECK(imm == 1 || imm == -1);
1722
1723 if (instruction->IsRem()) {
1724 __ Mov(out, 0);
1725 } else {
1726 if (imm == 1) {
1727 __ Mov(out, dividend);
1728 } else {
1729 __ Neg(out, dividend);
1730 }
1731 }
1732}
1733
1734void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1735 DCHECK(instruction->IsDiv() || instruction->IsRem());
1736
1737 LocationSummary* locations = instruction->GetLocations();
1738 Location second = locations->InAt(1);
1739 DCHECK(second.IsConstant());
1740
1741 Register out = OutputRegister(instruction);
1742 Register dividend = InputRegisterAt(instruction, 0);
1743 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001744 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001745 DCHECK(IsPowerOfTwo(abs_imm));
1746 int ctz_imm = CTZ(abs_imm);
1747
1748 UseScratchRegisterScope temps(GetVIXLAssembler());
1749 Register temp = temps.AcquireSameSizeAs(out);
1750
1751 if (instruction->IsDiv()) {
1752 __ Add(temp, dividend, abs_imm - 1);
1753 __ Cmp(dividend, 0);
1754 __ Csel(out, temp, dividend, lt);
1755 if (imm > 0) {
1756 __ Asr(out, out, ctz_imm);
1757 } else {
1758 __ Neg(out, Operand(out, ASR, ctz_imm));
1759 }
1760 } else {
1761 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1762 __ Asr(temp, dividend, bits - 1);
1763 __ Lsr(temp, temp, bits - ctz_imm);
1764 __ Add(out, dividend, temp);
1765 __ And(out, out, abs_imm - 1);
1766 __ Sub(out, out, temp);
1767 }
1768}
1769
1770void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1771 DCHECK(instruction->IsDiv() || instruction->IsRem());
1772
1773 LocationSummary* locations = instruction->GetLocations();
1774 Location second = locations->InAt(1);
1775 DCHECK(second.IsConstant());
1776
1777 Register out = OutputRegister(instruction);
1778 Register dividend = InputRegisterAt(instruction, 0);
1779 int64_t imm = Int64FromConstant(second.GetConstant());
1780
1781 Primitive::Type type = instruction->GetResultType();
1782 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1783
1784 int64_t magic;
1785 int shift;
1786 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1787
1788 UseScratchRegisterScope temps(GetVIXLAssembler());
1789 Register temp = temps.AcquireSameSizeAs(out);
1790
1791 // temp = get_high(dividend * magic)
1792 __ Mov(temp, magic);
1793 if (type == Primitive::kPrimLong) {
1794 __ Smulh(temp, dividend, temp);
1795 } else {
1796 __ Smull(temp.X(), dividend, temp);
1797 __ Lsr(temp.X(), temp.X(), 32);
1798 }
1799
1800 if (imm > 0 && magic < 0) {
1801 __ Add(temp, temp, dividend);
1802 } else if (imm < 0 && magic > 0) {
1803 __ Sub(temp, temp, dividend);
1804 }
1805
1806 if (shift != 0) {
1807 __ Asr(temp, temp, shift);
1808 }
1809
1810 if (instruction->IsDiv()) {
1811 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1812 } else {
1813 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1814 // TODO: Strength reduction for msub.
1815 Register temp_imm = temps.AcquireSameSizeAs(out);
1816 __ Mov(temp_imm, imm);
1817 __ Msub(out, temp, temp_imm, dividend);
1818 }
1819}
1820
1821void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1822 DCHECK(instruction->IsDiv() || instruction->IsRem());
1823 Primitive::Type type = instruction->GetResultType();
1824 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1825
1826 LocationSummary* locations = instruction->GetLocations();
1827 Register out = OutputRegister(instruction);
1828 Location second = locations->InAt(1);
1829
1830 if (second.IsConstant()) {
1831 int64_t imm = Int64FromConstant(second.GetConstant());
1832
1833 if (imm == 0) {
1834 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1835 } else if (imm == 1 || imm == -1) {
1836 DivRemOneOrMinusOne(instruction);
1837 } else if (IsPowerOfTwo(std::abs(imm))) {
1838 DivRemByPowerOfTwo(instruction);
1839 } else {
1840 DCHECK(imm <= -2 || imm >= 2);
1841 GenerateDivRemWithAnyConstant(instruction);
1842 }
1843 } else {
1844 Register dividend = InputRegisterAt(instruction, 0);
1845 Register divisor = InputRegisterAt(instruction, 1);
1846 if (instruction->IsDiv()) {
1847 __ Sdiv(out, dividend, divisor);
1848 } else {
1849 UseScratchRegisterScope temps(GetVIXLAssembler());
1850 Register temp = temps.AcquireSameSizeAs(out);
1851 __ Sdiv(temp, dividend, divisor);
1852 __ Msub(out, temp, divisor, dividend);
1853 }
1854 }
1855}
1856
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001857void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1858 LocationSummary* locations =
1859 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1860 switch (div->GetResultType()) {
1861 case Primitive::kPrimInt:
1862 case Primitive::kPrimLong:
1863 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001864 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1866 break;
1867
1868 case Primitive::kPrimFloat:
1869 case Primitive::kPrimDouble:
1870 locations->SetInAt(0, Location::RequiresFpuRegister());
1871 locations->SetInAt(1, Location::RequiresFpuRegister());
1872 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1873 break;
1874
1875 default:
1876 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1877 }
1878}
1879
1880void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1881 Primitive::Type type = div->GetResultType();
1882 switch (type) {
1883 case Primitive::kPrimInt:
1884 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001885 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001886 break;
1887
1888 case Primitive::kPrimFloat:
1889 case Primitive::kPrimDouble:
1890 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1891 break;
1892
1893 default:
1894 LOG(FATAL) << "Unexpected div type " << type;
1895 }
1896}
1897
Alexandre Rames67555f72014-11-18 10:55:16 +00001898void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1899 LocationSummary* locations =
1900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1901 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1902 if (instruction->HasUses()) {
1903 locations->SetOut(Location::SameAsFirstInput());
1904 }
1905}
1906
1907void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1908 SlowPathCodeARM64* slow_path =
1909 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1910 codegen_->AddSlowPath(slow_path);
1911 Location value = instruction->GetLocations()->InAt(0);
1912
Alexandre Rames3e69f162014-12-10 10:36:50 +00001913 Primitive::Type type = instruction->GetType();
1914
1915 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1916 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1917 return;
1918 }
1919
Alexandre Rames67555f72014-11-18 10:55:16 +00001920 if (value.IsConstant()) {
1921 int64_t divisor = Int64ConstantFrom(value);
1922 if (divisor == 0) {
1923 __ B(slow_path->GetEntryLabel());
1924 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001925 // A division by a non-null constant is valid. We don't need to perform
1926 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001927 }
1928 } else {
1929 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1930 }
1931}
1932
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001933void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1934 LocationSummary* locations =
1935 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1936 locations->SetOut(Location::ConstantLocation(constant));
1937}
1938
1939void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1940 UNUSED(constant);
1941 // Will be generated at use site.
1942}
1943
Alexandre Rames5319def2014-10-23 10:03:10 +01001944void LocationsBuilderARM64::VisitExit(HExit* exit) {
1945 exit->SetLocations(nullptr);
1946}
1947
1948void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001949 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001950}
1951
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001952void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1953 LocationSummary* locations =
1954 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1955 locations->SetOut(Location::ConstantLocation(constant));
1956}
1957
1958void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1959 UNUSED(constant);
1960 // Will be generated at use site.
1961}
1962
Alexandre Rames5319def2014-10-23 10:03:10 +01001963void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1964 got->SetLocations(nullptr);
1965}
1966
1967void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1968 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001969 DCHECK(!successor->IsExitBlock());
1970 HBasicBlock* block = got->GetBlock();
1971 HInstruction* previous = got->GetPrevious();
1972 HLoopInformation* info = block->GetLoopInformation();
1973
David Brazdil46e2a392015-03-16 17:31:52 +00001974 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001975 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1976 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1977 return;
1978 }
1979 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1980 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1981 }
1982 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001983 __ B(codegen_->GetLabelOf(successor));
1984 }
1985}
1986
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001987void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1988 vixl::Label* true_target,
1989 vixl::Label* false_target,
1990 vixl::Label* always_true_target) {
1991 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001992 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001993
Serban Constantinescu02164b32014-11-13 14:05:07 +00001994 if (cond->IsIntConstant()) {
1995 int32_t cond_value = cond->AsIntConstant()->GetValue();
1996 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001997 if (always_true_target != nullptr) {
1998 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001999 }
2000 return;
2001 } else {
2002 DCHECK_EQ(cond_value, 0);
2003 }
2004 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002005 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002006 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002007 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002008 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002009 } else {
2010 // The condition instruction has not been materialized, use its inputs as
2011 // the comparison and its condition as the branch condition.
2012 Register lhs = InputRegisterAt(condition, 0);
2013 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002014 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00002015 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2016 switch (arm64_cond) {
2017 case eq:
2018 __ Cbz(lhs, true_target);
2019 break;
2020 case ne:
2021 __ Cbnz(lhs, true_target);
2022 break;
2023 case lt:
2024 // Test the sign bit and branch accordingly.
2025 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2026 break;
2027 case ge:
2028 // Test the sign bit and branch accordingly.
2029 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2030 break;
2031 default:
2032 // Without the `static_cast` the compiler throws an error for
2033 // `-Werror=sign-promo`.
2034 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002035 }
2036 } else {
2037 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002038 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002039 }
2040 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002041 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002042 __ B(false_target);
2043 }
2044}
2045
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002046void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2048 HInstruction* cond = if_instr->InputAt(0);
2049 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2050 locations->SetInAt(0, Location::RequiresRegister());
2051 }
2052}
2053
2054void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2055 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2056 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2057 vixl::Label* always_true_target = true_target;
2058 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2059 if_instr->IfTrueSuccessor())) {
2060 always_true_target = nullptr;
2061 }
2062 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2063 if_instr->IfFalseSuccessor())) {
2064 false_target = nullptr;
2065 }
2066 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2067}
2068
2069void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2070 LocationSummary* locations = new (GetGraph()->GetArena())
2071 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2072 HInstruction* cond = deoptimize->InputAt(0);
2073 DCHECK(cond->IsCondition());
2074 if (cond->AsCondition()->NeedsMaterialization()) {
2075 locations->SetInAt(0, Location::RequiresRegister());
2076 }
2077}
2078
2079void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2080 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2081 DeoptimizationSlowPathARM64(deoptimize);
2082 codegen_->AddSlowPath(slow_path);
2083 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2084 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2085}
2086
Alexandre Rames5319def2014-10-23 10:03:10 +01002087void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002088 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002089}
2090
2091void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002092 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002093}
2094
2095void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002096 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002097}
2098
2099void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002100 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002101}
2102
Alexandre Rames67555f72014-11-18 10:55:16 +00002103void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2104 LocationSummary::CallKind call_kind =
2105 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2107 locations->SetInAt(0, Location::RequiresRegister());
2108 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002109 // The output does overlap inputs.
2110 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002111}
2112
2113void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2114 LocationSummary* locations = instruction->GetLocations();
2115 Register obj = InputRegisterAt(instruction, 0);;
2116 Register cls = InputRegisterAt(instruction, 1);;
2117 Register out = OutputRegister(instruction);
2118
2119 vixl::Label done;
2120
2121 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002122 // Avoid null check if we know `obj` is not null.
2123 if (instruction->MustDoNullCheck()) {
2124 __ Mov(out, 0);
2125 __ Cbz(obj, &done);
2126 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002127
2128 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002129 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002130 __ Cmp(out, cls);
2131 if (instruction->IsClassFinal()) {
2132 // Classes must be equal for the instanceof to succeed.
2133 __ Cset(out, eq);
2134 } else {
2135 // If the classes are not equal, we go into a slow path.
2136 DCHECK(locations->OnlyCallsOnSlowPath());
2137 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002138 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2139 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002140 codegen_->AddSlowPath(slow_path);
2141 __ B(ne, slow_path->GetEntryLabel());
2142 __ Mov(out, 1);
2143 __ Bind(slow_path->GetExitLabel());
2144 }
2145
2146 __ Bind(&done);
2147}
2148
Alexandre Rames5319def2014-10-23 10:03:10 +01002149void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2151 locations->SetOut(Location::ConstantLocation(constant));
2152}
2153
2154void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2155 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002156 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002157}
2158
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002159void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2161 locations->SetOut(Location::ConstantLocation(constant));
2162}
2163
2164void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2165 // Will be generated at use site.
2166 UNUSED(constant);
2167}
2168
Alexandre Rames5319def2014-10-23 10:03:10 +01002169void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002170 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002171 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002172}
2173
Alexandre Rames67555f72014-11-18 10:55:16 +00002174void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2175 HandleInvoke(invoke);
2176}
2177
2178void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2179 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002180 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2181 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2182 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002183 Location receiver = invoke->GetLocations()->InAt(0);
2184 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002185 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002186
2187 // The register ip1 is required to be used for the hidden argument in
2188 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002189 MacroAssembler* masm = GetVIXLAssembler();
2190 UseScratchRegisterScope scratch_scope(masm);
2191 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002192 scratch_scope.Exclude(ip1);
2193 __ Mov(ip1, invoke->GetDexMethodIndex());
2194
2195 // temp = object->GetClass();
2196 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002197 __ Ldr(temp.W(), StackOperandFrom(receiver));
2198 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002199 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002200 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002201 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002202 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00002203 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002204 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002205 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002206 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002207 // lr();
2208 __ Blr(lr);
2209 DCHECK(!codegen_->IsLeafMethod());
2210 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2211}
2212
2213void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002214 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2215 if (intrinsic.TryDispatch(invoke)) {
2216 return;
2217 }
2218
Alexandre Rames67555f72014-11-18 10:55:16 +00002219 HandleInvoke(invoke);
2220}
2221
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002222void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002223 // When we do not run baseline, explicit clinit checks triggered by static
2224 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2225 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002226
Andreas Gampe878d58c2015-01-15 23:24:00 -08002227 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2228 if (intrinsic.TryDispatch(invoke)) {
2229 return;
2230 }
2231
Alexandre Rames67555f72014-11-18 10:55:16 +00002232 HandleInvoke(invoke);
2233}
2234
Andreas Gampe878d58c2015-01-15 23:24:00 -08002235static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2236 if (invoke->GetLocations()->Intrinsified()) {
2237 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2238 intrinsic.Dispatch(invoke);
2239 return true;
2240 }
2241 return false;
2242}
2243
2244void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
2245 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
2246 DCHECK(temp.Is(kArtMethodRegister));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002247 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002248
2249 // TODO: Implement all kinds of calls:
2250 // 1) boot -> boot
2251 // 2) app -> boot
2252 // 3) app -> app
2253 //
2254 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2255
Jeff Hao848f70a2014-01-15 13:49:50 -08002256 if (invoke->IsStringInit()) {
2257 // temp = thread->string_init_entrypoint
Mathieu Chartiere401d142015-04-22 13:56:20 -07002258 __ Ldr(temp.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002259 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002260 __ Ldr(lr, MemOperand(
2261 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002262 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002263 __ Blr(lr);
2264 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002265 // temp = method;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002266 LoadCurrentMethod(temp.X());
Jeff Hao848f70a2014-01-15 13:49:50 -08002267 if (!invoke->IsRecursive()) {
2268 // temp = temp->dex_cache_resolved_methods_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002269 __ Ldr(temp.W(), MemOperand(temp.X(),
2270 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002271 // temp = temp[index_in_cache];
Mathieu Chartiere401d142015-04-22 13:56:20 -07002272 __ Ldr(temp.X(), MemOperand(temp, index_in_cache));
Jeff Hao848f70a2014-01-15 13:49:50 -08002273 // lr = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002274 __ Ldr(lr, MemOperand(temp.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2275 kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002276 // lr();
2277 __ Blr(lr);
2278 } else {
2279 __ Bl(&frame_entry_label_);
2280 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002281 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002282
Andreas Gampe878d58c2015-01-15 23:24:00 -08002283 DCHECK(!IsLeafMethod());
2284}
2285
2286void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002287 // When we do not run baseline, explicit clinit checks triggered by static
2288 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2289 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002290
Andreas Gampe878d58c2015-01-15 23:24:00 -08002291 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2292 return;
2293 }
2294
Alexandre Ramesd921d642015-04-16 15:07:16 +01002295 BlockPoolsScope block_pools(GetVIXLAssembler());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002296 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002297 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002298 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002299}
2300
2301void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002302 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2303 return;
2304 }
2305
Alexandre Rames5319def2014-10-23 10:03:10 +01002306 LocationSummary* locations = invoke->GetLocations();
2307 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002308 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2309 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2310 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002311 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002312 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002313
Alexandre Ramesd921d642015-04-16 15:07:16 +01002314 BlockPoolsScope block_pools(GetVIXLAssembler());
2315
Alexandre Rames5319def2014-10-23 10:03:10 +01002316 // temp = object->GetClass();
2317 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002318 __ Ldr(temp.W(), MemOperand(sp, receiver.GetStackIndex()));
2319 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002320 } else {
2321 DCHECK(receiver.IsRegister());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002322 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002324 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002325 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002327 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002328 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002329 // lr();
2330 __ Blr(lr);
2331 DCHECK(!codegen_->IsLeafMethod());
2332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2333}
2334
Alexandre Rames67555f72014-11-18 10:55:16 +00002335void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2336 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2337 : LocationSummary::kNoCall;
2338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002339 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 locations->SetOut(Location::RequiresRegister());
2341}
2342
2343void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2344 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002345 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002346 if (cls->IsReferrersClass()) {
2347 DCHECK(!cls->CanCallRuntime());
2348 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002349 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002350 } else {
2351 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002352 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002353 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002354
2355 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2356 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2357 codegen_->AddSlowPath(slow_path);
2358 __ Cbz(out, slow_path->GetEntryLabel());
2359 if (cls->MustGenerateClinitCheck()) {
2360 GenerateClassInitializationCheck(slow_path, out);
2361 } else {
2362 __ Bind(slow_path->GetExitLabel());
2363 }
2364 }
2365}
2366
2367void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2368 LocationSummary* locations =
2369 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2370 locations->SetOut(Location::RequiresRegister());
2371}
2372
2373void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2374 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2375 __ Ldr(OutputRegister(instruction), exception);
2376 __ Str(wzr, exception);
2377}
2378
Alexandre Rames5319def2014-10-23 10:03:10 +01002379void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2380 load->SetLocations(nullptr);
2381}
2382
2383void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2384 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002385 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002386}
2387
Alexandre Rames67555f72014-11-18 10:55:16 +00002388void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2389 LocationSummary* locations =
2390 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002391 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002392 locations->SetOut(Location::RequiresRegister());
2393}
2394
2395void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2396 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2397 codegen_->AddSlowPath(slow_path);
2398
2399 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002400 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002402 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002403 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002404 __ Cbz(out, slow_path->GetEntryLabel());
2405 __ Bind(slow_path->GetExitLabel());
2406}
2407
Alexandre Rames5319def2014-10-23 10:03:10 +01002408void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2409 local->SetLocations(nullptr);
2410}
2411
2412void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2413 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2414}
2415
2416void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2417 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2418 locations->SetOut(Location::ConstantLocation(constant));
2419}
2420
2421void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2422 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002423 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002424}
2425
Alexandre Rames67555f72014-11-18 10:55:16 +00002426void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2427 LocationSummary* locations =
2428 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2429 InvokeRuntimeCallingConvention calling_convention;
2430 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2431}
2432
2433void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2434 codegen_->InvokeRuntime(instruction->IsEnter()
2435 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2436 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002437 instruction->GetDexPc(),
2438 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002439 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002440}
2441
Alexandre Rames42d641b2014-10-27 14:00:51 +00002442void LocationsBuilderARM64::VisitMul(HMul* mul) {
2443 LocationSummary* locations =
2444 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2445 switch (mul->GetResultType()) {
2446 case Primitive::kPrimInt:
2447 case Primitive::kPrimLong:
2448 locations->SetInAt(0, Location::RequiresRegister());
2449 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002451 break;
2452
2453 case Primitive::kPrimFloat:
2454 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002455 locations->SetInAt(0, Location::RequiresFpuRegister());
2456 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002457 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2462 }
2463}
2464
2465void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2466 switch (mul->GetResultType()) {
2467 case Primitive::kPrimInt:
2468 case Primitive::kPrimLong:
2469 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2470 break;
2471
2472 case Primitive::kPrimFloat:
2473 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002474 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002475 break;
2476
2477 default:
2478 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2479 }
2480}
2481
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002482void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2485 switch (neg->GetResultType()) {
2486 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002487 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002488 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002489 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002490 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002491
2492 case Primitive::kPrimFloat:
2493 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002496 break;
2497
2498 default:
2499 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2500 }
2501}
2502
2503void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2504 switch (neg->GetResultType()) {
2505 case Primitive::kPrimInt:
2506 case Primitive::kPrimLong:
2507 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2508 break;
2509
2510 case Primitive::kPrimFloat:
2511 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002512 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2517 }
2518}
2519
2520void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2521 LocationSummary* locations =
2522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2523 InvokeRuntimeCallingConvention calling_convention;
2524 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002525 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002526 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002527 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2528 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002529 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002530}
2531
2532void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2533 LocationSummary* locations = instruction->GetLocations();
2534 InvokeRuntimeCallingConvention calling_convention;
2535 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2536 DCHECK(type_index.Is(w0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002537 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimLong);
2538 DCHECK(current_method.Is(x2));
2539 codegen_->LoadCurrentMethod(current_method.X());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002540 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002541 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002542 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2543 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002544 instruction->GetDexPc(),
2545 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002546 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002547}
2548
Alexandre Rames5319def2014-10-23 10:03:10 +01002549void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2550 LocationSummary* locations =
2551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2552 InvokeRuntimeCallingConvention calling_convention;
2553 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2554 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2555 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002556 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002557}
2558
2559void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2560 LocationSummary* locations = instruction->GetLocations();
2561 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2562 DCHECK(type_index.Is(w0));
2563 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2564 DCHECK(current_method.Is(w1));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002565 codegen_->LoadCurrentMethod(current_method.X());
Alexandre Rames5319def2014-10-23 10:03:10 +01002566 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002567 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002568 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2569 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002570 instruction->GetDexPc(),
2571 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002572 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002573}
2574
2575void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002577 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002578 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002579}
2580
2581void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002582 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002583 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002584 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002585 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002586 break;
2587
2588 default:
2589 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2590 }
2591}
2592
David Brazdil66d126e2015-04-03 16:02:44 +01002593void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2595 locations->SetInAt(0, Location::RequiresRegister());
2596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2597}
2598
2599void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002600 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2601}
2602
Alexandre Rames5319def2014-10-23 10:03:10 +01002603void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2604 LocationSummary* locations =
2605 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2606 locations->SetInAt(0, Location::RequiresRegister());
2607 if (instruction->HasUses()) {
2608 locations->SetOut(Location::SameAsFirstInput());
2609 }
2610}
2611
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002612void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002613 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2614 return;
2615 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002616
Alexandre Ramesd921d642015-04-16 15:07:16 +01002617 BlockPoolsScope block_pools(GetVIXLAssembler());
2618 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002619 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2620 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2621}
2622
2623void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002624 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2625 codegen_->AddSlowPath(slow_path);
2626
2627 LocationSummary* locations = instruction->GetLocations();
2628 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002629
2630 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002631}
2632
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002633void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2634 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2635 GenerateImplicitNullCheck(instruction);
2636 } else {
2637 GenerateExplicitNullCheck(instruction);
2638 }
2639}
2640
Alexandre Rames67555f72014-11-18 10:55:16 +00002641void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2642 HandleBinaryOp(instruction);
2643}
2644
2645void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2646 HandleBinaryOp(instruction);
2647}
2648
Alexandre Rames3e69f162014-12-10 10:36:50 +00002649void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2650 LOG(FATAL) << "Unreachable";
2651}
2652
2653void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2654 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2655}
2656
Alexandre Rames5319def2014-10-23 10:03:10 +01002657void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2658 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2659 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2660 if (location.IsStackSlot()) {
2661 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2662 } else if (location.IsDoubleStackSlot()) {
2663 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2664 }
2665 locations->SetOut(location);
2666}
2667
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002668void InstructionCodeGeneratorARM64::VisitParameterValue(
2669 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002670 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002671}
2672
2673void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2676 locations->SetOut(LocationFrom(x0));
2677}
2678
2679void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2680 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2681 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002682}
2683
2684void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2686 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2687 locations->SetInAt(i, Location::Any());
2688 }
2689 locations->SetOut(Location::Any());
2690}
2691
2692void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002693 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002694 LOG(FATAL) << "Unreachable";
2695}
2696
Serban Constantinescu02164b32014-11-13 14:05:07 +00002697void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002698 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002699 LocationSummary::CallKind call_kind =
2700 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002701 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2702
2703 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002704 case Primitive::kPrimInt:
2705 case Primitive::kPrimLong:
2706 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002707 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2709 break;
2710
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002711 case Primitive::kPrimFloat:
2712 case Primitive::kPrimDouble: {
2713 InvokeRuntimeCallingConvention calling_convention;
2714 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2715 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2716 locations->SetOut(calling_convention.GetReturnLocation(type));
2717
2718 break;
2719 }
2720
Serban Constantinescu02164b32014-11-13 14:05:07 +00002721 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002722 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002723 }
2724}
2725
2726void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2727 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002728
Serban Constantinescu02164b32014-11-13 14:05:07 +00002729 switch (type) {
2730 case Primitive::kPrimInt:
2731 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002732 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002733 break;
2734 }
2735
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002736 case Primitive::kPrimFloat:
2737 case Primitive::kPrimDouble: {
2738 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2739 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002740 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002741 break;
2742 }
2743
Serban Constantinescu02164b32014-11-13 14:05:07 +00002744 default:
2745 LOG(FATAL) << "Unexpected rem type " << type;
2746 }
2747}
2748
Calin Juravle27df7582015-04-17 19:12:31 +01002749void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2750 memory_barrier->SetLocations(nullptr);
2751}
2752
2753void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2754 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2755}
2756
Alexandre Rames5319def2014-10-23 10:03:10 +01002757void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2758 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2759 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002760 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002761}
2762
2763void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002764 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002765 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002766}
2767
2768void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2769 instruction->SetLocations(nullptr);
2770}
2771
2772void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002773 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002774 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002775}
2776
Serban Constantinescu02164b32014-11-13 14:05:07 +00002777void LocationsBuilderARM64::VisitShl(HShl* shl) {
2778 HandleShift(shl);
2779}
2780
2781void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2782 HandleShift(shl);
2783}
2784
2785void LocationsBuilderARM64::VisitShr(HShr* shr) {
2786 HandleShift(shr);
2787}
2788
2789void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2790 HandleShift(shr);
2791}
2792
Alexandre Rames5319def2014-10-23 10:03:10 +01002793void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2794 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2795 Primitive::Type field_type = store->InputAt(1)->GetType();
2796 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002797 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002798 case Primitive::kPrimBoolean:
2799 case Primitive::kPrimByte:
2800 case Primitive::kPrimChar:
2801 case Primitive::kPrimShort:
2802 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002803 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002804 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2805 break;
2806
2807 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002808 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002809 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2810 break;
2811
2812 default:
2813 LOG(FATAL) << "Unimplemented local type " << field_type;
2814 }
2815}
2816
2817void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002818 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002819}
2820
2821void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002822 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002823}
2824
2825void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002826 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002827}
2828
Alexandre Rames67555f72014-11-18 10:55:16 +00002829void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002830 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002831}
2832
2833void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002834 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002835}
2836
2837void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002838 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002839}
2840
Alexandre Rames67555f72014-11-18 10:55:16 +00002841void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002842 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002843}
2844
2845void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2846 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2847}
2848
2849void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002850 HBasicBlock* block = instruction->GetBlock();
2851 if (block->GetLoopInformation() != nullptr) {
2852 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2853 // The back edge will generate the suspend check.
2854 return;
2855 }
2856 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2857 // The goto will generate the suspend check.
2858 return;
2859 }
2860 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002861}
2862
2863void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2864 temp->SetLocations(nullptr);
2865}
2866
2867void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2868 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002869 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002870}
2871
Alexandre Rames67555f72014-11-18 10:55:16 +00002872void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2873 LocationSummary* locations =
2874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2875 InvokeRuntimeCallingConvention calling_convention;
2876 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2877}
2878
2879void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2880 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002881 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002882 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002883}
2884
2885void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2886 LocationSummary* locations =
2887 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2888 Primitive::Type input_type = conversion->GetInputType();
2889 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002890 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002891 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2892 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2893 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2894 }
2895
Alexandre Rames542361f2015-01-29 16:57:31 +00002896 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002897 locations->SetInAt(0, Location::RequiresFpuRegister());
2898 } else {
2899 locations->SetInAt(0, Location::RequiresRegister());
2900 }
2901
Alexandre Rames542361f2015-01-29 16:57:31 +00002902 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002903 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2904 } else {
2905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2906 }
2907}
2908
2909void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2910 Primitive::Type result_type = conversion->GetResultType();
2911 Primitive::Type input_type = conversion->GetInputType();
2912
2913 DCHECK_NE(input_type, result_type);
2914
Alexandre Rames542361f2015-01-29 16:57:31 +00002915 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002916 int result_size = Primitive::ComponentSize(result_type);
2917 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002918 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002919 Register output = OutputRegister(conversion);
2920 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002921 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2922 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2923 } else if ((result_type == Primitive::kPrimChar) ||
2924 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2925 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002926 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002927 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002928 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002929 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002930 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002931 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002932 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2933 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002934 } else if (Primitive::IsFloatingPointType(result_type) &&
2935 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002936 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2937 } else {
2938 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2939 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002940 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002941}
Alexandre Rames67555f72014-11-18 10:55:16 +00002942
Serban Constantinescu02164b32014-11-13 14:05:07 +00002943void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2944 HandleShift(ushr);
2945}
2946
2947void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2948 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002949}
2950
2951void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2952 HandleBinaryOp(instruction);
2953}
2954
2955void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2956 HandleBinaryOp(instruction);
2957}
2958
Calin Juravleb1498f62015-02-16 13:13:29 +00002959void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2960 // Nothing to do, this should be removed during prepare for register allocator.
2961 UNUSED(instruction);
2962 LOG(FATAL) << "Unreachable";
2963}
2964
2965void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2966 // Nothing to do, this should be removed during prepare for register allocator.
2967 UNUSED(instruction);
2968 LOG(FATAL) << "Unreachable";
2969}
2970
Alexandre Rames67555f72014-11-18 10:55:16 +00002971#undef __
2972#undef QUICK_ENTRY_POINT
2973
Alexandre Rames5319def2014-10-23 10:03:10 +01002974} // namespace arm64
2975} // namespace art