blob: 8035461239c60888114efc0165d05d6adf20ecdd [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"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000031#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "thread.h"
33#include "utils/arm64/assembler_arm64.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37
38using namespace vixl; // NOLINT(build/namespaces)
39
40#ifdef __
41#error "ARM64 Codegen VIXL macro-assembler macro already defined."
42#endif
43
Alexandre Rames5319def2014-10-23 10:03:10 +010044namespace art {
45
46namespace arm64 {
47
Andreas Gampe878d58c2015-01-15 23:24:00 -080048using helpers::CPURegisterFrom;
49using helpers::DRegisterFrom;
50using helpers::FPRegisterFrom;
51using helpers::HeapOperand;
52using helpers::HeapOperandFrom;
53using helpers::InputCPURegisterAt;
54using helpers::InputFPRegisterAt;
55using helpers::InputRegisterAt;
56using helpers::InputOperandAt;
57using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::LocationFrom;
59using helpers::OperandFromMemOperand;
60using helpers::OutputCPURegister;
61using helpers::OutputFPRegister;
62using helpers::OutputRegister;
63using helpers::RegisterFrom;
64using helpers::StackOperandFrom;
65using helpers::VIXLRegCodeFromART;
66using helpers::WRegisterFrom;
67using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000068using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080069using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070
Alexandre Rames5319def2014-10-23 10:03:10 +010071static constexpr int kCurrentMethodStackOffset = 0;
72
Alexandre Rames5319def2014-10-23 10:03:10 +010073inline Condition ARM64Condition(IfCondition cond) {
74 switch (cond) {
75 case kCondEQ: return eq;
76 case kCondNE: return ne;
77 case kCondLT: return lt;
78 case kCondLE: return le;
79 case kCondGT: return gt;
80 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010081 }
Roland Levillain7f63c522015-07-13 15:54:55 +000082 LOG(FATAL) << "Unreachable";
83 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010084}
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:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100194 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100195
Alexandre Rames67555f72014-11-18 10:55:16 +0000196 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100197 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000198 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199
Alexandre Rames5319def2014-10-23 10:03:10 +0100200 __ Bind(GetEntryLabel());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000201 // We're moving two locations to locations that could overlap, so we need a parallel
202 // move resolver.
203 InvokeRuntimeCallingConvention calling_convention;
204 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100205 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
206 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000207 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000208 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800209 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100210 }
211
Alexandre Rames8158f282015-08-07 10:26:17 +0100212 bool IsFatal() const OVERRIDE { return true; }
213
Alexandre Rames9931f312015-06-19 14:47:01 +0100214 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
215
Alexandre Rames5319def2014-10-23 10:03:10 +0100216 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000217 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
220};
221
Alexandre Rames67555f72014-11-18 10:55:16 +0000222class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
223 public:
224 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
225
226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
227 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
228 __ Bind(GetEntryLabel());
229 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000230 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800231 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000232 }
233
Alexandre Rames8158f282015-08-07 10:26:17 +0100234 bool IsFatal() const OVERRIDE { return true; }
235
Alexandre Rames9931f312015-06-19 14:47:01 +0100236 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
237
Alexandre Rames67555f72014-11-18 10:55:16 +0000238 private:
239 HDivZeroCheck* const instruction_;
240 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
241};
242
243class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
244 public:
245 LoadClassSlowPathARM64(HLoadClass* cls,
246 HInstruction* at,
247 uint32_t dex_pc,
248 bool do_clinit)
249 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
250 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
251 }
252
253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
254 LocationSummary* locations = at_->GetLocations();
255 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
256
257 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000258 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000259
260 InvokeRuntimeCallingConvention calling_convention;
261 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000262 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
263 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000264 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800265 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100266 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100268 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000270
271 // Move the class to the desired location.
272 Location out = locations->Out();
273 if (out.IsValid()) {
274 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
275 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000276 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 }
278
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000279 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 __ B(GetExitLabel());
281 }
282
Alexandre Rames9931f312015-06-19 14:47:01 +0100283 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
284
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 private:
286 // The class this slow path will load.
287 HLoadClass* const cls_;
288
289 // The instruction where this slow path is happening.
290 // (Might be the load class or an initialization check).
291 HInstruction* const at_;
292
293 // The dex PC of `at_`.
294 const uint32_t dex_pc_;
295
296 // Whether to initialize the class.
297 const bool do_clinit_;
298
299 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
300};
301
302class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
303 public:
304 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
305
306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
307 LocationSummary* locations = instruction_->GetLocations();
308 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
309 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
310
311 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313
314 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800315 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000317 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100318 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000320 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000321
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000322 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 __ B(GetExitLabel());
324 }
325
Alexandre Rames9931f312015-06-19 14:47:01 +0100326 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
327
Alexandre Rames67555f72014-11-18 10:55:16 +0000328 private:
329 HLoadString* const instruction_;
330
331 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
332};
333
Alexandre Rames5319def2014-10-23 10:03:10 +0100334class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
335 public:
336 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
337
Alexandre Rames67555f72014-11-18 10:55:16 +0000338 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
339 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100340 __ Bind(GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000342 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800343 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100344 }
345
Alexandre Rames8158f282015-08-07 10:26:17 +0100346 bool IsFatal() const OVERRIDE { return true; }
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:
Roland Levillain3887c462015-08-12 18:15:42 +0100358 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100359 : instruction_(instruction), successor_(successor) {}
360
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
362 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100363 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000364 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000366 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800367 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000368 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000369 if (successor_ == nullptr) {
370 __ B(GetReturnLabel());
371 } else {
372 __ B(arm64_codegen->GetLabelOf(successor_));
373 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 }
375
376 vixl::Label* GetReturnLabel() {
377 DCHECK(successor_ == nullptr);
378 return &return_label_;
379 }
380
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100381 HBasicBlock* GetSuccessor() const {
382 return successor_;
383 }
384
Alexandre Rames9931f312015-06-19 14:47:01 +0100385 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
386
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 private:
388 HSuspendCheck* const instruction_;
389 // If not null, the block to branch to after the suspend check.
390 HBasicBlock* const successor_;
391
392 // If `successor_` is null, the label to branch to after the suspend check.
393 vixl::Label return_label_;
394
395 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
396};
397
Alexandre Rames67555f72014-11-18 10:55:16 +0000398class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
399 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100400 explicit TypeCheckSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000401
402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000403 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100404 Location class_to_check = locations->InAt(1);
405 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
406 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000407 DCHECK(instruction_->IsCheckCast()
408 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
409 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100410 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000411
Alexandre Rames67555f72014-11-18 10:55:16 +0000412 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000413 SaveLiveRegisters(codegen, locations);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000414
415 // We're moving two locations to locations that could overlap, so we need a parallel
416 // move resolver.
417 InvokeRuntimeCallingConvention calling_convention;
418 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100419 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
420 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000421
422 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000423 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100424 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000425 Primitive::Type ret_type = instruction_->GetType();
426 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
427 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800428 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
429 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430 } else {
431 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100432 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800433 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434 }
435
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000436 RestoreLiveRegisters(codegen, locations);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000437 __ B(GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +0000438 }
439
Alexandre Rames9931f312015-06-19 14:47:01 +0100440 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
441
Alexandre Rames67555f72014-11-18 10:55:16 +0000442 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 HInstruction* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000444
Alexandre Rames67555f72014-11-18 10:55:16 +0000445 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
446};
447
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700448class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
449 public:
450 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
451 : instruction_(instruction) {}
452
453 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
454 __ Bind(GetEntryLabel());
455 SaveLiveRegisters(codegen, instruction_->GetLocations());
456 DCHECK(instruction_->IsDeoptimize());
457 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
458 uint32_t dex_pc = deoptimize->GetDexPc();
459 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
460 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
461 }
462
Alexandre Rames9931f312015-06-19 14:47:01 +0100463 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
464
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700465 private:
466 HInstruction* const instruction_;
467 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
468};
469
Alexandre Rames5319def2014-10-23 10:03:10 +0100470#undef __
471
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100472Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100473 Location next_location;
474 if (type == Primitive::kPrimVoid) {
475 LOG(FATAL) << "Unreachable type " << type;
476 }
477
Alexandre Rames542361f2015-01-29 16:57:31 +0000478 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100479 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
480 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000481 } else if (!Primitive::IsFloatingPointType(type) &&
482 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000483 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
484 } else {
485 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000486 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
487 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100488 }
489
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000490 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000491 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100492 return next_location;
493}
494
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100495Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100496 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100497}
498
Serban Constantinescu579885a2015-02-22 20:51:33 +0000499CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
500 const Arm64InstructionSetFeatures& isa_features,
501 const CompilerOptions& compiler_options)
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 : CodeGenerator(graph,
503 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000504 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000505 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000506 callee_saved_core_registers.list(),
507 callee_saved_fp_registers.list(),
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000508 compiler_options),
Alexandre Rames5319def2014-10-23 10:03:10 +0100509 block_labels_(nullptr),
510 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000512 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000513 isa_features_(isa_features),
514 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
515 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
516 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
517 relative_call_patches_(graph->GetArena()->Adapter()),
518 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000519 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000520 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000521}
Alexandre Rames5319def2014-10-23 10:03:10 +0100522
Alexandre Rames67555f72014-11-18 10:55:16 +0000523#undef __
524#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100525
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000526void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
527 // Ensure we emit the literal pool.
528 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000529
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000530 CodeGenerator::Finalize(allocator);
531}
532
Zheng Xuad4450e2015-04-17 18:48:56 +0800533void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
534 // Note: There are 6 kinds of moves:
535 // 1. constant -> GPR/FPR (non-cycle)
536 // 2. constant -> stack (non-cycle)
537 // 3. GPR/FPR -> GPR/FPR
538 // 4. GPR/FPR -> stack
539 // 5. stack -> GPR/FPR
540 // 6. stack -> stack (non-cycle)
541 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
542 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
543 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
544 // dependency.
545 vixl_temps_.Open(GetVIXLAssembler());
546}
547
548void ParallelMoveResolverARM64::FinishEmitNativeCode() {
549 vixl_temps_.Close();
550}
551
552Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
553 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
554 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
555 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
556 Location scratch = GetScratchLocation(kind);
557 if (!scratch.Equals(Location::NoLocation())) {
558 return scratch;
559 }
560 // Allocate from VIXL temp registers.
561 if (kind == Location::kRegister) {
562 scratch = LocationFrom(vixl_temps_.AcquireX());
563 } else {
564 DCHECK(kind == Location::kFpuRegister);
565 scratch = LocationFrom(vixl_temps_.AcquireD());
566 }
567 AddScratchLocation(scratch);
568 return scratch;
569}
570
571void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
572 if (loc.IsRegister()) {
573 vixl_temps_.Release(XRegisterFrom(loc));
574 } else {
575 DCHECK(loc.IsFpuRegister());
576 vixl_temps_.Release(DRegisterFrom(loc));
577 }
578 RemoveScratchLocation(loc);
579}
580
Alexandre Rames3e69f162014-12-10 10:36:50 +0000581void ParallelMoveResolverARM64::EmitMove(size_t index) {
582 MoveOperands* move = moves_.Get(index);
583 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
584}
585
Alexandre Rames5319def2014-10-23 10:03:10 +0100586void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100587 MacroAssembler* masm = GetVIXLAssembler();
588 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000589 __ Bind(&frame_entry_label_);
590
Serban Constantinescu02164b32014-11-13 14:05:07 +0000591 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
592 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100593 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000594 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000595 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000596 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000597 __ Ldr(wzr, MemOperand(temp, 0));
598 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000599 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100600
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000601 if (!HasEmptyFrame()) {
602 int frame_size = GetFrameSize();
603 // Stack layout:
604 // sp[frame_size - 8] : lr.
605 // ... : other preserved core registers.
606 // ... : other preserved fp registers.
607 // ... : reserved frame space.
608 // sp[0] : current method.
609 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100610 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800611 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
612 frame_size - GetCoreSpillSize());
613 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
614 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000615 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100616}
617
618void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100619 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100620 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000621 if (!HasEmptyFrame()) {
622 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800623 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
624 frame_size - FrameEntrySpillSize());
625 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
626 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000627 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100628 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000629 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100630 __ Ret();
631 GetAssembler()->cfi().RestoreState();
632 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100633}
634
Zheng Xuda403092015-04-24 17:35:39 +0800635vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
636 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
637 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
638 core_spill_mask_);
639}
640
641vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
642 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
643 GetNumberOfFloatingPointRegisters()));
644 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
645 fpu_spill_mask_);
646}
647
Alexandre Rames5319def2014-10-23 10:03:10 +0100648void CodeGeneratorARM64::Bind(HBasicBlock* block) {
649 __ Bind(GetLabelOf(block));
650}
651
Alexandre Rames5319def2014-10-23 10:03:10 +0100652void CodeGeneratorARM64::Move(HInstruction* instruction,
653 Location location,
654 HInstruction* move_for) {
655 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100656 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000657 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100658
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100659 if (instruction->IsFakeString()) {
660 // The fake string is an alias for null.
661 DCHECK(IsBaseline());
662 instruction = locations->Out().GetConstant();
663 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
664 }
665
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100666 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700667 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100668 } else if (locations != nullptr && locations->Out().Equals(location)) {
669 return;
670 } else if (instruction->IsIntConstant()
671 || instruction->IsLongConstant()
672 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000673 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100674 if (location.IsRegister()) {
675 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000676 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100677 (instruction->IsLongConstant() && dst.Is64Bits()));
678 __ Mov(dst, value);
679 } else {
680 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000681 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000682 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
683 ? temps.AcquireW()
684 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100685 __ Mov(temp, value);
686 __ Str(temp, StackOperandFrom(location));
687 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000688 } else if (instruction->IsTemporary()) {
689 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000690 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100691 } else if (instruction->IsLoadLocal()) {
692 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000693 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000694 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000695 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000696 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100697 }
698
699 } else {
700 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000701 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100702 }
703}
704
Alexandre Rames5319def2014-10-23 10:03:10 +0100705Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
706 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000707
Alexandre Rames5319def2014-10-23 10:03:10 +0100708 switch (type) {
709 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000710 case Primitive::kPrimInt:
711 case Primitive::kPrimFloat:
712 return Location::StackSlot(GetStackSlot(load->GetLocal()));
713
714 case Primitive::kPrimLong:
715 case Primitive::kPrimDouble:
716 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
717
Alexandre Rames5319def2014-10-23 10:03:10 +0100718 case Primitive::kPrimBoolean:
719 case Primitive::kPrimByte:
720 case Primitive::kPrimChar:
721 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100722 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100723 LOG(FATAL) << "Unexpected type " << type;
724 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000725
Alexandre Rames5319def2014-10-23 10:03:10 +0100726 LOG(FATAL) << "Unreachable";
727 return Location::NoLocation();
728}
729
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100730void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000731 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000733 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100734 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100735 if (value_can_be_null) {
736 __ Cbz(value, &done);
737 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100738 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
739 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000740 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100741 if (value_can_be_null) {
742 __ Bind(&done);
743 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100744}
745
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000746void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
747 // Blocked core registers:
748 // lr : Runtime reserved.
749 // tr : Runtime reserved.
750 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
751 // ip1 : VIXL core temp.
752 // ip0 : VIXL core temp.
753 //
754 // Blocked fp registers:
755 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100756 CPURegList reserved_core_registers = vixl_reserved_core_registers;
757 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100758 while (!reserved_core_registers.IsEmpty()) {
759 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
760 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000761
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000762 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800763 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000764 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
765 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000766
767 if (is_baseline) {
768 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
769 while (!reserved_core_baseline_registers.IsEmpty()) {
770 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
771 }
772
773 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
774 while (!reserved_fp_baseline_registers.IsEmpty()) {
775 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
776 }
777 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100778}
779
780Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
781 if (type == Primitive::kPrimVoid) {
782 LOG(FATAL) << "Unreachable type " << type;
783 }
784
Alexandre Rames542361f2015-01-29 16:57:31 +0000785 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000786 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
787 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100788 return Location::FpuRegisterLocation(reg);
789 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000790 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
791 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100792 return Location::RegisterLocation(reg);
793 }
794}
795
Alexandre Rames3e69f162014-12-10 10:36:50 +0000796size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
797 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
798 __ Str(reg, MemOperand(sp, stack_index));
799 return kArm64WordSize;
800}
801
802size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
803 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
804 __ Ldr(reg, MemOperand(sp, stack_index));
805 return kArm64WordSize;
806}
807
808size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
809 FPRegister reg = FPRegister(reg_id, kDRegSize);
810 __ Str(reg, MemOperand(sp, stack_index));
811 return kArm64WordSize;
812}
813
814size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
815 FPRegister reg = FPRegister(reg_id, kDRegSize);
816 __ Ldr(reg, MemOperand(sp, stack_index));
817 return kArm64WordSize;
818}
819
Alexandre Rames5319def2014-10-23 10:03:10 +0100820void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100821 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100822}
823
824void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100825 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100826}
827
Alexandre Rames67555f72014-11-18 10:55:16 +0000828void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000829 if (constant->IsIntConstant()) {
830 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
831 } else if (constant->IsLongConstant()) {
832 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
833 } else if (constant->IsNullConstant()) {
834 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000835 } else if (constant->IsFloatConstant()) {
836 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
837 } else {
838 DCHECK(constant->IsDoubleConstant());
839 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
840 }
841}
842
Alexandre Rames3e69f162014-12-10 10:36:50 +0000843
844static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
845 DCHECK(constant.IsConstant());
846 HConstant* cst = constant.GetConstant();
847 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000848 // Null is mapped to a core W register, which we associate with kPrimInt.
849 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000850 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
851 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
852 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
853}
854
855void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000856 if (source.Equals(destination)) {
857 return;
858 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000859
860 // A valid move can always be inferred from the destination and source
861 // locations. When moving from and to a register, the argument type can be
862 // used to generate 32bit instead of 64bit moves. In debug mode we also
863 // checks the coherency of the locations and the type.
864 bool unspecified_type = (type == Primitive::kPrimVoid);
865
866 if (destination.IsRegister() || destination.IsFpuRegister()) {
867 if (unspecified_type) {
868 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
869 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000870 (src_cst != nullptr && (src_cst->IsIntConstant()
871 || src_cst->IsFloatConstant()
872 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000873 // For stack slots and 32bit constants, a 64bit type is appropriate.
874 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000875 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000876 // If the source is a double stack slot or a 64bit constant, a 64bit
877 // type is appropriate. Else the source is a register, and since the
878 // type has not been specified, we chose a 64bit type to force a 64bit
879 // move.
880 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000881 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000882 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
884 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000885 CPURegister dst = CPURegisterFrom(destination, type);
886 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
887 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
888 __ Ldr(dst, StackOperandFrom(source));
889 } else if (source.IsConstant()) {
890 DCHECK(CoherentConstantAndType(source, type));
891 MoveConstant(dst, source.GetConstant());
892 } else {
893 if (destination.IsRegister()) {
894 __ Mov(Register(dst), RegisterFrom(source, type));
895 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800896 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000897 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
898 }
899 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000900 } else { // The destination is not a register. It must be a stack slot.
901 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
902 if (source.IsRegister() || source.IsFpuRegister()) {
903 if (unspecified_type) {
904 if (source.IsRegister()) {
905 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
906 } else {
907 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
908 }
909 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000910 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
911 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000912 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
913 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100914 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000915 UseScratchRegisterScope temps(GetVIXLAssembler());
916 HConstant* src_cst = source.GetConstant();
917 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000918 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000919 temp = temps.AcquireW();
920 } else if (src_cst->IsLongConstant()) {
921 temp = temps.AcquireX();
922 } else if (src_cst->IsFloatConstant()) {
923 temp = temps.AcquireS();
924 } else {
925 DCHECK(src_cst->IsDoubleConstant());
926 temp = temps.AcquireD();
927 }
928 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +0000929 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000930 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +0000931 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000932 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000933 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000934 // There is generally less pressure on FP registers.
935 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000936 __ Ldr(temp, StackOperandFrom(source));
937 __ Str(temp, StackOperandFrom(destination));
938 }
939 }
940}
941
942void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000943 CPURegister dst,
944 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000945 switch (type) {
946 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +0000947 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000948 break;
949 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +0000950 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000951 break;
952 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +0000953 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000954 break;
955 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +0000956 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000957 break;
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot:
960 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000961 case Primitive::kPrimFloat:
962 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +0000963 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +0000964 __ Ldr(dst, src);
965 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000966 case Primitive::kPrimVoid:
967 LOG(FATAL) << "Unreachable type " << type;
968 }
969}
970
Calin Juravle77520bc2015-01-12 18:45:46 +0000971void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000972 CPURegister dst,
973 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100974 MacroAssembler* masm = GetVIXLAssembler();
975 BlockPoolsScope block_pools(masm);
976 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000977 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +0000978 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000979
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000980 DCHECK(!src.IsPreIndex());
981 DCHECK(!src.IsPostIndex());
982
983 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800984 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000985 MemOperand base = MemOperand(temp_base);
986 switch (type) {
987 case Primitive::kPrimBoolean:
988 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000989 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000990 break;
991 case Primitive::kPrimByte:
992 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000993 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000994 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
995 break;
996 case Primitive::kPrimChar:
997 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +0000998 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +0000999 break;
1000 case Primitive::kPrimShort:
1001 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001002 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001003 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1004 break;
1005 case Primitive::kPrimInt:
1006 case Primitive::kPrimNot:
1007 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001008 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001009 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001010 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001011 break;
1012 case Primitive::kPrimFloat:
1013 case Primitive::kPrimDouble: {
1014 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001015 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001016
1017 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1018 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001019 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001020 __ Fmov(FPRegister(dst), temp);
1021 break;
1022 }
1023 case Primitive::kPrimVoid:
1024 LOG(FATAL) << "Unreachable type " << type;
1025 }
1026}
1027
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001028void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001029 CPURegister src,
1030 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001031 switch (type) {
1032 case Primitive::kPrimBoolean:
1033 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001035 break;
1036 case Primitive::kPrimChar:
1037 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001038 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001039 break;
1040 case Primitive::kPrimInt:
1041 case Primitive::kPrimNot:
1042 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001043 case Primitive::kPrimFloat:
1044 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001045 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001046 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001047 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001048 case Primitive::kPrimVoid:
1049 LOG(FATAL) << "Unreachable type " << type;
1050 }
1051}
1052
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001053void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1054 CPURegister src,
1055 const MemOperand& dst) {
1056 UseScratchRegisterScope temps(GetVIXLAssembler());
1057 Register temp_base = temps.AcquireX();
1058
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001059 DCHECK(!dst.IsPreIndex());
1060 DCHECK(!dst.IsPostIndex());
1061
1062 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001063 Operand op = OperandFromMemOperand(dst);
1064 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001065 MemOperand base = MemOperand(temp_base);
1066 switch (type) {
1067 case Primitive::kPrimBoolean:
1068 case Primitive::kPrimByte:
1069 __ Stlrb(Register(src), base);
1070 break;
1071 case Primitive::kPrimChar:
1072 case Primitive::kPrimShort:
1073 __ Stlrh(Register(src), base);
1074 break;
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot:
1077 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001078 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001079 __ Stlr(Register(src), base);
1080 break;
1081 case Primitive::kPrimFloat:
1082 case Primitive::kPrimDouble: {
1083 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001084 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001085
1086 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1087 __ Fmov(temp, FPRegister(src));
1088 __ Stlr(temp, base);
1089 break;
1090 }
1091 case Primitive::kPrimVoid:
1092 LOG(FATAL) << "Unreachable type " << type;
1093 }
1094}
1095
Alexandre Rames67555f72014-11-18 10:55:16 +00001096void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1097 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001098 uint32_t dex_pc,
1099 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001100 ValidateInvokeRuntime(instruction, 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);
Alexandre Rames67555f72014-11-18 10:55:16 +00001105}
1106
1107void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1108 vixl::Register class_reg) {
1109 UseScratchRegisterScope temps(GetVIXLAssembler());
1110 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001111 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001112 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001113
Serban Constantinescu02164b32014-11-13 14:05:07 +00001114 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001115 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001116 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1117 __ Add(temp, class_reg, status_offset);
1118 __ Ldar(temp, HeapOperand(temp));
1119 __ Cmp(temp, mirror::Class::kStatusInitialized);
1120 __ B(lt, slow_path->GetEntryLabel());
1121 } else {
1122 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1123 __ Cmp(temp, mirror::Class::kStatusInitialized);
1124 __ B(lt, slow_path->GetEntryLabel());
1125 __ Dmb(InnerShareable, BarrierReads);
1126 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001127 __ Bind(slow_path->GetExitLabel());
1128}
Alexandre Rames5319def2014-10-23 10:03:10 +01001129
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001130void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1131 BarrierType type = BarrierAll;
1132
1133 switch (kind) {
1134 case MemBarrierKind::kAnyAny:
1135 case MemBarrierKind::kAnyStore: {
1136 type = BarrierAll;
1137 break;
1138 }
1139 case MemBarrierKind::kLoadAny: {
1140 type = BarrierReads;
1141 break;
1142 }
1143 case MemBarrierKind::kStoreStore: {
1144 type = BarrierWrites;
1145 break;
1146 }
1147 default:
1148 LOG(FATAL) << "Unexpected memory barrier " << kind;
1149 }
1150 __ Dmb(InnerShareable, type);
1151}
1152
Serban Constantinescu02164b32014-11-13 14:05:07 +00001153void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1154 HBasicBlock* successor) {
1155 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001156 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1157 if (slow_path == nullptr) {
1158 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1159 instruction->SetSlowPath(slow_path);
1160 codegen_->AddSlowPath(slow_path);
1161 if (successor != nullptr) {
1162 DCHECK(successor->IsLoopHeader());
1163 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1164 }
1165 } else {
1166 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1167 }
1168
Serban Constantinescu02164b32014-11-13 14:05:07 +00001169 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1170 Register temp = temps.AcquireW();
1171
1172 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1173 if (successor == nullptr) {
1174 __ Cbnz(temp, slow_path->GetEntryLabel());
1175 __ Bind(slow_path->GetReturnLabel());
1176 } else {
1177 __ Cbz(temp, codegen_->GetLabelOf(successor));
1178 __ B(slow_path->GetEntryLabel());
1179 // slow_path will return to GetLabelOf(successor).
1180 }
1181}
1182
Alexandre Rames5319def2014-10-23 10:03:10 +01001183InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1184 CodeGeneratorARM64* codegen)
1185 : HGraphVisitor(graph),
1186 assembler_(codegen->GetAssembler()),
1187 codegen_(codegen) {}
1188
1189#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001190 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001191
1192#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1193
1194enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001195 // Using a base helps identify when we hit such breakpoints.
1196 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001197#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1198 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1199#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1200};
1201
1202#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1203 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001204 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001205 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1206 } \
1207 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1208 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1209 locations->SetOut(Location::Any()); \
1210 }
1211 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1212#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1213
1214#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001215#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001216
Alexandre Rames67555f72014-11-18 10:55:16 +00001217void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001218 DCHECK_EQ(instr->InputCount(), 2U);
1219 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1220 Primitive::Type type = instr->GetResultType();
1221 switch (type) {
1222 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001223 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001224 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001225 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001227 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001228
1229 case Primitive::kPrimFloat:
1230 case Primitive::kPrimDouble:
1231 locations->SetInAt(0, Location::RequiresFpuRegister());
1232 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001233 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001234 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001235
Alexandre Rames5319def2014-10-23 10:03:10 +01001236 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001237 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001238 }
1239}
1240
Alexandre Rames09a99962015-04-15 11:47:56 +01001241void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1242 LocationSummary* locations =
1243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1244 locations->SetInAt(0, Location::RequiresRegister());
1245 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1246 locations->SetOut(Location::RequiresFpuRegister());
1247 } else {
1248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1249 }
1250}
1251
1252void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1253 const FieldInfo& field_info) {
1254 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001255 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001256 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001257
1258 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1259 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1260
1261 if (field_info.IsVolatile()) {
1262 if (use_acquire_release) {
1263 // NB: LoadAcquire will record the pc info if needed.
1264 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1265 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001266 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001267 codegen_->MaybeRecordImplicitNullCheck(instruction);
1268 // For IRIW sequential consistency kLoadAny is not sufficient.
1269 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1270 }
1271 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001272 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001273 codegen_->MaybeRecordImplicitNullCheck(instruction);
1274 }
Roland Levillain4d027112015-07-01 15:41:14 +01001275
1276 if (field_type == Primitive::kPrimNot) {
1277 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1278 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001279}
1280
1281void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1282 LocationSummary* locations =
1283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1284 locations->SetInAt(0, Location::RequiresRegister());
1285 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1286 locations->SetInAt(1, Location::RequiresFpuRegister());
1287 } else {
1288 locations->SetInAt(1, Location::RequiresRegister());
1289 }
1290}
1291
1292void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001293 const FieldInfo& field_info,
1294 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001295 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001296 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001297
1298 Register obj = InputRegisterAt(instruction, 0);
1299 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001300 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001301 Offset offset = field_info.GetFieldOffset();
1302 Primitive::Type field_type = field_info.GetFieldType();
1303 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1304
Roland Levillain4d027112015-07-01 15:41:14 +01001305 {
1306 // We use a block to end the scratch scope before the write barrier, thus
1307 // freeing the temporary registers so they can be used in `MarkGCCard`.
1308 UseScratchRegisterScope temps(GetVIXLAssembler());
1309
1310 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1311 DCHECK(value.IsW());
1312 Register temp = temps.AcquireW();
1313 __ Mov(temp, value.W());
1314 GetAssembler()->PoisonHeapReference(temp.W());
1315 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001316 }
Roland Levillain4d027112015-07-01 15:41:14 +01001317
1318 if (field_info.IsVolatile()) {
1319 if (use_acquire_release) {
1320 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1321 codegen_->MaybeRecordImplicitNullCheck(instruction);
1322 } else {
1323 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1324 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1325 codegen_->MaybeRecordImplicitNullCheck(instruction);
1326 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1327 }
1328 } else {
1329 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1330 codegen_->MaybeRecordImplicitNullCheck(instruction);
1331 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001332 }
1333
1334 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001335 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001336 }
1337}
1338
Alexandre Rames67555f72014-11-18 10:55:16 +00001339void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001340 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001341
1342 switch (type) {
1343 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001344 case Primitive::kPrimLong: {
1345 Register dst = OutputRegister(instr);
1346 Register lhs = InputRegisterAt(instr, 0);
1347 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001348 if (instr->IsAdd()) {
1349 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001350 } else if (instr->IsAnd()) {
1351 __ And(dst, lhs, rhs);
1352 } else if (instr->IsOr()) {
1353 __ Orr(dst, lhs, rhs);
1354 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001355 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001356 } else {
1357 DCHECK(instr->IsXor());
1358 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001359 }
1360 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001361 }
1362 case Primitive::kPrimFloat:
1363 case Primitive::kPrimDouble: {
1364 FPRegister dst = OutputFPRegister(instr);
1365 FPRegister lhs = InputFPRegisterAt(instr, 0);
1366 FPRegister rhs = InputFPRegisterAt(instr, 1);
1367 if (instr->IsAdd()) {
1368 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001369 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001370 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001371 } else {
1372 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001373 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001374 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001375 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001377 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001378 }
1379}
1380
Serban Constantinescu02164b32014-11-13 14:05:07 +00001381void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1382 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1383
1384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1385 Primitive::Type type = instr->GetResultType();
1386 switch (type) {
1387 case Primitive::kPrimInt:
1388 case Primitive::kPrimLong: {
1389 locations->SetInAt(0, Location::RequiresRegister());
1390 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1391 locations->SetOut(Location::RequiresRegister());
1392 break;
1393 }
1394 default:
1395 LOG(FATAL) << "Unexpected shift type " << type;
1396 }
1397}
1398
1399void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1400 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1401
1402 Primitive::Type type = instr->GetType();
1403 switch (type) {
1404 case Primitive::kPrimInt:
1405 case Primitive::kPrimLong: {
1406 Register dst = OutputRegister(instr);
1407 Register lhs = InputRegisterAt(instr, 0);
1408 Operand rhs = InputOperandAt(instr, 1);
1409 if (rhs.IsImmediate()) {
1410 uint32_t shift_value = (type == Primitive::kPrimInt)
1411 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1412 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1413 if (instr->IsShl()) {
1414 __ Lsl(dst, lhs, shift_value);
1415 } else if (instr->IsShr()) {
1416 __ Asr(dst, lhs, shift_value);
1417 } else {
1418 __ Lsr(dst, lhs, shift_value);
1419 }
1420 } else {
1421 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1422
1423 if (instr->IsShl()) {
1424 __ Lsl(dst, lhs, rhs_reg);
1425 } else if (instr->IsShr()) {
1426 __ Asr(dst, lhs, rhs_reg);
1427 } else {
1428 __ Lsr(dst, lhs, rhs_reg);
1429 }
1430 }
1431 break;
1432 }
1433 default:
1434 LOG(FATAL) << "Unexpected shift operation type " << type;
1435 }
1436}
1437
Alexandre Rames5319def2014-10-23 10:03:10 +01001438void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001439 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001440}
1441
1442void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001443 HandleBinaryOp(instruction);
1444}
1445
1446void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1447 HandleBinaryOp(instruction);
1448}
1449
1450void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1451 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001452}
1453
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001454void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1455 LocationSummary* locations =
1456 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1457 locations->SetInAt(0, Location::RequiresRegister());
1458 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001459 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1460 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1461 } else {
1462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1463 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001464}
1465
1466void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1467 LocationSummary* locations = instruction->GetLocations();
1468 Primitive::Type type = instruction->GetType();
1469 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001470 Location index = locations->InAt(1);
1471 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001472 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001473 MacroAssembler* masm = GetVIXLAssembler();
1474 UseScratchRegisterScope temps(masm);
1475 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001476
1477 if (index.IsConstant()) {
1478 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001479 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001480 } else {
1481 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001482 __ Add(temp, obj, offset);
1483 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001484 }
1485
Alexandre Rames67555f72014-11-18 10:55:16 +00001486 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001487 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001488
1489 if (type == Primitive::kPrimNot) {
1490 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1491 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001492}
1493
Alexandre Rames5319def2014-10-23 10:03:10 +01001494void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1496 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001498}
1499
1500void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001501 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001502 __ Ldr(OutputRegister(instruction),
1503 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001504 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001505}
1506
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001507void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001508 if (instruction->NeedsTypeCheck()) {
1509 LocationSummary* locations =
1510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001511 InvokeRuntimeCallingConvention calling_convention;
1512 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1513 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1514 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1515 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001516 LocationSummary* locations =
1517 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001518 locations->SetInAt(0, Location::RequiresRegister());
1519 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001520 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1521 locations->SetInAt(2, Location::RequiresFpuRegister());
1522 } else {
1523 locations->SetInAt(2, Location::RequiresRegister());
1524 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001525 }
1526}
1527
1528void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1529 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001530 LocationSummary* locations = instruction->GetLocations();
1531 bool needs_runtime_call = locations->WillCall();
1532
1533 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001534 // Note: if heap poisoning is enabled, pAputObject takes cares
1535 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001536 codegen_->InvokeRuntime(
1537 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001538 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001539 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001540 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001541 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001542 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001543 Location index = locations->InAt(1);
1544 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001545 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001546 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001547 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001548 {
1549 // We use a block to end the scratch scope before the write barrier, thus
1550 // freeing the temporary registers so they can be used in `MarkGCCard`.
1551 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001552
Roland Levillain4d027112015-07-01 15:41:14 +01001553 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1554 DCHECK(value.IsW());
1555 Register temp = temps.AcquireW();
1556 __ Mov(temp, value.W());
1557 GetAssembler()->PoisonHeapReference(temp.W());
1558 source = temp;
1559 }
1560
Alexandre Rames97833a02015-04-16 15:07:12 +01001561 if (index.IsConstant()) {
1562 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1563 destination = HeapOperand(obj, offset);
1564 } else {
1565 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001566 __ Add(temp, obj, offset);
1567 destination = HeapOperand(temp,
1568 XRegisterFrom(index),
1569 LSL,
1570 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001571 }
1572
Roland Levillain4d027112015-07-01 15:41:14 +01001573 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001574 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001575 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001576 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001577 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001578 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001579 }
1580}
1581
Alexandre Rames67555f72014-11-18 10:55:16 +00001582void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
1583 LocationSummary* locations =
1584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1585 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001586 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001587 if (instruction->HasUses()) {
1588 locations->SetOut(Location::SameAsFirstInput());
1589 }
1590}
1591
1592void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001593 BoundsCheckSlowPathARM64* slow_path =
1594 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001595 codegen_->AddSlowPath(slow_path);
1596
1597 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1598 __ B(slow_path->GetEntryLabel(), hs);
1599}
1600
1601void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
1602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1603 instruction, LocationSummary::kCallOnSlowPath);
1604 locations->SetInAt(0, Location::RequiresRegister());
1605 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001606 // Note that TypeCheckSlowPathARM64 uses this register too.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001607 locations->AddTemp(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001608}
1609
1610void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001611 Register obj = InputRegisterAt(instruction, 0);;
1612 Register cls = InputRegisterAt(instruction, 1);;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001613 Register obj_cls = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
Alexandre Rames67555f72014-11-18 10:55:16 +00001614
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001615 SlowPathCodeARM64* slow_path =
1616 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001617 codegen_->AddSlowPath(slow_path);
1618
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01001619 // Avoid null check if we know obj is not null.
1620 if (instruction->MustDoNullCheck()) {
1621 __ Cbz(obj, slow_path->GetExitLabel());
1622 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001623 // Compare the class of `obj` with `cls`.
Alexandre Rames3e69f162014-12-10 10:36:50 +00001624 __ Ldr(obj_cls, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01001625 GetAssembler()->MaybeUnpoisonHeapReference(obj_cls.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001626 __ Cmp(obj_cls, cls);
Roland Levillain4d027112015-07-01 15:41:14 +01001627 // The checkcast succeeds if the classes are equal (fast path).
1628 // Otherwise, we need to go into the slow path to check the types.
Alexandre Rames67555f72014-11-18 10:55:16 +00001629 __ B(ne, slow_path->GetEntryLabel());
1630 __ Bind(slow_path->GetExitLabel());
1631}
1632
1633void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1634 LocationSummary* locations =
1635 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1636 locations->SetInAt(0, Location::RequiresRegister());
1637 if (check->HasUses()) {
1638 locations->SetOut(Location::SameAsFirstInput());
1639 }
1640}
1641
1642void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1643 // We assume the class is not null.
1644 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1645 check->GetLoadClass(), check, check->GetDexPc(), true);
1646 codegen_->AddSlowPath(slow_path);
1647 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1648}
1649
Roland Levillain7f63c522015-07-13 15:54:55 +00001650static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1651 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1652 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1653}
1654
Serban Constantinescu02164b32014-11-13 14:05:07 +00001655void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001656 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001657 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1658 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001659 switch (in_type) {
1660 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001661 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001662 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001663 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1664 break;
1665 }
1666 case Primitive::kPrimFloat:
1667 case Primitive::kPrimDouble: {
1668 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001669 locations->SetInAt(1,
1670 IsFloatingPointZeroConstant(compare->InputAt(1))
1671 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1672 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001673 locations->SetOut(Location::RequiresRegister());
1674 break;
1675 }
1676 default:
1677 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1678 }
1679}
1680
1681void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1682 Primitive::Type in_type = compare->InputAt(0)->GetType();
1683
1684 // 0 if: left == right
1685 // 1 if: left > right
1686 // -1 if: left < right
1687 switch (in_type) {
1688 case Primitive::kPrimLong: {
1689 Register result = OutputRegister(compare);
1690 Register left = InputRegisterAt(compare, 0);
1691 Operand right = InputOperandAt(compare, 1);
1692
1693 __ Cmp(left, right);
1694 __ Cset(result, ne);
1695 __ Cneg(result, result, lt);
1696 break;
1697 }
1698 case Primitive::kPrimFloat:
1699 case Primitive::kPrimDouble: {
1700 Register result = OutputRegister(compare);
1701 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001702 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001703 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1704 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001705 __ Fcmp(left, 0.0);
1706 } else {
1707 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1708 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001709 if (compare->IsGtBias()) {
1710 __ Cset(result, ne);
1711 } else {
1712 __ Csetm(result, ne);
1713 }
1714 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001715 break;
1716 }
1717 default:
1718 LOG(FATAL) << "Unimplemented compare type " << in_type;
1719 }
1720}
1721
1722void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001724
1725 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1726 locations->SetInAt(0, Location::RequiresFpuRegister());
1727 locations->SetInAt(1,
1728 IsFloatingPointZeroConstant(instruction->InputAt(1))
1729 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1730 : Location::RequiresFpuRegister());
1731 } else {
1732 // Integer cases.
1733 locations->SetInAt(0, Location::RequiresRegister());
1734 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1735 }
1736
Alexandre Rames5319def2014-10-23 10:03:10 +01001737 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001739 }
1740}
1741
1742void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1743 if (!instruction->NeedsMaterialization()) {
1744 return;
1745 }
1746
1747 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001748 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001749 IfCondition if_cond = instruction->GetCondition();
1750 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001751
Roland Levillain7f63c522015-07-13 15:54:55 +00001752 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1753 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1754 if (locations->InAt(1).IsConstant()) {
1755 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1756 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1757 __ Fcmp(lhs, 0.0);
1758 } else {
1759 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1760 }
1761 __ Cset(res, arm64_cond);
1762 if (instruction->IsFPConditionTrueIfNaN()) {
1763 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1764 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1765 } else if (instruction->IsFPConditionFalseIfNaN()) {
1766 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1767 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1768 }
1769 } else {
1770 // Integer cases.
1771 Register lhs = InputRegisterAt(instruction, 0);
1772 Operand rhs = InputOperandAt(instruction, 1);
1773 __ Cmp(lhs, rhs);
1774 __ Cset(res, arm64_cond);
1775 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001776}
1777
1778#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1779 M(Equal) \
1780 M(NotEqual) \
1781 M(LessThan) \
1782 M(LessThanOrEqual) \
1783 M(GreaterThan) \
1784 M(GreaterThanOrEqual)
1785#define DEFINE_CONDITION_VISITORS(Name) \
1786void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1787void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1788FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001789#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001790#undef FOR_EACH_CONDITION_INSTRUCTION
1791
Zheng Xuc6667102015-05-15 16:08:45 +08001792void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1793 DCHECK(instruction->IsDiv() || instruction->IsRem());
1794
1795 LocationSummary* locations = instruction->GetLocations();
1796 Location second = locations->InAt(1);
1797 DCHECK(second.IsConstant());
1798
1799 Register out = OutputRegister(instruction);
1800 Register dividend = InputRegisterAt(instruction, 0);
1801 int64_t imm = Int64FromConstant(second.GetConstant());
1802 DCHECK(imm == 1 || imm == -1);
1803
1804 if (instruction->IsRem()) {
1805 __ Mov(out, 0);
1806 } else {
1807 if (imm == 1) {
1808 __ Mov(out, dividend);
1809 } else {
1810 __ Neg(out, dividend);
1811 }
1812 }
1813}
1814
1815void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1816 DCHECK(instruction->IsDiv() || instruction->IsRem());
1817
1818 LocationSummary* locations = instruction->GetLocations();
1819 Location second = locations->InAt(1);
1820 DCHECK(second.IsConstant());
1821
1822 Register out = OutputRegister(instruction);
1823 Register dividend = InputRegisterAt(instruction, 0);
1824 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001825 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001826 DCHECK(IsPowerOfTwo(abs_imm));
1827 int ctz_imm = CTZ(abs_imm);
1828
1829 UseScratchRegisterScope temps(GetVIXLAssembler());
1830 Register temp = temps.AcquireSameSizeAs(out);
1831
1832 if (instruction->IsDiv()) {
1833 __ Add(temp, dividend, abs_imm - 1);
1834 __ Cmp(dividend, 0);
1835 __ Csel(out, temp, dividend, lt);
1836 if (imm > 0) {
1837 __ Asr(out, out, ctz_imm);
1838 } else {
1839 __ Neg(out, Operand(out, ASR, ctz_imm));
1840 }
1841 } else {
1842 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1843 __ Asr(temp, dividend, bits - 1);
1844 __ Lsr(temp, temp, bits - ctz_imm);
1845 __ Add(out, dividend, temp);
1846 __ And(out, out, abs_imm - 1);
1847 __ Sub(out, out, temp);
1848 }
1849}
1850
1851void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1852 DCHECK(instruction->IsDiv() || instruction->IsRem());
1853
1854 LocationSummary* locations = instruction->GetLocations();
1855 Location second = locations->InAt(1);
1856 DCHECK(second.IsConstant());
1857
1858 Register out = OutputRegister(instruction);
1859 Register dividend = InputRegisterAt(instruction, 0);
1860 int64_t imm = Int64FromConstant(second.GetConstant());
1861
1862 Primitive::Type type = instruction->GetResultType();
1863 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1864
1865 int64_t magic;
1866 int shift;
1867 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1868
1869 UseScratchRegisterScope temps(GetVIXLAssembler());
1870 Register temp = temps.AcquireSameSizeAs(out);
1871
1872 // temp = get_high(dividend * magic)
1873 __ Mov(temp, magic);
1874 if (type == Primitive::kPrimLong) {
1875 __ Smulh(temp, dividend, temp);
1876 } else {
1877 __ Smull(temp.X(), dividend, temp);
1878 __ Lsr(temp.X(), temp.X(), 32);
1879 }
1880
1881 if (imm > 0 && magic < 0) {
1882 __ Add(temp, temp, dividend);
1883 } else if (imm < 0 && magic > 0) {
1884 __ Sub(temp, temp, dividend);
1885 }
1886
1887 if (shift != 0) {
1888 __ Asr(temp, temp, shift);
1889 }
1890
1891 if (instruction->IsDiv()) {
1892 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1893 } else {
1894 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1895 // TODO: Strength reduction for msub.
1896 Register temp_imm = temps.AcquireSameSizeAs(out);
1897 __ Mov(temp_imm, imm);
1898 __ Msub(out, temp, temp_imm, dividend);
1899 }
1900}
1901
1902void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1903 DCHECK(instruction->IsDiv() || instruction->IsRem());
1904 Primitive::Type type = instruction->GetResultType();
1905 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1906
1907 LocationSummary* locations = instruction->GetLocations();
1908 Register out = OutputRegister(instruction);
1909 Location second = locations->InAt(1);
1910
1911 if (second.IsConstant()) {
1912 int64_t imm = Int64FromConstant(second.GetConstant());
1913
1914 if (imm == 0) {
1915 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1916 } else if (imm == 1 || imm == -1) {
1917 DivRemOneOrMinusOne(instruction);
1918 } else if (IsPowerOfTwo(std::abs(imm))) {
1919 DivRemByPowerOfTwo(instruction);
1920 } else {
1921 DCHECK(imm <= -2 || imm >= 2);
1922 GenerateDivRemWithAnyConstant(instruction);
1923 }
1924 } else {
1925 Register dividend = InputRegisterAt(instruction, 0);
1926 Register divisor = InputRegisterAt(instruction, 1);
1927 if (instruction->IsDiv()) {
1928 __ Sdiv(out, dividend, divisor);
1929 } else {
1930 UseScratchRegisterScope temps(GetVIXLAssembler());
1931 Register temp = temps.AcquireSameSizeAs(out);
1932 __ Sdiv(temp, dividend, divisor);
1933 __ Msub(out, temp, divisor, dividend);
1934 }
1935 }
1936}
1937
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001938void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1939 LocationSummary* locations =
1940 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1941 switch (div->GetResultType()) {
1942 case Primitive::kPrimInt:
1943 case Primitive::kPrimLong:
1944 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08001945 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1947 break;
1948
1949 case Primitive::kPrimFloat:
1950 case Primitive::kPrimDouble:
1951 locations->SetInAt(0, Location::RequiresFpuRegister());
1952 locations->SetInAt(1, Location::RequiresFpuRegister());
1953 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1954 break;
1955
1956 default:
1957 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1958 }
1959}
1960
1961void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
1962 Primitive::Type type = div->GetResultType();
1963 switch (type) {
1964 case Primitive::kPrimInt:
1965 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08001966 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001967 break;
1968
1969 case Primitive::kPrimFloat:
1970 case Primitive::kPrimDouble:
1971 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
1972 break;
1973
1974 default:
1975 LOG(FATAL) << "Unexpected div type " << type;
1976 }
1977}
1978
Alexandre Rames67555f72014-11-18 10:55:16 +00001979void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1980 LocationSummary* locations =
1981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1982 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1983 if (instruction->HasUses()) {
1984 locations->SetOut(Location::SameAsFirstInput());
1985 }
1986}
1987
1988void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1989 SlowPathCodeARM64* slow_path =
1990 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
1991 codegen_->AddSlowPath(slow_path);
1992 Location value = instruction->GetLocations()->InAt(0);
1993
Alexandre Rames3e69f162014-12-10 10:36:50 +00001994 Primitive::Type type = instruction->GetType();
1995
Serguei Katkov8c0676c2015-08-03 13:55:33 +06001996 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
1997 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00001998 return;
1999 }
2000
Alexandre Rames67555f72014-11-18 10:55:16 +00002001 if (value.IsConstant()) {
2002 int64_t divisor = Int64ConstantFrom(value);
2003 if (divisor == 0) {
2004 __ B(slow_path->GetEntryLabel());
2005 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002006 // A division by a non-null constant is valid. We don't need to perform
2007 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002008 }
2009 } else {
2010 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2011 }
2012}
2013
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002014void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2015 LocationSummary* locations =
2016 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2017 locations->SetOut(Location::ConstantLocation(constant));
2018}
2019
2020void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2021 UNUSED(constant);
2022 // Will be generated at use site.
2023}
2024
Alexandre Rames5319def2014-10-23 10:03:10 +01002025void LocationsBuilderARM64::VisitExit(HExit* exit) {
2026 exit->SetLocations(nullptr);
2027}
2028
2029void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002030 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002031}
2032
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002033void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2036 locations->SetOut(Location::ConstantLocation(constant));
2037}
2038
2039void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2040 UNUSED(constant);
2041 // Will be generated at use site.
2042}
2043
David Brazdilfc6a86a2015-06-26 10:33:45 +00002044void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002045 DCHECK(!successor->IsExitBlock());
2046 HBasicBlock* block = got->GetBlock();
2047 HInstruction* previous = got->GetPrevious();
2048 HLoopInformation* info = block->GetLoopInformation();
2049
David Brazdil46e2a392015-03-16 17:31:52 +00002050 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002051 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2052 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2053 return;
2054 }
2055 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2056 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2057 }
2058 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002059 __ B(codegen_->GetLabelOf(successor));
2060 }
2061}
2062
David Brazdilfc6a86a2015-06-26 10:33:45 +00002063void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2064 got->SetLocations(nullptr);
2065}
2066
2067void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2068 HandleGoto(got, got->GetSuccessor());
2069}
2070
2071void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2072 try_boundary->SetLocations(nullptr);
2073}
2074
2075void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2076 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2077 if (!successor->IsExitBlock()) {
2078 HandleGoto(try_boundary, successor);
2079 }
2080}
2081
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002082void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2083 vixl::Label* true_target,
2084 vixl::Label* false_target,
2085 vixl::Label* always_true_target) {
2086 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002087 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002088
Serban Constantinescu02164b32014-11-13 14:05:07 +00002089 if (cond->IsIntConstant()) {
2090 int32_t cond_value = cond->AsIntConstant()->GetValue();
2091 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002092 if (always_true_target != nullptr) {
2093 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002094 }
2095 return;
2096 } else {
2097 DCHECK_EQ(cond_value, 0);
2098 }
2099 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002100 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002101 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002102 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002103 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002104 } else {
2105 // The condition instruction has not been materialized, use its inputs as
2106 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002107 Primitive::Type type =
2108 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2109
2110 if (Primitive::IsFloatingPointType(type)) {
2111 // FP compares don't like null false_targets.
2112 if (false_target == nullptr) {
2113 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002114 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002115 FPRegister lhs = InputFPRegisterAt(condition, 0);
2116 if (condition->GetLocations()->InAt(1).IsConstant()) {
2117 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2118 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2119 __ Fcmp(lhs, 0.0);
2120 } else {
2121 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2122 }
2123 if (condition->IsFPConditionTrueIfNaN()) {
2124 __ B(vs, true_target); // VS for unordered.
2125 } else if (condition->IsFPConditionFalseIfNaN()) {
2126 __ B(vs, false_target); // VS for unordered.
2127 }
2128 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002129 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002130 // Integer cases.
2131 Register lhs = InputRegisterAt(condition, 0);
2132 Operand rhs = InputOperandAt(condition, 1);
2133 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2134 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2135 switch (arm64_cond) {
2136 case eq:
2137 __ Cbz(lhs, true_target);
2138 break;
2139 case ne:
2140 __ Cbnz(lhs, true_target);
2141 break;
2142 case lt:
2143 // Test the sign bit and branch accordingly.
2144 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2145 break;
2146 case ge:
2147 // Test the sign bit and branch accordingly.
2148 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2149 break;
2150 default:
2151 // Without the `static_cast` the compiler throws an error for
2152 // `-Werror=sign-promo`.
2153 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2154 }
2155 } else {
2156 __ Cmp(lhs, rhs);
2157 __ B(arm64_cond, true_target);
2158 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002159 }
2160 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002161 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002162 __ B(false_target);
2163 }
2164}
2165
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002166void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2168 HInstruction* cond = if_instr->InputAt(0);
2169 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2170 locations->SetInAt(0, Location::RequiresRegister());
2171 }
2172}
2173
2174void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2175 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2176 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2177 vixl::Label* always_true_target = true_target;
2178 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2179 if_instr->IfTrueSuccessor())) {
2180 always_true_target = nullptr;
2181 }
2182 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2183 if_instr->IfFalseSuccessor())) {
2184 false_target = nullptr;
2185 }
2186 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2187}
2188
2189void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2190 LocationSummary* locations = new (GetGraph()->GetArena())
2191 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2192 HInstruction* cond = deoptimize->InputAt(0);
2193 DCHECK(cond->IsCondition());
2194 if (cond->AsCondition()->NeedsMaterialization()) {
2195 locations->SetInAt(0, Location::RequiresRegister());
2196 }
2197}
2198
2199void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2200 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2201 DeoptimizationSlowPathARM64(deoptimize);
2202 codegen_->AddSlowPath(slow_path);
2203 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2204 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2205}
2206
Alexandre Rames5319def2014-10-23 10:03:10 +01002207void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002208 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002209}
2210
2211void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002212 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002213}
2214
2215void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002216 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002217}
2218
2219void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002220 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002221}
2222
Alexandre Rames67555f72014-11-18 10:55:16 +00002223void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
2224 LocationSummary::CallKind call_kind =
2225 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2227 locations->SetInAt(0, Location::RequiresRegister());
2228 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002229 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002230 // Note that TypeCheckSlowPathARM64 uses this register too.
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002231 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexandre Rames67555f72014-11-18 10:55:16 +00002232}
2233
2234void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2235 LocationSummary* locations = instruction->GetLocations();
2236 Register obj = InputRegisterAt(instruction, 0);;
2237 Register cls = InputRegisterAt(instruction, 1);;
2238 Register out = OutputRegister(instruction);
2239
2240 vixl::Label done;
2241
2242 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002243 // Avoid null check if we know `obj` is not null.
2244 if (instruction->MustDoNullCheck()) {
2245 __ Mov(out, 0);
2246 __ Cbz(obj, &done);
2247 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002248
2249 // Compare the class of `obj` with `cls`.
Serban Constantinescu02164b32014-11-13 14:05:07 +00002250 __ Ldr(out, HeapOperand(obj, mirror::Object::ClassOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002251 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002252 __ Cmp(out, cls);
2253 if (instruction->IsClassFinal()) {
2254 // Classes must be equal for the instanceof to succeed.
2255 __ Cset(out, eq);
2256 } else {
2257 // If the classes are not equal, we go into a slow path.
2258 DCHECK(locations->OnlyCallsOnSlowPath());
2259 SlowPathCodeARM64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002260 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002261 codegen_->AddSlowPath(slow_path);
2262 __ B(ne, slow_path->GetEntryLabel());
2263 __ Mov(out, 1);
2264 __ Bind(slow_path->GetExitLabel());
2265 }
2266
2267 __ Bind(&done);
2268}
2269
Alexandre Rames5319def2014-10-23 10:03:10 +01002270void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2272 locations->SetOut(Location::ConstantLocation(constant));
2273}
2274
2275void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2276 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002277 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002278}
2279
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002280void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2281 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2282 locations->SetOut(Location::ConstantLocation(constant));
2283}
2284
2285void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2286 // Will be generated at use site.
2287 UNUSED(constant);
2288}
2289
Alexandre Rames5319def2014-10-23 10:03:10 +01002290void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002291 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002292 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002293}
2294
Alexandre Rames67555f72014-11-18 10:55:16 +00002295void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2296 HandleInvoke(invoke);
2297}
2298
2299void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2300 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002301 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2302 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2303 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002304 Location receiver = invoke->GetLocations()->InAt(0);
2305 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002306 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002307
2308 // The register ip1 is required to be used for the hidden argument in
2309 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002310 MacroAssembler* masm = GetVIXLAssembler();
2311 UseScratchRegisterScope scratch_scope(masm);
2312 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002313 scratch_scope.Exclude(ip1);
2314 __ Mov(ip1, invoke->GetDexMethodIndex());
2315
2316 // temp = object->GetClass();
2317 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002318 __ Ldr(temp.W(), StackOperandFrom(receiver));
2319 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002320 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002321 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002322 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002323 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002324 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002325 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002327 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002328 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002329 // lr();
2330 __ Blr(lr);
2331 DCHECK(!codegen_->IsLeafMethod());
2332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2333}
2334
2335void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002336 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2337 if (intrinsic.TryDispatch(invoke)) {
2338 return;
2339 }
2340
Alexandre Rames67555f72014-11-18 10:55:16 +00002341 HandleInvoke(invoke);
2342}
2343
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002344void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002345 // When we do not run baseline, explicit clinit checks triggered by static
2346 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2347 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002348
Andreas Gampe878d58c2015-01-15 23:24:00 -08002349 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2350 if (intrinsic.TryDispatch(invoke)) {
2351 return;
2352 }
2353
Alexandre Rames67555f72014-11-18 10:55:16 +00002354 HandleInvoke(invoke);
2355}
2356
Andreas Gampe878d58c2015-01-15 23:24:00 -08002357static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2358 if (invoke->GetLocations()->Intrinsified()) {
2359 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2360 intrinsic.Dispatch(invoke);
2361 return true;
2362 }
2363 return false;
2364}
2365
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002366void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002367 // For better instruction scheduling we load the direct code pointer before the method pointer.
2368 bool direct_code_loaded = false;
2369 switch (invoke->GetCodePtrLocation()) {
2370 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2371 // LR = code address from literal pool with link-time patch.
2372 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2373 direct_code_loaded = true;
2374 break;
2375 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2376 // LR = invoke->GetDirectCodePtr();
2377 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2378 direct_code_loaded = true;
2379 break;
2380 default:
2381 break;
2382 }
2383
Andreas Gampe878d58c2015-01-15 23:24:00 -08002384 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002385 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2386 switch (invoke->GetMethodLoadKind()) {
2387 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2388 // temp = thread->string_init_entrypoint
2389 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2390 break;
2391 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2392 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2393 break;
2394 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2395 // Load method address from literal pool.
2396 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2397 break;
2398 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2399 // Load method address from literal pool with a link-time patch.
2400 __ Ldr(XRegisterFrom(temp).X(),
2401 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2402 break;
2403 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2404 // Add ADRP with its PC-relative DexCache access patch.
2405 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2406 invoke->GetDexCacheArrayOffset());
2407 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2408 {
2409 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2410 __ adrp(XRegisterFrom(temp).X(), 0);
2411 }
2412 __ Bind(pc_insn_label); // Bind after ADRP.
2413 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2414 // Add LDR with its PC-relative DexCache access patch.
2415 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2416 invoke->GetDexCacheArrayOffset());
2417 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2418 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2419 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2420 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002421 }
Vladimir Marko58155012015-08-19 12:49:41 +00002422 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2423 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2424 Register reg = XRegisterFrom(temp);
2425 Register method_reg;
2426 if (current_method.IsRegister()) {
2427 method_reg = XRegisterFrom(current_method);
2428 } else {
2429 DCHECK(invoke->GetLocations()->Intrinsified());
2430 DCHECK(!current_method.IsValid());
2431 method_reg = reg;
2432 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2433 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002434
Vladimir Marko58155012015-08-19 12:49:41 +00002435 // temp = current_method->dex_cache_resolved_methods_;
2436 __ Ldr(reg.W(), MemOperand(method_reg.X(),
2437 ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
2438 // temp = temp[index_in_cache];
2439 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2440 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2441 break;
2442 }
2443 }
2444
2445 switch (invoke->GetCodePtrLocation()) {
2446 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2447 __ Bl(&frame_entry_label_);
2448 break;
2449 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2450 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2451 vixl::Label* label = &relative_call_patches_.back().label;
2452 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2453 __ Bind(label); // Bind after BL.
2454 break;
2455 }
2456 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2457 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2458 // LR prepared above for better instruction scheduling.
2459 DCHECK(direct_code_loaded);
2460 // lr()
2461 __ Blr(lr);
2462 break;
2463 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2464 // LR = callee_method->entry_point_from_quick_compiled_code_;
2465 __ Ldr(lr, MemOperand(
2466 XRegisterFrom(callee_method).X(),
2467 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2468 // lr()
2469 __ Blr(lr);
2470 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002471 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002472
Andreas Gampe878d58c2015-01-15 23:24:00 -08002473 DCHECK(!IsLeafMethod());
2474}
2475
Vladimir Marko58155012015-08-19 12:49:41 +00002476void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2477 DCHECK(linker_patches->empty());
2478 size_t size =
2479 method_patches_.size() +
2480 call_patches_.size() +
2481 relative_call_patches_.size() +
2482 pc_rel_dex_cache_patches_.size();
2483 linker_patches->reserve(size);
2484 for (const auto& entry : method_patches_) {
2485 const MethodReference& target_method = entry.first;
2486 vixl::Literal<uint64_t>* literal = entry.second;
2487 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2488 target_method.dex_file,
2489 target_method.dex_method_index));
2490 }
2491 for (const auto& entry : call_patches_) {
2492 const MethodReference& target_method = entry.first;
2493 vixl::Literal<uint64_t>* literal = entry.second;
2494 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2495 target_method.dex_file,
2496 target_method.dex_method_index));
2497 }
2498 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2499 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2500 info.target_method.dex_file,
2501 info.target_method.dex_method_index));
2502 }
2503 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2504 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2505 &info.target_dex_file,
2506 info.pc_insn_label->location() - 4u,
2507 info.element_offset));
2508 }
2509}
2510
2511vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2512 // Look up the literal for value.
2513 auto lb = uint64_literals_.lower_bound(value);
2514 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2515 return lb->second;
2516 }
2517 // We don't have a literal for this value, insert a new one.
2518 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2519 uint64_literals_.PutBefore(lb, value, literal);
2520 return literal;
2521}
2522
2523vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2524 MethodReference target_method,
2525 MethodToLiteralMap* map) {
2526 // Look up the literal for target_method.
2527 auto lb = map->lower_bound(target_method);
2528 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2529 return lb->second;
2530 }
2531 // We don't have a literal for this method yet, insert a new one.
2532 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2533 map->PutBefore(lb, target_method, literal);
2534 return literal;
2535}
2536
2537vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2538 MethodReference target_method) {
2539 return DeduplicateMethodLiteral(target_method, &method_patches_);
2540}
2541
2542vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2543 MethodReference target_method) {
2544 return DeduplicateMethodLiteral(target_method, &call_patches_);
2545}
2546
2547
Andreas Gampe878d58c2015-01-15 23:24:00 -08002548void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002549 // When we do not run baseline, explicit clinit checks triggered by static
2550 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2551 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002552
Andreas Gampe878d58c2015-01-15 23:24:00 -08002553 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2554 return;
2555 }
2556
Alexandre Ramesd921d642015-04-16 15:07:16 +01002557 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002558 LocationSummary* locations = invoke->GetLocations();
2559 codegen_->GenerateStaticOrDirectCall(
2560 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002561 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002562}
2563
2564void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002565 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2566 return;
2567 }
2568
Alexandre Rames5319def2014-10-23 10:03:10 +01002569 LocationSummary* locations = invoke->GetLocations();
2570 Location receiver = locations->InAt(0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002571 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2572 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2573 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
Alexandre Rames5319def2014-10-23 10:03:10 +01002574 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002575 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames5319def2014-10-23 10:03:10 +01002576
Alexandre Ramesd921d642015-04-16 15:07:16 +01002577 BlockPoolsScope block_pools(GetVIXLAssembler());
2578
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002579 DCHECK(receiver.IsRegister());
2580 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002581 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002582 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames5319def2014-10-23 10:03:10 +01002583 // temp = temp->GetMethodAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002584 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames5319def2014-10-23 10:03:10 +01002585 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002586 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002587 // lr();
2588 __ Blr(lr);
2589 DCHECK(!codegen_->IsLeafMethod());
2590 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2591}
2592
Alexandre Rames67555f72014-11-18 10:55:16 +00002593void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2594 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2595 : LocationSummary::kNoCall;
2596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002597 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002598 locations->SetOut(Location::RequiresRegister());
2599}
2600
2601void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2602 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002603 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002604 if (cls->IsReferrersClass()) {
2605 DCHECK(!cls->CanCallRuntime());
2606 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002607 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002608 } else {
2609 DCHECK(cls->CanCallRuntime());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002610 __ Ldr(out, MemOperand(current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002611 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002612 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002613
2614 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2615 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2616 codegen_->AddSlowPath(slow_path);
2617 __ Cbz(out, slow_path->GetEntryLabel());
2618 if (cls->MustGenerateClinitCheck()) {
2619 GenerateClassInitializationCheck(slow_path, out);
2620 } else {
2621 __ Bind(slow_path->GetExitLabel());
2622 }
2623 }
2624}
2625
David Brazdilcb1c0552015-08-04 16:22:25 +01002626static MemOperand GetExceptionTlsAddress() {
2627 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2628}
2629
Alexandre Rames67555f72014-11-18 10:55:16 +00002630void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2633 locations->SetOut(Location::RequiresRegister());
2634}
2635
2636void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002637 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2638}
2639
2640void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2641 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2642}
2643
2644void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2645 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002646}
2647
Alexandre Rames5319def2014-10-23 10:03:10 +01002648void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2649 load->SetLocations(nullptr);
2650}
2651
2652void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2653 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002654 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002655}
2656
Alexandre Rames67555f72014-11-18 10:55:16 +00002657void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2658 LocationSummary* locations =
2659 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002660 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002661 locations->SetOut(Location::RequiresRegister());
2662}
2663
2664void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2665 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2666 codegen_->AddSlowPath(slow_path);
2667
2668 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002669 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002670 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08002671 __ Ldr(out, HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
Roland Levillain4d027112015-07-01 15:41:14 +01002672 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002673 __ Ldr(out, HeapOperand(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01002674 GetAssembler()->MaybeUnpoisonHeapReference(out.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002675 __ Cbz(out, slow_path->GetEntryLabel());
2676 __ Bind(slow_path->GetExitLabel());
2677}
2678
Alexandre Rames5319def2014-10-23 10:03:10 +01002679void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2680 local->SetLocations(nullptr);
2681}
2682
2683void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2684 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2685}
2686
2687void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
2688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2689 locations->SetOut(Location::ConstantLocation(constant));
2690}
2691
2692void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
2693 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002694 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002695}
2696
Alexandre Rames67555f72014-11-18 10:55:16 +00002697void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2700 InvokeRuntimeCallingConvention calling_convention;
2701 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2702}
2703
2704void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
2705 codegen_->InvokeRuntime(instruction->IsEnter()
2706 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
2707 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002708 instruction->GetDexPc(),
2709 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002710 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00002711}
2712
Alexandre Rames42d641b2014-10-27 14:00:51 +00002713void LocationsBuilderARM64::VisitMul(HMul* mul) {
2714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2716 switch (mul->GetResultType()) {
2717 case Primitive::kPrimInt:
2718 case Primitive::kPrimLong:
2719 locations->SetInAt(0, Location::RequiresRegister());
2720 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002721 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002722 break;
2723
2724 case Primitive::kPrimFloat:
2725 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002726 locations->SetInAt(0, Location::RequiresFpuRegister());
2727 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002728 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00002729 break;
2730
2731 default:
2732 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2733 }
2734}
2735
2736void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
2737 switch (mul->GetResultType()) {
2738 case Primitive::kPrimInt:
2739 case Primitive::kPrimLong:
2740 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
2741 break;
2742
2743 case Primitive::kPrimFloat:
2744 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002745 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00002746 break;
2747
2748 default:
2749 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2750 }
2751}
2752
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002753void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
2754 LocationSummary* locations =
2755 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2756 switch (neg->GetResultType()) {
2757 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00002758 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002759 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00002760 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002761 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002762
2763 case Primitive::kPrimFloat:
2764 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002765 locations->SetInAt(0, Location::RequiresFpuRegister());
2766 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002767 break;
2768
2769 default:
2770 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2771 }
2772}
2773
2774void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
2775 switch (neg->GetResultType()) {
2776 case Primitive::kPrimInt:
2777 case Primitive::kPrimLong:
2778 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
2779 break;
2780
2781 case Primitive::kPrimFloat:
2782 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00002783 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002784 break;
2785
2786 default:
2787 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2788 }
2789}
2790
2791void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
2792 LocationSummary* locations =
2793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2794 InvokeRuntimeCallingConvention calling_convention;
2795 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002796 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002797 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002798 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002799 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002800 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002801}
2802
2803void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
2804 LocationSummary* locations = instruction->GetLocations();
2805 InvokeRuntimeCallingConvention calling_convention;
2806 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2807 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002808 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002809 // Note: if heap poisoning is enabled, the entry point takes cares
2810 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002811 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002812 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2813 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002814 instruction->GetDexPc(),
2815 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002816 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002817}
2818
Alexandre Rames5319def2014-10-23 10:03:10 +01002819void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
2820 LocationSummary* locations =
2821 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2822 InvokeRuntimeCallingConvention calling_convention;
2823 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002824 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01002825 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002826 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002827}
2828
2829void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
2830 LocationSummary* locations = instruction->GetLocations();
2831 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
2832 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002833 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002834 // Note: if heap poisoning is enabled, the entry point takes cares
2835 // of poisoning the reference.
Alexandre Rames67555f72014-11-18 10:55:16 +00002836 codegen_->InvokeRuntime(
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002837 GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
2838 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002839 instruction->GetDexPc(),
2840 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002841 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01002842}
2843
2844void LocationsBuilderARM64::VisitNot(HNot* instruction) {
2845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00002846 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002848}
2849
2850void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002851 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002852 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002853 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01002854 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002855 break;
2856
2857 default:
2858 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2859 }
2860}
2861
David Brazdil66d126e2015-04-03 16:02:44 +01002862void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
2863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2864 locations->SetInAt(0, Location::RequiresRegister());
2865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2866}
2867
2868void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01002869 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
2870}
2871
Alexandre Rames5319def2014-10-23 10:03:10 +01002872void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
2873 LocationSummary* locations =
2874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2875 locations->SetInAt(0, Location::RequiresRegister());
2876 if (instruction->HasUses()) {
2877 locations->SetOut(Location::SameAsFirstInput());
2878 }
2879}
2880
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002881void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002882 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2883 return;
2884 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002885
Alexandre Ramesd921d642015-04-16 15:07:16 +01002886 BlockPoolsScope block_pools(GetVIXLAssembler());
2887 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002888 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
2889 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2890}
2891
2892void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002893 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
2894 codegen_->AddSlowPath(slow_path);
2895
2896 LocationSummary* locations = instruction->GetLocations();
2897 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002898
2899 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01002900}
2901
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002902void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
2903 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2904 GenerateImplicitNullCheck(instruction);
2905 } else {
2906 GenerateExplicitNullCheck(instruction);
2907 }
2908}
2909
Alexandre Rames67555f72014-11-18 10:55:16 +00002910void LocationsBuilderARM64::VisitOr(HOr* instruction) {
2911 HandleBinaryOp(instruction);
2912}
2913
2914void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
2915 HandleBinaryOp(instruction);
2916}
2917
Alexandre Rames3e69f162014-12-10 10:36:50 +00002918void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2919 LOG(FATAL) << "Unreachable";
2920}
2921
2922void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
2923 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2924}
2925
Alexandre Rames5319def2014-10-23 10:03:10 +01002926void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
2927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2928 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2929 if (location.IsStackSlot()) {
2930 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2931 } else if (location.IsDoubleStackSlot()) {
2932 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2933 }
2934 locations->SetOut(location);
2935}
2936
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002937void InstructionCodeGeneratorARM64::VisitParameterValue(
2938 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002939 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002940}
2941
2942void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
2943 LocationSummary* locations =
2944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002945 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002946}
2947
2948void InstructionCodeGeneratorARM64::VisitCurrentMethod(
2949 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
2950 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01002951}
2952
2953void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
2954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2955 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2956 locations->SetInAt(i, Location::Any());
2957 }
2958 locations->SetOut(Location::Any());
2959}
2960
2961void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002962 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002963 LOG(FATAL) << "Unreachable";
2964}
2965
Serban Constantinescu02164b32014-11-13 14:05:07 +00002966void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002967 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00002968 LocationSummary::CallKind call_kind =
2969 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2971
2972 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002973 case Primitive::kPrimInt:
2974 case Primitive::kPrimLong:
2975 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002976 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002977 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2978 break;
2979
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002980 case Primitive::kPrimFloat:
2981 case Primitive::kPrimDouble: {
2982 InvokeRuntimeCallingConvention calling_convention;
2983 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
2984 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
2985 locations->SetOut(calling_convention.GetReturnLocation(type));
2986
2987 break;
2988 }
2989
Serban Constantinescu02164b32014-11-13 14:05:07 +00002990 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002991 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00002992 }
2993}
2994
2995void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
2996 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002997
Serban Constantinescu02164b32014-11-13 14:05:07 +00002998 switch (type) {
2999 case Primitive::kPrimInt:
3000 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003001 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003002 break;
3003 }
3004
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003005 case Primitive::kPrimFloat:
3006 case Primitive::kPrimDouble: {
3007 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3008 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003009 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003010 break;
3011 }
3012
Serban Constantinescu02164b32014-11-13 14:05:07 +00003013 default:
3014 LOG(FATAL) << "Unexpected rem type " << type;
3015 }
3016}
3017
Calin Juravle27df7582015-04-17 19:12:31 +01003018void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3019 memory_barrier->SetLocations(nullptr);
3020}
3021
3022void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3023 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3024}
3025
Alexandre Rames5319def2014-10-23 10:03:10 +01003026void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3028 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003029 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003030}
3031
3032void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003033 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003034 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003035}
3036
3037void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3038 instruction->SetLocations(nullptr);
3039}
3040
3041void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003042 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003043 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003044}
3045
Serban Constantinescu02164b32014-11-13 14:05:07 +00003046void LocationsBuilderARM64::VisitShl(HShl* shl) {
3047 HandleShift(shl);
3048}
3049
3050void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3051 HandleShift(shl);
3052}
3053
3054void LocationsBuilderARM64::VisitShr(HShr* shr) {
3055 HandleShift(shr);
3056}
3057
3058void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3059 HandleShift(shr);
3060}
3061
Alexandre Rames5319def2014-10-23 10:03:10 +01003062void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3064 Primitive::Type field_type = store->InputAt(1)->GetType();
3065 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003066 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003067 case Primitive::kPrimBoolean:
3068 case Primitive::kPrimByte:
3069 case Primitive::kPrimChar:
3070 case Primitive::kPrimShort:
3071 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003072 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003073 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3074 break;
3075
3076 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003077 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003078 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3079 break;
3080
3081 default:
3082 LOG(FATAL) << "Unimplemented local type " << field_type;
3083 }
3084}
3085
3086void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003087 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003088}
3089
3090void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003091 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003092}
3093
3094void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003095 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003096}
3097
Alexandre Rames67555f72014-11-18 10:55:16 +00003098void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003099 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003100}
3101
3102void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003103 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003104}
3105
3106void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003107 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003108}
3109
Alexandre Rames67555f72014-11-18 10:55:16 +00003110void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003111 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003112}
3113
3114void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3115 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3116}
3117
3118void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003119 HBasicBlock* block = instruction->GetBlock();
3120 if (block->GetLoopInformation() != nullptr) {
3121 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3122 // The back edge will generate the suspend check.
3123 return;
3124 }
3125 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3126 // The goto will generate the suspend check.
3127 return;
3128 }
3129 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003130}
3131
3132void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3133 temp->SetLocations(nullptr);
3134}
3135
3136void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3137 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003138 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003139}
3140
Alexandre Rames67555f72014-11-18 10:55:16 +00003141void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3142 LocationSummary* locations =
3143 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3144 InvokeRuntimeCallingConvention calling_convention;
3145 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3146}
3147
3148void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3149 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003150 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003151 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003152}
3153
3154void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3155 LocationSummary* locations =
3156 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3157 Primitive::Type input_type = conversion->GetInputType();
3158 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003159 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003160 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3161 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3162 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3163 }
3164
Alexandre Rames542361f2015-01-29 16:57:31 +00003165 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003166 locations->SetInAt(0, Location::RequiresFpuRegister());
3167 } else {
3168 locations->SetInAt(0, Location::RequiresRegister());
3169 }
3170
Alexandre Rames542361f2015-01-29 16:57:31 +00003171 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003172 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3173 } else {
3174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3175 }
3176}
3177
3178void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3179 Primitive::Type result_type = conversion->GetResultType();
3180 Primitive::Type input_type = conversion->GetInputType();
3181
3182 DCHECK_NE(input_type, result_type);
3183
Alexandre Rames542361f2015-01-29 16:57:31 +00003184 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003185 int result_size = Primitive::ComponentSize(result_type);
3186 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003187 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003188 Register output = OutputRegister(conversion);
3189 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003190 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3191 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003192 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3193 // 'int' values are used directly as W registers, discarding the top
3194 // bits, so we don't need to sign-extend and can just perform a move.
3195 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3196 // top 32 bits of the target register. We theoretically could leave those
3197 // bits unchanged, but we would have to make sure that no code uses a
3198 // 32bit input value as a 64bit value assuming that the top 32 bits are
3199 // zero.
3200 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003201 } else if ((result_type == Primitive::kPrimChar) ||
3202 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3203 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003204 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003205 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003206 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003207 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003208 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003209 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003210 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3211 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003212 } else if (Primitive::IsFloatingPointType(result_type) &&
3213 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003214 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3215 } else {
3216 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3217 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003218 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003219}
Alexandre Rames67555f72014-11-18 10:55:16 +00003220
Serban Constantinescu02164b32014-11-13 14:05:07 +00003221void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3222 HandleShift(ushr);
3223}
3224
3225void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3226 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003227}
3228
3229void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3230 HandleBinaryOp(instruction);
3231}
3232
3233void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3234 HandleBinaryOp(instruction);
3235}
3236
Calin Juravleb1498f62015-02-16 13:13:29 +00003237void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3238 // Nothing to do, this should be removed during prepare for register allocator.
3239 UNUSED(instruction);
3240 LOG(FATAL) << "Unreachable";
3241}
3242
3243void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3244 // Nothing to do, this should be removed during prepare for register allocator.
3245 UNUSED(instruction);
3246 LOG(FATAL) << "Unreachable";
3247}
3248
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003249void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3250 DCHECK(codegen_->IsBaseline());
3251 LocationSummary* locations =
3252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3253 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3254}
3255
3256void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3257 DCHECK(codegen_->IsBaseline());
3258 // Will be generated at use site.
3259}
3260
Alexandre Rames67555f72014-11-18 10:55:16 +00003261#undef __
3262#undef QUICK_ENTRY_POINT
3263
Alexandre Rames5319def2014-10-23 10:03:10 +01003264} // namespace arm64
3265} // namespace art