blob: 07758e9df719df20bd011228642642f853599ed9 [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"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100193 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100194
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000197 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100198
Alexandre Rames5319def2014-10-23 10:03:10 +0100199 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100208 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
223};
224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
226 public:
227 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
231 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000232 if (instruction_->CanThrowIntoCatchBlock()) {
233 // Live registers will be restored in the catch block if caught.
234 SaveLiveRegisters(codegen, instruction_->GetLocations());
235 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000237 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800238 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 }
240
Alexandre Rames8158f282015-08-07 10:26:17 +0100241 bool IsFatal() const OVERRIDE { return true; }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
244
Alexandre Rames67555f72014-11-18 10:55:16 +0000245 private:
246 HDivZeroCheck* const instruction_;
247 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
248};
249
250class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
252 LoadClassSlowPathARM64(HLoadClass* cls,
253 HInstruction* at,
254 uint32_t dex_pc,
255 bool do_clinit)
256 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
257 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
258 }
259
260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
261 LocationSummary* locations = at_->GetLocations();
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
268 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
270 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 // Move the class to the desired location.
279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000283 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 }
285
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 __ B(GetExitLabel());
288 }
289
Alexandre Rames9931f312015-06-19 14:47:01 +0100290 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 private:
293 // The class this slow path will load.
294 HLoadClass* const cls_;
295
296 // The instruction where this slow path is happening.
297 // (Might be the load class or an initialization check).
298 HInstruction* const at_;
299
300 // The dex PC of `at_`.
301 const uint32_t dex_pc_;
302
303 // Whether to initialize the class.
304 const bool do_clinit_;
305
306 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
307};
308
309class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
312
313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
314 LocationSummary* locations = instruction_->GetLocations();
315 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
316 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
317
318 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320
321 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000324 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100325 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000327 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000329 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 __ B(GetExitLabel());
331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
334
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 private:
336 HLoadString* const instruction_;
337
338 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
339};
340
Alexandre Rames5319def2014-10-23 10:03:10 +0100341class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
342 public:
343 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
344
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100347 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000348 if (instruction_->CanThrowIntoCatchBlock()) {
349 // Live registers will be restored in the catch block if caught.
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 }
356
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 bool IsFatal() const OVERRIDE { return true; }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361 private:
362 HNullCheck* const instruction_;
363
364 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
365};
366
367class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100369 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100370 : instruction_(instruction), successor_(successor) {}
371
Alexandre Rames67555f72014-11-18 10:55:16 +0000372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000377 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800378 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000379 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 if (successor_ == nullptr) {
381 __ B(GetReturnLabel());
382 } else {
383 __ B(arm64_codegen->GetLabelOf(successor_));
384 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 }
386
387 vixl::Label* GetReturnLabel() {
388 DCHECK(successor_ == nullptr);
389 return &return_label_;
390 }
391
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100392 HBasicBlock* GetSuccessor() const {
393 return successor_;
394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
397
Alexandre Rames5319def2014-10-23 10:03:10 +0100398 private:
399 HSuspendCheck* const instruction_;
400 // If not null, the block to branch to after the suspend check.
401 HBasicBlock* const successor_;
402
403 // If `successor_` is null, the label to branch to after the suspend check.
404 vixl::Label return_label_;
405
406 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
407};
408
Alexandre Rames67555f72014-11-18 10:55:16 +0000409class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000411 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
412 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100416 Location class_to_check = locations->InAt(1);
417 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
418 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000419 DCHECK(instruction_->IsCheckCast()
420 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
421 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100422 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
Alexandre Rames67555f72014-11-18 10:55:16 +0000424 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000425
426 if (instruction_->IsCheckCast()) {
427 // The codegen for the instruction overwrites `temp`, so put it back in place.
428 Register obj = InputRegisterAt(instruction_, 0);
429 Register temp = WRegisterFrom(locations->GetTemp(0));
430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
431 __ Ldr(temp, HeapOperand(obj, class_offset));
432 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
433 }
434
435 if (!is_fatal_) {
436 SaveLiveRegisters(codegen, locations);
437 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438
439 // We're moving two locations to locations that could overlap, so we need a parallel
440 // move resolver.
441 InvokeRuntimeCallingConvention calling_convention;
442 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100443 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
444 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445
446 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000447 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100448 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449 Primitive::Type ret_type = instruction_->GetType();
450 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
451 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800452 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
453 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000454 } else {
455 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800457 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458 }
459
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000460 if (!is_fatal_) {
461 RestoreLiveRegisters(codegen, locations);
462 __ B(GetExitLabel());
463 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 }
465
Alexandre Rames9931f312015-06-19 14:47:01 +0100466 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000467 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100468
Alexandre Rames67555f72014-11-18 10:55:16 +0000469 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000470 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
474};
475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700476class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
477 public:
478 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100479 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 __ Bind(GetEntryLabel());
483 SaveLiveRegisters(codegen, instruction_->GetLocations());
484 DCHECK(instruction_->IsDeoptimize());
485 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
486 uint32_t dex_pc = deoptimize->GetDexPc();
487 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
488 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
489 }
490
Alexandre Rames9931f312015-06-19 14:47:01 +0100491 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
492
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493 private:
494 HInstruction* const instruction_;
495 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
496};
497
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100498class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
499 public:
500 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
501
502 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
503 LocationSummary* locations = instruction_->GetLocations();
504 __ Bind(GetEntryLabel());
505 SaveLiveRegisters(codegen, locations);
506
507 InvokeRuntimeCallingConvention calling_convention;
508 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
509 parallel_move.AddMove(
510 locations->InAt(0),
511 LocationFrom(calling_convention.GetRegisterAt(0)),
512 Primitive::kPrimNot,
513 nullptr);
514 parallel_move.AddMove(
515 locations->InAt(1),
516 LocationFrom(calling_convention.GetRegisterAt(1)),
517 Primitive::kPrimInt,
518 nullptr);
519 parallel_move.AddMove(
520 locations->InAt(2),
521 LocationFrom(calling_convention.GetRegisterAt(2)),
522 Primitive::kPrimNot,
523 nullptr);
524 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
525
526 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
527 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
528 instruction_,
529 instruction_->GetDexPc(),
530 this);
531 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
532 RestoreLiveRegisters(codegen, locations);
533 __ B(GetExitLabel());
534 }
535
536 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
537
538 private:
539 HInstruction* const instruction_;
540
541 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
542};
543
Alexandre Rames5319def2014-10-23 10:03:10 +0100544#undef __
545
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100546Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100547 Location next_location;
548 if (type == Primitive::kPrimVoid) {
549 LOG(FATAL) << "Unreachable type " << type;
550 }
551
Alexandre Rames542361f2015-01-29 16:57:31 +0000552 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100553 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
554 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000555 } else if (!Primitive::IsFloatingPointType(type) &&
556 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000557 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
558 } else {
559 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000560 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
561 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100562 }
563
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000564 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000565 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100566 return next_location;
567}
568
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100569Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100570 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100571}
572
Serban Constantinescu579885a2015-02-22 20:51:33 +0000573CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
574 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100575 const CompilerOptions& compiler_options,
576 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100577 : CodeGenerator(graph,
578 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000579 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000580 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000581 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000582 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100583 compiler_options,
584 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100585 block_labels_(nullptr),
586 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000587 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000588 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000589 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100590 uint64_literals_(std::less<uint64_t>(),
591 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
592 method_patches_(MethodReferenceComparator(),
593 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
594 call_patches_(MethodReferenceComparator(),
595 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
596 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
597 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000598 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000599 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000600}
Alexandre Rames5319def2014-10-23 10:03:10 +0100601
Alexandre Rames67555f72014-11-18 10:55:16 +0000602#undef __
603#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100604
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000605void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
606 // Ensure we emit the literal pool.
607 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000608
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000609 CodeGenerator::Finalize(allocator);
610}
611
Zheng Xuad4450e2015-04-17 18:48:56 +0800612void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
613 // Note: There are 6 kinds of moves:
614 // 1. constant -> GPR/FPR (non-cycle)
615 // 2. constant -> stack (non-cycle)
616 // 3. GPR/FPR -> GPR/FPR
617 // 4. GPR/FPR -> stack
618 // 5. stack -> GPR/FPR
619 // 6. stack -> stack (non-cycle)
620 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
621 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
622 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
623 // dependency.
624 vixl_temps_.Open(GetVIXLAssembler());
625}
626
627void ParallelMoveResolverARM64::FinishEmitNativeCode() {
628 vixl_temps_.Close();
629}
630
631Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
632 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
633 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
634 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
635 Location scratch = GetScratchLocation(kind);
636 if (!scratch.Equals(Location::NoLocation())) {
637 return scratch;
638 }
639 // Allocate from VIXL temp registers.
640 if (kind == Location::kRegister) {
641 scratch = LocationFrom(vixl_temps_.AcquireX());
642 } else {
643 DCHECK(kind == Location::kFpuRegister);
644 scratch = LocationFrom(vixl_temps_.AcquireD());
645 }
646 AddScratchLocation(scratch);
647 return scratch;
648}
649
650void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
651 if (loc.IsRegister()) {
652 vixl_temps_.Release(XRegisterFrom(loc));
653 } else {
654 DCHECK(loc.IsFpuRegister());
655 vixl_temps_.Release(DRegisterFrom(loc));
656 }
657 RemoveScratchLocation(loc);
658}
659
Alexandre Rames3e69f162014-12-10 10:36:50 +0000660void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100661 DCHECK_LT(index, moves_.size());
662 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100663 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000664}
665
Alexandre Rames5319def2014-10-23 10:03:10 +0100666void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100667 MacroAssembler* masm = GetVIXLAssembler();
668 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000669 __ Bind(&frame_entry_label_);
670
Serban Constantinescu02164b32014-11-13 14:05:07 +0000671 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
672 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100673 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000674 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000675 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000676 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000677 __ Ldr(wzr, MemOperand(temp, 0));
678 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000679 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100680
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000681 if (!HasEmptyFrame()) {
682 int frame_size = GetFrameSize();
683 // Stack layout:
684 // sp[frame_size - 8] : lr.
685 // ... : other preserved core registers.
686 // ... : other preserved fp registers.
687 // ... : reserved frame space.
688 // sp[0] : current method.
689 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100690 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800691 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
692 frame_size - GetCoreSpillSize());
693 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
694 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000695 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100696}
697
698void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100699 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100700 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000701 if (!HasEmptyFrame()) {
702 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800703 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
704 frame_size - FrameEntrySpillSize());
705 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
706 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000707 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100708 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000709 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100710 __ Ret();
711 GetAssembler()->cfi().RestoreState();
712 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100713}
714
Zheng Xuda403092015-04-24 17:35:39 +0800715vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
716 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
717 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
718 core_spill_mask_);
719}
720
721vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
722 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
723 GetNumberOfFloatingPointRegisters()));
724 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
725 fpu_spill_mask_);
726}
727
Alexandre Rames5319def2014-10-23 10:03:10 +0100728void CodeGeneratorARM64::Bind(HBasicBlock* block) {
729 __ Bind(GetLabelOf(block));
730}
731
Alexandre Rames5319def2014-10-23 10:03:10 +0100732void CodeGeneratorARM64::Move(HInstruction* instruction,
733 Location location,
734 HInstruction* move_for) {
735 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100736 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000737 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100738
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100739 if (instruction->IsFakeString()) {
740 // The fake string is an alias for null.
741 DCHECK(IsBaseline());
742 instruction = locations->Out().GetConstant();
743 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
744 }
745
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100746 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100747 MoveLocation(location,
748 Location::DoubleStackSlot(kCurrentMethodStackOffset),
749 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100750 } else if (locations != nullptr && locations->Out().Equals(location)) {
751 return;
752 } else if (instruction->IsIntConstant()
753 || instruction->IsLongConstant()
754 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000755 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100756 if (location.IsRegister()) {
757 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000758 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100759 (instruction->IsLongConstant() && dst.Is64Bits()));
760 __ Mov(dst, value);
761 } else {
762 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000763 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000764 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
765 ? temps.AcquireW()
766 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100767 __ Mov(temp, value);
768 __ Str(temp, StackOperandFrom(location));
769 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000770 } else if (instruction->IsTemporary()) {
771 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000772 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100773 } else if (instruction->IsLoadLocal()) {
774 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000775 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000776 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000777 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000778 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100779 }
780
781 } else {
782 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000783 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100784 }
785}
786
Calin Juravle175dc732015-08-25 15:42:32 +0100787void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
788 DCHECK(location.IsRegister());
789 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
790}
791
Calin Juravlee460d1d2015-09-29 04:52:17 +0100792void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
793 if (location.IsRegister()) {
794 locations->AddTemp(location);
795 } else {
796 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
797 }
798}
799
Alexandre Rames5319def2014-10-23 10:03:10 +0100800Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
801 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000802
Alexandre Rames5319def2014-10-23 10:03:10 +0100803 switch (type) {
804 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000805 case Primitive::kPrimInt:
806 case Primitive::kPrimFloat:
807 return Location::StackSlot(GetStackSlot(load->GetLocal()));
808
809 case Primitive::kPrimLong:
810 case Primitive::kPrimDouble:
811 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
812
Alexandre Rames5319def2014-10-23 10:03:10 +0100813 case Primitive::kPrimBoolean:
814 case Primitive::kPrimByte:
815 case Primitive::kPrimChar:
816 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100817 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100818 LOG(FATAL) << "Unexpected type " << type;
819 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000820
Alexandre Rames5319def2014-10-23 10:03:10 +0100821 LOG(FATAL) << "Unreachable";
822 return Location::NoLocation();
823}
824
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100825void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000826 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100827 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000828 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100829 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100830 if (value_can_be_null) {
831 __ Cbz(value, &done);
832 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100833 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
834 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000835 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100836 if (value_can_be_null) {
837 __ Bind(&done);
838 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100839}
840
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000841void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
842 // Blocked core registers:
843 // lr : Runtime reserved.
844 // tr : Runtime reserved.
845 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
846 // ip1 : VIXL core temp.
847 // ip0 : VIXL core temp.
848 //
849 // Blocked fp registers:
850 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100851 CPURegList reserved_core_registers = vixl_reserved_core_registers;
852 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100853 while (!reserved_core_registers.IsEmpty()) {
854 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
855 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000856
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000857 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800858 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000859 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
860 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000861
862 if (is_baseline) {
863 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
864 while (!reserved_core_baseline_registers.IsEmpty()) {
865 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
866 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100867 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000868
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100869 if (is_baseline || GetGraph()->IsDebuggable()) {
870 // Stubs do not save callee-save floating point registers. If the graph
871 // is debuggable, we need to deal with these registers differently. For
872 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000873 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
874 while (!reserved_fp_baseline_registers.IsEmpty()) {
875 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
876 }
877 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100878}
879
880Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
881 if (type == Primitive::kPrimVoid) {
882 LOG(FATAL) << "Unreachable type " << type;
883 }
884
Alexandre Rames542361f2015-01-29 16:57:31 +0000885 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000886 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
887 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100888 return Location::FpuRegisterLocation(reg);
889 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000890 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
891 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100892 return Location::RegisterLocation(reg);
893 }
894}
895
Alexandre Rames3e69f162014-12-10 10:36:50 +0000896size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
897 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
898 __ Str(reg, MemOperand(sp, stack_index));
899 return kArm64WordSize;
900}
901
902size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
903 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
904 __ Ldr(reg, MemOperand(sp, stack_index));
905 return kArm64WordSize;
906}
907
908size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
909 FPRegister reg = FPRegister(reg_id, kDRegSize);
910 __ Str(reg, MemOperand(sp, stack_index));
911 return kArm64WordSize;
912}
913
914size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
915 FPRegister reg = FPRegister(reg_id, kDRegSize);
916 __ Ldr(reg, MemOperand(sp, stack_index));
917 return kArm64WordSize;
918}
919
Alexandre Rames5319def2014-10-23 10:03:10 +0100920void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100921 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100922}
923
924void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100925 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100926}
927
Alexandre Rames67555f72014-11-18 10:55:16 +0000928void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000929 if (constant->IsIntConstant()) {
930 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
931 } else if (constant->IsLongConstant()) {
932 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
933 } else if (constant->IsNullConstant()) {
934 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000935 } else if (constant->IsFloatConstant()) {
936 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
937 } else {
938 DCHECK(constant->IsDoubleConstant());
939 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
940 }
941}
942
Alexandre Rames3e69f162014-12-10 10:36:50 +0000943
944static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
945 DCHECK(constant.IsConstant());
946 HConstant* cst = constant.GetConstant();
947 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000948 // Null is mapped to a core W register, which we associate with kPrimInt.
949 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000950 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
951 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
952 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
953}
954
Calin Juravlee460d1d2015-09-29 04:52:17 +0100955void CodeGeneratorARM64::MoveLocation(Location destination,
956 Location source,
957 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000958 if (source.Equals(destination)) {
959 return;
960 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000961
962 // A valid move can always be inferred from the destination and source
963 // locations. When moving from and to a register, the argument type can be
964 // used to generate 32bit instead of 64bit moves. In debug mode we also
965 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100966 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000967
968 if (destination.IsRegister() || destination.IsFpuRegister()) {
969 if (unspecified_type) {
970 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
971 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000972 (src_cst != nullptr && (src_cst->IsIntConstant()
973 || src_cst->IsFloatConstant()
974 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000975 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100976 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000977 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000978 // If the source is a double stack slot or a 64bit constant, a 64bit
979 // type is appropriate. Else the source is a register, and since the
980 // type has not been specified, we chose a 64bit type to force a 64bit
981 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100982 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000983 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000984 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100985 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
986 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
987 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000988 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
989 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
990 __ Ldr(dst, StackOperandFrom(source));
991 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100992 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000993 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100994 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000995 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100996 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000997 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800998 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100999 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1000 ? Primitive::kPrimLong
1001 : Primitive::kPrimInt;
1002 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1003 }
1004 } else {
1005 DCHECK(source.IsFpuRegister());
1006 if (destination.IsRegister()) {
1007 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1008 ? Primitive::kPrimDouble
1009 : Primitive::kPrimFloat;
1010 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1011 } else {
1012 DCHECK(destination.IsFpuRegister());
1013 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001014 }
1015 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001016 } else { // The destination is not a register. It must be a stack slot.
1017 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1018 if (source.IsRegister() || source.IsFpuRegister()) {
1019 if (unspecified_type) {
1020 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001022 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001023 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024 }
1025 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001026 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1027 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1028 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001029 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001030 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1031 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001032 UseScratchRegisterScope temps(GetVIXLAssembler());
1033 HConstant* src_cst = source.GetConstant();
1034 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001035 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001036 temp = temps.AcquireW();
1037 } else if (src_cst->IsLongConstant()) {
1038 temp = temps.AcquireX();
1039 } else if (src_cst->IsFloatConstant()) {
1040 temp = temps.AcquireS();
1041 } else {
1042 DCHECK(src_cst->IsDoubleConstant());
1043 temp = temps.AcquireD();
1044 }
1045 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001046 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001047 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001048 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001049 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001050 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001051 // There is generally less pressure on FP registers.
1052 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001053 __ Ldr(temp, StackOperandFrom(source));
1054 __ Str(temp, StackOperandFrom(destination));
1055 }
1056 }
1057}
1058
1059void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 CPURegister dst,
1061 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001062 switch (type) {
1063 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001064 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001065 break;
1066 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001067 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001068 break;
1069 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001070 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001071 break;
1072 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001073 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001074 break;
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot:
1077 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001078 case Primitive::kPrimFloat:
1079 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001080 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001081 __ Ldr(dst, src);
1082 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001083 case Primitive::kPrimVoid:
1084 LOG(FATAL) << "Unreachable type " << type;
1085 }
1086}
1087
Calin Juravle77520bc2015-01-12 18:45:46 +00001088void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001089 CPURegister dst,
1090 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001091 MacroAssembler* masm = GetVIXLAssembler();
1092 BlockPoolsScope block_pools(masm);
1093 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001094 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001095 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001097 DCHECK(!src.IsPreIndex());
1098 DCHECK(!src.IsPostIndex());
1099
1100 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001101 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001102 MemOperand base = MemOperand(temp_base);
1103 switch (type) {
1104 case Primitive::kPrimBoolean:
1105 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001106 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001107 break;
1108 case Primitive::kPrimByte:
1109 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001110 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001111 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1112 break;
1113 case Primitive::kPrimChar:
1114 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001115 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001116 break;
1117 case Primitive::kPrimShort:
1118 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001119 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001120 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1121 break;
1122 case Primitive::kPrimInt:
1123 case Primitive::kPrimNot:
1124 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001125 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001126 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001127 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001128 break;
1129 case Primitive::kPrimFloat:
1130 case Primitive::kPrimDouble: {
1131 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001132 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001133
1134 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1135 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001136 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001137 __ Fmov(FPRegister(dst), temp);
1138 break;
1139 }
1140 case Primitive::kPrimVoid:
1141 LOG(FATAL) << "Unreachable type " << type;
1142 }
1143}
1144
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001145void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001146 CPURegister src,
1147 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001148 switch (type) {
1149 case Primitive::kPrimBoolean:
1150 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001151 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001152 break;
1153 case Primitive::kPrimChar:
1154 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001155 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001156 break;
1157 case Primitive::kPrimInt:
1158 case Primitive::kPrimNot:
1159 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001160 case Primitive::kPrimFloat:
1161 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001162 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001163 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001164 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001165 case Primitive::kPrimVoid:
1166 LOG(FATAL) << "Unreachable type " << type;
1167 }
1168}
1169
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001170void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1171 CPURegister src,
1172 const MemOperand& dst) {
1173 UseScratchRegisterScope temps(GetVIXLAssembler());
1174 Register temp_base = temps.AcquireX();
1175
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001176 DCHECK(!dst.IsPreIndex());
1177 DCHECK(!dst.IsPostIndex());
1178
1179 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001180 Operand op = OperandFromMemOperand(dst);
1181 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001182 MemOperand base = MemOperand(temp_base);
1183 switch (type) {
1184 case Primitive::kPrimBoolean:
1185 case Primitive::kPrimByte:
1186 __ Stlrb(Register(src), base);
1187 break;
1188 case Primitive::kPrimChar:
1189 case Primitive::kPrimShort:
1190 __ Stlrh(Register(src), base);
1191 break;
1192 case Primitive::kPrimInt:
1193 case Primitive::kPrimNot:
1194 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001195 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001196 __ Stlr(Register(src), base);
1197 break;
1198 case Primitive::kPrimFloat:
1199 case Primitive::kPrimDouble: {
1200 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001201 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001202
1203 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1204 __ Fmov(temp, FPRegister(src));
1205 __ Stlr(temp, base);
1206 break;
1207 }
1208 case Primitive::kPrimVoid:
1209 LOG(FATAL) << "Unreachable type " << type;
1210 }
1211}
1212
Calin Juravle175dc732015-08-25 15:42:32 +01001213void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1214 HInstruction* instruction,
1215 uint32_t dex_pc,
1216 SlowPathCode* slow_path) {
1217 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1218 instruction,
1219 dex_pc,
1220 slow_path);
1221}
1222
Alexandre Rames67555f72014-11-18 10:55:16 +00001223void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1224 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001225 uint32_t dex_pc,
1226 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001227 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001228 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1230 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001231 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001232}
1233
1234void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1235 vixl::Register class_reg) {
1236 UseScratchRegisterScope temps(GetVIXLAssembler());
1237 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001238 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001239 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001240
Serban Constantinescu02164b32014-11-13 14:05:07 +00001241 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001242 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001243 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1244 __ Add(temp, class_reg, status_offset);
1245 __ Ldar(temp, HeapOperand(temp));
1246 __ Cmp(temp, mirror::Class::kStatusInitialized);
1247 __ B(lt, slow_path->GetEntryLabel());
1248 } else {
1249 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1250 __ Cmp(temp, mirror::Class::kStatusInitialized);
1251 __ B(lt, slow_path->GetEntryLabel());
1252 __ Dmb(InnerShareable, BarrierReads);
1253 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001254 __ Bind(slow_path->GetExitLabel());
1255}
Alexandre Rames5319def2014-10-23 10:03:10 +01001256
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001257void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1258 BarrierType type = BarrierAll;
1259
1260 switch (kind) {
1261 case MemBarrierKind::kAnyAny:
1262 case MemBarrierKind::kAnyStore: {
1263 type = BarrierAll;
1264 break;
1265 }
1266 case MemBarrierKind::kLoadAny: {
1267 type = BarrierReads;
1268 break;
1269 }
1270 case MemBarrierKind::kStoreStore: {
1271 type = BarrierWrites;
1272 break;
1273 }
1274 default:
1275 LOG(FATAL) << "Unexpected memory barrier " << kind;
1276 }
1277 __ Dmb(InnerShareable, type);
1278}
1279
Serban Constantinescu02164b32014-11-13 14:05:07 +00001280void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1281 HBasicBlock* successor) {
1282 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001283 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1284 if (slow_path == nullptr) {
1285 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1286 instruction->SetSlowPath(slow_path);
1287 codegen_->AddSlowPath(slow_path);
1288 if (successor != nullptr) {
1289 DCHECK(successor->IsLoopHeader());
1290 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1291 }
1292 } else {
1293 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1294 }
1295
Serban Constantinescu02164b32014-11-13 14:05:07 +00001296 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1297 Register temp = temps.AcquireW();
1298
1299 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1300 if (successor == nullptr) {
1301 __ Cbnz(temp, slow_path->GetEntryLabel());
1302 __ Bind(slow_path->GetReturnLabel());
1303 } else {
1304 __ Cbz(temp, codegen_->GetLabelOf(successor));
1305 __ B(slow_path->GetEntryLabel());
1306 // slow_path will return to GetLabelOf(successor).
1307 }
1308}
1309
Alexandre Rames5319def2014-10-23 10:03:10 +01001310InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1311 CodeGeneratorARM64* codegen)
1312 : HGraphVisitor(graph),
1313 assembler_(codegen->GetAssembler()),
1314 codegen_(codegen) {}
1315
1316#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001317 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001318
1319#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1320
1321enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 // Using a base helps identify when we hit such breakpoints.
1323 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001324#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1325 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1326#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1327};
1328
1329#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1330 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001331 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001332 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1333 } \
1334 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1336 locations->SetOut(Location::Any()); \
1337 }
1338 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1339#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1340
1341#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001342#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001343
Alexandre Rames67555f72014-11-18 10:55:16 +00001344void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 DCHECK_EQ(instr->InputCount(), 2U);
1346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1347 Primitive::Type type = instr->GetResultType();
1348 switch (type) {
1349 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001350 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001351 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001352 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001355
1356 case Primitive::kPrimFloat:
1357 case Primitive::kPrimDouble:
1358 locations->SetInAt(0, Location::RequiresFpuRegister());
1359 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001360 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001361 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001362
Alexandre Rames5319def2014-10-23 10:03:10 +01001363 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001364 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001365 }
1366}
1367
Alexandre Rames09a99962015-04-15 11:47:56 +01001368void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1369 LocationSummary* locations =
1370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1371 locations->SetInAt(0, Location::RequiresRegister());
1372 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1373 locations->SetOut(Location::RequiresFpuRegister());
1374 } else {
1375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1376 }
1377}
1378
1379void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1380 const FieldInfo& field_info) {
1381 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001382 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001383 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001384
1385 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1386 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1387
1388 if (field_info.IsVolatile()) {
1389 if (use_acquire_release) {
1390 // NB: LoadAcquire will record the pc info if needed.
1391 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1392 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001393 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001394 codegen_->MaybeRecordImplicitNullCheck(instruction);
1395 // For IRIW sequential consistency kLoadAny is not sufficient.
1396 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1397 }
1398 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001399 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001400 codegen_->MaybeRecordImplicitNullCheck(instruction);
1401 }
Roland Levillain4d027112015-07-01 15:41:14 +01001402
1403 if (field_type == Primitive::kPrimNot) {
1404 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1405 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001406}
1407
1408void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1409 LocationSummary* locations =
1410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1411 locations->SetInAt(0, Location::RequiresRegister());
1412 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 } else {
1415 locations->SetInAt(1, Location::RequiresRegister());
1416 }
1417}
1418
1419void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001420 const FieldInfo& field_info,
1421 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001422 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001423 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001424
1425 Register obj = InputRegisterAt(instruction, 0);
1426 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001427 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001428 Offset offset = field_info.GetFieldOffset();
1429 Primitive::Type field_type = field_info.GetFieldType();
1430 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1431
Roland Levillain4d027112015-07-01 15:41:14 +01001432 {
1433 // We use a block to end the scratch scope before the write barrier, thus
1434 // freeing the temporary registers so they can be used in `MarkGCCard`.
1435 UseScratchRegisterScope temps(GetVIXLAssembler());
1436
1437 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1438 DCHECK(value.IsW());
1439 Register temp = temps.AcquireW();
1440 __ Mov(temp, value.W());
1441 GetAssembler()->PoisonHeapReference(temp.W());
1442 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001443 }
Roland Levillain4d027112015-07-01 15:41:14 +01001444
1445 if (field_info.IsVolatile()) {
1446 if (use_acquire_release) {
1447 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1448 codegen_->MaybeRecordImplicitNullCheck(instruction);
1449 } else {
1450 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1451 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1452 codegen_->MaybeRecordImplicitNullCheck(instruction);
1453 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1454 }
1455 } else {
1456 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1457 codegen_->MaybeRecordImplicitNullCheck(instruction);
1458 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001459 }
1460
1461 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001462 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001463 }
1464}
1465
Alexandre Rames67555f72014-11-18 10:55:16 +00001466void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001467 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001468
1469 switch (type) {
1470 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001471 case Primitive::kPrimLong: {
1472 Register dst = OutputRegister(instr);
1473 Register lhs = InputRegisterAt(instr, 0);
1474 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001475 if (instr->IsAdd()) {
1476 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 } else if (instr->IsAnd()) {
1478 __ And(dst, lhs, rhs);
1479 } else if (instr->IsOr()) {
1480 __ Orr(dst, lhs, rhs);
1481 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001482 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001483 } else {
1484 DCHECK(instr->IsXor());
1485 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001486 }
1487 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001488 }
1489 case Primitive::kPrimFloat:
1490 case Primitive::kPrimDouble: {
1491 FPRegister dst = OutputFPRegister(instr);
1492 FPRegister lhs = InputFPRegisterAt(instr, 0);
1493 FPRegister rhs = InputFPRegisterAt(instr, 1);
1494 if (instr->IsAdd()) {
1495 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001496 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001497 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 } else {
1499 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001500 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001501 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001502 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001503 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001505 }
1506}
1507
Serban Constantinescu02164b32014-11-13 14:05:07 +00001508void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1509 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1510
1511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1512 Primitive::Type type = instr->GetResultType();
1513 switch (type) {
1514 case Primitive::kPrimInt:
1515 case Primitive::kPrimLong: {
1516 locations->SetInAt(0, Location::RequiresRegister());
1517 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1518 locations->SetOut(Location::RequiresRegister());
1519 break;
1520 }
1521 default:
1522 LOG(FATAL) << "Unexpected shift type " << type;
1523 }
1524}
1525
1526void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1527 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1528
1529 Primitive::Type type = instr->GetType();
1530 switch (type) {
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimLong: {
1533 Register dst = OutputRegister(instr);
1534 Register lhs = InputRegisterAt(instr, 0);
1535 Operand rhs = InputOperandAt(instr, 1);
1536 if (rhs.IsImmediate()) {
1537 uint32_t shift_value = (type == Primitive::kPrimInt)
1538 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1539 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1540 if (instr->IsShl()) {
1541 __ Lsl(dst, lhs, shift_value);
1542 } else if (instr->IsShr()) {
1543 __ Asr(dst, lhs, shift_value);
1544 } else {
1545 __ Lsr(dst, lhs, shift_value);
1546 }
1547 } else {
1548 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1549
1550 if (instr->IsShl()) {
1551 __ Lsl(dst, lhs, rhs_reg);
1552 } else if (instr->IsShr()) {
1553 __ Asr(dst, lhs, rhs_reg);
1554 } else {
1555 __ Lsr(dst, lhs, rhs_reg);
1556 }
1557 }
1558 break;
1559 }
1560 default:
1561 LOG(FATAL) << "Unexpected shift operation type " << type;
1562 }
1563}
1564
Alexandre Rames5319def2014-10-23 10:03:10 +01001565void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001566 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001567}
1568
1569void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 HandleBinaryOp(instruction);
1571}
1572
1573void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1574 HandleBinaryOp(instruction);
1575}
1576
1577void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1578 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001579}
1580
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001581void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1582 LocationSummary* locations =
1583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001586 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1588 } else {
1589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1590 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591}
1592
1593void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1594 LocationSummary* locations = instruction->GetLocations();
1595 Primitive::Type type = instruction->GetType();
1596 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001597 Location index = locations->InAt(1);
1598 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001599 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001600 MacroAssembler* masm = GetVIXLAssembler();
1601 UseScratchRegisterScope temps(masm);
1602 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001603
1604 if (index.IsConstant()) {
1605 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001606 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001607 } else {
1608 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001609 __ Add(temp, obj, offset);
1610 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001611 }
1612
Alexandre Rames67555f72014-11-18 10:55:16 +00001613 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001614 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001615
1616 if (type == Primitive::kPrimNot) {
1617 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1618 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001619}
1620
Alexandre Rames5319def2014-10-23 10:03:10 +01001621void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1623 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001624 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001625}
1626
1627void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001628 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001629 __ Ldr(OutputRegister(instruction),
1630 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001631 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001632}
1633
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001634void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1636 instruction,
1637 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1638 locations->SetInAt(0, Location::RequiresRegister());
1639 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1640 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1641 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001642 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001643 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001644 }
1645}
1646
1647void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1648 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001649 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001650 bool may_need_runtime_call = locations->CanCall();
1651 bool needs_write_barrier =
1652 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001653
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001654 Register array = InputRegisterAt(instruction, 0);
1655 CPURegister value = InputCPURegisterAt(instruction, 2);
1656 CPURegister source = value;
1657 Location index = locations->InAt(1);
1658 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1659 MemOperand destination = HeapOperand(array);
1660 MacroAssembler* masm = GetVIXLAssembler();
1661 BlockPoolsScope block_pools(masm);
1662
1663 if (!needs_write_barrier) {
1664 DCHECK(!may_need_runtime_call);
1665 if (index.IsConstant()) {
1666 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1667 destination = HeapOperand(array, offset);
1668 } else {
1669 UseScratchRegisterScope temps(masm);
1670 Register temp = temps.AcquireSameSizeAs(array);
1671 __ Add(temp, array, offset);
1672 destination = HeapOperand(temp,
1673 XRegisterFrom(index),
1674 LSL,
1675 Primitive::ComponentSizeShift(value_type));
1676 }
1677 codegen_->Store(value_type, value, destination);
1678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001679 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001680 DCHECK(needs_write_barrier);
1681 vixl::Label done;
1682 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001683 {
1684 // We use a block to end the scratch scope before the write barrier, thus
1685 // freeing the temporary registers so they can be used in `MarkGCCard`.
1686 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001687 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001688 if (index.IsConstant()) {
1689 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001690 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001691 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001692 destination = HeapOperand(temp,
1693 XRegisterFrom(index),
1694 LSL,
1695 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001696 }
1697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1699 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1700 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1701
1702 if (may_need_runtime_call) {
1703 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1704 codegen_->AddSlowPath(slow_path);
1705 if (instruction->GetValueCanBeNull()) {
1706 vixl::Label non_zero;
1707 __ Cbnz(Register(value), &non_zero);
1708 if (!index.IsConstant()) {
1709 __ Add(temp, array, offset);
1710 }
1711 __ Str(wzr, destination);
1712 codegen_->MaybeRecordImplicitNullCheck(instruction);
1713 __ B(&done);
1714 __ Bind(&non_zero);
1715 }
1716
1717 Register temp2 = temps.AcquireSameSizeAs(array);
1718 __ Ldr(temp, HeapOperand(array, class_offset));
1719 codegen_->MaybeRecordImplicitNullCheck(instruction);
1720 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1721 __ Ldr(temp, HeapOperand(temp, component_offset));
1722 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1723 // No need to poison/unpoison, we're comparing two poisoned references.
1724 __ Cmp(temp, temp2);
1725 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1726 vixl::Label do_put;
1727 __ B(eq, &do_put);
1728 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1729 __ Ldr(temp, HeapOperand(temp, super_offset));
1730 // No need to unpoison, we're comparing against null.
1731 __ Cbnz(temp, slow_path->GetEntryLabel());
1732 __ Bind(&do_put);
1733 } else {
1734 __ B(ne, slow_path->GetEntryLabel());
1735 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001736 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001737 }
1738
1739 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001740 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001741 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001742 __ Mov(temp2, value.W());
1743 GetAssembler()->PoisonHeapReference(temp2);
1744 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001745 }
1746
1747 if (!index.IsConstant()) {
1748 __ Add(temp, array, offset);
1749 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001750 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001751
1752 if (!may_need_runtime_call) {
1753 codegen_->MaybeRecordImplicitNullCheck(instruction);
1754 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001755 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001756
1757 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1758
1759 if (done.IsLinked()) {
1760 __ Bind(&done);
1761 }
1762
1763 if (slow_path != nullptr) {
1764 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001765 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001766 }
1767}
1768
Alexandre Rames67555f72014-11-18 10:55:16 +00001769void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001770 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1771 ? LocationSummary::kCallOnSlowPath
1772 : LocationSummary::kNoCall;
1773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001774 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001775 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001776 if (instruction->HasUses()) {
1777 locations->SetOut(Location::SameAsFirstInput());
1778 }
1779}
1780
1781void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001782 BoundsCheckSlowPathARM64* slow_path =
1783 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001784 codegen_->AddSlowPath(slow_path);
1785
1786 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1787 __ B(slow_path->GetEntryLabel(), hs);
1788}
1789
Alexandre Rames67555f72014-11-18 10:55:16 +00001790void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1791 LocationSummary* locations =
1792 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1793 locations->SetInAt(0, Location::RequiresRegister());
1794 if (check->HasUses()) {
1795 locations->SetOut(Location::SameAsFirstInput());
1796 }
1797}
1798
1799void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1800 // We assume the class is not null.
1801 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1802 check->GetLoadClass(), check, check->GetDexPc(), true);
1803 codegen_->AddSlowPath(slow_path);
1804 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1805}
1806
Roland Levillain7f63c522015-07-13 15:54:55 +00001807static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1808 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1809 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1810}
1811
Serban Constantinescu02164b32014-11-13 14:05:07 +00001812void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001813 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001814 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1815 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001816 switch (in_type) {
1817 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001818 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001819 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1821 break;
1822 }
1823 case Primitive::kPrimFloat:
1824 case Primitive::kPrimDouble: {
1825 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001826 locations->SetInAt(1,
1827 IsFloatingPointZeroConstant(compare->InputAt(1))
1828 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1829 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001830 locations->SetOut(Location::RequiresRegister());
1831 break;
1832 }
1833 default:
1834 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1835 }
1836}
1837
1838void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1839 Primitive::Type in_type = compare->InputAt(0)->GetType();
1840
1841 // 0 if: left == right
1842 // 1 if: left > right
1843 // -1 if: left < right
1844 switch (in_type) {
1845 case Primitive::kPrimLong: {
1846 Register result = OutputRegister(compare);
1847 Register left = InputRegisterAt(compare, 0);
1848 Operand right = InputOperandAt(compare, 1);
1849
1850 __ Cmp(left, right);
1851 __ Cset(result, ne);
1852 __ Cneg(result, result, lt);
1853 break;
1854 }
1855 case Primitive::kPrimFloat:
1856 case Primitive::kPrimDouble: {
1857 Register result = OutputRegister(compare);
1858 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001859 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001860 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1861 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001862 __ Fcmp(left, 0.0);
1863 } else {
1864 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1865 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001866 if (compare->IsGtBias()) {
1867 __ Cset(result, ne);
1868 } else {
1869 __ Csetm(result, ne);
1870 }
1871 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001872 break;
1873 }
1874 default:
1875 LOG(FATAL) << "Unimplemented compare type " << in_type;
1876 }
1877}
1878
1879void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1880 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001881
1882 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
1884 locations->SetInAt(1,
1885 IsFloatingPointZeroConstant(instruction->InputAt(1))
1886 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1887 : Location::RequiresFpuRegister());
1888 } else {
1889 // Integer cases.
1890 locations->SetInAt(0, Location::RequiresRegister());
1891 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1892 }
1893
Alexandre Rames5319def2014-10-23 10:03:10 +01001894 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001896 }
1897}
1898
1899void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1900 if (!instruction->NeedsMaterialization()) {
1901 return;
1902 }
1903
1904 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001905 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001906 IfCondition if_cond = instruction->GetCondition();
1907 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001908
Roland Levillain7f63c522015-07-13 15:54:55 +00001909 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1910 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1911 if (locations->InAt(1).IsConstant()) {
1912 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1913 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1914 __ Fcmp(lhs, 0.0);
1915 } else {
1916 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1917 }
1918 __ Cset(res, arm64_cond);
1919 if (instruction->IsFPConditionTrueIfNaN()) {
1920 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1921 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1922 } else if (instruction->IsFPConditionFalseIfNaN()) {
1923 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1924 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1925 }
1926 } else {
1927 // Integer cases.
1928 Register lhs = InputRegisterAt(instruction, 0);
1929 Operand rhs = InputOperandAt(instruction, 1);
1930 __ Cmp(lhs, rhs);
1931 __ Cset(res, arm64_cond);
1932 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001933}
1934
1935#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1936 M(Equal) \
1937 M(NotEqual) \
1938 M(LessThan) \
1939 M(LessThanOrEqual) \
1940 M(GreaterThan) \
1941 M(GreaterThanOrEqual)
1942#define DEFINE_CONDITION_VISITORS(Name) \
1943void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1944void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1945FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001946#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001947#undef FOR_EACH_CONDITION_INSTRUCTION
1948
Zheng Xuc6667102015-05-15 16:08:45 +08001949void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1950 DCHECK(instruction->IsDiv() || instruction->IsRem());
1951
1952 LocationSummary* locations = instruction->GetLocations();
1953 Location second = locations->InAt(1);
1954 DCHECK(second.IsConstant());
1955
1956 Register out = OutputRegister(instruction);
1957 Register dividend = InputRegisterAt(instruction, 0);
1958 int64_t imm = Int64FromConstant(second.GetConstant());
1959 DCHECK(imm == 1 || imm == -1);
1960
1961 if (instruction->IsRem()) {
1962 __ Mov(out, 0);
1963 } else {
1964 if (imm == 1) {
1965 __ Mov(out, dividend);
1966 } else {
1967 __ Neg(out, dividend);
1968 }
1969 }
1970}
1971
1972void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1973 DCHECK(instruction->IsDiv() || instruction->IsRem());
1974
1975 LocationSummary* locations = instruction->GetLocations();
1976 Location second = locations->InAt(1);
1977 DCHECK(second.IsConstant());
1978
1979 Register out = OutputRegister(instruction);
1980 Register dividend = InputRegisterAt(instruction, 0);
1981 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001982 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001983 DCHECK(IsPowerOfTwo(abs_imm));
1984 int ctz_imm = CTZ(abs_imm);
1985
1986 UseScratchRegisterScope temps(GetVIXLAssembler());
1987 Register temp = temps.AcquireSameSizeAs(out);
1988
1989 if (instruction->IsDiv()) {
1990 __ Add(temp, dividend, abs_imm - 1);
1991 __ Cmp(dividend, 0);
1992 __ Csel(out, temp, dividend, lt);
1993 if (imm > 0) {
1994 __ Asr(out, out, ctz_imm);
1995 } else {
1996 __ Neg(out, Operand(out, ASR, ctz_imm));
1997 }
1998 } else {
1999 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2000 __ Asr(temp, dividend, bits - 1);
2001 __ Lsr(temp, temp, bits - ctz_imm);
2002 __ Add(out, dividend, temp);
2003 __ And(out, out, abs_imm - 1);
2004 __ Sub(out, out, temp);
2005 }
2006}
2007
2008void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2009 DCHECK(instruction->IsDiv() || instruction->IsRem());
2010
2011 LocationSummary* locations = instruction->GetLocations();
2012 Location second = locations->InAt(1);
2013 DCHECK(second.IsConstant());
2014
2015 Register out = OutputRegister(instruction);
2016 Register dividend = InputRegisterAt(instruction, 0);
2017 int64_t imm = Int64FromConstant(second.GetConstant());
2018
2019 Primitive::Type type = instruction->GetResultType();
2020 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2021
2022 int64_t magic;
2023 int shift;
2024 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2025
2026 UseScratchRegisterScope temps(GetVIXLAssembler());
2027 Register temp = temps.AcquireSameSizeAs(out);
2028
2029 // temp = get_high(dividend * magic)
2030 __ Mov(temp, magic);
2031 if (type == Primitive::kPrimLong) {
2032 __ Smulh(temp, dividend, temp);
2033 } else {
2034 __ Smull(temp.X(), dividend, temp);
2035 __ Lsr(temp.X(), temp.X(), 32);
2036 }
2037
2038 if (imm > 0 && magic < 0) {
2039 __ Add(temp, temp, dividend);
2040 } else if (imm < 0 && magic > 0) {
2041 __ Sub(temp, temp, dividend);
2042 }
2043
2044 if (shift != 0) {
2045 __ Asr(temp, temp, shift);
2046 }
2047
2048 if (instruction->IsDiv()) {
2049 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2050 } else {
2051 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2052 // TODO: Strength reduction for msub.
2053 Register temp_imm = temps.AcquireSameSizeAs(out);
2054 __ Mov(temp_imm, imm);
2055 __ Msub(out, temp, temp_imm, dividend);
2056 }
2057}
2058
2059void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2060 DCHECK(instruction->IsDiv() || instruction->IsRem());
2061 Primitive::Type type = instruction->GetResultType();
2062 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2063
2064 LocationSummary* locations = instruction->GetLocations();
2065 Register out = OutputRegister(instruction);
2066 Location second = locations->InAt(1);
2067
2068 if (second.IsConstant()) {
2069 int64_t imm = Int64FromConstant(second.GetConstant());
2070
2071 if (imm == 0) {
2072 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2073 } else if (imm == 1 || imm == -1) {
2074 DivRemOneOrMinusOne(instruction);
2075 } else if (IsPowerOfTwo(std::abs(imm))) {
2076 DivRemByPowerOfTwo(instruction);
2077 } else {
2078 DCHECK(imm <= -2 || imm >= 2);
2079 GenerateDivRemWithAnyConstant(instruction);
2080 }
2081 } else {
2082 Register dividend = InputRegisterAt(instruction, 0);
2083 Register divisor = InputRegisterAt(instruction, 1);
2084 if (instruction->IsDiv()) {
2085 __ Sdiv(out, dividend, divisor);
2086 } else {
2087 UseScratchRegisterScope temps(GetVIXLAssembler());
2088 Register temp = temps.AcquireSameSizeAs(out);
2089 __ Sdiv(temp, dividend, divisor);
2090 __ Msub(out, temp, divisor, dividend);
2091 }
2092 }
2093}
2094
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2098 switch (div->GetResultType()) {
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimLong:
2101 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002102 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2104 break;
2105
2106 case Primitive::kPrimFloat:
2107 case Primitive::kPrimDouble:
2108 locations->SetInAt(0, Location::RequiresFpuRegister());
2109 locations->SetInAt(1, Location::RequiresFpuRegister());
2110 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2111 break;
2112
2113 default:
2114 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2115 }
2116}
2117
2118void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2119 Primitive::Type type = div->GetResultType();
2120 switch (type) {
2121 case Primitive::kPrimInt:
2122 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002123 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002124 break;
2125
2126 case Primitive::kPrimFloat:
2127 case Primitive::kPrimDouble:
2128 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2129 break;
2130
2131 default:
2132 LOG(FATAL) << "Unexpected div type " << type;
2133 }
2134}
2135
Alexandre Rames67555f72014-11-18 10:55:16 +00002136void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002137 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2138 ? LocationSummary::kCallOnSlowPath
2139 : LocationSummary::kNoCall;
2140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002141 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2142 if (instruction->HasUses()) {
2143 locations->SetOut(Location::SameAsFirstInput());
2144 }
2145}
2146
2147void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2148 SlowPathCodeARM64* slow_path =
2149 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2150 codegen_->AddSlowPath(slow_path);
2151 Location value = instruction->GetLocations()->InAt(0);
2152
Alexandre Rames3e69f162014-12-10 10:36:50 +00002153 Primitive::Type type = instruction->GetType();
2154
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002155 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2156 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002157 return;
2158 }
2159
Alexandre Rames67555f72014-11-18 10:55:16 +00002160 if (value.IsConstant()) {
2161 int64_t divisor = Int64ConstantFrom(value);
2162 if (divisor == 0) {
2163 __ B(slow_path->GetEntryLabel());
2164 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002165 // A division by a non-null constant is valid. We don't need to perform
2166 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 }
2168 } else {
2169 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2170 }
2171}
2172
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2174 LocationSummary* locations =
2175 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2176 locations->SetOut(Location::ConstantLocation(constant));
2177}
2178
2179void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2180 UNUSED(constant);
2181 // Will be generated at use site.
2182}
2183
Alexandre Rames5319def2014-10-23 10:03:10 +01002184void LocationsBuilderARM64::VisitExit(HExit* exit) {
2185 exit->SetLocations(nullptr);
2186}
2187
2188void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002189 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002190}
2191
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002192void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2193 LocationSummary* locations =
2194 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2195 locations->SetOut(Location::ConstantLocation(constant));
2196}
2197
2198void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2199 UNUSED(constant);
2200 // Will be generated at use site.
2201}
2202
David Brazdilfc6a86a2015-06-26 10:33:45 +00002203void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002204 DCHECK(!successor->IsExitBlock());
2205 HBasicBlock* block = got->GetBlock();
2206 HInstruction* previous = got->GetPrevious();
2207 HLoopInformation* info = block->GetLoopInformation();
2208
David Brazdil46e2a392015-03-16 17:31:52 +00002209 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002210 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2211 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2212 return;
2213 }
2214 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2215 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2216 }
2217 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002218 __ B(codegen_->GetLabelOf(successor));
2219 }
2220}
2221
David Brazdilfc6a86a2015-06-26 10:33:45 +00002222void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2223 got->SetLocations(nullptr);
2224}
2225
2226void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2227 HandleGoto(got, got->GetSuccessor());
2228}
2229
2230void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2231 try_boundary->SetLocations(nullptr);
2232}
2233
2234void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2235 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2236 if (!successor->IsExitBlock()) {
2237 HandleGoto(try_boundary, successor);
2238 }
2239}
2240
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002241void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2242 vixl::Label* true_target,
2243 vixl::Label* false_target,
2244 vixl::Label* always_true_target) {
2245 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002246 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002247
Serban Constantinescu02164b32014-11-13 14:05:07 +00002248 if (cond->IsIntConstant()) {
2249 int32_t cond_value = cond->AsIntConstant()->GetValue();
2250 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002251 if (always_true_target != nullptr) {
2252 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002253 }
2254 return;
2255 } else {
2256 DCHECK_EQ(cond_value, 0);
2257 }
2258 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002259 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002260 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002261 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002262 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 } else {
2264 // The condition instruction has not been materialized, use its inputs as
2265 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002266 Primitive::Type type =
2267 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2268
2269 if (Primitive::IsFloatingPointType(type)) {
2270 // FP compares don't like null false_targets.
2271 if (false_target == nullptr) {
2272 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002274 FPRegister lhs = InputFPRegisterAt(condition, 0);
2275 if (condition->GetLocations()->InAt(1).IsConstant()) {
2276 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2277 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2278 __ Fcmp(lhs, 0.0);
2279 } else {
2280 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2281 }
2282 if (condition->IsFPConditionTrueIfNaN()) {
2283 __ B(vs, true_target); // VS for unordered.
2284 } else if (condition->IsFPConditionFalseIfNaN()) {
2285 __ B(vs, false_target); // VS for unordered.
2286 }
2287 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002288 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002289 // Integer cases.
2290 Register lhs = InputRegisterAt(condition, 0);
2291 Operand rhs = InputOperandAt(condition, 1);
2292 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2293 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2294 switch (arm64_cond) {
2295 case eq:
2296 __ Cbz(lhs, true_target);
2297 break;
2298 case ne:
2299 __ Cbnz(lhs, true_target);
2300 break;
2301 case lt:
2302 // Test the sign bit and branch accordingly.
2303 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2304 break;
2305 case ge:
2306 // Test the sign bit and branch accordingly.
2307 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2308 break;
2309 default:
2310 // Without the `static_cast` the compiler throws an error for
2311 // `-Werror=sign-promo`.
2312 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2313 }
2314 } else {
2315 __ Cmp(lhs, rhs);
2316 __ B(arm64_cond, true_target);
2317 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002318 }
2319 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002320 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002321 __ B(false_target);
2322 }
2323}
2324
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002325void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2327 HInstruction* cond = if_instr->InputAt(0);
2328 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2329 locations->SetInAt(0, Location::RequiresRegister());
2330 }
2331}
2332
2333void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2334 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2335 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2336 vixl::Label* always_true_target = true_target;
2337 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2338 if_instr->IfTrueSuccessor())) {
2339 always_true_target = nullptr;
2340 }
2341 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2342 if_instr->IfFalseSuccessor())) {
2343 false_target = nullptr;
2344 }
2345 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2346}
2347
2348void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2349 LocationSummary* locations = new (GetGraph()->GetArena())
2350 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2351 HInstruction* cond = deoptimize->InputAt(0);
2352 DCHECK(cond->IsCondition());
2353 if (cond->AsCondition()->NeedsMaterialization()) {
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 }
2356}
2357
2358void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2359 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2360 DeoptimizationSlowPathARM64(deoptimize);
2361 codegen_->AddSlowPath(slow_path);
2362 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2363 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2364}
2365
Alexandre Rames5319def2014-10-23 10:03:10 +01002366void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002367 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002368}
2369
2370void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002372}
2373
2374void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002375 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002376}
2377
2378void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002379 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002380}
2381
Alexandre Rames67555f72014-11-18 10:55:16 +00002382void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002383 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2384 switch (instruction->GetTypeCheckKind()) {
2385 case TypeCheckKind::kExactCheck:
2386 case TypeCheckKind::kAbstractClassCheck:
2387 case TypeCheckKind::kClassHierarchyCheck:
2388 case TypeCheckKind::kArrayObjectCheck:
2389 call_kind = LocationSummary::kNoCall;
2390 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002391 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002392 case TypeCheckKind::kInterfaceCheck:
2393 call_kind = LocationSummary::kCall;
2394 break;
2395 case TypeCheckKind::kArrayCheck:
2396 call_kind = LocationSummary::kCallOnSlowPath;
2397 break;
2398 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002400 if (call_kind != LocationSummary::kCall) {
2401 locations->SetInAt(0, Location::RequiresRegister());
2402 locations->SetInAt(1, Location::RequiresRegister());
2403 // The out register is used as a temporary, so it overlaps with the inputs.
2404 // Note that TypeCheckSlowPathARM64 uses this register too.
2405 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2406 } else {
2407 InvokeRuntimeCallingConvention calling_convention;
2408 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2409 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2410 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2411 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002412}
2413
2414void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2415 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002416 Register obj = InputRegisterAt(instruction, 0);
2417 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002418 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002419 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2420 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2421 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2422 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002423
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002424 vixl::Label done, zero;
2425 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002426
2427 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002428 // Avoid null check if we know `obj` is not null.
2429 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002430 __ Cbz(obj, &zero);
2431 }
2432
Calin Juravle98893e12015-10-02 21:05:03 +01002433 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002434 // This is safe, as the register is caller-save, and the object must be in another
2435 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01002436 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
2437 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002438 ? obj
2439 : out;
2440 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2441 GetAssembler()->MaybeUnpoisonHeapReference(target);
2442
2443 switch (instruction->GetTypeCheckKind()) {
2444 case TypeCheckKind::kExactCheck: {
2445 __ Cmp(out, cls);
2446 __ Cset(out, eq);
2447 if (zero.IsLinked()) {
2448 __ B(&done);
2449 }
2450 break;
2451 }
2452 case TypeCheckKind::kAbstractClassCheck: {
2453 // If the class is abstract, we eagerly fetch the super class of the
2454 // object to avoid doing a comparison we know will fail.
2455 vixl::Label loop, success;
2456 __ Bind(&loop);
2457 __ Ldr(out, HeapOperand(out, super_offset));
2458 GetAssembler()->MaybeUnpoisonHeapReference(out);
2459 // If `out` is null, we use it for the result, and jump to `done`.
2460 __ Cbz(out, &done);
2461 __ Cmp(out, cls);
2462 __ B(ne, &loop);
2463 __ Mov(out, 1);
2464 if (zero.IsLinked()) {
2465 __ B(&done);
2466 }
2467 break;
2468 }
2469 case TypeCheckKind::kClassHierarchyCheck: {
2470 // Walk over the class hierarchy to find a match.
2471 vixl::Label loop, success;
2472 __ Bind(&loop);
2473 __ Cmp(out, cls);
2474 __ B(eq, &success);
2475 __ Ldr(out, HeapOperand(out, super_offset));
2476 GetAssembler()->MaybeUnpoisonHeapReference(out);
2477 __ Cbnz(out, &loop);
2478 // If `out` is null, we use it for the result, and jump to `done`.
2479 __ B(&done);
2480 __ Bind(&success);
2481 __ Mov(out, 1);
2482 if (zero.IsLinked()) {
2483 __ B(&done);
2484 }
2485 break;
2486 }
2487 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002488 // Do an exact check.
2489 vixl::Label exact_check;
2490 __ Cmp(out, cls);
2491 __ B(eq, &exact_check);
2492 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002493 __ Ldr(out, HeapOperand(out, component_offset));
2494 GetAssembler()->MaybeUnpoisonHeapReference(out);
2495 // If `out` is null, we use it for the result, and jump to `done`.
2496 __ Cbz(out, &done);
2497 __ Ldrh(out, HeapOperand(out, primitive_offset));
2498 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2499 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002500 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002501 __ Mov(out, 1);
2502 __ B(&done);
2503 break;
2504 }
2505 case TypeCheckKind::kArrayCheck: {
2506 __ Cmp(out, cls);
2507 DCHECK(locations->OnlyCallsOnSlowPath());
2508 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2509 instruction, /* is_fatal */ false);
2510 codegen_->AddSlowPath(slow_path);
2511 __ B(ne, slow_path->GetEntryLabel());
2512 __ Mov(out, 1);
2513 if (zero.IsLinked()) {
2514 __ B(&done);
2515 }
2516 break;
2517 }
Calin Juravle98893e12015-10-02 21:05:03 +01002518 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002519 case TypeCheckKind::kInterfaceCheck:
2520 default: {
2521 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2522 instruction,
2523 instruction->GetDexPc(),
2524 nullptr);
2525 if (zero.IsLinked()) {
2526 __ B(&done);
2527 }
2528 break;
2529 }
2530 }
2531
2532 if (zero.IsLinked()) {
2533 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002534 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002535 }
2536
2537 if (done.IsLinked()) {
2538 __ Bind(&done);
2539 }
2540
2541 if (slow_path != nullptr) {
2542 __ Bind(slow_path->GetExitLabel());
2543 }
2544}
2545
2546void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2547 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2548 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2549
2550 switch (instruction->GetTypeCheckKind()) {
2551 case TypeCheckKind::kExactCheck:
2552 case TypeCheckKind::kAbstractClassCheck:
2553 case TypeCheckKind::kClassHierarchyCheck:
2554 case TypeCheckKind::kArrayObjectCheck:
2555 call_kind = throws_into_catch
2556 ? LocationSummary::kCallOnSlowPath
2557 : LocationSummary::kNoCall;
2558 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002559 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002560 case TypeCheckKind::kInterfaceCheck:
2561 call_kind = LocationSummary::kCall;
2562 break;
2563 case TypeCheckKind::kArrayCheck:
2564 call_kind = LocationSummary::kCallOnSlowPath;
2565 break;
2566 }
2567
2568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2569 instruction, call_kind);
2570 if (call_kind != LocationSummary::kCall) {
2571 locations->SetInAt(0, Location::RequiresRegister());
2572 locations->SetInAt(1, Location::RequiresRegister());
2573 // Note that TypeCheckSlowPathARM64 uses this register too.
2574 locations->AddTemp(Location::RequiresRegister());
2575 } else {
2576 InvokeRuntimeCallingConvention calling_convention;
2577 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2578 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2579 }
2580}
2581
2582void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2583 LocationSummary* locations = instruction->GetLocations();
2584 Register obj = InputRegisterAt(instruction, 0);
2585 Register cls = InputRegisterAt(instruction, 1);
2586 Register temp;
2587 if (!locations->WillCall()) {
2588 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2589 }
2590
2591 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2592 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2593 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2594 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2595 SlowPathCodeARM64* slow_path = nullptr;
2596
2597 if (!locations->WillCall()) {
2598 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2599 instruction, !locations->CanCall());
2600 codegen_->AddSlowPath(slow_path);
2601 }
2602
2603 vixl::Label done;
2604 // Avoid null check if we know obj is not null.
2605 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002606 __ Cbz(obj, &done);
2607 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002609 if (locations->WillCall()) {
2610 __ Ldr(obj, HeapOperand(obj, class_offset));
2611 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002612 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002613 __ Ldr(temp, HeapOperand(obj, class_offset));
2614 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002615 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002616
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002617 switch (instruction->GetTypeCheckKind()) {
2618 case TypeCheckKind::kExactCheck:
2619 case TypeCheckKind::kArrayCheck: {
2620 __ Cmp(temp, cls);
2621 // Jump to slow path for throwing the exception or doing a
2622 // more involved array check.
2623 __ B(ne, slow_path->GetEntryLabel());
2624 break;
2625 }
2626 case TypeCheckKind::kAbstractClassCheck: {
2627 // If the class is abstract, we eagerly fetch the super class of the
2628 // object to avoid doing a comparison we know will fail.
2629 vixl::Label loop;
2630 __ Bind(&loop);
2631 __ Ldr(temp, HeapOperand(temp, super_offset));
2632 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2633 // Jump to the slow path to throw the exception.
2634 __ Cbz(temp, slow_path->GetEntryLabel());
2635 __ Cmp(temp, cls);
2636 __ B(ne, &loop);
2637 break;
2638 }
2639 case TypeCheckKind::kClassHierarchyCheck: {
2640 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002641 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002642 __ Bind(&loop);
2643 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002644 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002645 __ Ldr(temp, HeapOperand(temp, super_offset));
2646 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2647 __ Cbnz(temp, &loop);
2648 // Jump to the slow path to throw the exception.
2649 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002650 break;
2651 }
2652 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002653 // Do an exact check.
2654 __ Cmp(temp, cls);
2655 __ B(eq, &done);
2656 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002657 __ Ldr(temp, HeapOperand(temp, component_offset));
2658 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2659 __ Cbz(temp, slow_path->GetEntryLabel());
2660 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2661 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2662 __ Cbnz(temp, slow_path->GetEntryLabel());
2663 break;
2664 }
Calin Juravle98893e12015-10-02 21:05:03 +01002665 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002666 case TypeCheckKind::kInterfaceCheck:
2667 default:
2668 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2669 instruction,
2670 instruction->GetDexPc(),
2671 nullptr);
2672 break;
2673 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002674 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002675
2676 if (slow_path != nullptr) {
2677 __ Bind(slow_path->GetExitLabel());
2678 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002679}
2680
Alexandre Rames5319def2014-10-23 10:03:10 +01002681void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2683 locations->SetOut(Location::ConstantLocation(constant));
2684}
2685
2686void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2687 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002688 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002689}
2690
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002691void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2693 locations->SetOut(Location::ConstantLocation(constant));
2694}
2695
2696void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2697 // Will be generated at use site.
2698 UNUSED(constant);
2699}
2700
Calin Juravle175dc732015-08-25 15:42:32 +01002701void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2702 // The trampoline uses the same calling convention as dex calling conventions,
2703 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2704 // the method_idx.
2705 HandleInvoke(invoke);
2706}
2707
2708void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2709 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2710}
2711
Alexandre Rames5319def2014-10-23 10:03:10 +01002712void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002713 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002714 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002715}
2716
Alexandre Rames67555f72014-11-18 10:55:16 +00002717void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2718 HandleInvoke(invoke);
2719}
2720
2721void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2722 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002723 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2724 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2725 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002726 Location receiver = invoke->GetLocations()->InAt(0);
2727 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002728 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002729
2730 // The register ip1 is required to be used for the hidden argument in
2731 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002732 MacroAssembler* masm = GetVIXLAssembler();
2733 UseScratchRegisterScope scratch_scope(masm);
2734 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002735 scratch_scope.Exclude(ip1);
2736 __ Mov(ip1, invoke->GetDexMethodIndex());
2737
2738 // temp = object->GetClass();
2739 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002740 __ Ldr(temp.W(), StackOperandFrom(receiver));
2741 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002742 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002743 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002744 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002745 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002746 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002747 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002748 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002749 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002750 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002751 // lr();
2752 __ Blr(lr);
2753 DCHECK(!codegen_->IsLeafMethod());
2754 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2755}
2756
2757void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002758 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2759 if (intrinsic.TryDispatch(invoke)) {
2760 return;
2761 }
2762
Alexandre Rames67555f72014-11-18 10:55:16 +00002763 HandleInvoke(invoke);
2764}
2765
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002766void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002767 // When we do not run baseline, explicit clinit checks triggered by static
2768 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2769 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002770
Andreas Gampe878d58c2015-01-15 23:24:00 -08002771 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2772 if (intrinsic.TryDispatch(invoke)) {
2773 return;
2774 }
2775
Alexandre Rames67555f72014-11-18 10:55:16 +00002776 HandleInvoke(invoke);
2777}
2778
Andreas Gampe878d58c2015-01-15 23:24:00 -08002779static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2780 if (invoke->GetLocations()->Intrinsified()) {
2781 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2782 intrinsic.Dispatch(invoke);
2783 return true;
2784 }
2785 return false;
2786}
2787
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002788void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002789 // For better instruction scheduling we load the direct code pointer before the method pointer.
2790 bool direct_code_loaded = false;
2791 switch (invoke->GetCodePtrLocation()) {
2792 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2793 // LR = code address from literal pool with link-time patch.
2794 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2795 direct_code_loaded = true;
2796 break;
2797 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2798 // LR = invoke->GetDirectCodePtr();
2799 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2800 direct_code_loaded = true;
2801 break;
2802 default:
2803 break;
2804 }
2805
Andreas Gampe878d58c2015-01-15 23:24:00 -08002806 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002807 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2808 switch (invoke->GetMethodLoadKind()) {
2809 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2810 // temp = thread->string_init_entrypoint
2811 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2812 break;
2813 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2814 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2815 break;
2816 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2817 // Load method address from literal pool.
2818 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2819 break;
2820 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2821 // Load method address from literal pool with a link-time patch.
2822 __ Ldr(XRegisterFrom(temp).X(),
2823 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2824 break;
2825 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2826 // Add ADRP with its PC-relative DexCache access patch.
2827 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2828 invoke->GetDexCacheArrayOffset());
2829 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2830 {
2831 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2832 __ adrp(XRegisterFrom(temp).X(), 0);
2833 }
2834 __ Bind(pc_insn_label); // Bind after ADRP.
2835 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2836 // Add LDR with its PC-relative DexCache access patch.
2837 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2838 invoke->GetDexCacheArrayOffset());
2839 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2840 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2841 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2842 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002843 }
Vladimir Marko58155012015-08-19 12:49:41 +00002844 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2845 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2846 Register reg = XRegisterFrom(temp);
2847 Register method_reg;
2848 if (current_method.IsRegister()) {
2849 method_reg = XRegisterFrom(current_method);
2850 } else {
2851 DCHECK(invoke->GetLocations()->Intrinsified());
2852 DCHECK(!current_method.IsValid());
2853 method_reg = reg;
2854 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2855 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002856
Vladimir Marko58155012015-08-19 12:49:41 +00002857 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002858 __ Ldr(reg.X(),
2859 MemOperand(method_reg.X(),
2860 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002861 // temp = temp[index_in_cache];
2862 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2863 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2864 break;
2865 }
2866 }
2867
2868 switch (invoke->GetCodePtrLocation()) {
2869 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2870 __ Bl(&frame_entry_label_);
2871 break;
2872 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2873 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2874 vixl::Label* label = &relative_call_patches_.back().label;
2875 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2876 __ Bind(label); // Bind after BL.
2877 break;
2878 }
2879 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2880 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2881 // LR prepared above for better instruction scheduling.
2882 DCHECK(direct_code_loaded);
2883 // lr()
2884 __ Blr(lr);
2885 break;
2886 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2887 // LR = callee_method->entry_point_from_quick_compiled_code_;
2888 __ Ldr(lr, MemOperand(
2889 XRegisterFrom(callee_method).X(),
2890 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2891 // lr()
2892 __ Blr(lr);
2893 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002894 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002895
Andreas Gampe878d58c2015-01-15 23:24:00 -08002896 DCHECK(!IsLeafMethod());
2897}
2898
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002899void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2900 LocationSummary* locations = invoke->GetLocations();
2901 Location receiver = locations->InAt(0);
2902 Register temp = XRegisterFrom(temp_in);
2903 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2904 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2905 Offset class_offset = mirror::Object::ClassOffset();
2906 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2907
2908 BlockPoolsScope block_pools(GetVIXLAssembler());
2909
2910 DCHECK(receiver.IsRegister());
2911 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2912 MaybeRecordImplicitNullCheck(invoke);
2913 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2914 // temp = temp->GetMethodAt(method_offset);
2915 __ Ldr(temp, MemOperand(temp, method_offset));
2916 // lr = temp->GetEntryPoint();
2917 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2918 // lr();
2919 __ Blr(lr);
2920}
2921
Vladimir Marko58155012015-08-19 12:49:41 +00002922void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2923 DCHECK(linker_patches->empty());
2924 size_t size =
2925 method_patches_.size() +
2926 call_patches_.size() +
2927 relative_call_patches_.size() +
2928 pc_rel_dex_cache_patches_.size();
2929 linker_patches->reserve(size);
2930 for (const auto& entry : method_patches_) {
2931 const MethodReference& target_method = entry.first;
2932 vixl::Literal<uint64_t>* literal = entry.second;
2933 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2934 target_method.dex_file,
2935 target_method.dex_method_index));
2936 }
2937 for (const auto& entry : call_patches_) {
2938 const MethodReference& target_method = entry.first;
2939 vixl::Literal<uint64_t>* literal = entry.second;
2940 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2941 target_method.dex_file,
2942 target_method.dex_method_index));
2943 }
2944 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2945 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2946 info.target_method.dex_file,
2947 info.target_method.dex_method_index));
2948 }
2949 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2950 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2951 &info.target_dex_file,
2952 info.pc_insn_label->location() - 4u,
2953 info.element_offset));
2954 }
2955}
2956
2957vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2958 // Look up the literal for value.
2959 auto lb = uint64_literals_.lower_bound(value);
2960 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2961 return lb->second;
2962 }
2963 // We don't have a literal for this value, insert a new one.
2964 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2965 uint64_literals_.PutBefore(lb, value, literal);
2966 return literal;
2967}
2968
2969vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2970 MethodReference target_method,
2971 MethodToLiteralMap* map) {
2972 // Look up the literal for target_method.
2973 auto lb = map->lower_bound(target_method);
2974 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2975 return lb->second;
2976 }
2977 // We don't have a literal for this method yet, insert a new one.
2978 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2979 map->PutBefore(lb, target_method, literal);
2980 return literal;
2981}
2982
2983vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2984 MethodReference target_method) {
2985 return DeduplicateMethodLiteral(target_method, &method_patches_);
2986}
2987
2988vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2989 MethodReference target_method) {
2990 return DeduplicateMethodLiteral(target_method, &call_patches_);
2991}
2992
2993
Andreas Gampe878d58c2015-01-15 23:24:00 -08002994void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002995 // When we do not run baseline, explicit clinit checks triggered by static
2996 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2997 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002998
Andreas Gampe878d58c2015-01-15 23:24:00 -08002999 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3000 return;
3001 }
3002
Alexandre Ramesd921d642015-04-16 15:07:16 +01003003 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003004 LocationSummary* locations = invoke->GetLocations();
3005 codegen_->GenerateStaticOrDirectCall(
3006 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003007 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003008}
3009
3010void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003011 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3012 return;
3013 }
3014
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003015 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003016 DCHECK(!codegen_->IsLeafMethod());
3017 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3018}
3019
Alexandre Rames67555f72014-11-18 10:55:16 +00003020void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003021 InvokeRuntimeCallingConvention calling_convention;
3022 CodeGenerator::CreateLoadClassLocationSummary(
3023 cls,
3024 LocationFrom(calling_convention.GetRegisterAt(0)),
3025 LocationFrom(vixl::x0));
Alexandre Rames67555f72014-11-18 10:55:16 +00003026}
3027
3028void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
3029 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003030 Register current_method = InputRegisterAt(cls, 0);
Calin Juravle98893e12015-10-02 21:05:03 +01003031 if (cls->NeedsAccessCheck()) {
3032 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3033 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3034 cls,
3035 cls->GetDexPc(),
3036 nullptr);
3037 } else if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003038 DCHECK(!cls->CanCallRuntime());
3039 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003040 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003041 } else {
3042 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003043 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3044 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3045 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3046 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003047
3048 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3049 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3050 codegen_->AddSlowPath(slow_path);
3051 __ Cbz(out, slow_path->GetEntryLabel());
3052 if (cls->MustGenerateClinitCheck()) {
3053 GenerateClassInitializationCheck(slow_path, out);
3054 } else {
3055 __ Bind(slow_path->GetExitLabel());
3056 }
3057 }
3058}
3059
David Brazdilcb1c0552015-08-04 16:22:25 +01003060static MemOperand GetExceptionTlsAddress() {
3061 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3062}
3063
Alexandre Rames67555f72014-11-18 10:55:16 +00003064void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3065 LocationSummary* locations =
3066 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3067 locations->SetOut(Location::RequiresRegister());
3068}
3069
3070void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003071 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3072}
3073
3074void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3075 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3076}
3077
3078void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3079 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003080}
3081
Alexandre Rames5319def2014-10-23 10:03:10 +01003082void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3083 load->SetLocations(nullptr);
3084}
3085
3086void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3087 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003088 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003089}
3090
Alexandre Rames67555f72014-11-18 10:55:16 +00003091void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3092 LocationSummary* locations =
3093 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003094 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003095 locations->SetOut(Location::RequiresRegister());
3096}
3097
3098void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3099 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3100 codegen_->AddSlowPath(slow_path);
3101
3102 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003103 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003104 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003105 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3106 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3107 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003108 __ Cbz(out, slow_path->GetEntryLabel());
3109 __ Bind(slow_path->GetExitLabel());
3110}
3111
Alexandre Rames5319def2014-10-23 10:03:10 +01003112void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3113 local->SetLocations(nullptr);
3114}
3115
3116void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3117 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3118}
3119
3120void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3122 locations->SetOut(Location::ConstantLocation(constant));
3123}
3124
3125void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3126 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003127 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003128}
3129
Alexandre Rames67555f72014-11-18 10:55:16 +00003130void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3131 LocationSummary* locations =
3132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3133 InvokeRuntimeCallingConvention calling_convention;
3134 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3135}
3136
3137void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3138 codegen_->InvokeRuntime(instruction->IsEnter()
3139 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3140 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003141 instruction->GetDexPc(),
3142 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003143 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003144}
3145
Alexandre Rames42d641b2014-10-27 14:00:51 +00003146void LocationsBuilderARM64::VisitMul(HMul* mul) {
3147 LocationSummary* locations =
3148 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3149 switch (mul->GetResultType()) {
3150 case Primitive::kPrimInt:
3151 case Primitive::kPrimLong:
3152 locations->SetInAt(0, Location::RequiresRegister());
3153 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003155 break;
3156
3157 case Primitive::kPrimFloat:
3158 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003159 locations->SetInAt(0, Location::RequiresFpuRegister());
3160 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003161 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003162 break;
3163
3164 default:
3165 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3166 }
3167}
3168
3169void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3170 switch (mul->GetResultType()) {
3171 case Primitive::kPrimInt:
3172 case Primitive::kPrimLong:
3173 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3174 break;
3175
3176 case Primitive::kPrimFloat:
3177 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003178 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003179 break;
3180
3181 default:
3182 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3183 }
3184}
3185
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003186void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3187 LocationSummary* locations =
3188 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3189 switch (neg->GetResultType()) {
3190 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003191 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003192 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003194 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003195
3196 case Primitive::kPrimFloat:
3197 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003198 locations->SetInAt(0, Location::RequiresFpuRegister());
3199 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003200 break;
3201
3202 default:
3203 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3204 }
3205}
3206
3207void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3208 switch (neg->GetResultType()) {
3209 case Primitive::kPrimInt:
3210 case Primitive::kPrimLong:
3211 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3212 break;
3213
3214 case Primitive::kPrimFloat:
3215 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003216 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003217 break;
3218
3219 default:
3220 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3221 }
3222}
3223
3224void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3225 LocationSummary* locations =
3226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3227 InvokeRuntimeCallingConvention calling_convention;
3228 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003229 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003230 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003231 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003232 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003233 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003234}
3235
3236void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3237 LocationSummary* locations = instruction->GetLocations();
3238 InvokeRuntimeCallingConvention calling_convention;
3239 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3240 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003241 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003242 // Note: if heap poisoning is enabled, the entry point takes cares
3243 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003244 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3245 instruction,
3246 instruction->GetDexPc(),
3247 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003248 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003249}
3250
Alexandre Rames5319def2014-10-23 10:03:10 +01003251void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3252 LocationSummary* locations =
3253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3254 InvokeRuntimeCallingConvention calling_convention;
3255 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003256 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003257 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003258 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003259}
3260
3261void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3262 LocationSummary* locations = instruction->GetLocations();
3263 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3264 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003265 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003266 // Note: if heap poisoning is enabled, the entry point takes cares
3267 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003268 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3269 instruction,
3270 instruction->GetDexPc(),
3271 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003272 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003273}
3274
3275void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003277 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003278 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003279}
3280
3281void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003282 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003283 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003284 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003285 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003286 break;
3287
3288 default:
3289 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3290 }
3291}
3292
David Brazdil66d126e2015-04-03 16:02:44 +01003293void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3295 locations->SetInAt(0, Location::RequiresRegister());
3296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3297}
3298
3299void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003300 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3301}
3302
Alexandre Rames5319def2014-10-23 10:03:10 +01003303void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003304 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3305 ? LocationSummary::kCallOnSlowPath
3306 : LocationSummary::kNoCall;
3307 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003308 locations->SetInAt(0, Location::RequiresRegister());
3309 if (instruction->HasUses()) {
3310 locations->SetOut(Location::SameAsFirstInput());
3311 }
3312}
3313
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003314void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003315 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3316 return;
3317 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003318
Alexandre Ramesd921d642015-04-16 15:07:16 +01003319 BlockPoolsScope block_pools(GetVIXLAssembler());
3320 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003321 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3322 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3323}
3324
3325void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003326 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3327 codegen_->AddSlowPath(slow_path);
3328
3329 LocationSummary* locations = instruction->GetLocations();
3330 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003331
3332 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003333}
3334
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003335void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003336 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003337 GenerateImplicitNullCheck(instruction);
3338 } else {
3339 GenerateExplicitNullCheck(instruction);
3340 }
3341}
3342
Alexandre Rames67555f72014-11-18 10:55:16 +00003343void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3344 HandleBinaryOp(instruction);
3345}
3346
3347void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3348 HandleBinaryOp(instruction);
3349}
3350
Alexandre Rames3e69f162014-12-10 10:36:50 +00003351void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3352 LOG(FATAL) << "Unreachable";
3353}
3354
3355void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3356 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3357}
3358
Alexandre Rames5319def2014-10-23 10:03:10 +01003359void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3361 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3362 if (location.IsStackSlot()) {
3363 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3364 } else if (location.IsDoubleStackSlot()) {
3365 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3366 }
3367 locations->SetOut(location);
3368}
3369
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003370void InstructionCodeGeneratorARM64::VisitParameterValue(
3371 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003372 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003373}
3374
3375void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3376 LocationSummary* locations =
3377 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003378 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003379}
3380
3381void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3382 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3383 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003384}
3385
3386void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3387 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3388 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3389 locations->SetInAt(i, Location::Any());
3390 }
3391 locations->SetOut(Location::Any());
3392}
3393
3394void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003395 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003396 LOG(FATAL) << "Unreachable";
3397}
3398
Serban Constantinescu02164b32014-11-13 14:05:07 +00003399void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003400 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003401 LocationSummary::CallKind call_kind =
3402 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3404
3405 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003406 case Primitive::kPrimInt:
3407 case Primitive::kPrimLong:
3408 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003409 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3411 break;
3412
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003413 case Primitive::kPrimFloat:
3414 case Primitive::kPrimDouble: {
3415 InvokeRuntimeCallingConvention calling_convention;
3416 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3417 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3418 locations->SetOut(calling_convention.GetReturnLocation(type));
3419
3420 break;
3421 }
3422
Serban Constantinescu02164b32014-11-13 14:05:07 +00003423 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003424 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003425 }
3426}
3427
3428void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3429 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003430
Serban Constantinescu02164b32014-11-13 14:05:07 +00003431 switch (type) {
3432 case Primitive::kPrimInt:
3433 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003434 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003435 break;
3436 }
3437
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003438 case Primitive::kPrimFloat:
3439 case Primitive::kPrimDouble: {
3440 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3441 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003442 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003443 break;
3444 }
3445
Serban Constantinescu02164b32014-11-13 14:05:07 +00003446 default:
3447 LOG(FATAL) << "Unexpected rem type " << type;
3448 }
3449}
3450
Calin Juravle27df7582015-04-17 19:12:31 +01003451void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3452 memory_barrier->SetLocations(nullptr);
3453}
3454
3455void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3456 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3457}
3458
Alexandre Rames5319def2014-10-23 10:03:10 +01003459void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3461 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003462 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003463}
3464
3465void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003466 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003467 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003468}
3469
3470void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3471 instruction->SetLocations(nullptr);
3472}
3473
3474void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003475 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003476 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003477}
3478
Serban Constantinescu02164b32014-11-13 14:05:07 +00003479void LocationsBuilderARM64::VisitShl(HShl* shl) {
3480 HandleShift(shl);
3481}
3482
3483void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3484 HandleShift(shl);
3485}
3486
3487void LocationsBuilderARM64::VisitShr(HShr* shr) {
3488 HandleShift(shr);
3489}
3490
3491void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3492 HandleShift(shr);
3493}
3494
Alexandre Rames5319def2014-10-23 10:03:10 +01003495void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3497 Primitive::Type field_type = store->InputAt(1)->GetType();
3498 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003499 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003500 case Primitive::kPrimBoolean:
3501 case Primitive::kPrimByte:
3502 case Primitive::kPrimChar:
3503 case Primitive::kPrimShort:
3504 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003505 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003506 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3507 break;
3508
3509 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003510 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003511 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3512 break;
3513
3514 default:
3515 LOG(FATAL) << "Unimplemented local type " << field_type;
3516 }
3517}
3518
3519void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003520 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003521}
3522
3523void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003525}
3526
3527void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003529}
3530
Alexandre Rames67555f72014-11-18 10:55:16 +00003531void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003532 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003533}
3534
3535void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003536 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003537}
3538
3539void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003540 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003541}
3542
Alexandre Rames67555f72014-11-18 10:55:16 +00003543void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003544 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003545}
3546
Calin Juravlee460d1d2015-09-29 04:52:17 +01003547void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3548 HUnresolvedInstanceFieldGet* instruction) {
3549 FieldAccessCallingConventionARM64 calling_convention;
3550 codegen_->CreateUnresolvedFieldLocationSummary(
3551 instruction, instruction->GetFieldType(), calling_convention);
3552}
3553
3554void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3555 HUnresolvedInstanceFieldGet* instruction) {
3556 FieldAccessCallingConventionARM64 calling_convention;
3557 codegen_->GenerateUnresolvedFieldAccess(instruction,
3558 instruction->GetFieldType(),
3559 instruction->GetFieldIndex(),
3560 instruction->GetDexPc(),
3561 calling_convention);
3562}
3563
3564void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3565 HUnresolvedInstanceFieldSet* instruction) {
3566 FieldAccessCallingConventionARM64 calling_convention;
3567 codegen_->CreateUnresolvedFieldLocationSummary(
3568 instruction, instruction->GetFieldType(), calling_convention);
3569}
3570
3571void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3572 HUnresolvedInstanceFieldSet* instruction) {
3573 FieldAccessCallingConventionARM64 calling_convention;
3574 codegen_->GenerateUnresolvedFieldAccess(instruction,
3575 instruction->GetFieldType(),
3576 instruction->GetFieldIndex(),
3577 instruction->GetDexPc(),
3578 calling_convention);
3579}
3580
3581void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3582 HUnresolvedStaticFieldGet* instruction) {
3583 FieldAccessCallingConventionARM64 calling_convention;
3584 codegen_->CreateUnresolvedFieldLocationSummary(
3585 instruction, instruction->GetFieldType(), calling_convention);
3586}
3587
3588void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3589 HUnresolvedStaticFieldGet* instruction) {
3590 FieldAccessCallingConventionARM64 calling_convention;
3591 codegen_->GenerateUnresolvedFieldAccess(instruction,
3592 instruction->GetFieldType(),
3593 instruction->GetFieldIndex(),
3594 instruction->GetDexPc(),
3595 calling_convention);
3596}
3597
3598void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3599 HUnresolvedStaticFieldSet* instruction) {
3600 FieldAccessCallingConventionARM64 calling_convention;
3601 codegen_->CreateUnresolvedFieldLocationSummary(
3602 instruction, instruction->GetFieldType(), calling_convention);
3603}
3604
3605void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3606 HUnresolvedStaticFieldSet* instruction) {
3607 FieldAccessCallingConventionARM64 calling_convention;
3608 codegen_->GenerateUnresolvedFieldAccess(instruction,
3609 instruction->GetFieldType(),
3610 instruction->GetFieldIndex(),
3611 instruction->GetDexPc(),
3612 calling_convention);
3613}
3614
Alexandre Rames5319def2014-10-23 10:03:10 +01003615void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3616 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3617}
3618
3619void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003620 HBasicBlock* block = instruction->GetBlock();
3621 if (block->GetLoopInformation() != nullptr) {
3622 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3623 // The back edge will generate the suspend check.
3624 return;
3625 }
3626 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3627 // The goto will generate the suspend check.
3628 return;
3629 }
3630 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003631}
3632
3633void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3634 temp->SetLocations(nullptr);
3635}
3636
3637void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3638 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003639 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003640}
3641
Alexandre Rames67555f72014-11-18 10:55:16 +00003642void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3643 LocationSummary* locations =
3644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3645 InvokeRuntimeCallingConvention calling_convention;
3646 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3647}
3648
3649void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3650 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003651 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003652 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003653}
3654
3655void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3656 LocationSummary* locations =
3657 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3658 Primitive::Type input_type = conversion->GetInputType();
3659 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003660 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003661 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3662 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3663 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3664 }
3665
Alexandre Rames542361f2015-01-29 16:57:31 +00003666 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003667 locations->SetInAt(0, Location::RequiresFpuRegister());
3668 } else {
3669 locations->SetInAt(0, Location::RequiresRegister());
3670 }
3671
Alexandre Rames542361f2015-01-29 16:57:31 +00003672 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003673 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3674 } else {
3675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3676 }
3677}
3678
3679void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3680 Primitive::Type result_type = conversion->GetResultType();
3681 Primitive::Type input_type = conversion->GetInputType();
3682
3683 DCHECK_NE(input_type, result_type);
3684
Alexandre Rames542361f2015-01-29 16:57:31 +00003685 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003686 int result_size = Primitive::ComponentSize(result_type);
3687 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003688 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003689 Register output = OutputRegister(conversion);
3690 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003691 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3692 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003693 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3694 // 'int' values are used directly as W registers, discarding the top
3695 // bits, so we don't need to sign-extend and can just perform a move.
3696 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3697 // top 32 bits of the target register. We theoretically could leave those
3698 // bits unchanged, but we would have to make sure that no code uses a
3699 // 32bit input value as a 64bit value assuming that the top 32 bits are
3700 // zero.
3701 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003702 } else if ((result_type == Primitive::kPrimChar) ||
3703 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3704 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003705 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003706 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003707 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003708 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003709 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003710 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003711 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3712 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003713 } else if (Primitive::IsFloatingPointType(result_type) &&
3714 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003715 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3716 } else {
3717 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3718 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003719 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003720}
Alexandre Rames67555f72014-11-18 10:55:16 +00003721
Serban Constantinescu02164b32014-11-13 14:05:07 +00003722void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3723 HandleShift(ushr);
3724}
3725
3726void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3727 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003728}
3729
3730void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3731 HandleBinaryOp(instruction);
3732}
3733
3734void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3735 HandleBinaryOp(instruction);
3736}
3737
Calin Juravleb1498f62015-02-16 13:13:29 +00003738void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3739 // Nothing to do, this should be removed during prepare for register allocator.
3740 UNUSED(instruction);
3741 LOG(FATAL) << "Unreachable";
3742}
3743
3744void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3745 // Nothing to do, this should be removed during prepare for register allocator.
3746 UNUSED(instruction);
3747 LOG(FATAL) << "Unreachable";
3748}
3749
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003750void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3751 DCHECK(codegen_->IsBaseline());
3752 LocationSummary* locations =
3753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3754 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3755}
3756
3757void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3758 DCHECK(codegen_->IsBaseline());
3759 // Will be generated at use site.
3760}
3761
Mark Mendellfe57faa2015-09-18 09:26:15 -04003762// Simple implementation of packed switch - generate cascaded compare/jumps.
3763void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3764 LocationSummary* locations =
3765 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3766 locations->SetInAt(0, Location::RequiresRegister());
3767}
3768
3769void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3770 int32_t lower_bound = switch_instr->GetStartValue();
3771 int32_t num_entries = switch_instr->GetNumEntries();
3772 Register value_reg = InputRegisterAt(switch_instr, 0);
3773 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3774
3775 // Create a series of compare/jumps.
3776 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3777 for (int32_t i = 0; i < num_entries; i++) {
3778 int32_t case_value = lower_bound + i;
3779 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3780 if (case_value == 0) {
3781 __ Cbz(value_reg, succ);
3782 } else {
3783 __ Cmp(value_reg, vixl::Operand(case_value));
3784 __ B(eq, succ);
3785 }
3786 }
3787
3788 // And the default for any other value.
3789 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3790 __ B(codegen_->GetLabelOf(default_block));
3791 }
3792}
3793
Alexandre Rames67555f72014-11-18 10:55:16 +00003794#undef __
3795#undef QUICK_ENTRY_POINT
3796
Alexandre Rames5319def2014-10-23 10:03:10 +01003797} // namespace arm64
3798} // namespace art