blob: 3c8f1170113b81c36d44e5a38207ec5241b90d79 [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) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010096 } else if (return_type == Primitive::kPrimVoid) {
97 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000098 } else {
99 return LocationFrom(w0);
100 }
101}
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000104 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105}
106
Alexandre Rames67555f72014-11-18 10:55:16 +0000107#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
Zheng Xuda403092015-04-24 17:35:39 +0800110// Calculate memory accessing operand for save/restore live registers.
111static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
112 RegisterSet* register_set,
113 int64_t spill_offset,
114 bool is_save) {
115 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
116 codegen->GetNumberOfCoreRegisters(),
117 register_set->GetFloatingPointRegisters(),
118 codegen->GetNumberOfFloatingPointRegisters()));
119
120 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
121 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
122 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
123 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
124
125 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
126 UseScratchRegisterScope temps(masm);
127
128 Register base = masm->StackPointer();
129 int64_t core_spill_size = core_list.TotalSizeInBytes();
130 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
131 int64_t reg_size = kXRegSizeInBytes;
132 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
133 uint32_t ls_access_size = WhichPowerOf2(reg_size);
134 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
135 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
136 // If the offset does not fit in the instruction's immediate field, use an alternate register
137 // to compute the base address(float point registers spill base address).
138 Register new_base = temps.AcquireSameSizeAs(base);
139 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
140 base = new_base;
141 spill_offset = -core_spill_size;
142 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
143 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
144 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
145 }
146
147 if (is_save) {
148 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
149 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
150 } else {
151 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
152 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 }
154}
155
156void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
157 RegisterSet* register_set = locations->GetLiveRegisters();
158 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
159 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
160 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
161 // If the register holds an object, update the stack mask.
162 if (locations->RegisterContainsObject(i)) {
163 locations->SetStackBit(stack_offset / kVRegSize);
164 }
165 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
166 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
167 saved_core_stack_offsets_[i] = stack_offset;
168 stack_offset += kXRegSizeInBytes;
169 }
170 }
171
172 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
173 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
174 register_set->ContainsFloatingPointRegister(i)) {
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_fpu_stack_offsets_[i] = stack_offset;
178 stack_offset += kDRegSizeInBytes;
179 }
180 }
181
182 SaveRestoreLiveRegistersHelper(codegen, register_set,
183 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
184}
185
186void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 SaveRestoreLiveRegistersHelper(codegen, register_set,
189 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
190}
191
Alexandre Rames5319def2014-10-23 10:03:10 +0100192class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
193 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000194 BoundsCheckSlowPathARM64(HBoundsCheck* instruction,
195 Location index_location,
196 Location length_location)
197 : instruction_(instruction),
198 index_location_(index_location),
199 length_location_(length_location) {}
200
Alexandre Rames5319def2014-10-23 10:03:10 +0100201
Alexandre Rames67555f72014-11-18 10:55:16 +0000202 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000203 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100204 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 // We're moving two locations to locations that could overlap, so we need a parallel
206 // move resolver.
207 InvokeRuntimeCallingConvention calling_convention;
208 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100209 index_location_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
210 length_location_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000211 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000212 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800213 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100214 }
215
216 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000217 HBoundsCheck* const instruction_;
218 const Location index_location_;
219 const Location length_location_;
220
Alexandre Rames5319def2014-10-23 10:03:10 +0100221 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
222};
223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
225 public:
226 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
227
228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
229 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
230 __ Bind(GetEntryLabel());
231 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000232 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800233 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000234 }
235
236 private:
237 HDivZeroCheck* const instruction_;
238 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
239};
240
241class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
242 public:
243 LoadClassSlowPathARM64(HLoadClass* cls,
244 HInstruction* at,
245 uint32_t dex_pc,
246 bool do_clinit)
247 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
248 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
249 }
250
251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
252 LocationSummary* locations = at_->GetLocations();
253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
254
255 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 InvokeRuntimeCallingConvention calling_convention;
259 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
261 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000262 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800263 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800265 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000268
269 // Move the class to the desired location.
270 Location out = locations->Out();
271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
273 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000274 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 }
276
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000278 __ B(GetExitLabel());
279 }
280
281 private:
282 // The class this slow path will load.
283 HLoadClass* const cls_;
284
285 // The instruction where this slow path is happening.
286 // (Might be the load class or an initialization check).
287 HInstruction* const at_;
288
289 // The dex PC of `at_`.
290 const uint32_t dex_pc_;
291
292 // Whether to initialize the class.
293 const bool do_clinit_;
294
295 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
296};
297
298class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
299 public:
300 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
301
302 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
303 LocationSummary* locations = instruction_->GetLocations();
304 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
305 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
306
307 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000308 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000309
310 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800311 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000313 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100314 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000316 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000318 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 __ B(GetExitLabel());
320 }
321
322 private:
323 HLoadString* const instruction_;
324
325 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
326};
327
Alexandre Rames5319def2014-10-23 10:03:10 +0100328class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
329 public:
330 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
331
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
333 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100334 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000336 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800337 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100338 }
339
340 private:
341 HNullCheck* const instruction_;
342
343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
344};
345
346class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
347 public:
348 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
349 HBasicBlock* successor)
350 : instruction_(instruction), successor_(successor) {}
351
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
353 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100354 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000355 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000357 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800358 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 if (successor_ == nullptr) {
361 __ B(GetReturnLabel());
362 } else {
363 __ B(arm64_codegen->GetLabelOf(successor_));
364 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 }
366
367 vixl::Label* GetReturnLabel() {
368 DCHECK(successor_ == nullptr);
369 return &return_label_;
370 }
371
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100372 HBasicBlock* GetSuccessor() const {
373 return successor_;
374 }
375
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 private:
377 HSuspendCheck* const instruction_;
378 // If not null, the block to branch to after the suspend check.
379 HBasicBlock* const successor_;
380
381 // If `successor_` is null, the label to branch to after the suspend check.
382 vixl::Label return_label_;
383
384 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
385};
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
388 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000389 TypeCheckSlowPathARM64(HInstruction* instruction,
390 Location class_to_check,
391 Location object_class,
392 uint32_t dex_pc)
393 : instruction_(instruction),
394 class_to_check_(class_to_check),
395 object_class_(object_class),
396 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000397
398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000399 LocationSummary* locations = instruction_->GetLocations();
400 DCHECK(instruction_->IsCheckCast()
401 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
402 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
403
Alexandre Rames67555f72014-11-18 10:55:16 +0000404 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000405 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000406
407 // We're moving two locations to locations that could overlap, so we need a parallel
408 // move resolver.
409 InvokeRuntimeCallingConvention calling_convention;
410 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100411 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
412 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000413
414 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000415 arm64_codegen->InvokeRuntime(
416 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000417 Primitive::Type ret_type = instruction_->GetType();
418 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
419 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800420 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
421 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000422 } else {
423 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000424 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800425 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426 }
427
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000428 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000429 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000430 }
431
432 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000433 HInstruction* const instruction_;
434 const Location class_to_check_;
435 const Location object_class_;
436 uint32_t dex_pc_;
437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
439};
440
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700441class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
442 public:
443 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
444 : instruction_(instruction) {}
445
446 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
447 __ Bind(GetEntryLabel());
448 SaveLiveRegisters(codegen, instruction_->GetLocations());
449 DCHECK(instruction_->IsDeoptimize());
450 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
451 uint32_t dex_pc = deoptimize->GetDexPc();
452 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
453 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
454 }
455
456 private:
457 HInstruction* const instruction_;
458 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
459};
460
Alexandre Rames5319def2014-10-23 10:03:10 +0100461#undef __
462
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100463Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100464 Location next_location;
465 if (type == Primitive::kPrimVoid) {
466 LOG(FATAL) << "Unreachable type " << type;
467 }
468
Alexandre Rames542361f2015-01-29 16:57:31 +0000469 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100470 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
471 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000472 } else if (!Primitive::IsFloatingPointType(type) &&
473 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000474 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
475 } else {
476 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000477 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
478 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100479 }
480
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000481 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000482 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 return next_location;
484}
485
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100486Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100487 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100488}
489
Serban Constantinescu579885a2015-02-22 20:51:33 +0000490CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
491 const Arm64InstructionSetFeatures& isa_features,
492 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100493 : CodeGenerator(graph,
494 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000495 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000496 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000497 callee_saved_core_registers.list(),
498 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000499 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100500 block_labels_(nullptr),
501 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000502 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000503 move_resolver_(graph->GetArena(), this),
504 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000505 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000506 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000507}
Alexandre Rames5319def2014-10-23 10:03:10 +0100508
Alexandre Rames67555f72014-11-18 10:55:16 +0000509#undef __
510#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100511
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000512void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
513 // Ensure we emit the literal pool.
514 __ FinalizeCode();
515 CodeGenerator::Finalize(allocator);
516}
517
Zheng Xuad4450e2015-04-17 18:48:56 +0800518void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
519 // Note: There are 6 kinds of moves:
520 // 1. constant -> GPR/FPR (non-cycle)
521 // 2. constant -> stack (non-cycle)
522 // 3. GPR/FPR -> GPR/FPR
523 // 4. GPR/FPR -> stack
524 // 5. stack -> GPR/FPR
525 // 6. stack -> stack (non-cycle)
526 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
527 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
528 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
529 // dependency.
530 vixl_temps_.Open(GetVIXLAssembler());
531}
532
533void ParallelMoveResolverARM64::FinishEmitNativeCode() {
534 vixl_temps_.Close();
535}
536
537Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
538 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
539 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
540 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
541 Location scratch = GetScratchLocation(kind);
542 if (!scratch.Equals(Location::NoLocation())) {
543 return scratch;
544 }
545 // Allocate from VIXL temp registers.
546 if (kind == Location::kRegister) {
547 scratch = LocationFrom(vixl_temps_.AcquireX());
548 } else {
549 DCHECK(kind == Location::kFpuRegister);
550 scratch = LocationFrom(vixl_temps_.AcquireD());
551 }
552 AddScratchLocation(scratch);
553 return scratch;
554}
555
556void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
557 if (loc.IsRegister()) {
558 vixl_temps_.Release(XRegisterFrom(loc));
559 } else {
560 DCHECK(loc.IsFpuRegister());
561 vixl_temps_.Release(DRegisterFrom(loc));
562 }
563 RemoveScratchLocation(loc);
564}
565
Alexandre Rames3e69f162014-12-10 10:36:50 +0000566void ParallelMoveResolverARM64::EmitMove(size_t index) {
567 MoveOperands* move = moves_.Get(index);
568 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
569}
570
Alexandre Rames5319def2014-10-23 10:03:10 +0100571void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100572 MacroAssembler* masm = GetVIXLAssembler();
573 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000574 __ Bind(&frame_entry_label_);
575
Serban Constantinescu02164b32014-11-13 14:05:07 +0000576 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
577 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100578 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000579 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000580 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000581 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000582 __ Ldr(wzr, MemOperand(temp, 0));
583 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000584 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100585
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000586 if (!HasEmptyFrame()) {
587 int frame_size = GetFrameSize();
588 // Stack layout:
589 // sp[frame_size - 8] : lr.
590 // ... : other preserved core registers.
591 // ... : other preserved fp registers.
592 // ... : reserved frame space.
593 // sp[0] : current method.
594 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100595 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800596 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
597 frame_size - GetCoreSpillSize());
598 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
599 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000600 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100601}
602
603void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100604 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100605 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000606 if (!HasEmptyFrame()) {
607 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800608 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
609 frame_size - FrameEntrySpillSize());
610 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
611 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000612 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100613 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000614 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100615 __ Ret();
616 GetAssembler()->cfi().RestoreState();
617 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100618}
619
Zheng Xuda403092015-04-24 17:35:39 +0800620vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
621 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
622 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
623 core_spill_mask_);
624}
625
626vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
627 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
628 GetNumberOfFloatingPointRegisters()));
629 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
630 fpu_spill_mask_);
631}
632
Alexandre Rames5319def2014-10-23 10:03:10 +0100633void CodeGeneratorARM64::Bind(HBasicBlock* block) {
634 __ Bind(GetLabelOf(block));
635}
636
Alexandre Rames5319def2014-10-23 10:03:10 +0100637void CodeGeneratorARM64::Move(HInstruction* instruction,
638 Location location,
639 HInstruction* move_for) {
640 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100641 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000642 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100643
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100644 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700645 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100646 } else if (locations != nullptr && locations->Out().Equals(location)) {
647 return;
648 } else if (instruction->IsIntConstant()
649 || instruction->IsLongConstant()
650 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000651 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100652 if (location.IsRegister()) {
653 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000654 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100655 (instruction->IsLongConstant() && dst.Is64Bits()));
656 __ Mov(dst, value);
657 } else {
658 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000659 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000660 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
661 ? temps.AcquireW()
662 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100663 __ Mov(temp, value);
664 __ Str(temp, StackOperandFrom(location));
665 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000666 } else if (instruction->IsTemporary()) {
667 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000668 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100669 } else if (instruction->IsLoadLocal()) {
670 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000671 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000672 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000673 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000674 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100675 }
676
677 } else {
678 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000679 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100680 }
681}
682
Alexandre Rames5319def2014-10-23 10:03:10 +0100683Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
684 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000685
Alexandre Rames5319def2014-10-23 10:03:10 +0100686 switch (type) {
687 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000688 case Primitive::kPrimInt:
689 case Primitive::kPrimFloat:
690 return Location::StackSlot(GetStackSlot(load->GetLocal()));
691
692 case Primitive::kPrimLong:
693 case Primitive::kPrimDouble:
694 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
695
Alexandre Rames5319def2014-10-23 10:03:10 +0100696 case Primitive::kPrimBoolean:
697 case Primitive::kPrimByte:
698 case Primitive::kPrimChar:
699 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100700 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100701 LOG(FATAL) << "Unexpected type " << type;
702 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000703
Alexandre Rames5319def2014-10-23 10:03:10 +0100704 LOG(FATAL) << "Unreachable";
705 return Location::NoLocation();
706}
707
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100708void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000709 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100710 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000711 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100712 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100713 if (value_can_be_null) {
714 __ Cbz(value, &done);
715 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100716 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
717 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000718 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100719 if (value_can_be_null) {
720 __ Bind(&done);
721 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100722}
723
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000724void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
725 // Blocked core registers:
726 // lr : Runtime reserved.
727 // tr : Runtime reserved.
728 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
729 // ip1 : VIXL core temp.
730 // ip0 : VIXL core temp.
731 //
732 // Blocked fp registers:
733 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100734 CPURegList reserved_core_registers = vixl_reserved_core_registers;
735 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100736 while (!reserved_core_registers.IsEmpty()) {
737 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
738 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000739
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000740 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800741 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000742 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
743 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000744
745 if (is_baseline) {
746 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
747 while (!reserved_core_baseline_registers.IsEmpty()) {
748 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
749 }
750
751 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
752 while (!reserved_fp_baseline_registers.IsEmpty()) {
753 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
754 }
755 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100756}
757
758Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
759 if (type == Primitive::kPrimVoid) {
760 LOG(FATAL) << "Unreachable type " << type;
761 }
762
Alexandre Rames542361f2015-01-29 16:57:31 +0000763 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000764 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
765 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100766 return Location::FpuRegisterLocation(reg);
767 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000768 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
769 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100770 return Location::RegisterLocation(reg);
771 }
772}
773
Alexandre Rames3e69f162014-12-10 10:36:50 +0000774size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
775 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
776 __ Str(reg, MemOperand(sp, stack_index));
777 return kArm64WordSize;
778}
779
780size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
781 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
782 __ Ldr(reg, MemOperand(sp, stack_index));
783 return kArm64WordSize;
784}
785
786size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
787 FPRegister reg = FPRegister(reg_id, kDRegSize);
788 __ Str(reg, MemOperand(sp, stack_index));
789 return kArm64WordSize;
790}
791
792size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
793 FPRegister reg = FPRegister(reg_id, kDRegSize);
794 __ Ldr(reg, MemOperand(sp, stack_index));
795 return kArm64WordSize;
796}
797
Alexandre Rames5319def2014-10-23 10:03:10 +0100798void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100799 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100800}
801
802void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100803 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100804}
805
Alexandre Rames67555f72014-11-18 10:55:16 +0000806void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000807 if (constant->IsIntConstant()) {
808 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
809 } else if (constant->IsLongConstant()) {
810 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
811 } else if (constant->IsNullConstant()) {
812 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000813 } else if (constant->IsFloatConstant()) {
814 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
815 } else {
816 DCHECK(constant->IsDoubleConstant());
817 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
818 }
819}
820
Alexandre Rames3e69f162014-12-10 10:36:50 +0000821
822static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
823 DCHECK(constant.IsConstant());
824 HConstant* cst = constant.GetConstant();
825 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000826 // Null is mapped to a core W register, which we associate with kPrimInt.
827 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000828 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
829 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
830 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
831}
832
833void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000834 if (source.Equals(destination)) {
835 return;
836 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000837
838 // A valid move can always be inferred from the destination and source
839 // locations. When moving from and to a register, the argument type can be
840 // used to generate 32bit instead of 64bit moves. In debug mode we also
841 // checks the coherency of the locations and the type.
842 bool unspecified_type = (type == Primitive::kPrimVoid);
843
844 if (destination.IsRegister() || destination.IsFpuRegister()) {
845 if (unspecified_type) {
846 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
847 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 (src_cst != nullptr && (src_cst->IsIntConstant()
849 || src_cst->IsFloatConstant()
850 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000851 // For stack slots and 32bit constants, a 64bit type is appropriate.
852 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000853 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000854 // If the source is a double stack slot or a 64bit constant, a 64bit
855 // type is appropriate. Else the source is a register, and since the
856 // type has not been specified, we chose a 64bit type to force a 64bit
857 // move.
858 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000859 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000860 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000861 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
862 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000863 CPURegister dst = CPURegisterFrom(destination, type);
864 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
865 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
866 __ Ldr(dst, StackOperandFrom(source));
867 } else if (source.IsConstant()) {
868 DCHECK(CoherentConstantAndType(source, type));
869 MoveConstant(dst, source.GetConstant());
870 } else {
871 if (destination.IsRegister()) {
872 __ Mov(Register(dst), RegisterFrom(source, type));
873 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800874 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000875 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
876 }
877 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 } else { // The destination is not a register. It must be a stack slot.
879 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
880 if (source.IsRegister() || source.IsFpuRegister()) {
881 if (unspecified_type) {
882 if (source.IsRegister()) {
883 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
884 } else {
885 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
886 }
887 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000888 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
889 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000890 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
891 } else if (source.IsConstant()) {
892 DCHECK(unspecified_type || CoherentConstantAndType(source, type));
893 UseScratchRegisterScope temps(GetVIXLAssembler());
894 HConstant* src_cst = source.GetConstant();
895 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000896 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000897 temp = temps.AcquireW();
898 } else if (src_cst->IsLongConstant()) {
899 temp = temps.AcquireX();
900 } else if (src_cst->IsFloatConstant()) {
901 temp = temps.AcquireS();
902 } else {
903 DCHECK(src_cst->IsDoubleConstant());
904 temp = temps.AcquireD();
905 }
906 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000907 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000908 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000909 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000910 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000911 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000912 // There is generally less pressure on FP registers.
913 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000914 __ Ldr(temp, StackOperandFrom(source));
915 __ Str(temp, StackOperandFrom(destination));
916 }
917 }
918}
919
920void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000921 CPURegister dst,
922 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000923 switch (type) {
924 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000925 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000926 break;
927 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000928 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000929 break;
930 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000931 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000932 break;
933 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000934 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000935 break;
936 case Primitive::kPrimInt:
937 case Primitive::kPrimNot:
938 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000939 case Primitive::kPrimFloat:
940 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000941 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000942 __ Ldr(dst, src);
943 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000944 case Primitive::kPrimVoid:
945 LOG(FATAL) << "Unreachable type " << type;
946 }
947}
948
Calin Juravle77520bc2015-01-12 18:45:46 +0000949void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000950 CPURegister dst,
951 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100952 MacroAssembler* masm = GetVIXLAssembler();
953 BlockPoolsScope block_pools(masm);
954 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000955 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000956 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000957
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000958 DCHECK(!src.IsPreIndex());
959 DCHECK(!src.IsPostIndex());
960
961 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800962 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000963 MemOperand base = MemOperand(temp_base);
964 switch (type) {
965 case Primitive::kPrimBoolean:
966 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000967 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000968 break;
969 case Primitive::kPrimByte:
970 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000971 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000972 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
973 break;
974 case Primitive::kPrimChar:
975 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000976 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977 break;
978 case Primitive::kPrimShort:
979 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000980 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000981 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
982 break;
983 case Primitive::kPrimInt:
984 case Primitive::kPrimNot:
985 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +0000986 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000987 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000988 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000989 break;
990 case Primitive::kPrimFloat:
991 case Primitive::kPrimDouble: {
992 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +0000993 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994
995 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
996 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000997 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000998 __ Fmov(FPRegister(dst), temp);
999 break;
1000 }
1001 case Primitive::kPrimVoid:
1002 LOG(FATAL) << "Unreachable type " << type;
1003 }
1004}
1005
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001006void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001007 CPURegister src,
1008 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001009 switch (type) {
1010 case Primitive::kPrimBoolean:
1011 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001012 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001013 break;
1014 case Primitive::kPrimChar:
1015 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001017 break;
1018 case Primitive::kPrimInt:
1019 case Primitive::kPrimNot:
1020 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001021 case Primitive::kPrimFloat:
1022 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001023 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001024 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001025 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001026 case Primitive::kPrimVoid:
1027 LOG(FATAL) << "Unreachable type " << type;
1028 }
1029}
1030
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001031void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1032 CPURegister src,
1033 const MemOperand& dst) {
1034 UseScratchRegisterScope temps(GetVIXLAssembler());
1035 Register temp_base = temps.AcquireX();
1036
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001037 DCHECK(!dst.IsPreIndex());
1038 DCHECK(!dst.IsPostIndex());
1039
1040 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001041 Operand op = OperandFromMemOperand(dst);
1042 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001043 MemOperand base = MemOperand(temp_base);
1044 switch (type) {
1045 case Primitive::kPrimBoolean:
1046 case Primitive::kPrimByte:
1047 __ Stlrb(Register(src), base);
1048 break;
1049 case Primitive::kPrimChar:
1050 case Primitive::kPrimShort:
1051 __ Stlrh(Register(src), base);
1052 break;
1053 case Primitive::kPrimInt:
1054 case Primitive::kPrimNot:
1055 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001056 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001057 __ Stlr(Register(src), base);
1058 break;
1059 case Primitive::kPrimFloat:
1060 case Primitive::kPrimDouble: {
1061 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001062 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001063
1064 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1065 __ Fmov(temp, FPRegister(src));
1066 __ Stlr(temp, base);
1067 break;
1068 }
1069 case Primitive::kPrimVoid:
1070 LOG(FATAL) << "Unreachable type " << type;
1071 }
1072}
1073
Alexandre Rames67555f72014-11-18 10:55:16 +00001074void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1075 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001076 uint32_t dex_pc,
1077 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001078 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001079 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1080 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001081 RecordPcInfo(instruction, dex_pc, slow_path);
1082 DCHECK(instruction->IsSuspendCheck()
1083 || instruction->IsBoundsCheck()
1084 || instruction->IsNullCheck()
1085 || instruction->IsDivZeroCheck()
1086 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001087}
1088
1089void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1090 vixl::Register class_reg) {
1091 UseScratchRegisterScope temps(GetVIXLAssembler());
1092 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001093 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001094 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001095
Serban Constantinescu02164b32014-11-13 14:05:07 +00001096 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001097 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001098 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1099 __ Add(temp, class_reg, status_offset);
1100 __ Ldar(temp, HeapOperand(temp));
1101 __ Cmp(temp, mirror::Class::kStatusInitialized);
1102 __ B(lt, slow_path->GetEntryLabel());
1103 } else {
1104 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1105 __ Cmp(temp, mirror::Class::kStatusInitialized);
1106 __ B(lt, slow_path->GetEntryLabel());
1107 __ Dmb(InnerShareable, BarrierReads);
1108 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001109 __ Bind(slow_path->GetExitLabel());
1110}
Alexandre Rames5319def2014-10-23 10:03:10 +01001111
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001112void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1113 BarrierType type = BarrierAll;
1114
1115 switch (kind) {
1116 case MemBarrierKind::kAnyAny:
1117 case MemBarrierKind::kAnyStore: {
1118 type = BarrierAll;
1119 break;
1120 }
1121 case MemBarrierKind::kLoadAny: {
1122 type = BarrierReads;
1123 break;
1124 }
1125 case MemBarrierKind::kStoreStore: {
1126 type = BarrierWrites;
1127 break;
1128 }
1129 default:
1130 LOG(FATAL) << "Unexpected memory barrier " << kind;
1131 }
1132 __ Dmb(InnerShareable, type);
1133}
1134
Serban Constantinescu02164b32014-11-13 14:05:07 +00001135void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1136 HBasicBlock* successor) {
1137 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001138 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1139 if (slow_path == nullptr) {
1140 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1141 instruction->SetSlowPath(slow_path);
1142 codegen_->AddSlowPath(slow_path);
1143 if (successor != nullptr) {
1144 DCHECK(successor->IsLoopHeader());
1145 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1146 }
1147 } else {
1148 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1149 }
1150
Serban Constantinescu02164b32014-11-13 14:05:07 +00001151 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1152 Register temp = temps.AcquireW();
1153
1154 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1155 if (successor == nullptr) {
1156 __ Cbnz(temp, slow_path->GetEntryLabel());
1157 __ Bind(slow_path->GetReturnLabel());
1158 } else {
1159 __ Cbz(temp, codegen_->GetLabelOf(successor));
1160 __ B(slow_path->GetEntryLabel());
1161 // slow_path will return to GetLabelOf(successor).
1162 }
1163}
1164
Alexandre Rames5319def2014-10-23 10:03:10 +01001165InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1166 CodeGeneratorARM64* codegen)
1167 : HGraphVisitor(graph),
1168 assembler_(codegen->GetAssembler()),
1169 codegen_(codegen) {}
1170
1171#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001172 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001173
1174#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1175
1176enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001177 // Using a base helps identify when we hit such breakpoints.
1178 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001179#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1180 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1181#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1182};
1183
1184#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1185 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001186 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001187 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1188 } \
1189 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1190 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1191 locations->SetOut(Location::Any()); \
1192 }
1193 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1194#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1195
1196#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001197#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001198
Alexandre Rames67555f72014-11-18 10:55:16 +00001199void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001200 DCHECK_EQ(instr->InputCount(), 2U);
1201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1202 Primitive::Type type = instr->GetResultType();
1203 switch (type) {
1204 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001205 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001206 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001207 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001209 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001210
1211 case Primitive::kPrimFloat:
1212 case Primitive::kPrimDouble:
1213 locations->SetInAt(0, Location::RequiresFpuRegister());
1214 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001215 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001216 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001217
Alexandre Rames5319def2014-10-23 10:03:10 +01001218 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001219 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001220 }
1221}
1222
Alexandre Rames09a99962015-04-15 11:47:56 +01001223void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1224 LocationSummary* locations =
1225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1226 locations->SetInAt(0, Location::RequiresRegister());
1227 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1228 locations->SetOut(Location::RequiresFpuRegister());
1229 } else {
1230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1231 }
1232}
1233
1234void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1235 const FieldInfo& field_info) {
1236 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001237 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001238
1239 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1240 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1241
1242 if (field_info.IsVolatile()) {
1243 if (use_acquire_release) {
1244 // NB: LoadAcquire will record the pc info if needed.
1245 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1246 } else {
1247 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1248 codegen_->MaybeRecordImplicitNullCheck(instruction);
1249 // For IRIW sequential consistency kLoadAny is not sufficient.
1250 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1251 }
1252 } else {
1253 codegen_->Load(field_info.GetFieldType(), OutputCPURegister(instruction), field);
1254 codegen_->MaybeRecordImplicitNullCheck(instruction);
1255 }
1256}
1257
1258void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1259 LocationSummary* locations =
1260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1263 locations->SetInAt(1, Location::RequiresFpuRegister());
1264 } else {
1265 locations->SetInAt(1, Location::RequiresRegister());
1266 }
1267}
1268
1269void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001270 const FieldInfo& field_info,
1271 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001272 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001273 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001274
1275 Register obj = InputRegisterAt(instruction, 0);
1276 CPURegister value = InputCPURegisterAt(instruction, 1);
1277 Offset offset = field_info.GetFieldOffset();
1278 Primitive::Type field_type = field_info.GetFieldType();
1279 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1280
1281 if (field_info.IsVolatile()) {
1282 if (use_acquire_release) {
1283 codegen_->StoreRelease(field_type, value, HeapOperand(obj, offset));
1284 codegen_->MaybeRecordImplicitNullCheck(instruction);
1285 } else {
1286 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1287 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1288 codegen_->MaybeRecordImplicitNullCheck(instruction);
1289 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1290 }
1291 } else {
1292 codegen_->Store(field_type, value, HeapOperand(obj, offset));
1293 codegen_->MaybeRecordImplicitNullCheck(instruction);
1294 }
1295
1296 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001297 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001298 }
1299}
1300
Alexandre Rames67555f72014-11-18 10:55:16 +00001301void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001302 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001303
1304 switch (type) {
1305 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001306 case Primitive::kPrimLong: {
1307 Register dst = OutputRegister(instr);
1308 Register lhs = InputRegisterAt(instr, 0);
1309 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001310 if (instr->IsAdd()) {
1311 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001312 } else if (instr->IsAnd()) {
1313 __ And(dst, lhs, rhs);
1314 } else if (instr->IsOr()) {
1315 __ Orr(dst, lhs, rhs);
1316 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001317 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 } else {
1319 DCHECK(instr->IsXor());
1320 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001321 }
1322 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001323 }
1324 case Primitive::kPrimFloat:
1325 case Primitive::kPrimDouble: {
1326 FPRegister dst = OutputFPRegister(instr);
1327 FPRegister lhs = InputFPRegisterAt(instr, 0);
1328 FPRegister rhs = InputFPRegisterAt(instr, 1);
1329 if (instr->IsAdd()) {
1330 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001331 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001332 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001333 } else {
1334 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001335 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001336 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001337 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001338 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001339 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001340 }
1341}
1342
Serban Constantinescu02164b32014-11-13 14:05:07 +00001343void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1344 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1345
1346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1347 Primitive::Type type = instr->GetResultType();
1348 switch (type) {
1349 case Primitive::kPrimInt:
1350 case Primitive::kPrimLong: {
1351 locations->SetInAt(0, Location::RequiresRegister());
1352 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1353 locations->SetOut(Location::RequiresRegister());
1354 break;
1355 }
1356 default:
1357 LOG(FATAL) << "Unexpected shift type " << type;
1358 }
1359}
1360
1361void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1362 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1363
1364 Primitive::Type type = instr->GetType();
1365 switch (type) {
1366 case Primitive::kPrimInt:
1367 case Primitive::kPrimLong: {
1368 Register dst = OutputRegister(instr);
1369 Register lhs = InputRegisterAt(instr, 0);
1370 Operand rhs = InputOperandAt(instr, 1);
1371 if (rhs.IsImmediate()) {
1372 uint32_t shift_value = (type == Primitive::kPrimInt)
1373 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1374 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1375 if (instr->IsShl()) {
1376 __ Lsl(dst, lhs, shift_value);
1377 } else if (instr->IsShr()) {
1378 __ Asr(dst, lhs, shift_value);
1379 } else {
1380 __ Lsr(dst, lhs, shift_value);
1381 }
1382 } else {
1383 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1384
1385 if (instr->IsShl()) {
1386 __ Lsl(dst, lhs, rhs_reg);
1387 } else if (instr->IsShr()) {
1388 __ Asr(dst, lhs, rhs_reg);
1389 } else {
1390 __ Lsr(dst, lhs, rhs_reg);
1391 }
1392 }
1393 break;
1394 }
1395 default:
1396 LOG(FATAL) << "Unexpected shift operation type " << type;
1397 }
1398}
1399
Alexandre Rames5319def2014-10-23 10:03:10 +01001400void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001401 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001402}
1403
1404void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001405 HandleBinaryOp(instruction);
1406}
1407
1408void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1409 HandleBinaryOp(instruction);
1410}
1411
1412void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1413 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001414}
1415
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001416void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1417 LocationSummary* locations =
1418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1419 locations->SetInAt(0, Location::RequiresRegister());
1420 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001421 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1422 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1423 } else {
1424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1425 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001426}
1427
1428void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1429 LocationSummary* locations = instruction->GetLocations();
1430 Primitive::Type type = instruction->GetType();
1431 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432 Location index = locations->InAt(1);
1433 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001434 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001435 MacroAssembler* masm = GetVIXLAssembler();
1436 UseScratchRegisterScope temps(masm);
1437 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001438
1439 if (index.IsConstant()) {
1440 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001441 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001442 } else {
1443 Register temp = temps.AcquireSameSizeAs(obj);
1444 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
1445 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(type)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001446 source = HeapOperand(temp, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001447 }
1448
Alexandre Rames67555f72014-11-18 10:55:16 +00001449 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001450 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001451}
1452
Alexandre Rames5319def2014-10-23 10:03:10 +01001453void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1455 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001457}
1458
1459void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001460 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001461 __ Ldr(OutputRegister(instruction),
1462 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001463 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001464}
1465
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001466void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001467 if (instruction->NeedsTypeCheck()) {
1468 LocationSummary* locations =
1469 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001470 InvokeRuntimeCallingConvention calling_convention;
1471 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1472 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1473 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1474 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001475 LocationSummary* locations =
1476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001479 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1480 locations->SetInAt(2, Location::RequiresFpuRegister());
1481 } else {
1482 locations->SetInAt(2, Location::RequiresRegister());
1483 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001484 }
1485}
1486
1487void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1488 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001489 LocationSummary* locations = instruction->GetLocations();
1490 bool needs_runtime_call = locations->WillCall();
1491
1492 if (needs_runtime_call) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001493 codegen_->InvokeRuntime(
1494 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001495 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001496 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001497 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 CPURegister value = InputCPURegisterAt(instruction, 2);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001499 Location index = locations->InAt(1);
1500 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001501 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001502 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001503 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001504 {
1505 // We use a block to end the scratch scope before the write barrier, thus
1506 // freeing the temporary registers so they can be used in `MarkGCCard`.
1507 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001508
Alexandre Rames97833a02015-04-16 15:07:12 +01001509 if (index.IsConstant()) {
1510 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1511 destination = HeapOperand(obj, offset);
1512 } else {
1513 Register temp = temps.AcquireSameSizeAs(obj);
1514 Register index_reg = InputRegisterAt(instruction, 1);
1515 __ Add(temp, obj, Operand(index_reg, LSL, Primitive::ComponentSizeShift(value_type)));
1516 destination = HeapOperand(temp, offset);
1517 }
1518
1519 codegen_->Store(value_type, value, destination);
1520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001521 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001522 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001523 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001524 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 }
1526}
1527
Alexandre Rames67555f72014-11-18 10:55:16 +00001528void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1529 LocationSummary* locations =
1530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1531 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001532 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001533 if (instruction->HasUses()) {
1534 locations->SetOut(Location::SameAsFirstInput());
1535 }
1536}
1537
1538void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 LocationSummary* locations = instruction->GetLocations();
1540 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1541 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001542 codegen_->AddSlowPath(slow_path);
1543
1544 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1545 __ B(slow_path->GetEntryLabel(), hs);
1546}
1547
1548void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1550 instruction, LocationSummary::kCallOnSlowPath);
1551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001553 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001554}
1555
1556void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001557 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001558 Register obj = InputRegisterAt(instruction, 0);;
1559 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001560 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001561
Alexandre Rames3e69f162014-12-10 10:36:50 +00001562 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1563 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001564 codegen_->AddSlowPath(slow_path);
1565
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001566 // Avoid null check if we know obj is not null.
1567 if (instruction->MustDoNullCheck()) {
1568 __ Cbz(obj, slow_path->GetExitLabel());
1569 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001571 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
1572 __ Cmp(obj_cls, cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00001573 __ B(ne, slow_path->GetEntryLabel());
1574 __ Bind(slow_path->GetExitLabel());
1575}
1576
1577void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1578 LocationSummary* locations =
1579 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1580 locations->SetInAt(0, Location::RequiresRegister());
1581 if (check->HasUses()) {
1582 locations->SetOut(Location::SameAsFirstInput());
1583 }
1584}
1585
1586void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1587 // We assume the class is not null.
1588 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1589 check->GetLoadClass(), check, check->GetDexPc(), true);
1590 codegen_->AddSlowPath(slow_path);
1591 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1592}
1593
Serban Constantinescu02164b32014-11-13 14:05:07 +00001594void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001595 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001596 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1597 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001598 switch (in_type) {
1599 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001600 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001601 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1603 break;
1604 }
1605 case Primitive::kPrimFloat:
1606 case Primitive::kPrimDouble: {
1607 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001608 HInstruction* right = compare->InputAt(1);
1609 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1610 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1611 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1612 } else {
1613 locations->SetInAt(1, Location::RequiresFpuRegister());
1614 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001615 locations->SetOut(Location::RequiresRegister());
1616 break;
1617 }
1618 default:
1619 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1620 }
1621}
1622
1623void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1624 Primitive::Type in_type = compare->InputAt(0)->GetType();
1625
1626 // 0 if: left == right
1627 // 1 if: left > right
1628 // -1 if: left < right
1629 switch (in_type) {
1630 case Primitive::kPrimLong: {
1631 Register result = OutputRegister(compare);
1632 Register left = InputRegisterAt(compare, 0);
1633 Operand right = InputOperandAt(compare, 1);
1634
1635 __ Cmp(left, right);
1636 __ Cset(result, ne);
1637 __ Cneg(result, result, lt);
1638 break;
1639 }
1640 case Primitive::kPrimFloat:
1641 case Primitive::kPrimDouble: {
1642 Register result = OutputRegister(compare);
1643 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001644 if (compare->GetLocations()->InAt(1).IsConstant()) {
1645 if (kIsDebugBuild) {
1646 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1647 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1648 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1649 }
1650 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1651 __ Fcmp(left, 0.0);
1652 } else {
1653 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1654 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001655 if (compare->IsGtBias()) {
1656 __ Cset(result, ne);
1657 } else {
1658 __ Csetm(result, ne);
1659 }
1660 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001661 break;
1662 }
1663 default:
1664 LOG(FATAL) << "Unimplemented compare type " << in_type;
1665 }
1666}
1667
1668void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1670 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001671 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001672 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001674 }
1675}
1676
1677void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1678 if (!instruction->NeedsMaterialization()) {
1679 return;
1680 }
1681
1682 LocationSummary* locations = instruction->GetLocations();
1683 Register lhs = InputRegisterAt(instruction, 0);
1684 Operand rhs = InputOperandAt(instruction, 1);
1685 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1686 Condition cond = ARM64Condition(instruction->GetCondition());
1687
1688 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001689 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001690}
1691
1692#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1693 M(Equal) \
1694 M(NotEqual) \
1695 M(LessThan) \
1696 M(LessThanOrEqual) \
1697 M(GreaterThan) \
1698 M(GreaterThanOrEqual)
1699#define DEFINE_CONDITION_VISITORS(Name) \
1700void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1701void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1702FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001703#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001704#undef FOR_EACH_CONDITION_INSTRUCTION
1705
Zheng Xuc6667102015-05-15 16:08:45 +08001706void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1707 DCHECK(instruction->IsDiv() || instruction->IsRem());
1708
1709 LocationSummary* locations = instruction->GetLocations();
1710 Location second = locations->InAt(1);
1711 DCHECK(second.IsConstant());
1712
1713 Register out = OutputRegister(instruction);
1714 Register dividend = InputRegisterAt(instruction, 0);
1715 int64_t imm = Int64FromConstant(second.GetConstant());
1716 DCHECK(imm == 1 || imm == -1);
1717
1718 if (instruction->IsRem()) {
1719 __ Mov(out, 0);
1720 } else {
1721 if (imm == 1) {
1722 __ Mov(out, dividend);
1723 } else {
1724 __ Neg(out, dividend);
1725 }
1726 }
1727}
1728
1729void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1730 DCHECK(instruction->IsDiv() || instruction->IsRem());
1731
1732 LocationSummary* locations = instruction->GetLocations();
1733 Location second = locations->InAt(1);
1734 DCHECK(second.IsConstant());
1735
1736 Register out = OutputRegister(instruction);
1737 Register dividend = InputRegisterAt(instruction, 0);
1738 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001739 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001740 DCHECK(IsPowerOfTwo(abs_imm));
1741 int ctz_imm = CTZ(abs_imm);
1742
1743 UseScratchRegisterScope temps(GetVIXLAssembler());
1744 Register temp = temps.AcquireSameSizeAs(out);
1745
1746 if (instruction->IsDiv()) {
1747 __ Add(temp, dividend, abs_imm - 1);
1748 __ Cmp(dividend, 0);
1749 __ Csel(out, temp, dividend, lt);
1750 if (imm > 0) {
1751 __ Asr(out, out, ctz_imm);
1752 } else {
1753 __ Neg(out, Operand(out, ASR, ctz_imm));
1754 }
1755 } else {
1756 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1757 __ Asr(temp, dividend, bits - 1);
1758 __ Lsr(temp, temp, bits - ctz_imm);
1759 __ Add(out, dividend, temp);
1760 __ And(out, out, abs_imm - 1);
1761 __ Sub(out, out, temp);
1762 }
1763}
1764
1765void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1766 DCHECK(instruction->IsDiv() || instruction->IsRem());
1767
1768 LocationSummary* locations = instruction->GetLocations();
1769 Location second = locations->InAt(1);
1770 DCHECK(second.IsConstant());
1771
1772 Register out = OutputRegister(instruction);
1773 Register dividend = InputRegisterAt(instruction, 0);
1774 int64_t imm = Int64FromConstant(second.GetConstant());
1775
1776 Primitive::Type type = instruction->GetResultType();
1777 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1778
1779 int64_t magic;
1780 int shift;
1781 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1782
1783 UseScratchRegisterScope temps(GetVIXLAssembler());
1784 Register temp = temps.AcquireSameSizeAs(out);
1785
1786 // temp = get_high(dividend * magic)
1787 __ Mov(temp, magic);
1788 if (type == Primitive::kPrimLong) {
1789 __ Smulh(temp, dividend, temp);
1790 } else {
1791 __ Smull(temp.X(), dividend, temp);
1792 __ Lsr(temp.X(), temp.X(), 32);
1793 }
1794
1795 if (imm > 0 && magic < 0) {
1796 __ Add(temp, temp, dividend);
1797 } else if (imm < 0 && magic > 0) {
1798 __ Sub(temp, temp, dividend);
1799 }
1800
1801 if (shift != 0) {
1802 __ Asr(temp, temp, shift);
1803 }
1804
1805 if (instruction->IsDiv()) {
1806 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1807 } else {
1808 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1809 // TODO: Strength reduction for msub.
1810 Register temp_imm = temps.AcquireSameSizeAs(out);
1811 __ Mov(temp_imm, imm);
1812 __ Msub(out, temp, temp_imm, dividend);
1813 }
1814}
1815
1816void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1817 DCHECK(instruction->IsDiv() || instruction->IsRem());
1818 Primitive::Type type = instruction->GetResultType();
1819 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1820
1821 LocationSummary* locations = instruction->GetLocations();
1822 Register out = OutputRegister(instruction);
1823 Location second = locations->InAt(1);
1824
1825 if (second.IsConstant()) {
1826 int64_t imm = Int64FromConstant(second.GetConstant());
1827
1828 if (imm == 0) {
1829 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1830 } else if (imm == 1 || imm == -1) {
1831 DivRemOneOrMinusOne(instruction);
1832 } else if (IsPowerOfTwo(std::abs(imm))) {
1833 DivRemByPowerOfTwo(instruction);
1834 } else {
1835 DCHECK(imm <= -2 || imm >= 2);
1836 GenerateDivRemWithAnyConstant(instruction);
1837 }
1838 } else {
1839 Register dividend = InputRegisterAt(instruction, 0);
1840 Register divisor = InputRegisterAt(instruction, 1);
1841 if (instruction->IsDiv()) {
1842 __ Sdiv(out, dividend, divisor);
1843 } else {
1844 UseScratchRegisterScope temps(GetVIXLAssembler());
1845 Register temp = temps.AcquireSameSizeAs(out);
1846 __ Sdiv(temp, dividend, divisor);
1847 __ Msub(out, temp, divisor, dividend);
1848 }
1849 }
1850}
1851
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001852void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1853 LocationSummary* locations =
1854 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1855 switch (div->GetResultType()) {
1856 case Primitive::kPrimInt:
1857 case Primitive::kPrimLong:
1858 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001859 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1861 break;
1862
1863 case Primitive::kPrimFloat:
1864 case Primitive::kPrimDouble:
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetInAt(1, Location::RequiresFpuRegister());
1867 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1868 break;
1869
1870 default:
1871 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1872 }
1873}
1874
1875void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1876 Primitive::Type type = div->GetResultType();
1877 switch (type) {
1878 case Primitive::kPrimInt:
1879 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001880 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001881 break;
1882
1883 case Primitive::kPrimFloat:
1884 case Primitive::kPrimDouble:
1885 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1886 break;
1887
1888 default:
1889 LOG(FATAL) << "Unexpected div type " << type;
1890 }
1891}
1892
Alexandre Rames67555f72014-11-18 10:55:16 +00001893void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1894 LocationSummary* locations =
1895 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1896 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1897 if (instruction->HasUses()) {
1898 locations->SetOut(Location::SameAsFirstInput());
1899 }
1900}
1901
1902void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1903 SlowPathCodeARM64* slow_path =
1904 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1905 codegen_->AddSlowPath(slow_path);
1906 Location value = instruction->GetLocations()->InAt(0);
1907
Alexandre Rames3e69f162014-12-10 10:36:50 +00001908 Primitive::Type type = instruction->GetType();
1909
1910 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1911 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1912 return;
1913 }
1914
Alexandre Rames67555f72014-11-18 10:55:16 +00001915 if (value.IsConstant()) {
1916 int64_t divisor = Int64ConstantFrom(value);
1917 if (divisor == 0) {
1918 __ B(slow_path->GetEntryLabel());
1919 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001920 // A division by a non-null constant is valid. We don't need to perform
1921 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001922 }
1923 } else {
1924 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1925 }
1926}
1927
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001928void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1929 LocationSummary* locations =
1930 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1931 locations->SetOut(Location::ConstantLocation(constant));
1932}
1933
1934void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1935 UNUSED(constant);
1936 // Will be generated at use site.
1937}
1938
Alexandre Rames5319def2014-10-23 10:03:10 +01001939void LocationsBuilderARM64::VisitExit(HExit* exit) {
1940 exit->SetLocations(nullptr);
1941}
1942
1943void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001944 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01001945}
1946
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001947void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
1948 LocationSummary* locations =
1949 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1950 locations->SetOut(Location::ConstantLocation(constant));
1951}
1952
1953void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
1954 UNUSED(constant);
1955 // Will be generated at use site.
1956}
1957
Alexandre Rames5319def2014-10-23 10:03:10 +01001958void LocationsBuilderARM64::VisitGoto(HGoto* got) {
1959 got->SetLocations(nullptr);
1960}
1961
1962void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
1963 HBasicBlock* successor = got->GetSuccessor();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001964 DCHECK(!successor->IsExitBlock());
1965 HBasicBlock* block = got->GetBlock();
1966 HInstruction* previous = got->GetPrevious();
1967 HLoopInformation* info = block->GetLoopInformation();
1968
David Brazdil46e2a392015-03-16 17:31:52 +00001969 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001970 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1971 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1972 return;
1973 }
1974 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1975 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1976 }
1977 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001978 __ B(codegen_->GetLabelOf(successor));
1979 }
1980}
1981
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001982void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
1983 vixl::Label* true_target,
1984 vixl::Label* false_target,
1985 vixl::Label* always_true_target) {
1986 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01001987 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01001988
Serban Constantinescu02164b32014-11-13 14:05:07 +00001989 if (cond->IsIntConstant()) {
1990 int32_t cond_value = cond->AsIntConstant()->GetValue();
1991 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001992 if (always_true_target != nullptr) {
1993 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001994 }
1995 return;
1996 } else {
1997 DCHECK_EQ(cond_value, 0);
1998 }
1999 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002000 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002001 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002002 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002003 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002004 } else {
2005 // The condition instruction has not been materialized, use its inputs as
2006 // the comparison and its condition as the branch condition.
2007 Register lhs = InputRegisterAt(condition, 0);
2008 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002009 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00002010 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2011 switch (arm64_cond) {
2012 case eq:
2013 __ Cbz(lhs, true_target);
2014 break;
2015 case ne:
2016 __ Cbnz(lhs, true_target);
2017 break;
2018 case lt:
2019 // Test the sign bit and branch accordingly.
2020 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2021 break;
2022 case ge:
2023 // Test the sign bit and branch accordingly.
2024 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2025 break;
2026 default:
2027 // Without the `static_cast` the compiler throws an error for
2028 // `-Werror=sign-promo`.
2029 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002030 }
2031 } else {
2032 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002033 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002034 }
2035 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002036 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002037 __ B(false_target);
2038 }
2039}
2040
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002041void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2043 HInstruction* cond = if_instr->InputAt(0);
2044 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2045 locations->SetInAt(0, Location::RequiresRegister());
2046 }
2047}
2048
2049void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2050 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2051 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2052 vixl::Label* always_true_target = true_target;
2053 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2054 if_instr->IfTrueSuccessor())) {
2055 always_true_target = nullptr;
2056 }
2057 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2058 if_instr->IfFalseSuccessor())) {
2059 false_target = nullptr;
2060 }
2061 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2062}
2063
2064void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2065 LocationSummary* locations = new (GetGraph()->GetArena())
2066 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2067 HInstruction* cond = deoptimize->InputAt(0);
2068 DCHECK(cond->IsCondition());
2069 if (cond->AsCondition()->NeedsMaterialization()) {
2070 locations->SetInAt(0, Location::RequiresRegister());
2071 }
2072}
2073
2074void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2075 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2076 DeoptimizationSlowPathARM64(deoptimize);
2077 codegen_->AddSlowPath(slow_path);
2078 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2079 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2080}
2081
Alexandre Rames5319def2014-10-23 10:03:10 +01002082void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002083 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002084}
2085
2086void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002087 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002088}
2089
2090void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002091 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002092}
2093
2094void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002095 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002096}
2097
Alexandre Rames67555f72014-11-18 10:55:16 +00002098void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2099 LocationSummary::CallKind call_kind =
2100 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2101 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2102 locations->SetInAt(0, Location::RequiresRegister());
2103 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002104 // The output does overlap inputs.
2105 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002106}
2107
2108void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2109 LocationSummary* locations = instruction->GetLocations();
2110 Register obj = InputRegisterAt(instruction, 0);;
2111 Register cls = InputRegisterAt(instruction, 1);;
2112 Register out = OutputRegister(instruction);
2113
2114 vixl::Label done;
2115
2116 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002117 // Avoid null check if we know `obj` is not null.
2118 if (instruction->MustDoNullCheck()) {
2119 __ Mov(out, 0);
2120 __ Cbz(obj, &done);
2121 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002122
2123 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002124 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002125 __ Cmp(out, cls);
2126 if (instruction->IsClassFinal()) {
2127 // Classes must be equal for the instanceof to succeed.
2128 __ Cset(out, eq);
2129 } else {
2130 // If the classes are not equal, we go into a slow path.
2131 DCHECK(locations->OnlyCallsOnSlowPath());
2132 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002133 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2134 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002135 codegen_->AddSlowPath(slow_path);
2136 __ B(ne, slow_path->GetEntryLabel());
2137 __ Mov(out, 1);
2138 __ Bind(slow_path->GetExitLabel());
2139 }
2140
2141 __ Bind(&done);
2142}
2143
Alexandre Rames5319def2014-10-23 10:03:10 +01002144void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2146 locations->SetOut(Location::ConstantLocation(constant));
2147}
2148
2149void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2150 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002151 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002152}
2153
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002154void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2156 locations->SetOut(Location::ConstantLocation(constant));
2157}
2158
2159void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2160 // Will be generated at use site.
2161 UNUSED(constant);
2162}
2163
Alexandre Rames5319def2014-10-23 10:03:10 +01002164void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002165 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002166 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002167}
2168
Alexandre Rames67555f72014-11-18 10:55:16 +00002169void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2170 HandleInvoke(invoke);
2171}
2172
2173void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2174 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002175 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2176 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2177 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002178 Location receiver = invoke->GetLocations()->InAt(0);
2179 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002180 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002181
2182 // The register ip1 is required to be used for the hidden argument in
2183 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002184 MacroAssembler* masm = GetVIXLAssembler();
2185 UseScratchRegisterScope scratch_scope(masm);
2186 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002187 scratch_scope.Exclude(ip1);
2188 __ Mov(ip1, invoke->GetDexMethodIndex());
2189
2190 // temp = object->GetClass();
2191 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002192 __ Ldr(temp.W(), StackOperandFrom(receiver));
2193 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002194 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002195 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002196 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002197 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00002198 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002199 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002200 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002201 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002202 // lr();
2203 __ Blr(lr);
2204 DCHECK(!codegen_->IsLeafMethod());
2205 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2206}
2207
2208void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002209 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2210 if (intrinsic.TryDispatch(invoke)) {
2211 return;
2212 }
2213
Alexandre Rames67555f72014-11-18 10:55:16 +00002214 HandleInvoke(invoke);
2215}
2216
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002217void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002218 // When we do not run baseline, explicit clinit checks triggered by static
2219 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2220 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002221
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
Andreas Gampe878d58c2015-01-15 23:24:00 -08002230static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2231 if (invoke->GetLocations()->Intrinsified()) {
2232 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2233 intrinsic.Dispatch(invoke);
2234 return true;
2235 }
2236 return false;
2237}
2238
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002239void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002240 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002241 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002242
2243 // TODO: Implement all kinds of calls:
2244 // 1) boot -> boot
2245 // 2) app -> boot
2246 // 3) app -> app
2247 //
2248 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2249
Jeff Hao848f70a2014-01-15 13:49:50 -08002250 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002251 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002252 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002253 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002254 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002255 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002256 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002257 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002258 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002259 } else if (invoke->IsRecursive()) {
2260 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002261 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002262 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002263 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002264 Register method_reg;
2265 if (current_method.IsRegister()) {
2266 method_reg = XRegisterFrom(current_method);
2267 } else {
2268 DCHECK(invoke->GetLocations()->Intrinsified());
2269 DCHECK(!current_method.IsValid());
2270 method_reg = reg;
2271 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2272 }
2273
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002274 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002275 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002276 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2277 // temp = temp[index_in_cache];
2278 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2279 // lr = temp->entry_point_from_quick_compiled_code_;
2280 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2281 kArm64WordSize).Int32Value()));
2282 // lr();
2283 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002284 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002285
Andreas Gampe878d58c2015-01-15 23:24:00 -08002286 DCHECK(!IsLeafMethod());
2287}
2288
2289void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002290 // When we do not run baseline, explicit clinit checks triggered by static
2291 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2292 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002293
Andreas Gampe878d58c2015-01-15 23:24:00 -08002294 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2295 return;
2296 }
2297
Alexandre Ramesd921d642015-04-16 15:07:16 +01002298 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002299 LocationSummary* locations = invoke->GetLocations();
2300 codegen_->GenerateStaticOrDirectCall(
2301 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002302 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002303}
2304
2305void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002306 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2307 return;
2308 }
2309
Alexandre Rames5319def2014-10-23 10:03:10 +01002310 LocationSummary* locations = invoke->GetLocations();
2311 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002312 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2313 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2314 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002315 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002316 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002317
Alexandre Ramesd921d642015-04-16 15:07:16 +01002318 BlockPoolsScope block_pools(GetVIXLAssembler());
2319
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002320 DCHECK(receiver.IsRegister());
2321 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002322 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames5319def2014-10-23 10:03:10 +01002323 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002324 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002325 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002327 // lr();
2328 __ Blr(lr);
2329 DCHECK(!codegen_->IsLeafMethod());
2330 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2331}
2332
Alexandre Rames67555f72014-11-18 10:55:16 +00002333void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2334 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2335 : LocationSummary::kNoCall;
2336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002337 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002338 locations->SetOut(Location::RequiresRegister());
2339}
2340
2341void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2342 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002343 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002344 if (cls->IsReferrersClass()) {
2345 DCHECK(!cls->CanCallRuntime());
2346 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002347 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002348 } else {
2349 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002350 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002351 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002352
2353 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2354 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2355 codegen_->AddSlowPath(slow_path);
2356 __ Cbz(out, slow_path->GetEntryLabel());
2357 if (cls->MustGenerateClinitCheck()) {
2358 GenerateClassInitializationCheck(slow_path, out);
2359 } else {
2360 __ Bind(slow_path->GetExitLabel());
2361 }
2362 }
2363}
2364
2365void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2366 LocationSummary* locations =
2367 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2368 locations->SetOut(Location::RequiresRegister());
2369}
2370
2371void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2372 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2373 __ Ldr(OutputRegister(instruction), exception);
2374 __ Str(wzr, exception);
2375}
2376
Alexandre Rames5319def2014-10-23 10:03:10 +01002377void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2378 load->SetLocations(nullptr);
2379}
2380
2381void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2382 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002383 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002384}
2385
Alexandre Rames67555f72014-11-18 10:55:16 +00002386void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2387 LocationSummary* locations =
2388 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002389 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002390 locations->SetOut(Location::RequiresRegister());
2391}
2392
2393void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2394 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2395 codegen_->AddSlowPath(slow_path);
2396
2397 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002398 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002399 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002400 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002401 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Alexandre Rames67555f72014-11-18 10:55:16 +00002402 __ Cbz(out, slow_path->GetEntryLabel());
2403 __ Bind(slow_path->GetExitLabel());
2404}
2405
Alexandre Rames5319def2014-10-23 10:03:10 +01002406void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2407 local->SetLocations(nullptr);
2408}
2409
2410void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2411 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2412}
2413
2414void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2416 locations->SetOut(Location::ConstantLocation(constant));
2417}
2418
2419void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2420 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002421 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002422}
2423
Alexandre Rames67555f72014-11-18 10:55:16 +00002424void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2425 LocationSummary* locations =
2426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2427 InvokeRuntimeCallingConvention calling_convention;
2428 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2429}
2430
2431void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2432 codegen_->InvokeRuntime(instruction->IsEnter()
2433 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2434 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002435 instruction->GetDexPc(),
2436 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002437 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002438}
2439
Alexandre Rames42d641b2014-10-27 14:00:51 +00002440void LocationsBuilderARM64::VisitMul(HMul* mul) {
2441 LocationSummary* locations =
2442 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2443 switch (mul->GetResultType()) {
2444 case Primitive::kPrimInt:
2445 case Primitive::kPrimLong:
2446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002449 break;
2450
2451 case Primitive::kPrimFloat:
2452 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002453 locations->SetInAt(0, Location::RequiresFpuRegister());
2454 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002455 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002456 break;
2457
2458 default:
2459 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2460 }
2461}
2462
2463void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2464 switch (mul->GetResultType()) {
2465 case Primitive::kPrimInt:
2466 case Primitive::kPrimLong:
2467 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2468 break;
2469
2470 case Primitive::kPrimFloat:
2471 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002472 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002473 break;
2474
2475 default:
2476 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2477 }
2478}
2479
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002480void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2481 LocationSummary* locations =
2482 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2483 switch (neg->GetResultType()) {
2484 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002485 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002486 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002488 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002489
2490 case Primitive::kPrimFloat:
2491 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002492 locations->SetInAt(0, Location::RequiresFpuRegister());
2493 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2498 }
2499}
2500
2501void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2502 switch (neg->GetResultType()) {
2503 case Primitive::kPrimInt:
2504 case Primitive::kPrimLong:
2505 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2506 break;
2507
2508 case Primitive::kPrimFloat:
2509 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002510 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2515 }
2516}
2517
2518void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2519 LocationSummary* locations =
2520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2521 InvokeRuntimeCallingConvention calling_convention;
2522 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002523 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002524 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002525 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002526 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002527 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002528}
2529
2530void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2531 LocationSummary* locations = instruction->GetLocations();
2532 InvokeRuntimeCallingConvention calling_convention;
2533 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2534 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002535 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002536 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002537 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2538 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002539 instruction->GetDexPc(),
2540 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002541 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002542}
2543
Alexandre Rames5319def2014-10-23 10:03:10 +01002544void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2545 LocationSummary* locations =
2546 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2547 InvokeRuntimeCallingConvention calling_convention;
2548 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002549 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002550 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002551 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002552}
2553
2554void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2555 LocationSummary* locations = instruction->GetLocations();
2556 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2557 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002558 __ Mov(type_index, instruction->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +00002559 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002560 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2561 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002562 instruction->GetDexPc(),
2563 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002564 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002565}
2566
2567void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002569 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002571}
2572
2573void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002574 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002575 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002576 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002577 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2582 }
2583}
2584
David Brazdil66d126e2015-04-03 16:02:44 +01002585void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2587 locations->SetInAt(0, Location::RequiresRegister());
2588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2589}
2590
2591void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002592 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2593}
2594
Alexandre Rames5319def2014-10-23 10:03:10 +01002595void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2596 LocationSummary* locations =
2597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 if (instruction->HasUses()) {
2600 locations->SetOut(Location::SameAsFirstInput());
2601 }
2602}
2603
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002604void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002605 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2606 return;
2607 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002608
Alexandre Ramesd921d642015-04-16 15:07:16 +01002609 BlockPoolsScope block_pools(GetVIXLAssembler());
2610 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002611 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2612 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2613}
2614
2615void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002616 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2617 codegen_->AddSlowPath(slow_path);
2618
2619 LocationSummary* locations = instruction->GetLocations();
2620 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002621
2622 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002623}
2624
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002625void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2626 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2627 GenerateImplicitNullCheck(instruction);
2628 } else {
2629 GenerateExplicitNullCheck(instruction);
2630 }
2631}
2632
Alexandre Rames67555f72014-11-18 10:55:16 +00002633void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2634 HandleBinaryOp(instruction);
2635}
2636
2637void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2638 HandleBinaryOp(instruction);
2639}
2640
Alexandre Rames3e69f162014-12-10 10:36:50 +00002641void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2642 LOG(FATAL) << "Unreachable";
2643}
2644
2645void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2646 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2647}
2648
Alexandre Rames5319def2014-10-23 10:03:10 +01002649void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2650 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2651 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2652 if (location.IsStackSlot()) {
2653 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2654 } else if (location.IsDoubleStackSlot()) {
2655 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2656 }
2657 locations->SetOut(location);
2658}
2659
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002660void InstructionCodeGeneratorARM64::VisitParameterValue(
2661 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002662 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002663}
2664
2665void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2666 LocationSummary* locations =
2667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002668 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002669}
2670
2671void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2672 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2673 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002674}
2675
2676void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2677 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2678 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2679 locations->SetInAt(i, Location::Any());
2680 }
2681 locations->SetOut(Location::Any());
2682}
2683
2684void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002685 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002686 LOG(FATAL) << "Unreachable";
2687}
2688
Serban Constantinescu02164b32014-11-13 14:05:07 +00002689void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002690 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002691 LocationSummary::CallKind call_kind =
2692 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2694
2695 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002696 case Primitive::kPrimInt:
2697 case Primitive::kPrimLong:
2698 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002699 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2701 break;
2702
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002703 case Primitive::kPrimFloat:
2704 case Primitive::kPrimDouble: {
2705 InvokeRuntimeCallingConvention calling_convention;
2706 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2707 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2708 locations->SetOut(calling_convention.GetReturnLocation(type));
2709
2710 break;
2711 }
2712
Serban Constantinescu02164b32014-11-13 14:05:07 +00002713 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002714 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002715 }
2716}
2717
2718void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2719 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002720
Serban Constantinescu02164b32014-11-13 14:05:07 +00002721 switch (type) {
2722 case Primitive::kPrimInt:
2723 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002724 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002725 break;
2726 }
2727
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002728 case Primitive::kPrimFloat:
2729 case Primitive::kPrimDouble: {
2730 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2731 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002732 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002733 break;
2734 }
2735
Serban Constantinescu02164b32014-11-13 14:05:07 +00002736 default:
2737 LOG(FATAL) << "Unexpected rem type " << type;
2738 }
2739}
2740
Calin Juravle27df7582015-04-17 19:12:31 +01002741void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2742 memory_barrier->SetLocations(nullptr);
2743}
2744
2745void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2746 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2747}
2748
Alexandre Rames5319def2014-10-23 10:03:10 +01002749void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2750 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2751 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002752 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002753}
2754
2755void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002756 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002757 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002758}
2759
2760void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2761 instruction->SetLocations(nullptr);
2762}
2763
2764void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002765 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002766 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002767}
2768
Serban Constantinescu02164b32014-11-13 14:05:07 +00002769void LocationsBuilderARM64::VisitShl(HShl* shl) {
2770 HandleShift(shl);
2771}
2772
2773void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2774 HandleShift(shl);
2775}
2776
2777void LocationsBuilderARM64::VisitShr(HShr* shr) {
2778 HandleShift(shr);
2779}
2780
2781void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2782 HandleShift(shr);
2783}
2784
Alexandre Rames5319def2014-10-23 10:03:10 +01002785void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2786 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2787 Primitive::Type field_type = store->InputAt(1)->GetType();
2788 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002789 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002790 case Primitive::kPrimBoolean:
2791 case Primitive::kPrimByte:
2792 case Primitive::kPrimChar:
2793 case Primitive::kPrimShort:
2794 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002795 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002796 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2797 break;
2798
2799 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002800 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002801 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2802 break;
2803
2804 default:
2805 LOG(FATAL) << "Unimplemented local type " << field_type;
2806 }
2807}
2808
2809void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002810 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002811}
2812
2813void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002814 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002815}
2816
2817void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002818 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002819}
2820
Alexandre Rames67555f72014-11-18 10:55:16 +00002821void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002822 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002823}
2824
2825void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002826 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002827}
2828
2829void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002830 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002831}
2832
Alexandre Rames67555f72014-11-18 10:55:16 +00002833void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002834 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002835}
2836
2837void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2838 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2839}
2840
2841void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002842 HBasicBlock* block = instruction->GetBlock();
2843 if (block->GetLoopInformation() != nullptr) {
2844 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2845 // The back edge will generate the suspend check.
2846 return;
2847 }
2848 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2849 // The goto will generate the suspend check.
2850 return;
2851 }
2852 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002853}
2854
2855void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2856 temp->SetLocations(nullptr);
2857}
2858
2859void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2860 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002861 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002862}
2863
Alexandre Rames67555f72014-11-18 10:55:16 +00002864void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2865 LocationSummary* locations =
2866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2867 InvokeRuntimeCallingConvention calling_convention;
2868 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2869}
2870
2871void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2872 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002873 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002874 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002875}
2876
2877void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2878 LocationSummary* locations =
2879 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2880 Primitive::Type input_type = conversion->GetInputType();
2881 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002882 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002883 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2884 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2885 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2886 }
2887
Alexandre Rames542361f2015-01-29 16:57:31 +00002888 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002889 locations->SetInAt(0, Location::RequiresFpuRegister());
2890 } else {
2891 locations->SetInAt(0, Location::RequiresRegister());
2892 }
2893
Alexandre Rames542361f2015-01-29 16:57:31 +00002894 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002895 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2896 } else {
2897 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2898 }
2899}
2900
2901void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2902 Primitive::Type result_type = conversion->GetResultType();
2903 Primitive::Type input_type = conversion->GetInputType();
2904
2905 DCHECK_NE(input_type, result_type);
2906
Alexandre Rames542361f2015-01-29 16:57:31 +00002907 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002908 int result_size = Primitive::ComponentSize(result_type);
2909 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002910 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002911 Register output = OutputRegister(conversion);
2912 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002913 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
2914 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
2915 } else if ((result_type == Primitive::kPrimChar) ||
2916 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
2917 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002918 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002919 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00002920 }
Alexandre Rames542361f2015-01-29 16:57:31 +00002921 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002922 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002923 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002924 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
2925 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00002926 } else if (Primitive::IsFloatingPointType(result_type) &&
2927 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002928 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
2929 } else {
2930 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
2931 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00002932 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002933}
Alexandre Rames67555f72014-11-18 10:55:16 +00002934
Serban Constantinescu02164b32014-11-13 14:05:07 +00002935void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
2936 HandleShift(ushr);
2937}
2938
2939void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
2940 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00002941}
2942
2943void LocationsBuilderARM64::VisitXor(HXor* instruction) {
2944 HandleBinaryOp(instruction);
2945}
2946
2947void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
2948 HandleBinaryOp(instruction);
2949}
2950
Calin Juravleb1498f62015-02-16 13:13:29 +00002951void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
2952 // Nothing to do, this should be removed during prepare for register allocator.
2953 UNUSED(instruction);
2954 LOG(FATAL) << "Unreachable";
2955}
2956
2957void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
2958 // Nothing to do, this should be removed during prepare for register allocator.
2959 UNUSED(instruction);
2960 LOG(FATAL) << "Unreachable";
2961}
2962
Alexandre Rames67555f72014-11-18 10:55:16 +00002963#undef __
2964#undef QUICK_ENTRY_POINT
2965
Alexandre Rames5319def2014-10-23 10:03:10 +01002966} // namespace arm64
2967} // namespace art