blob: fbe26b02ec6060ccafe9f4b0d2039e5daf0339d7 [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
Serban Constantinescu579885a2015-02-22 20:51:33 +0000485CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
486 const Arm64InstructionSetFeatures& isa_features,
487 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100488 : CodeGenerator(graph,
489 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000490 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000491 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000492 callee_saved_core_registers.list(),
493 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000494 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100495 block_labels_(nullptr),
496 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000498 move_resolver_(graph->GetArena(), this),
499 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000500 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000501 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000502}
Alexandre Rames5319def2014-10-23 10:03:10 +0100503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504#undef __
505#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100506
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000507void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
508 // Ensure we emit the literal pool.
509 __ FinalizeCode();
510 CodeGenerator::Finalize(allocator);
511}
512
Zheng Xuad4450e2015-04-17 18:48:56 +0800513void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
514 // Note: There are 6 kinds of moves:
515 // 1. constant -> GPR/FPR (non-cycle)
516 // 2. constant -> stack (non-cycle)
517 // 3. GPR/FPR -> GPR/FPR
518 // 4. GPR/FPR -> stack
519 // 5. stack -> GPR/FPR
520 // 6. stack -> stack (non-cycle)
521 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
522 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
523 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
524 // dependency.
525 vixl_temps_.Open(GetVIXLAssembler());
526}
527
528void ParallelMoveResolverARM64::FinishEmitNativeCode() {
529 vixl_temps_.Close();
530}
531
532Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
533 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
534 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
535 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
536 Location scratch = GetScratchLocation(kind);
537 if (!scratch.Equals(Location::NoLocation())) {
538 return scratch;
539 }
540 // Allocate from VIXL temp registers.
541 if (kind == Location::kRegister) {
542 scratch = LocationFrom(vixl_temps_.AcquireX());
543 } else {
544 DCHECK(kind == Location::kFpuRegister);
545 scratch = LocationFrom(vixl_temps_.AcquireD());
546 }
547 AddScratchLocation(scratch);
548 return scratch;
549}
550
551void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
552 if (loc.IsRegister()) {
553 vixl_temps_.Release(XRegisterFrom(loc));
554 } else {
555 DCHECK(loc.IsFpuRegister());
556 vixl_temps_.Release(DRegisterFrom(loc));
557 }
558 RemoveScratchLocation(loc);
559}
560
Alexandre Rames3e69f162014-12-10 10:36:50 +0000561void ParallelMoveResolverARM64::EmitMove(size_t index) {
562 MoveOperands* move = moves_.Get(index);
563 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
564}
565
Alexandre Rames5319def2014-10-23 10:03:10 +0100566void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100567 MacroAssembler* masm = GetVIXLAssembler();
568 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000569 __ Bind(&frame_entry_label_);
570
Serban Constantinescu02164b32014-11-13 14:05:07 +0000571 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
572 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100573 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000574 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000575 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000576 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000577 __ Ldr(wzr, MemOperand(temp, 0));
578 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000579 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100580
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000581 if (!HasEmptyFrame()) {
582 int frame_size = GetFrameSize();
583 // Stack layout:
584 // sp[frame_size - 8] : lr.
585 // ... : other preserved core registers.
586 // ... : other preserved fp registers.
587 // ... : reserved frame space.
588 // sp[0] : current method.
589 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100590 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800591 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
592 frame_size - GetCoreSpillSize());
593 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
594 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000595 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100596}
597
598void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100599 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100600 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000601 if (!HasEmptyFrame()) {
602 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800603 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
604 frame_size - FrameEntrySpillSize());
605 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
606 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000607 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100608 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000609 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100610 __ Ret();
611 GetAssembler()->cfi().RestoreState();
612 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100613}
614
Zheng Xuda403092015-04-24 17:35:39 +0800615vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
616 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
617 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
618 core_spill_mask_);
619}
620
621vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
622 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
623 GetNumberOfFloatingPointRegisters()));
624 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
625 fpu_spill_mask_);
626}
627
Alexandre Rames5319def2014-10-23 10:03:10 +0100628void CodeGeneratorARM64::Bind(HBasicBlock* block) {
629 __ Bind(GetLabelOf(block));
630}
631
Alexandre Rames5319def2014-10-23 10:03:10 +0100632void CodeGeneratorARM64::Move(HInstruction* instruction,
633 Location location,
634 HInstruction* move_for) {
635 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100636 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000637 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100638
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100639 if (instruction->IsCurrentMethod()) {
640 MoveLocation(location, Location::StackSlot(kCurrentMethodStackOffset));
641 } else if (locations != nullptr && locations->Out().Equals(location)) {
642 return;
643 } else if (instruction->IsIntConstant()
644 || instruction->IsLongConstant()
645 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000646 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100647 if (location.IsRegister()) {
648 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000649 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100650 (instruction->IsLongConstant() && dst.Is64Bits()));
651 __ Mov(dst, value);
652 } else {
653 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000654 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000655 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
656 ? temps.AcquireW()
657 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100658 __ Mov(temp, value);
659 __ Str(temp, StackOperandFrom(location));
660 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000661 } else if (instruction->IsTemporary()) {
662 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000663 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100664 } else if (instruction->IsLoadLocal()) {
665 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000666 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000667 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000668 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000669 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100670 }
671
672 } else {
673 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000674 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100675 }
676}
677
Alexandre Rames5319def2014-10-23 10:03:10 +0100678Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
679 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000680
Alexandre Rames5319def2014-10-23 10:03:10 +0100681 switch (type) {
682 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000683 case Primitive::kPrimInt:
684 case Primitive::kPrimFloat:
685 return Location::StackSlot(GetStackSlot(load->GetLocal()));
686
687 case Primitive::kPrimLong:
688 case Primitive::kPrimDouble:
689 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
690
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 case Primitive::kPrimBoolean:
692 case Primitive::kPrimByte:
693 case Primitive::kPrimChar:
694 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100695 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100696 LOG(FATAL) << "Unexpected type " << type;
697 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000698
Alexandre Rames5319def2014-10-23 10:03:10 +0100699 LOG(FATAL) << "Unreachable";
700 return Location::NoLocation();
701}
702
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100703void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000704 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100705 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000706 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100707 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100708 if (value_can_be_null) {
709 __ Cbz(value, &done);
710 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100711 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
712 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000713 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100714 if (value_can_be_null) {
715 __ Bind(&done);
716 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100717}
718
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000719void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
720 // Blocked core registers:
721 // lr : Runtime reserved.
722 // tr : Runtime reserved.
723 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
724 // ip1 : VIXL core temp.
725 // ip0 : VIXL core temp.
726 //
727 // Blocked fp registers:
728 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100729 CPURegList reserved_core_registers = vixl_reserved_core_registers;
730 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100731 while (!reserved_core_registers.IsEmpty()) {
732 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
733 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000734
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000735 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800736 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000737 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
738 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000739
740 if (is_baseline) {
741 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
742 while (!reserved_core_baseline_registers.IsEmpty()) {
743 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
744 }
745
746 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
747 while (!reserved_fp_baseline_registers.IsEmpty()) {
748 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
749 }
750 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100751}
752
753Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
754 if (type == Primitive::kPrimVoid) {
755 LOG(FATAL) << "Unreachable type " << type;
756 }
757
Alexandre Rames542361f2015-01-29 16:57:31 +0000758 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000759 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
760 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100761 return Location::FpuRegisterLocation(reg);
762 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000763 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
764 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100765 return Location::RegisterLocation(reg);
766 }
767}
768
Alexandre Rames3e69f162014-12-10 10:36:50 +0000769size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
770 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
771 __ Str(reg, MemOperand(sp, stack_index));
772 return kArm64WordSize;
773}
774
775size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
776 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
777 __ Ldr(reg, MemOperand(sp, stack_index));
778 return kArm64WordSize;
779}
780
781size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
782 FPRegister reg = FPRegister(reg_id, kDRegSize);
783 __ Str(reg, MemOperand(sp, stack_index));
784 return kArm64WordSize;
785}
786
787size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
788 FPRegister reg = FPRegister(reg_id, kDRegSize);
789 __ Ldr(reg, MemOperand(sp, stack_index));
790 return kArm64WordSize;
791}
792
Alexandre Rames5319def2014-10-23 10:03:10 +0100793void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100794 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100795}
796
797void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100798 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100799}
800
Alexandre Rames67555f72014-11-18 10:55:16 +0000801void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000802 if (constant->IsIntConstant()) {
803 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
804 } else if (constant->IsLongConstant()) {
805 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
806 } else if (constant->IsNullConstant()) {
807 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000808 } else if (constant->IsFloatConstant()) {
809 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
810 } else {
811 DCHECK(constant->IsDoubleConstant());
812 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
813 }
814}
815
Alexandre Rames3e69f162014-12-10 10:36:50 +0000816
817static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
818 DCHECK(constant.IsConstant());
819 HConstant* cst = constant.GetConstant();
820 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000821 // Null is mapped to a core W register, which we associate with kPrimInt.
822 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000823 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
824 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
825 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
826}
827
828void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000829 if (source.Equals(destination)) {
830 return;
831 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000832
833 // A valid move can always be inferred from the destination and source
834 // locations. When moving from and to a register, the argument type can be
835 // used to generate 32bit instead of 64bit moves. In debug mode we also
836 // checks the coherency of the locations and the type.
837 bool unspecified_type = (type == Primitive::kPrimVoid);
838
839 if (destination.IsRegister() || destination.IsFpuRegister()) {
840 if (unspecified_type) {
841 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
842 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000843 (src_cst != nullptr && (src_cst->IsIntConstant()
844 || src_cst->IsFloatConstant()
845 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000846 // For stack slots and 32bit constants, a 64bit type is appropriate.
847 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000848 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000849 // If the source is a double stack slot or a 64bit constant, a 64bit
850 // type is appropriate. Else the source is a register, and since the
851 // type has not been specified, we chose a 64bit type to force a 64bit
852 // move.
853 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000854 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000855 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000856 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
857 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000858 CPURegister dst = CPURegisterFrom(destination, type);
859 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
860 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
861 __ Ldr(dst, StackOperandFrom(source));
862 } else if (source.IsConstant()) {
863 DCHECK(CoherentConstantAndType(source, type));
864 MoveConstant(dst, source.GetConstant());
865 } else {
866 if (destination.IsRegister()) {
867 __ Mov(Register(dst), RegisterFrom(source, type));
868 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800869 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000870 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
871 }
872 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000873 } else { // The destination is not a register. It must be a stack slot.
874 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
875 if (source.IsRegister() || source.IsFpuRegister()) {
876 if (unspecified_type) {
877 if (source.IsRegister()) {
878 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
879 } else {
880 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
881 }
882 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
884 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000885 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
886 } else if (source.IsConstant()) {
887 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
888 UseScratchRegisterScope temps(GetVIXLAssembler());
889 HConstant* src_cst = source.GetConstant();
890 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000891 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000892 temp = temps.AcquireW();
893 } else if (src_cst->IsLongConstant()) {
894 temp = temps.AcquireX();
895 } else if (src_cst->IsFloatConstant()) {
896 temp = temps.AcquireS();
897 } else {
898 DCHECK(src_cst->IsDoubleConstant());
899 temp = temps.AcquireD();
900 }
901 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000902 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000903 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000904 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000905 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000906 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000907 // There is generally less pressure on FP registers.
908 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000909 __ Ldr(temp, StackOperandFrom(source));
910 __ Str(temp, StackOperandFrom(destination));
911 }
912 }
913}
914
915void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000916 CPURegister dst,
917 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000918 switch (type) {
919 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000920 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000921 break;
922 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000923 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000924 break;
925 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000926 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000927 break;
928 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000929 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000930 break;
931 case Primitive::kPrimInt:
932 case Primitive::kPrimNot:
933 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000934 case Primitive::kPrimFloat:
935 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000936 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000937 __ Ldr(dst, src);
938 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000939 case Primitive::kPrimVoid:
940 LOG(FATAL) << "Unreachable type " << type;
941 }
942}
943
Calin Juravle77520bc2015-01-12 18:45:46 +0000944void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000945 CPURegister dst,
946 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100947 MacroAssembler* masm = GetVIXLAssembler();
948 BlockPoolsScope block_pools(masm);
949 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000950 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000951 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000952
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000953 DCHECK(!src.IsPreIndex());
954 DCHECK(!src.IsPostIndex());
955
956 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800957 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000958 MemOperand base = MemOperand(temp_base);
959 switch (type) {
960 case Primitive::kPrimBoolean:
961 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000962 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000963 break;
964 case Primitive::kPrimByte:
965 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000966 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000967 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
968 break;
969 case Primitive::kPrimChar:
970 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000971 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000972 break;
973 case Primitive::kPrimShort:
974 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000975 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000976 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
977 break;
978 case Primitive::kPrimInt:
979 case Primitive::kPrimNot:
980 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000981 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000982 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000983 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000984 break;
985 case Primitive::kPrimFloat:
986 case Primitive::kPrimDouble: {
987 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000988 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000989
990 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
991 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000992 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000993 __ Fmov(FPRegister(dst), temp);
994 break;
995 }
996 case Primitive::kPrimVoid:
997 LOG(FATAL) << "Unreachable type " << type;
998 }
999}
1000
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001001void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001002 CPURegister src,
1003 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001004 switch (type) {
1005 case Primitive::kPrimBoolean:
1006 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001007 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001008 break;
1009 case Primitive::kPrimChar:
1010 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001011 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001012 break;
1013 case Primitive::kPrimInt:
1014 case Primitive::kPrimNot:
1015 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001016 case Primitive::kPrimFloat:
1017 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001018 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001019 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001020 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001021 case Primitive::kPrimVoid:
1022 LOG(FATAL) << "Unreachable type " << type;
1023 }
1024}
1025
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001026void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1027 CPURegister src,
1028 const MemOperand& dst) {
1029 UseScratchRegisterScope temps(GetVIXLAssembler());
1030 Register temp_base = temps.AcquireX();
1031
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001032 DCHECK(!dst.IsPreIndex());
1033 DCHECK(!dst.IsPostIndex());
1034
1035 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001036 Operand op = OperandFromMemOperand(dst);
1037 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001038 MemOperand base = MemOperand(temp_base);
1039 switch (type) {
1040 case Primitive::kPrimBoolean:
1041 case Primitive::kPrimByte:
1042 __ Stlrb(Register(src), base);
1043 break;
1044 case Primitive::kPrimChar:
1045 case Primitive::kPrimShort:
1046 __ Stlrh(Register(src), base);
1047 break;
1048 case Primitive::kPrimInt:
1049 case Primitive::kPrimNot:
1050 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001051 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 __ Stlr(Register(src), base);
1053 break;
1054 case Primitive::kPrimFloat:
1055 case Primitive::kPrimDouble: {
1056 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001057 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001058
1059 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1060 __ Fmov(temp, FPRegister(src));
1061 __ Stlr(temp, base);
1062 break;
1063 }
1064 case Primitive::kPrimVoid:
1065 LOG(FATAL) << "Unreachable type " << type;
1066 }
1067}
1068
Alexandre Rames67555f72014-11-18 10:55:16 +00001069void CodeGeneratorARM64::LoadCurrentMethod(vixl::Register current_method) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001070 DCHECK(RequiresCurrentMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001071 CHECK(current_method.IsX());
Alexandre Rames67555f72014-11-18 10:55:16 +00001072 __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset));
1073}
1074
1075void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1076 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001077 uint32_t dex_pc,
1078 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001079 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001080 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1081 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001082 RecordPcInfo(instruction, dex_pc, slow_path);
1083 DCHECK(instruction->IsSuspendCheck()
1084 || instruction->IsBoundsCheck()
1085 || instruction->IsNullCheck()
1086 || instruction->IsDivZeroCheck()
1087 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001088}
1089
1090void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1091 vixl::Register class_reg) {
1092 UseScratchRegisterScope temps(GetVIXLAssembler());
1093 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001094 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001095 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096
Serban Constantinescu02164b32014-11-13 14:05:07 +00001097 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001098 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001099 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1100 __ Add(temp, class_reg, status_offset);
1101 __ Ldar(temp, HeapOperand(temp));
1102 __ Cmp(temp, mirror::Class::kStatusInitialized);
1103 __ B(lt, slow_path->GetEntryLabel());
1104 } else {
1105 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1106 __ Cmp(temp, mirror::Class::kStatusInitialized);
1107 __ B(lt, slow_path->GetEntryLabel());
1108 __ Dmb(InnerShareable, BarrierReads);
1109 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001110 __ Bind(slow_path->GetExitLabel());
1111}
Alexandre Rames5319def2014-10-23 10:03:10 +01001112
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001113void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1114 BarrierType type = BarrierAll;
1115
1116 switch (kind) {
1117 case MemBarrierKind::kAnyAny:
1118 case MemBarrierKind::kAnyStore: {
1119 type = BarrierAll;
1120 break;
1121 }
1122 case MemBarrierKind::kLoadAny: {
1123 type = BarrierReads;
1124 break;
1125 }
1126 case MemBarrierKind::kStoreStore: {
1127 type = BarrierWrites;
1128 break;
1129 }
1130 default:
1131 LOG(FATAL) << "Unexpected memory barrier " << kind;
1132 }
1133 __ Dmb(InnerShareable, type);
1134}
1135
Serban Constantinescu02164b32014-11-13 14:05:07 +00001136void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1137 HBasicBlock* successor) {
1138 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001139 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1140 if (slow_path == nullptr) {
1141 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1142 instruction->SetSlowPath(slow_path);
1143 codegen_->AddSlowPath(slow_path);
1144 if (successor != nullptr) {
1145 DCHECK(successor->IsLoopHeader());
1146 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1147 }
1148 } else {
1149 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1150 }
1151
Serban Constantinescu02164b32014-11-13 14:05:07 +00001152 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1153 Register temp = temps.AcquireW();
1154
1155 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1156 if (successor == nullptr) {
1157 __ Cbnz(temp, slow_path->GetEntryLabel());
1158 __ Bind(slow_path->GetReturnLabel());
1159 } else {
1160 __ Cbz(temp, codegen_->GetLabelOf(successor));
1161 __ B(slow_path->GetEntryLabel());
1162 // slow_path will return to GetLabelOf(successor).
1163 }
1164}
1165
Alexandre Rames5319def2014-10-23 10:03:10 +01001166InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1167 CodeGeneratorARM64* codegen)
1168 : HGraphVisitor(graph),
1169 assembler_(codegen->GetAssembler()),
1170 codegen_(codegen) {}
1171
1172#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001173 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001174
1175#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1176
1177enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001178 // Using a base helps identify when we hit such breakpoints.
1179 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001180#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1181 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1182#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1183};
1184
1185#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1186 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001187 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001188 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1189 } \
1190 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1192 locations->SetOut(Location::Any()); \
1193 }
1194 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1195#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1196
1197#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001198#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001199
Alexandre Rames67555f72014-11-18 10:55:16 +00001200void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001201 DCHECK_EQ(instr->InputCount(), 2U);
1202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1203 Primitive::Type type = instr->GetResultType();
1204 switch (type) {
1205 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001206 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001207 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001208 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001209 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001210 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001211
1212 case Primitive::kPrimFloat:
1213 case Primitive::kPrimDouble:
1214 locations->SetInAt(0, Location::RequiresFpuRegister());
1215 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001216 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001217 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001218
Alexandre Rames5319def2014-10-23 10:03:10 +01001219 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001220 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001221 }
1222}
1223
Alexandre Rames09a99962015-04-15 11:47:56 +01001224void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1225 LocationSummary* locations =
1226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1227 locations->SetInAt(0, Location::RequiresRegister());
1228 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1229 locations->SetOut(Location::RequiresFpuRegister());
1230 } else {
1231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1232 }
1233}
1234
1235void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1236 const FieldInfo& field_info) {
1237 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001238 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001239
1240 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1241 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1242
1243 if (field_info.IsVolatile()) {
1244 if (use_acquire_release) {
1245 // NB: LoadAcquire will record the pc info if needed.
1246 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1247 } else {
1248 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1249 codegen_->MaybeRecordImplicitNullCheck(instruction);
1250 // For IRIW sequential consistency kLoadAny is not sufficient.
1251 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1252 }
1253 } else {
1254 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1255 codegen_->MaybeRecordImplicitNullCheck(instruction);
1256 }
1257}
1258
1259void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1260 LocationSummary* locations =
1261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1262 locations->SetInAt(0, Location::RequiresRegister());
1263 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1264 locations->SetInAt(1, Location::RequiresFpuRegister());
1265 } else {
1266 locations->SetInAt(1, Location::RequiresRegister());
1267 }
1268}
1269
1270void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001271 const FieldInfo& field_info,
1272 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001273 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001274 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001275
1276 Register obj = InputRegisterAt(instruction, 0);
1277 CPURegister value = InputCPURegisterAt(instruction, 1);
1278 Offset offset = field_info.GetFieldOffset();
1279 Primitive::Type field_type = field_info.GetFieldType();
1280 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1281
1282 if (field_info.IsVolatile()) {
1283 if (use_acquire_release) {
1284 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1285 codegen_->MaybeRecordImplicitNullCheck(instruction);
1286 } else {
1287 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1288 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1289 codegen_->MaybeRecordImplicitNullCheck(instruction);
1290 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1291 }
1292 } else {
1293 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1294 codegen_->MaybeRecordImplicitNullCheck(instruction);
1295 }
1296
1297 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001298 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001299 }
1300}
1301
Alexandre Rames67555f72014-11-18 10:55:16 +00001302void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001303 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001304
1305 switch (type) {
1306 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001307 case Primitive::kPrimLong: {
1308 Register dst = OutputRegister(instr);
1309 Register lhs = InputRegisterAt(instr, 0);
1310 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001311 if (instr->IsAdd()) {
1312 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 } else if (instr->IsAnd()) {
1314 __ And(dst, lhs, rhs);
1315 } else if (instr->IsOr()) {
1316 __ Orr(dst, lhs, rhs);
1317 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001318 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 } else {
1320 DCHECK(instr->IsXor());
1321 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001322 }
1323 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001324 }
1325 case Primitive::kPrimFloat:
1326 case Primitive::kPrimDouble: {
1327 FPRegister dst = OutputFPRegister(instr);
1328 FPRegister lhs = InputFPRegisterAt(instr, 0);
1329 FPRegister rhs = InputFPRegisterAt(instr, 1);
1330 if (instr->IsAdd()) {
1331 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001332 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001333 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 } else {
1335 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001336 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001337 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001338 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001339 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001340 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001341 }
1342}
1343
Serban Constantinescu02164b32014-11-13 14:05:07 +00001344void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1345 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1346
1347 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1348 Primitive::Type type = instr->GetResultType();
1349 switch (type) {
1350 case Primitive::kPrimInt:
1351 case Primitive::kPrimLong: {
1352 locations->SetInAt(0, Location::RequiresRegister());
1353 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1354 locations->SetOut(Location::RequiresRegister());
1355 break;
1356 }
1357 default:
1358 LOG(FATAL) << "Unexpected shift type " << type;
1359 }
1360}
1361
1362void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1363 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1364
1365 Primitive::Type type = instr->GetType();
1366 switch (type) {
1367 case Primitive::kPrimInt:
1368 case Primitive::kPrimLong: {
1369 Register dst = OutputRegister(instr);
1370 Register lhs = InputRegisterAt(instr, 0);
1371 Operand rhs = InputOperandAt(instr, 1);
1372 if (rhs.IsImmediate()) {
1373 uint32_t shift_value = (type == Primitive::kPrimInt)
1374 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1375 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1376 if (instr->IsShl()) {
1377 __ Lsl(dst, lhs, shift_value);
1378 } else if (instr->IsShr()) {
1379 __ Asr(dst, lhs, shift_value);
1380 } else {
1381 __ Lsr(dst, lhs, shift_value);
1382 }
1383 } else {
1384 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1385
1386 if (instr->IsShl()) {
1387 __ Lsl(dst, lhs, rhs_reg);
1388 } else if (instr->IsShr()) {
1389 __ Asr(dst, lhs, rhs_reg);
1390 } else {
1391 __ Lsr(dst, lhs, rhs_reg);
1392 }
1393 }
1394 break;
1395 }
1396 default:
1397 LOG(FATAL) << "Unexpected shift operation type " << type;
1398 }
1399}
1400
Alexandre Rames5319def2014-10-23 10:03:10 +01001401void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001402 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001403}
1404
1405void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001406 HandleBinaryOp(instruction);
1407}
1408
1409void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1410 HandleBinaryOp(instruction);
1411}
1412
1413void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1414 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001415}
1416
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001417void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1418 LocationSummary* locations =
1419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001422 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1423 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1424 } else {
1425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1426 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001427}
1428
1429void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1430 LocationSummary* locations = instruction->GetLocations();
1431 Primitive::Type type = instruction->GetType();
1432 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001433 Location index = locations->InAt(1);
1434 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001435 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001436 MacroAssembler* masm = GetVIXLAssembler();
1437 UseScratchRegisterScope temps(masm);
1438 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001439
1440 if (index.IsConstant()) {
1441 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001442 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001443 } else {
1444 Register temp = temps.AcquireSameSizeAs(obj);
1445 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1446 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001447 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001448 }
1449
Alexandre Rames67555f72014-11-18 10:55:16 +00001450 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001451 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001452}
1453
Alexandre Rames5319def2014-10-23 10:03:10 +01001454void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1455 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1456 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001458}
1459
1460void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001461 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001462 __ Ldr(OutputRegister(instruction),
1463 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001464 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001465}
1466
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001467void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001468 if (instruction->NeedsTypeCheck()) {
1469 LocationSummary* locations =
1470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001471 InvokeRuntimeCallingConvention calling_convention;
1472 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1473 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1474 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1475 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001476 LocationSummary* locations =
1477 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001478 locations->SetInAt(0, Location::RequiresRegister());
1479 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001480 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1481 locations->SetInAt(2, Location::RequiresFpuRegister());
1482 } else {
1483 locations->SetInAt(2, Location::RequiresRegister());
1484 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001485 }
1486}
1487
1488void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1489 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001490 LocationSummary* locations = instruction->GetLocations();
1491 bool needs_runtime_call = locations->WillCall();
1492
1493 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001494 codegen_->InvokeRuntime(
1495 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001496 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001497 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001498 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001499 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001500 Location index = locations->InAt(1);
1501 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001502 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001503 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001504 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001505 {
1506 // We use a block to end the scratch scope before the write barrier, thus
1507 // freeing the temporary registers so they can be used in `MarkGCCard`.
1508 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001509
Alexandre Rames97833a02015-04-16 15:07:12 +01001510 if (index.IsConstant()) {
1511 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1512 destination = HeapOperand(obj, offset);
1513 } else {
1514 Register temp = temps.AcquireSameSizeAs(obj);
1515 Register index_reg = InputRegisterAt(instruction, 1);
1516 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1517 destination = HeapOperand(temp, offset);
1518 }
1519
1520 codegen_->Store(value_type, value, destination);
1521 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001522 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001523 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001524 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001525 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001526 }
1527}
1528
Alexandre Rames67555f72014-11-18 10:55:16 +00001529void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1530 LocationSummary* locations =
1531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1532 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001533 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001534 if (instruction->HasUses()) {
1535 locations->SetOut(Location::SameAsFirstInput());
1536 }
1537}
1538
1539void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001540 LocationSummary* locations = instruction->GetLocations();
1541 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1542 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001543 codegen_->AddSlowPath(slow_path);
1544
1545 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1546 __ B(slow_path->GetEntryLabel(), hs);
1547}
1548
1549void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1551 instruction, LocationSummary::kCallOnSlowPath);
1552 locations->SetInAt(0, Location::RequiresRegister());
1553 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001555}
1556
1557void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001558 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001559 Register obj = InputRegisterAt(instruction, 0);;
1560 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001561 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001562
Alexandre Rames3e69f162014-12-10 10:36:50 +00001563 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1564 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001565 codegen_->AddSlowPath(slow_path);
1566
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001567 // Avoid null check if we know obj is not null.
1568 if (instruction->MustDoNullCheck()) {
1569 __ Cbz(obj, slow_path->GetExitLabel());
1570 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001572 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1573 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001574 __ B(ne, slow_path->GetEntryLabel());
1575 __ Bind(slow_path->GetExitLabel());
1576}
1577
1578void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1579 LocationSummary* locations =
1580 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1581 locations->SetInAt(0, Location::RequiresRegister());
1582 if (check->HasUses()) {
1583 locations->SetOut(Location::SameAsFirstInput());
1584 }
1585}
1586
1587void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1588 // We assume the class is not null.
1589 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1590 check->GetLoadClass(), check, check->GetDexPc(), true);
1591 codegen_->AddSlowPath(slow_path);
1592 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1593}
1594
Serban Constantinescu02164b32014-11-13 14:05:07 +00001595void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001596 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001597 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1598 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001599 switch (in_type) {
1600 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001601 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001602 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1604 break;
1605 }
1606 case Primitive::kPrimFloat:
1607 case Primitive::kPrimDouble: {
1608 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001609 HInstruction* right = compare->InputAt(1);
1610 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1611 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1612 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1613 } else {
1614 locations->SetInAt(1, Location::RequiresFpuRegister());
1615 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001616 locations->SetOut(Location::RequiresRegister());
1617 break;
1618 }
1619 default:
1620 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1621 }
1622}
1623
1624void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1625 Primitive::Type in_type = compare->InputAt(0)->GetType();
1626
1627 // 0 if: left == right
1628 // 1 if: left > right
1629 // -1 if: left < right
1630 switch (in_type) {
1631 case Primitive::kPrimLong: {
1632 Register result = OutputRegister(compare);
1633 Register left = InputRegisterAt(compare, 0);
1634 Operand right = InputOperandAt(compare, 1);
1635
1636 __ Cmp(left, right);
1637 __ Cset(result, ne);
1638 __ Cneg(result, result, lt);
1639 break;
1640 }
1641 case Primitive::kPrimFloat:
1642 case Primitive::kPrimDouble: {
1643 Register result = OutputRegister(compare);
1644 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001645 if (compare->GetLocations()->InAt(1).IsConstant()) {
1646 if (kIsDebugBuild) {
1647 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1648 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1649 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1650 }
1651 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1652 __ Fcmp(left, 0.0);
1653 } else {
1654 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1655 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001656 if (compare->IsGtBias()) {
1657 __ Cset(result, ne);
1658 } else {
1659 __ Csetm(result, ne);
1660 }
1661 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 break;
1663 }
1664 default:
1665 LOG(FATAL) << "Unimplemented compare type " << in_type;
1666 }
1667}
1668
1669void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1670 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1671 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001672 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001673 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001675 }
1676}
1677
1678void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1679 if (!instruction->NeedsMaterialization()) {
1680 return;
1681 }
1682
1683 LocationSummary* locations = instruction->GetLocations();
1684 Register lhs = InputRegisterAt(instruction, 0);
1685 Operand rhs = InputOperandAt(instruction, 1);
1686 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1687 Condition cond = ARM64Condition(instruction->GetCondition());
1688
1689 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001690 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001691}
1692
1693#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1694 M(Equal) \
1695 M(NotEqual) \
1696 M(LessThan) \
1697 M(LessThanOrEqual) \
1698 M(GreaterThan) \
1699 M(GreaterThanOrEqual)
1700#define DEFINE_CONDITION_VISITORS(Name) \
1701void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1702void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1703FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001704#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001705#undef FOR_EACH_CONDITION_INSTRUCTION
1706
Zheng Xuc6667102015-05-15 16:08:45 +08001707void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1708 DCHECK(instruction->IsDiv() || instruction->IsRem());
1709
1710 LocationSummary* locations = instruction->GetLocations();
1711 Location second = locations->InAt(1);
1712 DCHECK(second.IsConstant());
1713
1714 Register out = OutputRegister(instruction);
1715 Register dividend = InputRegisterAt(instruction, 0);
1716 int64_t imm = Int64FromConstant(second.GetConstant());
1717 DCHECK(imm == 1 || imm == -1);
1718
1719 if (instruction->IsRem()) {
1720 __ Mov(out, 0);
1721 } else {
1722 if (imm == 1) {
1723 __ Mov(out, dividend);
1724 } else {
1725 __ Neg(out, dividend);
1726 }
1727 }
1728}
1729
1730void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1731 DCHECK(instruction->IsDiv() || instruction->IsRem());
1732
1733 LocationSummary* locations = instruction->GetLocations();
1734 Location second = locations->InAt(1);
1735 DCHECK(second.IsConstant());
1736
1737 Register out = OutputRegister(instruction);
1738 Register dividend = InputRegisterAt(instruction, 0);
1739 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001740 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001741 DCHECK(IsPowerOfTwo(abs_imm));
1742 int ctz_imm = CTZ(abs_imm);
1743
1744 UseScratchRegisterScope temps(GetVIXLAssembler());
1745 Register temp = temps.AcquireSameSizeAs(out);
1746
1747 if (instruction->IsDiv()) {
1748 __ Add(temp, dividend, abs_imm - 1);
1749 __ Cmp(dividend, 0);
1750 __ Csel(out, temp, dividend, lt);
1751 if (imm > 0) {
1752 __ Asr(out, out, ctz_imm);
1753 } else {
1754 __ Neg(out, Operand(out, ASR, ctz_imm));
1755 }
1756 } else {
1757 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1758 __ Asr(temp, dividend, bits - 1);
1759 __ Lsr(temp, temp, bits - ctz_imm);
1760 __ Add(out, dividend, temp);
1761 __ And(out, out, abs_imm - 1);
1762 __ Sub(out, out, temp);
1763 }
1764}
1765
1766void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1767 DCHECK(instruction->IsDiv() || instruction->IsRem());
1768
1769 LocationSummary* locations = instruction->GetLocations();
1770 Location second = locations->InAt(1);
1771 DCHECK(second.IsConstant());
1772
1773 Register out = OutputRegister(instruction);
1774 Register dividend = InputRegisterAt(instruction, 0);
1775 int64_t imm = Int64FromConstant(second.GetConstant());
1776
1777 Primitive::Type type = instruction->GetResultType();
1778 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1779
1780 int64_t magic;
1781 int shift;
1782 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1783
1784 UseScratchRegisterScope temps(GetVIXLAssembler());
1785 Register temp = temps.AcquireSameSizeAs(out);
1786
1787 // temp = get_high(dividend * magic)
1788 __ Mov(temp, magic);
1789 if (type == Primitive::kPrimLong) {
1790 __ Smulh(temp, dividend, temp);
1791 } else {
1792 __ Smull(temp.X(), dividend, temp);
1793 __ Lsr(temp.X(), temp.X(), 32);
1794 }
1795
1796 if (imm > 0 && magic < 0) {
1797 __ Add(temp, temp, dividend);
1798 } else if (imm < 0 && magic > 0) {
1799 __ Sub(temp, temp, dividend);
1800 }
1801
1802 if (shift != 0) {
1803 __ Asr(temp, temp, shift);
1804 }
1805
1806 if (instruction->IsDiv()) {
1807 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1808 } else {
1809 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1810 // TODO: Strength reduction for msub.
1811 Register temp_imm = temps.AcquireSameSizeAs(out);
1812 __ Mov(temp_imm, imm);
1813 __ Msub(out, temp, temp_imm, dividend);
1814 }
1815}
1816
1817void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1818 DCHECK(instruction->IsDiv() || instruction->IsRem());
1819 Primitive::Type type = instruction->GetResultType();
1820 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1821
1822 LocationSummary* locations = instruction->GetLocations();
1823 Register out = OutputRegister(instruction);
1824 Location second = locations->InAt(1);
1825
1826 if (second.IsConstant()) {
1827 int64_t imm = Int64FromConstant(second.GetConstant());
1828
1829 if (imm == 0) {
1830 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1831 } else if (imm == 1 || imm == -1) {
1832 DivRemOneOrMinusOne(instruction);
1833 } else if (IsPowerOfTwo(std::abs(imm))) {
1834 DivRemByPowerOfTwo(instruction);
1835 } else {
1836 DCHECK(imm <= -2 || imm >= 2);
1837 GenerateDivRemWithAnyConstant(instruction);
1838 }
1839 } else {
1840 Register dividend = InputRegisterAt(instruction, 0);
1841 Register divisor = InputRegisterAt(instruction, 1);
1842 if (instruction->IsDiv()) {
1843 __ Sdiv(out, dividend, divisor);
1844 } else {
1845 UseScratchRegisterScope temps(GetVIXLAssembler());
1846 Register temp = temps.AcquireSameSizeAs(out);
1847 __ Sdiv(temp, dividend, divisor);
1848 __ Msub(out, temp, divisor, dividend);
1849 }
1850 }
1851}
1852
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001853void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1854 LocationSummary* locations =
1855 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1856 switch (div->GetResultType()) {
1857 case Primitive::kPrimInt:
1858 case Primitive::kPrimLong:
1859 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001860 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1862 break;
1863
1864 case Primitive::kPrimFloat:
1865 case Primitive::kPrimDouble:
1866 locations->SetInAt(0, Location::RequiresFpuRegister());
1867 locations->SetInAt(1, Location::RequiresFpuRegister());
1868 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1869 break;
1870
1871 default:
1872 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1873 }
1874}
1875
1876void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1877 Primitive::Type type = div->GetResultType();
1878 switch (type) {
1879 case Primitive::kPrimInt:
1880 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001881 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001882 break;
1883
1884 case Primitive::kPrimFloat:
1885 case Primitive::kPrimDouble:
1886 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1887 break;
1888
1889 default:
1890 LOG(FATAL) << "Unexpected div type " << type;
1891 }
1892}
1893
Alexandre Rames67555f72014-11-18 10:55:16 +00001894void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1895 LocationSummary* locations =
1896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1897 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1898 if (instruction->HasUses()) {
1899 locations->SetOut(Location::SameAsFirstInput());
1900 }
1901}
1902
1903void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1904 SlowPathCodeARM64* slow_path =
1905 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1906 codegen_->AddSlowPath(slow_path);
1907 Location value = instruction->GetLocations()->InAt(0);
1908
Alexandre Rames3e69f162014-12-10 10:36:50 +00001909 Primitive::Type type = instruction->GetType();
1910
1911 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1912 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1913 return;
1914 }
1915
Alexandre Rames67555f72014-11-18 10:55:16 +00001916 if (value.IsConstant()) {
1917 int64_t divisor = Int64ConstantFrom(value);
1918 if (divisor == 0) {
1919 __ B(slow_path->GetEntryLabel());
1920 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001921 // A division by a non-null constant is valid. We don't need to perform
1922 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001923 }
1924 } else {
1925 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1926 }
1927}
1928
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001929void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1932 locations->SetOut(Location::ConstantLocation(constant));
1933}
1934
1935void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1936 UNUSED(constant);
1937 // Will be generated at use site.
1938}
1939
Alexandre Rames5319def2014-10-23 10:03:10 +01001940void LocationsBuilderARM64::VisitExit(HExit* exit) {
1941 exit->SetLocations(nullptr);
1942}
1943
1944void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001945 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001946}
1947
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001948void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1949 LocationSummary* locations =
1950 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1951 locations->SetOut(Location::ConstantLocation(constant));
1952}
1953
1954void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1955 UNUSED(constant);
1956 // Will be generated at use site.
1957}
1958
Alexandre Rames5319def2014-10-23 10:03:10 +01001959void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1960 got->SetLocations(nullptr);
1961}
1962
1963void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1964 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001965 DCHECK(!successor->IsExitBlock());
1966 HBasicBlock* block = got->GetBlock();
1967 HInstruction* previous = got->GetPrevious();
1968 HLoopInformation* info = block->GetLoopInformation();
1969
David Brazdil46e2a392015-03-16 17:31:52 +00001970 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001971 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1972 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1973 return;
1974 }
1975 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1976 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1977 }
1978 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001979 __ B(codegen_->GetLabelOf(successor));
1980 }
1981}
1982
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001983void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1984 vixl::Label* true_target,
1985 vixl::Label* false_target,
1986 vixl::Label* always_true_target) {
1987 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001988 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001989
Serban Constantinescu02164b32014-11-13 14:05:07 +00001990 if (cond->IsIntConstant()) {
1991 int32_t cond_value = cond->AsIntConstant()->GetValue();
1992 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001993 if (always_true_target != nullptr) {
1994 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001995 }
1996 return;
1997 } else {
1998 DCHECK_EQ(cond_value, 0);
1999 }
2000 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002001 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002002 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002003 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002004 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002005 } else {
2006 // The condition instruction has not been materialized, use its inputs as
2007 // the comparison and its condition as the branch condition.
2008 Register lhs = InputRegisterAt(condition, 0);
2009 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002010 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00002011 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2012 switch (arm64_cond) {
2013 case eq:
2014 __ Cbz(lhs, true_target);
2015 break;
2016 case ne:
2017 __ Cbnz(lhs, true_target);
2018 break;
2019 case lt:
2020 // Test the sign bit and branch accordingly.
2021 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2022 break;
2023 case ge:
2024 // Test the sign bit and branch accordingly.
2025 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2026 break;
2027 default:
2028 // Without the `static_cast` the compiler throws an error for
2029 // `-Werror=sign-promo`.
2030 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002031 }
2032 } else {
2033 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002034 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002035 }
2036 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002037 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002038 __ B(false_target);
2039 }
2040}
2041
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002042void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2044 HInstruction* cond = if_instr->InputAt(0);
2045 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2046 locations->SetInAt(0, Location::RequiresRegister());
2047 }
2048}
2049
2050void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2051 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2052 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2053 vixl::Label* always_true_target = true_target;
2054 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2055 if_instr->IfTrueSuccessor())) {
2056 always_true_target = nullptr;
2057 }
2058 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2059 if_instr->IfFalseSuccessor())) {
2060 false_target = nullptr;
2061 }
2062 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2063}
2064
2065void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2066 LocationSummary* locations = new (GetGraph()->GetArena())
2067 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2068 HInstruction* cond = deoptimize->InputAt(0);
2069 DCHECK(cond->IsCondition());
2070 if (cond->AsCondition()->NeedsMaterialization()) {
2071 locations->SetInAt(0, Location::RequiresRegister());
2072 }
2073}
2074
2075void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2076 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2077 DeoptimizationSlowPathARM64(deoptimize);
2078 codegen_->AddSlowPath(slow_path);
2079 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2080 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2081}
2082
Alexandre Rames5319def2014-10-23 10:03:10 +01002083void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002084 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002085}
2086
2087void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002088 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002089}
2090
2091void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002092 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002093}
2094
2095void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002096 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002097}
2098
Alexandre Rames67555f72014-11-18 10:55:16 +00002099void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2100 LocationSummary::CallKind call_kind =
2101 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2102 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2103 locations->SetInAt(0, Location::RequiresRegister());
2104 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002105 // The output does overlap inputs.
2106 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002107}
2108
2109void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2110 LocationSummary* locations = instruction->GetLocations();
2111 Register obj = InputRegisterAt(instruction, 0);;
2112 Register cls = InputRegisterAt(instruction, 1);;
2113 Register out = OutputRegister(instruction);
2114
2115 vixl::Label done;
2116
2117 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002118 // Avoid null check if we know `obj` is not null.
2119 if (instruction->MustDoNullCheck()) {
2120 __ Mov(out, 0);
2121 __ Cbz(obj, &done);
2122 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002123
2124 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002125 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002126 __ Cmp(out, cls);
2127 if (instruction->IsClassFinal()) {
2128 // Classes must be equal for the instanceof to succeed.
2129 __ Cset(out, eq);
2130 } else {
2131 // If the classes are not equal, we go into a slow path.
2132 DCHECK(locations->OnlyCallsOnSlowPath());
2133 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002134 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2135 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002136 codegen_->AddSlowPath(slow_path);
2137 __ B(ne, slow_path->GetEntryLabel());
2138 __ Mov(out, 1);
2139 __ Bind(slow_path->GetExitLabel());
2140 }
2141
2142 __ Bind(&done);
2143}
2144
Alexandre Rames5319def2014-10-23 10:03:10 +01002145void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2147 locations->SetOut(Location::ConstantLocation(constant));
2148}
2149
2150void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2151 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002152 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002153}
2154
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002155void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2156 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2157 locations->SetOut(Location::ConstantLocation(constant));
2158}
2159
2160void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2161 // Will be generated at use site.
2162 UNUSED(constant);
2163}
2164
Alexandre Rames5319def2014-10-23 10:03:10 +01002165void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
2166 LocationSummary* locations =
2167 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
2168 locations->AddTemp(LocationFrom(x0));
2169
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002170 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillain3e3d7332015-04-28 11:00:54 +01002171 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 HInstruction* input = invoke->InputAt(i);
2173 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
2174 }
2175
2176 Primitive::Type return_type = invoke->GetType();
2177 if (return_type != Primitive::kPrimVoid) {
2178 locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type));
2179 }
2180}
2181
Alexandre Rames67555f72014-11-18 10:55:16 +00002182void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2183 HandleInvoke(invoke);
2184}
2185
2186void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2187 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002188 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2189 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2190 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002191 Location receiver = invoke->GetLocations()->InAt(0);
2192 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002193 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002194
2195 // The register ip1 is required to be used for the hidden argument in
2196 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002197 MacroAssembler* masm = GetVIXLAssembler();
2198 UseScratchRegisterScope scratch_scope(masm);
2199 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002200 scratch_scope.Exclude(ip1);
2201 __ Mov(ip1, invoke->GetDexMethodIndex());
2202
2203 // temp = object->GetClass();
2204 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002205 __ Ldr(temp.W(), StackOperandFrom(receiver));
2206 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002207 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002208 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002209 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002210 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00002211 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002212 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002213 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002214 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002215 // lr();
2216 __ Blr(lr);
2217 DCHECK(!codegen_->IsLeafMethod());
2218 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2219}
2220
2221void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002222 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2223 if (intrinsic.TryDispatch(invoke)) {
2224 return;
2225 }
2226
Alexandre Rames67555f72014-11-18 10:55:16 +00002227 HandleInvoke(invoke);
2228}
2229
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002230void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002231 // When we do not run baseline, explicit clinit checks triggered by static
2232 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2233 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002234
Andreas Gampe878d58c2015-01-15 23:24:00 -08002235 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2236 if (intrinsic.TryDispatch(invoke)) {
2237 return;
2238 }
2239
Alexandre Rames67555f72014-11-18 10:55:16 +00002240 HandleInvoke(invoke);
2241}
2242
Andreas Gampe878d58c2015-01-15 23:24:00 -08002243static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2244 if (invoke->GetLocations()->Intrinsified()) {
2245 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2246 intrinsic.Dispatch(invoke);
2247 return true;
2248 }
2249 return false;
2250}
2251
2252void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Register temp) {
2253 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
2254 DCHECK(temp.Is(kArtMethodRegister));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002255 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002256
2257 // TODO: Implement all kinds of calls:
2258 // 1) boot -> boot
2259 // 2) app -> boot
2260 // 3) app -> app
2261 //
2262 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2263
Jeff Hao848f70a2014-01-15 13:49:50 -08002264 if (invoke->IsStringInit()) {
2265 // temp = thread->string_init_entrypoint
Mathieu Chartiere401d142015-04-22 13:56:20 -07002266 __ Ldr(temp.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002267 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002268 __ Ldr(lr, MemOperand(
2269 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002270 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002271 __ Blr(lr);
2272 } else {
Jeff Hao848f70a2014-01-15 13:49:50 -08002273 // temp = method;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002274 LoadCurrentMethod(temp.X());
Jeff Hao848f70a2014-01-15 13:49:50 -08002275 if (!invoke->IsRecursive()) {
2276 // temp = temp->dex_cache_resolved_methods_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002277 __ Ldr(temp.W(), MemOperand(temp.X(),
2278 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002279 // temp = temp[index_in_cache];
Mathieu Chartiere401d142015-04-22 13:56:20 -07002280 __ Ldr(temp.X(), MemOperand(temp, index_in_cache));
Jeff Hao848f70a2014-01-15 13:49:50 -08002281 // lr = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002282 __ Ldr(lr, MemOperand(temp.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2283 kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002284 // lr();
2285 __ Blr(lr);
2286 } else {
2287 __ Bl(&frame_entry_label_);
2288 }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002289 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002290
Andreas Gampe878d58c2015-01-15 23:24:00 -08002291 DCHECK(!IsLeafMethod());
2292}
2293
2294void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002295 // When we do not run baseline, explicit clinit checks triggered by static
2296 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2297 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002298
Andreas Gampe878d58c2015-01-15 23:24:00 -08002299 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2300 return;
2301 }
2302
Alexandre Ramesd921d642015-04-16 15:07:16 +01002303 BlockPoolsScope block_pools(GetVIXLAssembler());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002304 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002305 codegen_->GenerateStaticOrDirectCall(invoke, temp);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002306 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002307}
2308
2309void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002310 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2311 return;
2312 }
2313
Alexandre Rames5319def2014-10-23 10:03:10 +01002314 LocationSummary* locations = invoke->GetLocations();
2315 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002316 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2317 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2318 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002319 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002320 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002321
Alexandre Ramesd921d642015-04-16 15:07:16 +01002322 BlockPoolsScope block_pools(GetVIXLAssembler());
2323
Alexandre Rames5319def2014-10-23 10:03:10 +01002324 // temp = object->GetClass();
2325 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 __ Ldr(temp.W(), MemOperand(sp, receiver.GetStackIndex()));
2327 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002328 } else {
2329 DCHECK(receiver.IsRegister());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002330 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002331 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002332 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002333 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002334 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002335 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002337 // lr();
2338 __ Blr(lr);
2339 DCHECK(!codegen_->IsLeafMethod());
2340 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2341}
2342
Alexandre Rames67555f72014-11-18 10:55:16 +00002343void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2344 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2345 : LocationSummary::kNoCall;
2346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002347 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 locations->SetOut(Location::RequiresRegister());
2349}
2350
2351void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2352 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002353 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002354 if (cls->IsReferrersClass()) {
2355 DCHECK(!cls->CanCallRuntime());
2356 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002358 } else {
2359 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002360 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002361 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002362
2363 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2364 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2365 codegen_->AddSlowPath(slow_path);
2366 __ Cbz(out, slow_path->GetEntryLabel());
2367 if (cls->MustGenerateClinitCheck()) {
2368 GenerateClassInitializationCheck(slow_path, out);
2369 } else {
2370 __ Bind(slow_path->GetExitLabel());
2371 }
2372 }
2373}
2374
2375void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2376 LocationSummary* locations =
2377 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2378 locations->SetOut(Location::RequiresRegister());
2379}
2380
2381void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2382 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2383 __ Ldr(OutputRegister(instruction), exception);
2384 __ Str(wzr, exception);
2385}
2386
Alexandre Rames5319def2014-10-23 10:03:10 +01002387void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2388 load->SetLocations(nullptr);
2389}
2390
2391void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2392 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002393 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002394}
2395
Alexandre Rames67555f72014-11-18 10:55:16 +00002396void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2397 LocationSummary* locations =
2398 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002399 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002400 locations->SetOut(Location::RequiresRegister());
2401}
2402
2403void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2404 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2405 codegen_->AddSlowPath(slow_path);
2406
2407 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002408 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002409 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002410 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002411 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002412 __ Cbz(out, slow_path->GetEntryLabel());
2413 __ Bind(slow_path->GetExitLabel());
2414}
2415
Alexandre Rames5319def2014-10-23 10:03:10 +01002416void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2417 local->SetLocations(nullptr);
2418}
2419
2420void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2421 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2422}
2423
2424void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2426 locations->SetOut(Location::ConstantLocation(constant));
2427}
2428
2429void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2430 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002431 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002432}
2433
Alexandre Rames67555f72014-11-18 10:55:16 +00002434void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2435 LocationSummary* locations =
2436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2437 InvokeRuntimeCallingConvention calling_convention;
2438 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2439}
2440
2441void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2442 codegen_->InvokeRuntime(instruction->IsEnter()
2443 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2444 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002445 instruction->GetDexPc(),
2446 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002447 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002448}
2449
Alexandre Rames42d641b2014-10-27 14:00:51 +00002450void LocationsBuilderARM64::VisitMul(HMul* mul) {
2451 LocationSummary* locations =
2452 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2453 switch (mul->GetResultType()) {
2454 case Primitive::kPrimInt:
2455 case Primitive::kPrimLong:
2456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002459 break;
2460
2461 case Primitive::kPrimFloat:
2462 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002463 locations->SetInAt(0, Location::RequiresFpuRegister());
2464 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002465 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002466 break;
2467
2468 default:
2469 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2470 }
2471}
2472
2473void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2474 switch (mul->GetResultType()) {
2475 case Primitive::kPrimInt:
2476 case Primitive::kPrimLong:
2477 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2478 break;
2479
2480 case Primitive::kPrimFloat:
2481 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002482 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002483 break;
2484
2485 default:
2486 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2487 }
2488}
2489
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002490void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2491 LocationSummary* locations =
2492 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2493 switch (neg->GetResultType()) {
2494 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002495 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002496 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002498 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002499
2500 case Primitive::kPrimFloat:
2501 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002502 locations->SetInAt(0, Location::RequiresFpuRegister());
2503 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002504 break;
2505
2506 default:
2507 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2508 }
2509}
2510
2511void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2512 switch (neg->GetResultType()) {
2513 case Primitive::kPrimInt:
2514 case Primitive::kPrimLong:
2515 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2516 break;
2517
2518 case Primitive::kPrimFloat:
2519 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002520 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002521 break;
2522
2523 default:
2524 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2525 }
2526}
2527
2528void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2529 LocationSummary* locations =
2530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2531 InvokeRuntimeCallingConvention calling_convention;
2532 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002533 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002534 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002535 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2536 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002537 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002538}
2539
2540void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2541 LocationSummary* locations = instruction->GetLocations();
2542 InvokeRuntimeCallingConvention calling_convention;
2543 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2544 DCHECK(type_index.Is(w0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002545 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimLong);
2546 DCHECK(current_method.Is(x2));
2547 codegen_->LoadCurrentMethod(current_method.X());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002548 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002549 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002550 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2551 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002552 instruction->GetDexPc(),
2553 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002554 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002555}
2556
Alexandre Rames5319def2014-10-23 10:03:10 +01002557void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2558 LocationSummary* locations =
2559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2560 InvokeRuntimeCallingConvention calling_convention;
2561 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2562 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2563 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002564 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002565}
2566
2567void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2568 LocationSummary* locations = instruction->GetLocations();
2569 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2570 DCHECK(type_index.Is(w0));
2571 Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot);
2572 DCHECK(current_method.Is(w1));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002573 codegen_->LoadCurrentMethod(current_method.X());
Alexandre Rames5319def2014-10-23 10:03:10 +01002574 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002575 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002576 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2577 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002578 instruction->GetDexPc(),
2579 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002580 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002581}
2582
2583void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002585 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002587}
2588
2589void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002590 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002591 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002592 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002593 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002594 break;
2595
2596 default:
2597 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2598 }
2599}
2600
David Brazdil66d126e2015-04-03 16:02:44 +01002601void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2603 locations->SetInAt(0, Location::RequiresRegister());
2604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2605}
2606
2607void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002608 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2609}
2610
Alexandre Rames5319def2014-10-23 10:03:10 +01002611void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2612 LocationSummary* locations =
2613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2614 locations->SetInAt(0, Location::RequiresRegister());
2615 if (instruction->HasUses()) {
2616 locations->SetOut(Location::SameAsFirstInput());
2617 }
2618}
2619
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002620void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002621 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2622 return;
2623 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002624
Alexandre Ramesd921d642015-04-16 15:07:16 +01002625 BlockPoolsScope block_pools(GetVIXLAssembler());
2626 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002627 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2628 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2629}
2630
2631void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002632 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2633 codegen_->AddSlowPath(slow_path);
2634
2635 LocationSummary* locations = instruction->GetLocations();
2636 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002637
2638 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002639}
2640
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002641void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2642 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2643 GenerateImplicitNullCheck(instruction);
2644 } else {
2645 GenerateExplicitNullCheck(instruction);
2646 }
2647}
2648
Alexandre Rames67555f72014-11-18 10:55:16 +00002649void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2650 HandleBinaryOp(instruction);
2651}
2652
2653void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2654 HandleBinaryOp(instruction);
2655}
2656
Alexandre Rames3e69f162014-12-10 10:36:50 +00002657void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2658 LOG(FATAL) << "Unreachable";
2659}
2660
2661void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2662 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2663}
2664
Alexandre Rames5319def2014-10-23 10:03:10 +01002665void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2667 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2668 if (location.IsStackSlot()) {
2669 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2670 } else if (location.IsDoubleStackSlot()) {
2671 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2672 }
2673 locations->SetOut(location);
2674}
2675
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002676void InstructionCodeGeneratorARM64::VisitParameterValue(
2677 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002678 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002679}
2680
2681void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2682 LocationSummary* locations =
2683 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2684 locations->SetOut(LocationFrom(x0));
2685}
2686
2687void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2688 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2689 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002690}
2691
2692void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2694 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2695 locations->SetInAt(i, Location::Any());
2696 }
2697 locations->SetOut(Location::Any());
2698}
2699
2700void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002701 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002702 LOG(FATAL) << "Unreachable";
2703}
2704
Serban Constantinescu02164b32014-11-13 14:05:07 +00002705void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002706 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002707 LocationSummary::CallKind call_kind =
2708 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2710
2711 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002712 case Primitive::kPrimInt:
2713 case Primitive::kPrimLong:
2714 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002715 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002716 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2717 break;
2718
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002719 case Primitive::kPrimFloat:
2720 case Primitive::kPrimDouble: {
2721 InvokeRuntimeCallingConvention calling_convention;
2722 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2723 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2724 locations->SetOut(calling_convention.GetReturnLocation(type));
2725
2726 break;
2727 }
2728
Serban Constantinescu02164b32014-11-13 14:05:07 +00002729 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002730 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002731 }
2732}
2733
2734void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2735 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002736
Serban Constantinescu02164b32014-11-13 14:05:07 +00002737 switch (type) {
2738 case Primitive::kPrimInt:
2739 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002740 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002741 break;
2742 }
2743
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002744 case Primitive::kPrimFloat:
2745 case Primitive::kPrimDouble: {
2746 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2747 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002748 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002749 break;
2750 }
2751
Serban Constantinescu02164b32014-11-13 14:05:07 +00002752 default:
2753 LOG(FATAL) << "Unexpected rem type " << type;
2754 }
2755}
2756
Calin Juravle27df7582015-04-17 19:12:31 +01002757void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2758 memory_barrier->SetLocations(nullptr);
2759}
2760
2761void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2762 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2763}
2764
Alexandre Rames5319def2014-10-23 10:03:10 +01002765void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2766 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2767 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002768 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002769}
2770
2771void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002772 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002773 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002774}
2775
2776void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2777 instruction->SetLocations(nullptr);
2778}
2779
2780void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002781 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002782 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002783}
2784
Serban Constantinescu02164b32014-11-13 14:05:07 +00002785void LocationsBuilderARM64::VisitShl(HShl* shl) {
2786 HandleShift(shl);
2787}
2788
2789void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2790 HandleShift(shl);
2791}
2792
2793void LocationsBuilderARM64::VisitShr(HShr* shr) {
2794 HandleShift(shr);
2795}
2796
2797void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2798 HandleShift(shr);
2799}
2800
Alexandre Rames5319def2014-10-23 10:03:10 +01002801void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2803 Primitive::Type field_type = store->InputAt(1)->GetType();
2804 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002805 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002806 case Primitive::kPrimBoolean:
2807 case Primitive::kPrimByte:
2808 case Primitive::kPrimChar:
2809 case Primitive::kPrimShort:
2810 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002811 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002812 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2813 break;
2814
2815 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002816 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002817 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2818 break;
2819
2820 default:
2821 LOG(FATAL) << "Unimplemented local type " << field_type;
2822 }
2823}
2824
2825void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002826 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002827}
2828
2829void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002830 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002831}
2832
2833void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002834 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002835}
2836
Alexandre Rames67555f72014-11-18 10:55:16 +00002837void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002838 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002839}
2840
2841void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002842 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002843}
2844
2845void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002846 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002847}
2848
Alexandre Rames67555f72014-11-18 10:55:16 +00002849void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002850 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002851}
2852
2853void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2855}
2856
2857void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002858 HBasicBlock* block = instruction->GetBlock();
2859 if (block->GetLoopInformation() != nullptr) {
2860 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2861 // The back edge will generate the suspend check.
2862 return;
2863 }
2864 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2865 // The goto will generate the suspend check.
2866 return;
2867 }
2868 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002869}
2870
2871void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2872 temp->SetLocations(nullptr);
2873}
2874
2875void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2876 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002877 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002878}
2879
Alexandre Rames67555f72014-11-18 10:55:16 +00002880void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2881 LocationSummary* locations =
2882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2883 InvokeRuntimeCallingConvention calling_convention;
2884 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2885}
2886
2887void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2888 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002889 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002890 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002891}
2892
2893void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2894 LocationSummary* locations =
2895 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2896 Primitive::Type input_type = conversion->GetInputType();
2897 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002898 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002899 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2900 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2901 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2902 }
2903
Alexandre Rames542361f2015-01-29 16:57:31 +00002904 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002905 locations->SetInAt(0, Location::RequiresFpuRegister());
2906 } else {
2907 locations->SetInAt(0, Location::RequiresRegister());
2908 }
2909
Alexandre Rames542361f2015-01-29 16:57:31 +00002910 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2912 } else {
2913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2914 }
2915}
2916
2917void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2918 Primitive::Type result_type = conversion->GetResultType();
2919 Primitive::Type input_type = conversion->GetInputType();
2920
2921 DCHECK_NE(input_type, result_type);
2922
Alexandre Rames542361f2015-01-29 16:57:31 +00002923 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002924 int result_size = Primitive::ComponentSize(result_type);
2925 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002926 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002927 Register output = OutputRegister(conversion);
2928 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002929 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2930 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2931 } else if ((result_type == Primitive::kPrimChar) ||
2932 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2933 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002934 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002935 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002936 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002937 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002938 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002939 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002940 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2941 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002942 } else if (Primitive::IsFloatingPointType(result_type) &&
2943 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002944 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2945 } else {
2946 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2947 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002948 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002949}
Alexandre Rames67555f72014-11-18 10:55:16 +00002950
Serban Constantinescu02164b32014-11-13 14:05:07 +00002951void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2952 HandleShift(ushr);
2953}
2954
2955void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2956 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002957}
2958
2959void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2960 HandleBinaryOp(instruction);
2961}
2962
2963void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2964 HandleBinaryOp(instruction);
2965}
2966
Calin Juravleb1498f62015-02-16 13:13:29 +00002967void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2968 // Nothing to do, this should be removed during prepare for register allocator.
2969 UNUSED(instruction);
2970 LOG(FATAL) << "Unreachable";
2971}
2972
2973void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2974 // Nothing to do, this should be removed during prepare for register allocator.
2975 UNUSED(instruction);
2976 LOG(FATAL) << "Unreachable";
2977}
2978
Alexandre Rames67555f72014-11-18 10:55:16 +00002979#undef __
2980#undef QUICK_ENTRY_POINT
2981
Alexandre Rames5319def2014-10-23 10:03:10 +01002982} // namespace arm64
2983} // namespace art