blob: 069c9e1dea58a6dff2a9083d6ba87c86e13a848a [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
Alexandre Rames9931f312015-06-19 14:47:01 +0100216 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
217
Alexandre Rames5319def2014-10-23 10:03:10 +0100218 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000219 HBoundsCheck* const instruction_;
220 const Location index_location_;
221 const Location length_location_;
222
Alexandre Rames5319def2014-10-23 10:03:10 +0100223 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
224};
225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
227 public:
228 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
232 __ Bind(GetEntryLabel());
233 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000234 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800235 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 }
237
Alexandre Rames9931f312015-06-19 14:47:01 +0100238 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
239
Alexandre Rames67555f72014-11-18 10:55:16 +0000240 private:
241 HDivZeroCheck* const instruction_;
242 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
243};
244
245class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
246 public:
247 LoadClassSlowPathARM64(HLoadClass* cls,
248 HInstruction* at,
249 uint32_t dex_pc,
250 bool do_clinit)
251 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
252 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
253 }
254
255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
256 LocationSummary* locations = at_->GetLocations();
257 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
258
259 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000261
262 InvokeRuntimeCallingConvention calling_convention;
263 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000264 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
265 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000266 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100268 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100270 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000272
273 // Move the class to the desired location.
274 Location out = locations->Out();
275 if (out.IsValid()) {
276 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
277 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000278 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000279 }
280
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000281 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000282 __ B(GetExitLabel());
283 }
284
Alexandre Rames9931f312015-06-19 14:47:01 +0100285 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
286
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 private:
288 // The class this slow path will load.
289 HLoadClass* const cls_;
290
291 // The instruction where this slow path is happening.
292 // (Might be the load class or an initialization check).
293 HInstruction* const at_;
294
295 // The dex PC of `at_`.
296 const uint32_t dex_pc_;
297
298 // Whether to initialize the class.
299 const bool do_clinit_;
300
301 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
302};
303
304class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
305 public:
306 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
307
308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
309 LocationSummary* locations = instruction_->GetLocations();
310 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
311 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
312
313 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000314 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000315
316 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800317 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000318 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000319 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100320 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000322 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000324 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000325 __ B(GetExitLabel());
326 }
327
Alexandre Rames9931f312015-06-19 14:47:01 +0100328 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
329
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 private:
331 HLoadString* const instruction_;
332
333 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
334};
335
Alexandre Rames5319def2014-10-23 10:03:10 +0100336class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
337 public:
338 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
339
Alexandre Rames67555f72014-11-18 10:55:16 +0000340 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
341 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100342 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000344 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100346 }
347
Alexandre Rames9931f312015-06-19 14:47:01 +0100348 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
349
Alexandre Rames5319def2014-10-23 10:03:10 +0100350 private:
351 HNullCheck* const instruction_;
352
353 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
354};
355
356class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
357 public:
358 explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction,
359 HBasicBlock* successor)
360 : instruction_(instruction), successor_(successor) {}
361
Alexandre Rames67555f72014-11-18 10:55:16 +0000362 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
363 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100364 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000365 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000367 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800368 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000369 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000370 if (successor_ == nullptr) {
371 __ B(GetReturnLabel());
372 } else {
373 __ B(arm64_codegen->GetLabelOf(successor_));
374 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 }
376
377 vixl::Label* GetReturnLabel() {
378 DCHECK(successor_ == nullptr);
379 return &return_label_;
380 }
381
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100382 HBasicBlock* GetSuccessor() const {
383 return successor_;
384 }
385
Alexandre Rames9931f312015-06-19 14:47:01 +0100386 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
387
Alexandre Rames5319def2014-10-23 10:03:10 +0100388 private:
389 HSuspendCheck* const instruction_;
390 // If not null, the block to branch to after the suspend check.
391 HBasicBlock* const successor_;
392
393 // If `successor_` is null, the label to branch to after the suspend check.
394 vixl::Label return_label_;
395
396 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
397};
398
Alexandre Rames67555f72014-11-18 10:55:16 +0000399class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
400 public:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000401 TypeCheckSlowPathARM64(HInstruction* instruction,
402 Location class_to_check,
403 Location object_class,
404 uint32_t dex_pc)
405 : instruction_(instruction),
406 class_to_check_(class_to_check),
407 object_class_(object_class),
408 dex_pc_(dex_pc) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000409
410 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000411 LocationSummary* locations = instruction_->GetLocations();
412 DCHECK(instruction_->IsCheckCast()
413 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
414 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
415
Alexandre Rames67555f72014-11-18 10:55:16 +0000416 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000417 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000418
419 // We're moving two locations to locations that could overlap, so we need a parallel
420 // move resolver.
421 InvokeRuntimeCallingConvention calling_convention;
422 codegen->EmitParallelMoves(
Nicolas Geoffray90218252015-04-15 11:56:51 +0100423 class_to_check_, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
424 object_class_, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000425
426 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000427 arm64_codegen->InvokeRuntime(
428 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc_, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000429 Primitive::Type ret_type = instruction_->GetType();
430 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
431 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800432 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
433 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 } else {
435 DCHECK(instruction_->IsCheckCast());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000436 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800437 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438 }
439
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000440 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000441 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000442 }
443
Alexandre Rames9931f312015-06-19 14:47:01 +0100444 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
445
Alexandre Rames67555f72014-11-18 10:55:16 +0000446 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447 HInstruction* const instruction_;
448 const Location class_to_check_;
449 const Location object_class_;
450 uint32_t dex_pc_;
451
Alexandre Rames67555f72014-11-18 10:55:16 +0000452 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
453};
454
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700455class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
456 public:
457 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
458 : instruction_(instruction) {}
459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
461 __ Bind(GetEntryLabel());
462 SaveLiveRegisters(codegen, instruction_->GetLocations());
463 DCHECK(instruction_->IsDeoptimize());
464 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
465 uint32_t dex_pc = deoptimize->GetDexPc();
466 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
467 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
468 }
469
Alexandre Rames9931f312015-06-19 14:47:01 +0100470 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
471
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700472 private:
473 HInstruction* const instruction_;
474 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
475};
476
Alexandre Rames5319def2014-10-23 10:03:10 +0100477#undef __
478
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100479Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100480 Location next_location;
481 if (type == Primitive::kPrimVoid) {
482 LOG(FATAL) << "Unreachable type " << type;
483 }
484
Alexandre Rames542361f2015-01-29 16:57:31 +0000485 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100486 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
487 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000488 } else if (!Primitive::IsFloatingPointType(type) &&
489 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000490 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
491 } else {
492 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000493 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
494 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100495 }
496
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000497 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000498 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100499 return next_location;
500}
501
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100502Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100503 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100504}
505
Serban Constantinescu579885a2015-02-22 20:51:33 +0000506CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
507 const Arm64InstructionSetFeatures& isa_features,
508 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100509 : CodeGenerator(graph,
510 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000511 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000512 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000513 callee_saved_core_registers.list(),
514 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000515 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100516 block_labels_(nullptr),
517 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000518 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000519 move_resolver_(graph->GetArena(), this),
520 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000521 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000522 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000523}
Alexandre Rames5319def2014-10-23 10:03:10 +0100524
Alexandre Rames67555f72014-11-18 10:55:16 +0000525#undef __
526#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100527
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000528void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
529 // Ensure we emit the literal pool.
530 __ FinalizeCode();
531 CodeGenerator::Finalize(allocator);
532}
533
Zheng Xuad4450e2015-04-17 18:48:56 +0800534void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
535 // Note: There are 6 kinds of moves:
536 // 1. constant -> GPR/FPR (non-cycle)
537 // 2. constant -> stack (non-cycle)
538 // 3. GPR/FPR -> GPR/FPR
539 // 4. GPR/FPR -> stack
540 // 5. stack -> GPR/FPR
541 // 6. stack -> stack (non-cycle)
542 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
543 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
544 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
545 // dependency.
546 vixl_temps_.Open(GetVIXLAssembler());
547}
548
549void ParallelMoveResolverARM64::FinishEmitNativeCode() {
550 vixl_temps_.Close();
551}
552
553Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
554 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
555 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
556 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
557 Location scratch = GetScratchLocation(kind);
558 if (!scratch.Equals(Location::NoLocation())) {
559 return scratch;
560 }
561 // Allocate from VIXL temp registers.
562 if (kind == Location::kRegister) {
563 scratch = LocationFrom(vixl_temps_.AcquireX());
564 } else {
565 DCHECK(kind == Location::kFpuRegister);
566 scratch = LocationFrom(vixl_temps_.AcquireD());
567 }
568 AddScratchLocation(scratch);
569 return scratch;
570}
571
572void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
573 if (loc.IsRegister()) {
574 vixl_temps_.Release(XRegisterFrom(loc));
575 } else {
576 DCHECK(loc.IsFpuRegister());
577 vixl_temps_.Release(DRegisterFrom(loc));
578 }
579 RemoveScratchLocation(loc);
580}
581
Alexandre Rames3e69f162014-12-10 10:36:50 +0000582void ParallelMoveResolverARM64::EmitMove(size_t index) {
583 MoveOperands* move = moves_.Get(index);
584 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
585}
586
Alexandre Rames5319def2014-10-23 10:03:10 +0100587void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100588 MacroAssembler* masm = GetVIXLAssembler();
589 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000590 __ Bind(&frame_entry_label_);
591
Serban Constantinescu02164b32014-11-13 14:05:07 +0000592 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
593 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100594 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000595 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000596 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000597 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000598 __ Ldr(wzr, MemOperand(temp, 0));
599 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000600 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100601
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000602 if (!HasEmptyFrame()) {
603 int frame_size = GetFrameSize();
604 // Stack layout:
605 // sp[frame_size - 8] : lr.
606 // ... : other preserved core registers.
607 // ... : other preserved fp registers.
608 // ... : reserved frame space.
609 // sp[0] : current method.
610 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100611 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800612 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
613 frame_size - GetCoreSpillSize());
614 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
615 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000616 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100617}
618
619void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100620 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100621 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000622 if (!HasEmptyFrame()) {
623 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800624 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
625 frame_size - FrameEntrySpillSize());
626 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
627 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000628 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100629 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000630 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100631 __ Ret();
632 GetAssembler()->cfi().RestoreState();
633 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100634}
635
Zheng Xuda403092015-04-24 17:35:39 +0800636vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
637 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
638 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
639 core_spill_mask_);
640}
641
642vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
643 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
644 GetNumberOfFloatingPointRegisters()));
645 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
646 fpu_spill_mask_);
647}
648
Alexandre Rames5319def2014-10-23 10:03:10 +0100649void CodeGeneratorARM64::Bind(HBasicBlock* block) {
650 __ Bind(GetLabelOf(block));
651}
652
Alexandre Rames5319def2014-10-23 10:03:10 +0100653void CodeGeneratorARM64::Move(HInstruction* instruction,
654 Location location,
655 HInstruction* move_for) {
656 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100657 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000658 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100659
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100660 if (instruction->IsFakeString()) {
661 // The fake string is an alias for null.
662 DCHECK(IsBaseline());
663 instruction = locations->Out().GetConstant();
664 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
665 }
666
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100667 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700668 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100669 } else if (locations != nullptr && locations->Out().Equals(location)) {
670 return;
671 } else if (instruction->IsIntConstant()
672 || instruction->IsLongConstant()
673 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000674 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100675 if (location.IsRegister()) {
676 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000677 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100678 (instruction->IsLongConstant() && dst.Is64Bits()));
679 __ Mov(dst, value);
680 } else {
681 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000682 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000683 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
684 ? temps.AcquireW()
685 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100686 __ Mov(temp, value);
687 __ Str(temp, StackOperandFrom(location));
688 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000689 } else if (instruction->IsTemporary()) {
690 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000691 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100692 } else if (instruction->IsLoadLocal()) {
693 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000694 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000695 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000696 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000697 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100698 }
699
700 } else {
701 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000702 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100703 }
704}
705
Alexandre Rames5319def2014-10-23 10:03:10 +0100706Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
707 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000708
Alexandre Rames5319def2014-10-23 10:03:10 +0100709 switch (type) {
710 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000711 case Primitive::kPrimInt:
712 case Primitive::kPrimFloat:
713 return Location::StackSlot(GetStackSlot(load->GetLocal()));
714
715 case Primitive::kPrimLong:
716 case Primitive::kPrimDouble:
717 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
718
Alexandre Rames5319def2014-10-23 10:03:10 +0100719 case Primitive::kPrimBoolean:
720 case Primitive::kPrimByte:
721 case Primitive::kPrimChar:
722 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100723 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100724 LOG(FATAL) << "Unexpected type " << type;
725 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000726
Alexandre Rames5319def2014-10-23 10:03:10 +0100727 LOG(FATAL) << "Unreachable";
728 return Location::NoLocation();
729}
730
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100731void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000732 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100733 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000734 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100735 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100736 if (value_can_be_null) {
737 __ Cbz(value, &done);
738 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100739 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
740 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000741 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100742 if (value_can_be_null) {
743 __ Bind(&done);
744 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100745}
746
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000747void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
748 // Blocked core registers:
749 // lr : Runtime reserved.
750 // tr : Runtime reserved.
751 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
752 // ip1 : VIXL core temp.
753 // ip0 : VIXL core temp.
754 //
755 // Blocked fp registers:
756 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100757 CPURegList reserved_core_registers = vixl_reserved_core_registers;
758 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 while (!reserved_core_registers.IsEmpty()) {
760 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
761 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000762
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000763 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800764 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000765 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
766 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000767
768 if (is_baseline) {
769 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
770 while (!reserved_core_baseline_registers.IsEmpty()) {
771 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
772 }
773
774 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
775 while (!reserved_fp_baseline_registers.IsEmpty()) {
776 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
777 }
778 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100779}
780
781Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
782 if (type == Primitive::kPrimVoid) {
783 LOG(FATAL) << "Unreachable type " << type;
784 }
785
Alexandre Rames542361f2015-01-29 16:57:31 +0000786 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000787 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
788 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100789 return Location::FpuRegisterLocation(reg);
790 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000791 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
792 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100793 return Location::RegisterLocation(reg);
794 }
795}
796
Alexandre Rames3e69f162014-12-10 10:36:50 +0000797size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
798 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
799 __ Str(reg, MemOperand(sp, stack_index));
800 return kArm64WordSize;
801}
802
803size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
804 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
805 __ Ldr(reg, MemOperand(sp, stack_index));
806 return kArm64WordSize;
807}
808
809size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
810 FPRegister reg = FPRegister(reg_id, kDRegSize);
811 __ Str(reg, MemOperand(sp, stack_index));
812 return kArm64WordSize;
813}
814
815size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
816 FPRegister reg = FPRegister(reg_id, kDRegSize);
817 __ Ldr(reg, MemOperand(sp, stack_index));
818 return kArm64WordSize;
819}
820
Alexandre Rames5319def2014-10-23 10:03:10 +0100821void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100822 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100823}
824
825void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100826 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100827}
828
Alexandre Rames67555f72014-11-18 10:55:16 +0000829void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000830 if (constant->IsIntConstant()) {
831 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
832 } else if (constant->IsLongConstant()) {
833 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
834 } else if (constant->IsNullConstant()) {
835 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000836 } else if (constant->IsFloatConstant()) {
837 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
838 } else {
839 DCHECK(constant->IsDoubleConstant());
840 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
841 }
842}
843
Alexandre Rames3e69f162014-12-10 10:36:50 +0000844
845static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
846 DCHECK(constant.IsConstant());
847 HConstant* cst = constant.GetConstant();
848 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000849 // Null is mapped to a core W register, which we associate with kPrimInt.
850 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000851 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
852 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
853 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
854}
855
856void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000857 if (source.Equals(destination)) {
858 return;
859 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000860
861 // A valid move can always be inferred from the destination and source
862 // locations. When moving from and to a register, the argument type can be
863 // used to generate 32bit instead of 64bit moves. In debug mode we also
864 // checks the coherency of the locations and the type.
865 bool unspecified_type = (type == Primitive::kPrimVoid);
866
867 if (destination.IsRegister() || destination.IsFpuRegister()) {
868 if (unspecified_type) {
869 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
870 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000871 (src_cst != nullptr && (src_cst->IsIntConstant()
872 || src_cst->IsFloatConstant()
873 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000874 // For stack slots and 32bit constants, a 64bit type is appropriate.
875 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000876 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000877 // If the source is a double stack slot or a 64bit constant, a 64bit
878 // type is appropriate. Else the source is a register, and since the
879 // type has not been specified, we chose a 64bit type to force a 64bit
880 // move.
881 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000882 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000883 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000884 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
885 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000886 CPURegister dst = CPURegisterFrom(destination, type);
887 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
888 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
889 __ Ldr(dst, StackOperandFrom(source));
890 } else if (source.IsConstant()) {
891 DCHECK(CoherentConstantAndType(source, type));
892 MoveConstant(dst, source.GetConstant());
893 } else {
894 if (destination.IsRegister()) {
895 __ Mov(Register(dst), RegisterFrom(source, type));
896 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800897 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000898 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
899 }
900 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000901 } else { // The destination is not a register. It must be a stack slot.
902 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
903 if (source.IsRegister() || source.IsFpuRegister()) {
904 if (unspecified_type) {
905 if (source.IsRegister()) {
906 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
907 } else {
908 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
909 }
910 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000911 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
912 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000913 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
914 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100915 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000916 UseScratchRegisterScope temps(GetVIXLAssembler());
917 HConstant* src_cst = source.GetConstant();
918 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000919 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000920 temp = temps.AcquireW();
921 } else if (src_cst->IsLongConstant()) {
922 temp = temps.AcquireX();
923 } else if (src_cst->IsFloatConstant()) {
924 temp = temps.AcquireS();
925 } else {
926 DCHECK(src_cst->IsDoubleConstant());
927 temp = temps.AcquireD();
928 }
929 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000930 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000931 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000932 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000933 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000934 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000935 // There is generally less pressure on FP registers.
936 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000937 __ Ldr(temp, StackOperandFrom(source));
938 __ Str(temp, StackOperandFrom(destination));
939 }
940 }
941}
942
943void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000944 CPURegister dst,
945 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000946 switch (type) {
947 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000948 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000949 break;
950 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000951 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000952 break;
953 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000954 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000955 break;
956 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000957 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000958 break;
959 case Primitive::kPrimInt:
960 case Primitive::kPrimNot:
961 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000962 case Primitive::kPrimFloat:
963 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000964 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000965 __ Ldr(dst, src);
966 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000967 case Primitive::kPrimVoid:
968 LOG(FATAL) << "Unreachable type " << type;
969 }
970}
971
Calin Juravle77520bc2015-01-12 18:45:46 +0000972void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000973 CPURegister dst,
974 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100975 MacroAssembler* masm = GetVIXLAssembler();
976 BlockPoolsScope block_pools(masm);
977 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000978 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000979 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000980
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000981 DCHECK(!src.IsPreIndex());
982 DCHECK(!src.IsPostIndex());
983
984 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800985 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000986 MemOperand base = MemOperand(temp_base);
987 switch (type) {
988 case Primitive::kPrimBoolean:
989 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000990 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000991 break;
992 case Primitive::kPrimByte:
993 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000994 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000995 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
996 break;
997 case Primitive::kPrimChar:
998 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000999 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001000 break;
1001 case Primitive::kPrimShort:
1002 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001003 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001004 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1005 break;
1006 case Primitive::kPrimInt:
1007 case Primitive::kPrimNot:
1008 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001009 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001010 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001011 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001012 break;
1013 case Primitive::kPrimFloat:
1014 case Primitive::kPrimDouble: {
1015 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001016 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001017
1018 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1019 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001020 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001021 __ Fmov(FPRegister(dst), temp);
1022 break;
1023 }
1024 case Primitive::kPrimVoid:
1025 LOG(FATAL) << "Unreachable type " << type;
1026 }
1027}
1028
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001029void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001030 CPURegister src,
1031 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001032 switch (type) {
1033 case Primitive::kPrimBoolean:
1034 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001035 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001036 break;
1037 case Primitive::kPrimChar:
1038 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001039 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001040 break;
1041 case Primitive::kPrimInt:
1042 case Primitive::kPrimNot:
1043 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001044 case Primitive::kPrimFloat:
1045 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001046 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001047 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001048 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001049 case Primitive::kPrimVoid:
1050 LOG(FATAL) << "Unreachable type " << type;
1051 }
1052}
1053
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001054void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1055 CPURegister src,
1056 const MemOperand& dst) {
1057 UseScratchRegisterScope temps(GetVIXLAssembler());
1058 Register temp_base = temps.AcquireX();
1059
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 DCHECK(!dst.IsPreIndex());
1061 DCHECK(!dst.IsPostIndex());
1062
1063 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001064 Operand op = OperandFromMemOperand(dst);
1065 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001066 MemOperand base = MemOperand(temp_base);
1067 switch (type) {
1068 case Primitive::kPrimBoolean:
1069 case Primitive::kPrimByte:
1070 __ Stlrb(Register(src), base);
1071 break;
1072 case Primitive::kPrimChar:
1073 case Primitive::kPrimShort:
1074 __ Stlrh(Register(src), base);
1075 break;
1076 case Primitive::kPrimInt:
1077 case Primitive::kPrimNot:
1078 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001079 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001080 __ Stlr(Register(src), base);
1081 break;
1082 case Primitive::kPrimFloat:
1083 case Primitive::kPrimDouble: {
1084 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001085 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001086
1087 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1088 __ Fmov(temp, FPRegister(src));
1089 __ Stlr(temp, base);
1090 break;
1091 }
1092 case Primitive::kPrimVoid:
1093 LOG(FATAL) << "Unreachable type " << type;
1094 }
1095}
1096
Alexandre Rames67555f72014-11-18 10:55:16 +00001097void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1098 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001099 uint32_t dex_pc,
1100 SlowPathCode* slow_path) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001101 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001102 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1103 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001104 RecordPcInfo(instruction, dex_pc, slow_path);
1105 DCHECK(instruction->IsSuspendCheck()
1106 || instruction->IsBoundsCheck()
1107 || instruction->IsNullCheck()
1108 || instruction->IsDivZeroCheck()
1109 || !IsLeafMethod());
Alexandre Rames67555f72014-11-18 10:55:16 +00001110}
1111
1112void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1113 vixl::Register class_reg) {
1114 UseScratchRegisterScope temps(GetVIXLAssembler());
1115 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001116 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001117 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001118
Serban Constantinescu02164b32014-11-13 14:05:07 +00001119 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001120 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001121 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1122 __ Add(temp, class_reg, status_offset);
1123 __ Ldar(temp, HeapOperand(temp));
1124 __ Cmp(temp, mirror::Class::kStatusInitialized);
1125 __ B(lt, slow_path->GetEntryLabel());
1126 } else {
1127 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1128 __ Cmp(temp, mirror::Class::kStatusInitialized);
1129 __ B(lt, slow_path->GetEntryLabel());
1130 __ Dmb(InnerShareable, BarrierReads);
1131 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001132 __ Bind(slow_path->GetExitLabel());
1133}
Alexandre Rames5319def2014-10-23 10:03:10 +01001134
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001135void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1136 BarrierType type = BarrierAll;
1137
1138 switch (kind) {
1139 case MemBarrierKind::kAnyAny:
1140 case MemBarrierKind::kAnyStore: {
1141 type = BarrierAll;
1142 break;
1143 }
1144 case MemBarrierKind::kLoadAny: {
1145 type = BarrierReads;
1146 break;
1147 }
1148 case MemBarrierKind::kStoreStore: {
1149 type = BarrierWrites;
1150 break;
1151 }
1152 default:
1153 LOG(FATAL) << "Unexpected memory barrier " << kind;
1154 }
1155 __ Dmb(InnerShareable, type);
1156}
1157
Serban Constantinescu02164b32014-11-13 14:05:07 +00001158void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1159 HBasicBlock* successor) {
1160 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001161 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1162 if (slow_path == nullptr) {
1163 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1164 instruction->SetSlowPath(slow_path);
1165 codegen_->AddSlowPath(slow_path);
1166 if (successor != nullptr) {
1167 DCHECK(successor->IsLoopHeader());
1168 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1169 }
1170 } else {
1171 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1172 }
1173
Serban Constantinescu02164b32014-11-13 14:05:07 +00001174 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1175 Register temp = temps.AcquireW();
1176
1177 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1178 if (successor == nullptr) {
1179 __ Cbnz(temp, slow_path->GetEntryLabel());
1180 __ Bind(slow_path->GetReturnLabel());
1181 } else {
1182 __ Cbz(temp, codegen_->GetLabelOf(successor));
1183 __ B(slow_path->GetEntryLabel());
1184 // slow_path will return to GetLabelOf(successor).
1185 }
1186}
1187
Alexandre Rames5319def2014-10-23 10:03:10 +01001188InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1189 CodeGeneratorARM64* codegen)
1190 : HGraphVisitor(graph),
1191 assembler_(codegen->GetAssembler()),
1192 codegen_(codegen) {}
1193
1194#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001196
1197#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1198
1199enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001200 // Using a base helps identify when we hit such breakpoints.
1201 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001202#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1203 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1204#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1205};
1206
1207#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1208 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001209 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001210 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1211 } \
1212 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1214 locations->SetOut(Location::Any()); \
1215 }
1216 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1217#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1218
1219#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001220#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001221
Alexandre Rames67555f72014-11-18 10:55:16 +00001222void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001223 DCHECK_EQ(instr->InputCount(), 2U);
1224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1225 Primitive::Type type = instr->GetResultType();
1226 switch (type) {
1227 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001228 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001229 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001230 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001232 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001233
1234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble:
1236 locations->SetInAt(0, Location::RequiresFpuRegister());
1237 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001238 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001239 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001240
Alexandre Rames5319def2014-10-23 10:03:10 +01001241 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001242 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001243 }
1244}
1245
Alexandre Rames09a99962015-04-15 11:47:56 +01001246void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1247 LocationSummary* locations =
1248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1249 locations->SetInAt(0, Location::RequiresRegister());
1250 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1251 locations->SetOut(Location::RequiresFpuRegister());
1252 } else {
1253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1254 }
1255}
1256
1257void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1258 const FieldInfo& field_info) {
1259 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001260 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001261 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001262
1263 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1264 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1265
1266 if (field_info.IsVolatile()) {
1267 if (use_acquire_release) {
1268 // NB: LoadAcquire will record the pc info if needed.
1269 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1270 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001271 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001272 codegen_->MaybeRecordImplicitNullCheck(instruction);
1273 // For IRIW sequential consistency kLoadAny is not sufficient.
1274 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1275 }
1276 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001277 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001278 codegen_->MaybeRecordImplicitNullCheck(instruction);
1279 }
Roland Levillain4d027112015-07-01 15:41:14 +01001280
1281 if (field_type == Primitive::kPrimNot) {
1282 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1283 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001284}
1285
1286void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1287 LocationSummary* locations =
1288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1289 locations->SetInAt(0, Location::RequiresRegister());
1290 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1291 locations->SetInAt(1, Location::RequiresFpuRegister());
1292 } else {
1293 locations->SetInAt(1, Location::RequiresRegister());
1294 }
1295}
1296
1297void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001298 const FieldInfo& field_info,
1299 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001300 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001301 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001302
1303 Register obj = InputRegisterAt(instruction, 0);
1304 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001305 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001306 Offset offset = field_info.GetFieldOffset();
1307 Primitive::Type field_type = field_info.GetFieldType();
1308 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1309
Roland Levillain4d027112015-07-01 15:41:14 +01001310 {
1311 // We use a block to end the scratch scope before the write barrier, thus
1312 // freeing the temporary registers so they can be used in `MarkGCCard`.
1313 UseScratchRegisterScope temps(GetVIXLAssembler());
1314
1315 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1316 DCHECK(value.IsW());
1317 Register temp = temps.AcquireW();
1318 __ Mov(temp, value.W());
1319 GetAssembler()->PoisonHeapReference(temp.W());
1320 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001321 }
Roland Levillain4d027112015-07-01 15:41:14 +01001322
1323 if (field_info.IsVolatile()) {
1324 if (use_acquire_release) {
1325 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1326 codegen_->MaybeRecordImplicitNullCheck(instruction);
1327 } else {
1328 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1329 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1330 codegen_->MaybeRecordImplicitNullCheck(instruction);
1331 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1332 }
1333 } else {
1334 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1335 codegen_->MaybeRecordImplicitNullCheck(instruction);
1336 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001337 }
1338
1339 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001340 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001341 }
1342}
1343
Alexandre Rames67555f72014-11-18 10:55:16 +00001344void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001346
1347 switch (type) {
1348 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001349 case Primitive::kPrimLong: {
1350 Register dst = OutputRegister(instr);
1351 Register lhs = InputRegisterAt(instr, 0);
1352 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001353 if (instr->IsAdd()) {
1354 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 } else if (instr->IsAnd()) {
1356 __ And(dst, lhs, rhs);
1357 } else if (instr->IsOr()) {
1358 __ Orr(dst, lhs, rhs);
1359 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001361 } else {
1362 DCHECK(instr->IsXor());
1363 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 }
1365 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001366 }
1367 case Primitive::kPrimFloat:
1368 case Primitive::kPrimDouble: {
1369 FPRegister dst = OutputFPRegister(instr);
1370 FPRegister lhs = InputFPRegisterAt(instr, 0);
1371 FPRegister rhs = InputFPRegisterAt(instr, 1);
1372 if (instr->IsAdd()) {
1373 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001374 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001375 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001376 } else {
1377 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001378 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001379 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001380 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001382 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001383 }
1384}
1385
Serban Constantinescu02164b32014-11-13 14:05:07 +00001386void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1387 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1388
1389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1390 Primitive::Type type = instr->GetResultType();
1391 switch (type) {
1392 case Primitive::kPrimInt:
1393 case Primitive::kPrimLong: {
1394 locations->SetInAt(0, Location::RequiresRegister());
1395 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1396 locations->SetOut(Location::RequiresRegister());
1397 break;
1398 }
1399 default:
1400 LOG(FATAL) << "Unexpected shift type " << type;
1401 }
1402}
1403
1404void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1405 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1406
1407 Primitive::Type type = instr->GetType();
1408 switch (type) {
1409 case Primitive::kPrimInt:
1410 case Primitive::kPrimLong: {
1411 Register dst = OutputRegister(instr);
1412 Register lhs = InputRegisterAt(instr, 0);
1413 Operand rhs = InputOperandAt(instr, 1);
1414 if (rhs.IsImmediate()) {
1415 uint32_t shift_value = (type == Primitive::kPrimInt)
1416 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1417 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1418 if (instr->IsShl()) {
1419 __ Lsl(dst, lhs, shift_value);
1420 } else if (instr->IsShr()) {
1421 __ Asr(dst, lhs, shift_value);
1422 } else {
1423 __ Lsr(dst, lhs, shift_value);
1424 }
1425 } else {
1426 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1427
1428 if (instr->IsShl()) {
1429 __ Lsl(dst, lhs, rhs_reg);
1430 } else if (instr->IsShr()) {
1431 __ Asr(dst, lhs, rhs_reg);
1432 } else {
1433 __ Lsr(dst, lhs, rhs_reg);
1434 }
1435 }
1436 break;
1437 }
1438 default:
1439 LOG(FATAL) << "Unexpected shift operation type " << type;
1440 }
1441}
1442
Alexandre Rames5319def2014-10-23 10:03:10 +01001443void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001444 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001445}
1446
1447void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001448 HandleBinaryOp(instruction);
1449}
1450
1451void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1452 HandleBinaryOp(instruction);
1453}
1454
1455void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1456 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001457}
1458
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001459void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1460 LocationSummary* locations =
1461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1462 locations->SetInAt(0, Location::RequiresRegister());
1463 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001464 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1465 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1466 } else {
1467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1468 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001469}
1470
1471void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1472 LocationSummary* locations = instruction->GetLocations();
1473 Primitive::Type type = instruction->GetType();
1474 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001475 Location index = locations->InAt(1);
1476 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001477 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001478 MacroAssembler* masm = GetVIXLAssembler();
1479 UseScratchRegisterScope temps(masm);
1480 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001481
1482 if (index.IsConstant()) {
1483 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001484 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001485 } else {
1486 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001487 __ Add(temp, obj, offset);
1488 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001489 }
1490
Alexandre Rames67555f72014-11-18 10:55:16 +00001491 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001492 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001493
1494 if (type == Primitive::kPrimNot) {
1495 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1496 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001497}
1498
Alexandre Rames5319def2014-10-23 10:03:10 +01001499void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1501 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001503}
1504
1505void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001506 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001507 __ Ldr(OutputRegister(instruction),
1508 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001509 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001510}
1511
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001512void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001513 if (instruction->NeedsTypeCheck()) {
1514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001516 InvokeRuntimeCallingConvention calling_convention;
1517 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1518 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1519 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1520 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001521 LocationSummary* locations =
1522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001523 locations->SetInAt(0, Location::RequiresRegister());
1524 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001525 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1526 locations->SetInAt(2, Location::RequiresFpuRegister());
1527 } else {
1528 locations->SetInAt(2, Location::RequiresRegister());
1529 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001530 }
1531}
1532
1533void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1534 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001535 LocationSummary* locations = instruction->GetLocations();
1536 bool needs_runtime_call = locations->WillCall();
1537
1538 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001539 // Note: if heap poisoning is enabled, pAputObject takes cares
1540 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001541 codegen_->InvokeRuntime(
1542 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001543 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001544 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001545 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001546 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001547 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001548 Location index = locations->InAt(1);
1549 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001550 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001551 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001552 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001553 {
1554 // We use a block to end the scratch scope before the write barrier, thus
1555 // freeing the temporary registers so they can be used in `MarkGCCard`.
1556 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001557
Roland Levillain4d027112015-07-01 15:41:14 +01001558 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1559 DCHECK(value.IsW());
1560 Register temp = temps.AcquireW();
1561 __ Mov(temp, value.W());
1562 GetAssembler()->PoisonHeapReference(temp.W());
1563 source = temp;
1564 }
1565
Alexandre Rames97833a02015-04-16 15:07:12 +01001566 if (index.IsConstant()) {
1567 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1568 destination = HeapOperand(obj, offset);
1569 } else {
1570 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001571 __ Add(temp, obj, offset);
1572 destination = HeapOperand(temp,
1573 XRegisterFrom(index),
1574 LSL,
1575 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001576 }
1577
Roland Levillain4d027112015-07-01 15:41:14 +01001578 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001579 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001581 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001582 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001583 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001584 }
1585}
1586
Alexandre Rames67555f72014-11-18 10:55:16 +00001587void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1588 LocationSummary* locations =
1589 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1590 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001591 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001592 if (instruction->HasUses()) {
1593 locations->SetOut(Location::SameAsFirstInput());
1594 }
1595}
1596
1597void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001598 LocationSummary* locations = instruction->GetLocations();
1599 BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(
1600 instruction, locations->InAt(0), locations->InAt(1));
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 codegen_->AddSlowPath(slow_path);
1602
1603 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1604 __ B(slow_path->GetEntryLabel(), hs);
1605}
1606
1607void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1609 instruction, LocationSummary::kCallOnSlowPath);
1610 locations->SetInAt(0, Location::RequiresRegister());
1611 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001612 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001613}
1614
1615void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001616 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +00001617 Register obj = InputRegisterAt(instruction, 0);;
1618 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001619 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001620
Alexandre Rames3e69f162014-12-10 10:36:50 +00001621 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
1622 instruction, locations->InAt(1), LocationFrom(obj_cls), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00001623 codegen_->AddSlowPath(slow_path);
1624
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001625 // Avoid null check if we know obj is not null.
1626 if (instruction->MustDoNullCheck()) {
1627 __ Cbz(obj, slow_path->GetExitLabel());
1628 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001630 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001631 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001632 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001633 // The checkcast succeeds if the classes are equal (fast path).
1634 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001635 __ B(ne, slow_path->GetEntryLabel());
1636 __ Bind(slow_path->GetExitLabel());
1637}
1638
1639void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1640 LocationSummary* locations =
1641 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1642 locations->SetInAt(0, Location::RequiresRegister());
1643 if (check->HasUses()) {
1644 locations->SetOut(Location::SameAsFirstInput());
1645 }
1646}
1647
1648void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1649 // We assume the class is not null.
1650 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1651 check->GetLoadClass(), check, check->GetDexPc(), true);
1652 codegen_->AddSlowPath(slow_path);
1653 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1654}
1655
Serban Constantinescu02164b32014-11-13 14:05:07 +00001656void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001657 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001658 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1659 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001660 switch (in_type) {
1661 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001662 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001663 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1665 break;
1666 }
1667 case Primitive::kPrimFloat:
1668 case Primitive::kPrimDouble: {
1669 locations->SetInAt(0, Location::RequiresFpuRegister());
Alexandre Rames93415462015-02-17 15:08:20 +00001670 HInstruction* right = compare->InputAt(1);
1671 if ((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1672 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0))) {
1673 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1674 } else {
1675 locations->SetInAt(1, Location::RequiresFpuRegister());
1676 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001677 locations->SetOut(Location::RequiresRegister());
1678 break;
1679 }
1680 default:
1681 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1682 }
1683}
1684
1685void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1686 Primitive::Type in_type = compare->InputAt(0)->GetType();
1687
1688 // 0 if: left == right
1689 // 1 if: left > right
1690 // -1 if: left < right
1691 switch (in_type) {
1692 case Primitive::kPrimLong: {
1693 Register result = OutputRegister(compare);
1694 Register left = InputRegisterAt(compare, 0);
1695 Operand right = InputOperandAt(compare, 1);
1696
1697 __ Cmp(left, right);
1698 __ Cset(result, ne);
1699 __ Cneg(result, result, lt);
1700 break;
1701 }
1702 case Primitive::kPrimFloat:
1703 case Primitive::kPrimDouble: {
1704 Register result = OutputRegister(compare);
1705 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001706 if (compare->GetLocations()->InAt(1).IsConstant()) {
1707 if (kIsDebugBuild) {
1708 HInstruction* right = compare->GetLocations()->InAt(1).GetConstant();
1709 DCHECK((right->IsFloatConstant() && (right->AsFloatConstant()->GetValue() == 0.0f)) ||
1710 (right->IsDoubleConstant() && (right->AsDoubleConstant()->GetValue() == 0.0)));
1711 }
1712 // 0.0 is the only immediate that can be encoded directly in a FCMP instruction.
1713 __ Fcmp(left, 0.0);
1714 } else {
1715 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1716 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001717 if (compare->IsGtBias()) {
1718 __ Cset(result, ne);
1719 } else {
1720 __ Csetm(result, ne);
1721 }
1722 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001723 break;
1724 }
1725 default:
1726 LOG(FATAL) << "Unimplemented compare type " << in_type;
1727 }
1728}
1729
1730void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1732 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001733 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames5319def2014-10-23 10:03:10 +01001734 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001736 }
1737}
1738
1739void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1740 if (!instruction->NeedsMaterialization()) {
1741 return;
1742 }
1743
1744 LocationSummary* locations = instruction->GetLocations();
1745 Register lhs = InputRegisterAt(instruction, 0);
1746 Operand rhs = InputOperandAt(instruction, 1);
1747 Register res = RegisterFrom(locations->Out(), instruction->GetType());
1748 Condition cond = ARM64Condition(instruction->GetCondition());
1749
1750 __ Cmp(lhs, rhs);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001751 __ Cset(res, cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001752}
1753
1754#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1755 M(Equal) \
1756 M(NotEqual) \
1757 M(LessThan) \
1758 M(LessThanOrEqual) \
1759 M(GreaterThan) \
1760 M(GreaterThanOrEqual)
1761#define DEFINE_CONDITION_VISITORS(Name) \
1762void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1763void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1764FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001765#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001766#undef FOR_EACH_CONDITION_INSTRUCTION
1767
Zheng Xuc6667102015-05-15 16:08:45 +08001768void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1769 DCHECK(instruction->IsDiv() || instruction->IsRem());
1770
1771 LocationSummary* locations = instruction->GetLocations();
1772 Location second = locations->InAt(1);
1773 DCHECK(second.IsConstant());
1774
1775 Register out = OutputRegister(instruction);
1776 Register dividend = InputRegisterAt(instruction, 0);
1777 int64_t imm = Int64FromConstant(second.GetConstant());
1778 DCHECK(imm == 1 || imm == -1);
1779
1780 if (instruction->IsRem()) {
1781 __ Mov(out, 0);
1782 } else {
1783 if (imm == 1) {
1784 __ Mov(out, dividend);
1785 } else {
1786 __ Neg(out, dividend);
1787 }
1788 }
1789}
1790
1791void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1792 DCHECK(instruction->IsDiv() || instruction->IsRem());
1793
1794 LocationSummary* locations = instruction->GetLocations();
1795 Location second = locations->InAt(1);
1796 DCHECK(second.IsConstant());
1797
1798 Register out = OutputRegister(instruction);
1799 Register dividend = InputRegisterAt(instruction, 0);
1800 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001801 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001802 DCHECK(IsPowerOfTwo(abs_imm));
1803 int ctz_imm = CTZ(abs_imm);
1804
1805 UseScratchRegisterScope temps(GetVIXLAssembler());
1806 Register temp = temps.AcquireSameSizeAs(out);
1807
1808 if (instruction->IsDiv()) {
1809 __ Add(temp, dividend, abs_imm - 1);
1810 __ Cmp(dividend, 0);
1811 __ Csel(out, temp, dividend, lt);
1812 if (imm > 0) {
1813 __ Asr(out, out, ctz_imm);
1814 } else {
1815 __ Neg(out, Operand(out, ASR, ctz_imm));
1816 }
1817 } else {
1818 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1819 __ Asr(temp, dividend, bits - 1);
1820 __ Lsr(temp, temp, bits - ctz_imm);
1821 __ Add(out, dividend, temp);
1822 __ And(out, out, abs_imm - 1);
1823 __ Sub(out, out, temp);
1824 }
1825}
1826
1827void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1828 DCHECK(instruction->IsDiv() || instruction->IsRem());
1829
1830 LocationSummary* locations = instruction->GetLocations();
1831 Location second = locations->InAt(1);
1832 DCHECK(second.IsConstant());
1833
1834 Register out = OutputRegister(instruction);
1835 Register dividend = InputRegisterAt(instruction, 0);
1836 int64_t imm = Int64FromConstant(second.GetConstant());
1837
1838 Primitive::Type type = instruction->GetResultType();
1839 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1840
1841 int64_t magic;
1842 int shift;
1843 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1844
1845 UseScratchRegisterScope temps(GetVIXLAssembler());
1846 Register temp = temps.AcquireSameSizeAs(out);
1847
1848 // temp = get_high(dividend * magic)
1849 __ Mov(temp, magic);
1850 if (type == Primitive::kPrimLong) {
1851 __ Smulh(temp, dividend, temp);
1852 } else {
1853 __ Smull(temp.X(), dividend, temp);
1854 __ Lsr(temp.X(), temp.X(), 32);
1855 }
1856
1857 if (imm > 0 && magic < 0) {
1858 __ Add(temp, temp, dividend);
1859 } else if (imm < 0 && magic > 0) {
1860 __ Sub(temp, temp, dividend);
1861 }
1862
1863 if (shift != 0) {
1864 __ Asr(temp, temp, shift);
1865 }
1866
1867 if (instruction->IsDiv()) {
1868 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1869 } else {
1870 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1871 // TODO: Strength reduction for msub.
1872 Register temp_imm = temps.AcquireSameSizeAs(out);
1873 __ Mov(temp_imm, imm);
1874 __ Msub(out, temp, temp_imm, dividend);
1875 }
1876}
1877
1878void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1879 DCHECK(instruction->IsDiv() || instruction->IsRem());
1880 Primitive::Type type = instruction->GetResultType();
1881 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1882
1883 LocationSummary* locations = instruction->GetLocations();
1884 Register out = OutputRegister(instruction);
1885 Location second = locations->InAt(1);
1886
1887 if (second.IsConstant()) {
1888 int64_t imm = Int64FromConstant(second.GetConstant());
1889
1890 if (imm == 0) {
1891 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1892 } else if (imm == 1 || imm == -1) {
1893 DivRemOneOrMinusOne(instruction);
1894 } else if (IsPowerOfTwo(std::abs(imm))) {
1895 DivRemByPowerOfTwo(instruction);
1896 } else {
1897 DCHECK(imm <= -2 || imm >= 2);
1898 GenerateDivRemWithAnyConstant(instruction);
1899 }
1900 } else {
1901 Register dividend = InputRegisterAt(instruction, 0);
1902 Register divisor = InputRegisterAt(instruction, 1);
1903 if (instruction->IsDiv()) {
1904 __ Sdiv(out, dividend, divisor);
1905 } else {
1906 UseScratchRegisterScope temps(GetVIXLAssembler());
1907 Register temp = temps.AcquireSameSizeAs(out);
1908 __ Sdiv(temp, dividend, divisor);
1909 __ Msub(out, temp, divisor, dividend);
1910 }
1911 }
1912}
1913
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001914void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1917 switch (div->GetResultType()) {
1918 case Primitive::kPrimInt:
1919 case Primitive::kPrimLong:
1920 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001921 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1923 break;
1924
1925 case Primitive::kPrimFloat:
1926 case Primitive::kPrimDouble:
1927 locations->SetInAt(0, Location::RequiresFpuRegister());
1928 locations->SetInAt(1, Location::RequiresFpuRegister());
1929 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1930 break;
1931
1932 default:
1933 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1934 }
1935}
1936
1937void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1938 Primitive::Type type = div->GetResultType();
1939 switch (type) {
1940 case Primitive::kPrimInt:
1941 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001942 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001943 break;
1944
1945 case Primitive::kPrimFloat:
1946 case Primitive::kPrimDouble:
1947 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1948 break;
1949
1950 default:
1951 LOG(FATAL) << "Unexpected div type " << type;
1952 }
1953}
1954
Alexandre Rames67555f72014-11-18 10:55:16 +00001955void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1958 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1959 if (instruction->HasUses()) {
1960 locations->SetOut(Location::SameAsFirstInput());
1961 }
1962}
1963
1964void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1965 SlowPathCodeARM64* slow_path =
1966 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1967 codegen_->AddSlowPath(slow_path);
1968 Location value = instruction->GetLocations()->InAt(0);
1969
Alexandre Rames3e69f162014-12-10 10:36:50 +00001970 Primitive::Type type = instruction->GetType();
1971
1972 if ((type != Primitive::kPrimInt) && (type != Primitive::kPrimLong)) {
1973 LOG(FATAL) << "Unexpected type " << type << "for DivZeroCheck.";
1974 return;
1975 }
1976
Alexandre Rames67555f72014-11-18 10:55:16 +00001977 if (value.IsConstant()) {
1978 int64_t divisor = Int64ConstantFrom(value);
1979 if (divisor == 0) {
1980 __ B(slow_path->GetEntryLabel());
1981 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001982 // A division by a non-null constant is valid. We don't need to perform
1983 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00001984 }
1985 } else {
1986 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
1987 }
1988}
1989
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001990void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1993 locations->SetOut(Location::ConstantLocation(constant));
1994}
1995
1996void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
1997 UNUSED(constant);
1998 // Will be generated at use site.
1999}
2000
Alexandre Rames5319def2014-10-23 10:03:10 +01002001void LocationsBuilderARM64::VisitExit(HExit* exit) {
2002 exit->SetLocations(nullptr);
2003}
2004
2005void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002006 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002007}
2008
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002009void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2010 LocationSummary* locations =
2011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2012 locations->SetOut(Location::ConstantLocation(constant));
2013}
2014
2015void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2016 UNUSED(constant);
2017 // Will be generated at use site.
2018}
2019
David Brazdilfc6a86a2015-06-26 10:33:45 +00002020void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002021 DCHECK(!successor->IsExitBlock());
2022 HBasicBlock* block = got->GetBlock();
2023 HInstruction* previous = got->GetPrevious();
2024 HLoopInformation* info = block->GetLoopInformation();
2025
David Brazdil46e2a392015-03-16 17:31:52 +00002026 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002027 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2028 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2029 return;
2030 }
2031 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2032 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2033 }
2034 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002035 __ B(codegen_->GetLabelOf(successor));
2036 }
2037}
2038
David Brazdilfc6a86a2015-06-26 10:33:45 +00002039void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2040 got->SetLocations(nullptr);
2041}
2042
2043void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2044 HandleGoto(got, got->GetSuccessor());
2045}
2046
2047void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2048 try_boundary->SetLocations(nullptr);
2049}
2050
2051void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2052 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2053 if (!successor->IsExitBlock()) {
2054 HandleGoto(try_boundary, successor);
2055 }
2056}
2057
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002058void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2059 vixl::Label* true_target,
2060 vixl::Label* false_target,
2061 vixl::Label* always_true_target) {
2062 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002063 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002064
Serban Constantinescu02164b32014-11-13 14:05:07 +00002065 if (cond->IsIntConstant()) {
2066 int32_t cond_value = cond->AsIntConstant()->GetValue();
2067 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002068 if (always_true_target != nullptr) {
2069 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002070 }
2071 return;
2072 } else {
2073 DCHECK_EQ(cond_value, 0);
2074 }
2075 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002076 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002077 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002078 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002079 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002080 } else {
2081 // The condition instruction has not been materialized, use its inputs as
2082 // the comparison and its condition as the branch condition.
2083 Register lhs = InputRegisterAt(condition, 0);
2084 Operand rhs = InputOperandAt(condition, 1);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002085 Condition arm64_cond = ARM64Condition(condition->GetCondition());
Alexandre Rames4388dcc2015-02-03 10:28:33 +00002086 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2087 switch (arm64_cond) {
2088 case eq:
2089 __ Cbz(lhs, true_target);
2090 break;
2091 case ne:
2092 __ Cbnz(lhs, true_target);
2093 break;
2094 case lt:
2095 // Test the sign bit and branch accordingly.
2096 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2097 break;
2098 case ge:
2099 // Test the sign bit and branch accordingly.
2100 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2101 break;
2102 default:
2103 // Without the `static_cast` the compiler throws an error for
2104 // `-Werror=sign-promo`.
2105 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002106 }
2107 } else {
2108 __ Cmp(lhs, rhs);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002109 __ B(arm64_cond, true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002110 }
2111 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002112 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002113 __ B(false_target);
2114 }
2115}
2116
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002117void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2119 HInstruction* cond = if_instr->InputAt(0);
2120 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2121 locations->SetInAt(0, Location::RequiresRegister());
2122 }
2123}
2124
2125void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2126 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2127 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2128 vixl::Label* always_true_target = true_target;
2129 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2130 if_instr->IfTrueSuccessor())) {
2131 always_true_target = nullptr;
2132 }
2133 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2134 if_instr->IfFalseSuccessor())) {
2135 false_target = nullptr;
2136 }
2137 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2138}
2139
2140void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2141 LocationSummary* locations = new (GetGraph()->GetArena())
2142 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2143 HInstruction* cond = deoptimize->InputAt(0);
2144 DCHECK(cond->IsCondition());
2145 if (cond->AsCondition()->NeedsMaterialization()) {
2146 locations->SetInAt(0, Location::RequiresRegister());
2147 }
2148}
2149
2150void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2151 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2152 DeoptimizationSlowPathARM64(deoptimize);
2153 codegen_->AddSlowPath(slow_path);
2154 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2155 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2156}
2157
Alexandre Rames5319def2014-10-23 10:03:10 +01002158void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002159 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002160}
2161
2162void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002163 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002164}
2165
2166void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002167 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002168}
2169
2170void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002171 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002172}
2173
Alexandre Rames67555f72014-11-18 10:55:16 +00002174void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2175 LocationSummary::CallKind call_kind =
2176 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2177 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2178 locations->SetInAt(0, Location::RequiresRegister());
2179 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002180 // The output does overlap inputs.
2181 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002182}
2183
2184void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2185 LocationSummary* locations = instruction->GetLocations();
2186 Register obj = InputRegisterAt(instruction, 0);;
2187 Register cls = InputRegisterAt(instruction, 1);;
2188 Register out = OutputRegister(instruction);
2189
2190 vixl::Label done;
2191
2192 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002193 // Avoid null check if we know `obj` is not null.
2194 if (instruction->MustDoNullCheck()) {
2195 __ Mov(out, 0);
2196 __ Cbz(obj, &done);
2197 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002198
2199 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002200 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002201 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002202 __ Cmp(out, cls);
2203 if (instruction->IsClassFinal()) {
2204 // Classes must be equal for the instanceof to succeed.
2205 __ Cset(out, eq);
2206 } else {
2207 // If the classes are not equal, we go into a slow path.
2208 DCHECK(locations->OnlyCallsOnSlowPath());
2209 SlowPathCodeARM64* slow_path =
Alexandre Rames3e69f162014-12-10 10:36:50 +00002210 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2211 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Alexandre Rames67555f72014-11-18 10:55:16 +00002212 codegen_->AddSlowPath(slow_path);
2213 __ B(ne, slow_path->GetEntryLabel());
2214 __ Mov(out, 1);
2215 __ Bind(slow_path->GetExitLabel());
2216 }
2217
2218 __ Bind(&done);
2219}
2220
Alexandre Rames5319def2014-10-23 10:03:10 +01002221void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2222 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2223 locations->SetOut(Location::ConstantLocation(constant));
2224}
2225
2226void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2227 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002228 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002229}
2230
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002231void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2233 locations->SetOut(Location::ConstantLocation(constant));
2234}
2235
2236void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2237 // Will be generated at use site.
2238 UNUSED(constant);
2239}
2240
Alexandre Rames5319def2014-10-23 10:03:10 +01002241void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002242 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002243 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002244}
2245
Alexandre Rames67555f72014-11-18 10:55:16 +00002246void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2247 HandleInvoke(invoke);
2248}
2249
2250void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2251 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002252 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2253 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2254 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002255 Location receiver = invoke->GetLocations()->InAt(0);
2256 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002257 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002258
2259 // The register ip1 is required to be used for the hidden argument in
2260 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002261 MacroAssembler* masm = GetVIXLAssembler();
2262 UseScratchRegisterScope scratch_scope(masm);
2263 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002264 scratch_scope.Exclude(ip1);
2265 __ Mov(ip1, invoke->GetDexMethodIndex());
2266
2267 // temp = object->GetClass();
2268 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002269 __ Ldr(temp.W(), StackOperandFrom(receiver));
2270 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002271 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002272 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002273 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002274 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002275 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002276 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002277 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002278 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002279 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002280 // lr();
2281 __ Blr(lr);
2282 DCHECK(!codegen_->IsLeafMethod());
2283 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2284}
2285
2286void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002287 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2288 if (intrinsic.TryDispatch(invoke)) {
2289 return;
2290 }
2291
Alexandre Rames67555f72014-11-18 10:55:16 +00002292 HandleInvoke(invoke);
2293}
2294
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002295void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002296 // When we do not run baseline, explicit clinit checks triggered by static
2297 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2298 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002299
Andreas Gampe878d58c2015-01-15 23:24:00 -08002300 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2301 if (intrinsic.TryDispatch(invoke)) {
2302 return;
2303 }
2304
Alexandre Rames67555f72014-11-18 10:55:16 +00002305 HandleInvoke(invoke);
2306}
2307
Andreas Gampe878d58c2015-01-15 23:24:00 -08002308static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2309 if (invoke->GetLocations()->Intrinsified()) {
2310 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2311 intrinsic.Dispatch(invoke);
2312 return true;
2313 }
2314 return false;
2315}
2316
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002317void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002318 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319 size_t index_in_cache = GetCachePointerOffset(invoke->GetDexMethodIndex());
Alexandre Rames5319def2014-10-23 10:03:10 +01002320
2321 // TODO: Implement all kinds of calls:
2322 // 1) boot -> boot
2323 // 2) app -> boot
2324 // 3) app -> app
2325 //
2326 // Currently we implement the app -> app logic, which looks up in the resolve cache.
2327
Jeff Hao848f70a2014-01-15 13:49:50 -08002328 if (invoke->IsStringInit()) {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002329 Register reg = XRegisterFrom(temp);
Jeff Hao848f70a2014-01-15 13:49:50 -08002330 // temp = thread->string_init_entrypoint
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002331 __ Ldr(reg.X(), MemOperand(tr, invoke->GetStringInitOffset()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002332 // LR = temp->entry_point_from_quick_compiled_code_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 __ Ldr(lr, MemOperand(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002334 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
Jeff Hao848f70a2014-01-15 13:49:50 -08002335 // lr()
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002336 __ Blr(lr);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002337 } else if (invoke->IsRecursive()) {
2338 __ Bl(&frame_entry_label_);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002339 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002340 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002341 Register reg = XRegisterFrom(temp);
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002342 Register method_reg;
2343 if (current_method.IsRegister()) {
2344 method_reg = XRegisterFrom(current_method);
2345 } else {
2346 DCHECK(invoke->GetLocations()->Intrinsified());
2347 DCHECK(!current_method.IsValid());
2348 method_reg = reg;
2349 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2350 }
2351
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002352 // temp = current_method->dex_cache_resolved_methods_;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01002353 __ Ldr(reg.W(), MemOperand(method_reg.X(),
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002354 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2355 // temp = temp[index_in_cache];
2356 __ Ldr(reg.X(), MemOperand(reg, index_in_cache));
2357 // lr = temp->entry_point_from_quick_compiled_code_;
2358 __ Ldr(lr, MemOperand(reg.X(), ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2359 kArm64WordSize).Int32Value()));
2360 // lr();
2361 __ Blr(lr);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002362 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002363
Andreas Gampe878d58c2015-01-15 23:24:00 -08002364 DCHECK(!IsLeafMethod());
2365}
2366
2367void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002368 // When we do not run baseline, explicit clinit checks triggered by static
2369 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2370 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002371
Andreas Gampe878d58c2015-01-15 23:24:00 -08002372 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2373 return;
2374 }
2375
Alexandre Ramesd921d642015-04-16 15:07:16 +01002376 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002377 LocationSummary* locations = invoke->GetLocations();
2378 codegen_->GenerateStaticOrDirectCall(
2379 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002380 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002381}
2382
2383void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002384 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2385 return;
2386 }
2387
Alexandre Rames5319def2014-10-23 10:03:10 +01002388 LocationSummary* locations = invoke->GetLocations();
2389 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2391 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2392 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002393 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002394 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002395
Alexandre Ramesd921d642015-04-16 15:07:16 +01002396 BlockPoolsScope block_pools(GetVIXLAssembler());
2397
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002398 DCHECK(receiver.IsRegister());
2399 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002400 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002401 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002402 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002403 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002404 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002405 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002406 // lr();
2407 __ Blr(lr);
2408 DCHECK(!codegen_->IsLeafMethod());
2409 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2410}
2411
Alexandre Rames67555f72014-11-18 10:55:16 +00002412void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2413 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2414 : LocationSummary::kNoCall;
2415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002416 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002417 locations->SetOut(Location::RequiresRegister());
2418}
2419
2420void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2421 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002422 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002423 if (cls->IsReferrersClass()) {
2424 DCHECK(!cls->CanCallRuntime());
2425 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002426 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002427 } else {
2428 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002429 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002430 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002431 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002432
2433 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2434 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2435 codegen_->AddSlowPath(slow_path);
2436 __ Cbz(out, slow_path->GetEntryLabel());
2437 if (cls->MustGenerateClinitCheck()) {
2438 GenerateClassInitializationCheck(slow_path, out);
2439 } else {
2440 __ Bind(slow_path->GetExitLabel());
2441 }
2442 }
2443}
2444
2445void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2446 LocationSummary* locations =
2447 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2448 locations->SetOut(Location::RequiresRegister());
2449}
2450
2451void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
2452 MemOperand exception = MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2453 __ Ldr(OutputRegister(instruction), exception);
2454 __ Str(wzr, exception);
2455}
2456
Alexandre Rames5319def2014-10-23 10:03:10 +01002457void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2458 load->SetLocations(nullptr);
2459}
2460
2461void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2462 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002463 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002464}
2465
Alexandre Rames67555f72014-11-18 10:55:16 +00002466void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2467 LocationSummary* locations =
2468 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002469 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002470 locations->SetOut(Location::RequiresRegister());
2471}
2472
2473void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2474 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2475 codegen_->AddSlowPath(slow_path);
2476
2477 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002478 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002479 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002480 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002481 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002482 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002483 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002484 __ Cbz(out, slow_path->GetEntryLabel());
2485 __ Bind(slow_path->GetExitLabel());
2486}
2487
Alexandre Rames5319def2014-10-23 10:03:10 +01002488void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2489 local->SetLocations(nullptr);
2490}
2491
2492void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2493 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2494}
2495
2496void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2498 locations->SetOut(Location::ConstantLocation(constant));
2499}
2500
2501void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2502 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002503 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002504}
2505
Alexandre Rames67555f72014-11-18 10:55:16 +00002506void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2507 LocationSummary* locations =
2508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2509 InvokeRuntimeCallingConvention calling_convention;
2510 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2511}
2512
2513void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2514 codegen_->InvokeRuntime(instruction->IsEnter()
2515 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2516 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002517 instruction->GetDexPc(),
2518 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002519 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002520}
2521
Alexandre Rames42d641b2014-10-27 14:00:51 +00002522void LocationsBuilderARM64::VisitMul(HMul* mul) {
2523 LocationSummary* locations =
2524 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2525 switch (mul->GetResultType()) {
2526 case Primitive::kPrimInt:
2527 case Primitive::kPrimLong:
2528 locations->SetInAt(0, Location::RequiresRegister());
2529 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002531 break;
2532
2533 case Primitive::kPrimFloat:
2534 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002535 locations->SetInAt(0, Location::RequiresFpuRegister());
2536 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002537 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002538 break;
2539
2540 default:
2541 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2542 }
2543}
2544
2545void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2546 switch (mul->GetResultType()) {
2547 case Primitive::kPrimInt:
2548 case Primitive::kPrimLong:
2549 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2550 break;
2551
2552 case Primitive::kPrimFloat:
2553 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002554 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002555 break;
2556
2557 default:
2558 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2559 }
2560}
2561
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002562void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2565 switch (neg->GetResultType()) {
2566 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002567 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002568 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002570 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002571
2572 case Primitive::kPrimFloat:
2573 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002574 locations->SetInAt(0, Location::RequiresFpuRegister());
2575 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2580 }
2581}
2582
2583void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2584 switch (neg->GetResultType()) {
2585 case Primitive::kPrimInt:
2586 case Primitive::kPrimLong:
2587 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2588 break;
2589
2590 case Primitive::kPrimFloat:
2591 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002592 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2597 }
2598}
2599
2600void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2601 LocationSummary* locations =
2602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2603 InvokeRuntimeCallingConvention calling_convention;
2604 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002605 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002606 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002607 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002608 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002609 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002610}
2611
2612void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2613 LocationSummary* locations = instruction->GetLocations();
2614 InvokeRuntimeCallingConvention calling_convention;
2615 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2616 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002617 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002618 // Note: if heap poisoning is enabled, the entry point takes cares
2619 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002620 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002621 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2622 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002623 instruction->GetDexPc(),
2624 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002625 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002626}
2627
Alexandre Rames5319def2014-10-23 10:03:10 +01002628void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2629 LocationSummary* locations =
2630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2631 InvokeRuntimeCallingConvention calling_convention;
2632 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002633 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002634 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002635 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002636}
2637
2638void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2639 LocationSummary* locations = instruction->GetLocations();
2640 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2641 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002642 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002643 // Note: if heap poisoning is enabled, the entry point takes cares
2644 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002645 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002646 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2647 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002648 instruction->GetDexPc(),
2649 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002650 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002651}
2652
2653void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2654 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002655 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002657}
2658
2659void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002660 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002661 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002662 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002663 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002664 break;
2665
2666 default:
2667 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2668 }
2669}
2670
David Brazdil66d126e2015-04-03 16:02:44 +01002671void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2673 locations->SetInAt(0, Location::RequiresRegister());
2674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2675}
2676
2677void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002678 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2679}
2680
Alexandre Rames5319def2014-10-23 10:03:10 +01002681void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2682 LocationSummary* locations =
2683 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2684 locations->SetInAt(0, Location::RequiresRegister());
2685 if (instruction->HasUses()) {
2686 locations->SetOut(Location::SameAsFirstInput());
2687 }
2688}
2689
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002690void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002691 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2692 return;
2693 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002694
Alexandre Ramesd921d642015-04-16 15:07:16 +01002695 BlockPoolsScope block_pools(GetVIXLAssembler());
2696 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002697 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2698 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2699}
2700
2701void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002702 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2703 codegen_->AddSlowPath(slow_path);
2704
2705 LocationSummary* locations = instruction->GetLocations();
2706 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002707
2708 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002709}
2710
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002711void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2712 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2713 GenerateImplicitNullCheck(instruction);
2714 } else {
2715 GenerateExplicitNullCheck(instruction);
2716 }
2717}
2718
Alexandre Rames67555f72014-11-18 10:55:16 +00002719void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2720 HandleBinaryOp(instruction);
2721}
2722
2723void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2724 HandleBinaryOp(instruction);
2725}
2726
Alexandre Rames3e69f162014-12-10 10:36:50 +00002727void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2728 LOG(FATAL) << "Unreachable";
2729}
2730
2731void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2732 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2733}
2734
Alexandre Rames5319def2014-10-23 10:03:10 +01002735void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2736 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2737 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2738 if (location.IsStackSlot()) {
2739 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2740 } else if (location.IsDoubleStackSlot()) {
2741 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2742 }
2743 locations->SetOut(location);
2744}
2745
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002746void InstructionCodeGeneratorARM64::VisitParameterValue(
2747 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002748 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002749}
2750
2751void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2752 LocationSummary* locations =
2753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002754 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002755}
2756
2757void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2758 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2759 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002760}
2761
2762void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2764 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2765 locations->SetInAt(i, Location::Any());
2766 }
2767 locations->SetOut(Location::Any());
2768}
2769
2770void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002771 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002772 LOG(FATAL) << "Unreachable";
2773}
2774
Serban Constantinescu02164b32014-11-13 14:05:07 +00002775void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002776 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002777 LocationSummary::CallKind call_kind =
2778 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2780
2781 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002782 case Primitive::kPrimInt:
2783 case Primitive::kPrimLong:
2784 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002785 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2787 break;
2788
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002789 case Primitive::kPrimFloat:
2790 case Primitive::kPrimDouble: {
2791 InvokeRuntimeCallingConvention calling_convention;
2792 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2793 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2794 locations->SetOut(calling_convention.GetReturnLocation(type));
2795
2796 break;
2797 }
2798
Serban Constantinescu02164b32014-11-13 14:05:07 +00002799 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002800 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002801 }
2802}
2803
2804void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2805 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002806
Serban Constantinescu02164b32014-11-13 14:05:07 +00002807 switch (type) {
2808 case Primitive::kPrimInt:
2809 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08002810 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002811 break;
2812 }
2813
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002814 case Primitive::kPrimFloat:
2815 case Primitive::kPrimDouble: {
2816 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
2817 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002818 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002819 break;
2820 }
2821
Serban Constantinescu02164b32014-11-13 14:05:07 +00002822 default:
2823 LOG(FATAL) << "Unexpected rem type " << type;
2824 }
2825}
2826
Calin Juravle27df7582015-04-17 19:12:31 +01002827void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2828 memory_barrier->SetLocations(nullptr);
2829}
2830
2831void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2832 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
2833}
2834
Alexandre Rames5319def2014-10-23 10:03:10 +01002835void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
2836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2837 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002838 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01002839}
2840
2841void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002842 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002843 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002844}
2845
2846void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
2847 instruction->SetLocations(nullptr);
2848}
2849
2850void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002851 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002852 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01002853}
2854
Serban Constantinescu02164b32014-11-13 14:05:07 +00002855void LocationsBuilderARM64::VisitShl(HShl* shl) {
2856 HandleShift(shl);
2857}
2858
2859void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
2860 HandleShift(shl);
2861}
2862
2863void LocationsBuilderARM64::VisitShr(HShr* shr) {
2864 HandleShift(shr);
2865}
2866
2867void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
2868 HandleShift(shr);
2869}
2870
Alexandre Rames5319def2014-10-23 10:03:10 +01002871void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
2872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
2873 Primitive::Type field_type = store->InputAt(1)->GetType();
2874 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002875 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01002876 case Primitive::kPrimBoolean:
2877 case Primitive::kPrimByte:
2878 case Primitive::kPrimChar:
2879 case Primitive::kPrimShort:
2880 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002881 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01002882 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
2883 break;
2884
2885 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002886 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01002887 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
2888 break;
2889
2890 default:
2891 LOG(FATAL) << "Unimplemented local type " << field_type;
2892 }
2893}
2894
2895void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002896 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01002897}
2898
2899void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002900 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002901}
2902
2903void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002904 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002905}
2906
Alexandre Rames67555f72014-11-18 10:55:16 +00002907void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002908 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002909}
2910
2911void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002912 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00002913}
2914
2915void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002916 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002917}
2918
Alexandre Rames67555f72014-11-18 10:55:16 +00002919void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002920 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002921}
2922
2923void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
2924 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2925}
2926
2927void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002928 HBasicBlock* block = instruction->GetBlock();
2929 if (block->GetLoopInformation() != nullptr) {
2930 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2931 // The back edge will generate the suspend check.
2932 return;
2933 }
2934 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2935 // The goto will generate the suspend check.
2936 return;
2937 }
2938 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01002939}
2940
2941void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
2942 temp->SetLocations(nullptr);
2943}
2944
2945void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
2946 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002947 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01002948}
2949
Alexandre Rames67555f72014-11-18 10:55:16 +00002950void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
2951 LocationSummary* locations =
2952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2953 InvokeRuntimeCallingConvention calling_convention;
2954 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2955}
2956
2957void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
2958 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002959 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002960 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002961}
2962
2963void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
2964 LocationSummary* locations =
2965 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2966 Primitive::Type input_type = conversion->GetInputType();
2967 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002968 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00002969 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
2970 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
2971 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
2972 }
2973
Alexandre Rames542361f2015-01-29 16:57:31 +00002974 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002975 locations->SetInAt(0, Location::RequiresFpuRegister());
2976 } else {
2977 locations->SetInAt(0, Location::RequiresRegister());
2978 }
2979
Alexandre Rames542361f2015-01-29 16:57:31 +00002980 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002981 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2982 } else {
2983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2984 }
2985}
2986
2987void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
2988 Primitive::Type result_type = conversion->GetResultType();
2989 Primitive::Type input_type = conversion->GetInputType();
2990
2991 DCHECK_NE(input_type, result_type);
2992
Alexandre Rames542361f2015-01-29 16:57:31 +00002993 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002994 int result_size = Primitive::ComponentSize(result_type);
2995 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002996 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002997 Register output = OutputRegister(conversion);
2998 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00002999 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3000 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
3001 } else if ((result_type == Primitive::kPrimChar) ||
3002 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3003 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003004 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003005 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003006 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003007 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003008 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003009 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003010 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3011 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003012 } else if (Primitive::IsFloatingPointType(result_type) &&
3013 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003014 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3015 } else {
3016 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3017 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003018 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003019}
Alexandre Rames67555f72014-11-18 10:55:16 +00003020
Serban Constantinescu02164b32014-11-13 14:05:07 +00003021void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3022 HandleShift(ushr);
3023}
3024
3025void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3026 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003027}
3028
3029void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3030 HandleBinaryOp(instruction);
3031}
3032
3033void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3034 HandleBinaryOp(instruction);
3035}
3036
Calin Juravleb1498f62015-02-16 13:13:29 +00003037void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3038 // Nothing to do, this should be removed during prepare for register allocator.
3039 UNUSED(instruction);
3040 LOG(FATAL) << "Unreachable";
3041}
3042
3043void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3044 // Nothing to do, this should be removed during prepare for register allocator.
3045 UNUSED(instruction);
3046 LOG(FATAL) << "Unreachable";
3047}
3048
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003049void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3050 DCHECK(codegen_->IsBaseline());
3051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3053 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3054}
3055
3056void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3057 DCHECK(codegen_->IsBaseline());
3058 // Will be generated at use site.
3059}
3060
Alexandre Rames67555f72014-11-18 10:55:16 +00003061#undef __
3062#undef QUICK_ENTRY_POINT
3063
Alexandre Rames5319def2014-10-23 10:03:10 +01003064} // namespace arm64
3065} // namespace art