blob: 39d6db77da658db7e4e79b2b7efdb26b3aae9f52 [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 }
867
868 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
869 while (!reserved_fp_baseline_registers.IsEmpty()) {
870 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
871 }
872 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100873}
874
875Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
876 if (type == Primitive::kPrimVoid) {
877 LOG(FATAL) << "Unreachable type " << type;
878 }
879
Alexandre Rames542361f2015-01-29 16:57:31 +0000880 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000881 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
882 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100883 return Location::FpuRegisterLocation(reg);
884 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000885 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
886 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100887 return Location::RegisterLocation(reg);
888 }
889}
890
Alexandre Rames3e69f162014-12-10 10:36:50 +0000891size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
892 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
893 __ Str(reg, MemOperand(sp, stack_index));
894 return kArm64WordSize;
895}
896
897size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
898 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
899 __ Ldr(reg, MemOperand(sp, stack_index));
900 return kArm64WordSize;
901}
902
903size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
904 FPRegister reg = FPRegister(reg_id, kDRegSize);
905 __ Str(reg, MemOperand(sp, stack_index));
906 return kArm64WordSize;
907}
908
909size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
910 FPRegister reg = FPRegister(reg_id, kDRegSize);
911 __ Ldr(reg, MemOperand(sp, stack_index));
912 return kArm64WordSize;
913}
914
Alexandre Rames5319def2014-10-23 10:03:10 +0100915void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100916 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100917}
918
919void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100920 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100921}
922
Alexandre Rames67555f72014-11-18 10:55:16 +0000923void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000924 if (constant->IsIntConstant()) {
925 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
926 } else if (constant->IsLongConstant()) {
927 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
928 } else if (constant->IsNullConstant()) {
929 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000930 } else if (constant->IsFloatConstant()) {
931 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
932 } else {
933 DCHECK(constant->IsDoubleConstant());
934 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
935 }
936}
937
Alexandre Rames3e69f162014-12-10 10:36:50 +0000938
939static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
940 DCHECK(constant.IsConstant());
941 HConstant* cst = constant.GetConstant();
942 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000943 // Null is mapped to a core W register, which we associate with kPrimInt.
944 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000945 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
946 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
947 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
948}
949
Calin Juravlee460d1d2015-09-29 04:52:17 +0100950void CodeGeneratorARM64::MoveLocation(Location destination,
951 Location source,
952 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000953 if (source.Equals(destination)) {
954 return;
955 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000956
957 // A valid move can always be inferred from the destination and source
958 // locations. When moving from and to a register, the argument type can be
959 // used to generate 32bit instead of 64bit moves. In debug mode we also
960 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100961 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000962
963 if (destination.IsRegister() || destination.IsFpuRegister()) {
964 if (unspecified_type) {
965 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
966 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000967 (src_cst != nullptr && (src_cst->IsIntConstant()
968 || src_cst->IsFloatConstant()
969 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000970 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100971 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000972 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000973 // If the source is a double stack slot or a 64bit constant, a 64bit
974 // type is appropriate. Else the source is a register, and since the
975 // type has not been specified, we chose a 64bit type to force a 64bit
976 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100977 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000978 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000979 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100980 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
981 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
982 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000983 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
984 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
985 __ Ldr(dst, StackOperandFrom(source));
986 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100987 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000988 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100989 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000990 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100991 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000992 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800993 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100994 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
995 ? Primitive::kPrimLong
996 : Primitive::kPrimInt;
997 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
998 }
999 } else {
1000 DCHECK(source.IsFpuRegister());
1001 if (destination.IsRegister()) {
1002 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1003 ? Primitive::kPrimDouble
1004 : Primitive::kPrimFloat;
1005 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1006 } else {
1007 DCHECK(destination.IsFpuRegister());
1008 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001009 }
1010 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001011 } else { // The destination is not a register. It must be a stack slot.
1012 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1013 if (source.IsRegister() || source.IsFpuRegister()) {
1014 if (unspecified_type) {
1015 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001016 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001017 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001018 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001019 }
1020 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1022 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1023 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001025 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1026 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001027 UseScratchRegisterScope temps(GetVIXLAssembler());
1028 HConstant* src_cst = source.GetConstant();
1029 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001030 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001031 temp = temps.AcquireW();
1032 } else if (src_cst->IsLongConstant()) {
1033 temp = temps.AcquireX();
1034 } else if (src_cst->IsFloatConstant()) {
1035 temp = temps.AcquireS();
1036 } else {
1037 DCHECK(src_cst->IsDoubleConstant());
1038 temp = temps.AcquireD();
1039 }
1040 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001041 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001042 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001043 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001044 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001045 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001046 // There is generally less pressure on FP registers.
1047 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001048 __ Ldr(temp, StackOperandFrom(source));
1049 __ Str(temp, StackOperandFrom(destination));
1050 }
1051 }
1052}
1053
1054void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001055 CPURegister dst,
1056 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001057 switch (type) {
1058 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001059 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001060 break;
1061 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001062 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001063 break;
1064 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001065 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001066 break;
1067 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001068 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001069 break;
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimNot:
1072 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001073 case Primitive::kPrimFloat:
1074 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001075 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001076 __ Ldr(dst, src);
1077 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001078 case Primitive::kPrimVoid:
1079 LOG(FATAL) << "Unreachable type " << type;
1080 }
1081}
1082
Calin Juravle77520bc2015-01-12 18:45:46 +00001083void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001084 CPURegister dst,
1085 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001086 MacroAssembler* masm = GetVIXLAssembler();
1087 BlockPoolsScope block_pools(masm);
1088 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001089 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001090 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001091
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001092 DCHECK(!src.IsPreIndex());
1093 DCHECK(!src.IsPostIndex());
1094
1095 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001096 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001097 MemOperand base = MemOperand(temp_base);
1098 switch (type) {
1099 case Primitive::kPrimBoolean:
1100 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001101 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001102 break;
1103 case Primitive::kPrimByte:
1104 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001105 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001106 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1107 break;
1108 case Primitive::kPrimChar:
1109 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001110 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001111 break;
1112 case Primitive::kPrimShort:
1113 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001114 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001115 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1116 break;
1117 case Primitive::kPrimInt:
1118 case Primitive::kPrimNot:
1119 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001120 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001121 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001122 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001123 break;
1124 case Primitive::kPrimFloat:
1125 case Primitive::kPrimDouble: {
1126 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001127 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001128
1129 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1130 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001131 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001132 __ Fmov(FPRegister(dst), temp);
1133 break;
1134 }
1135 case Primitive::kPrimVoid:
1136 LOG(FATAL) << "Unreachable type " << type;
1137 }
1138}
1139
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001140void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001141 CPURegister src,
1142 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001143 switch (type) {
1144 case Primitive::kPrimBoolean:
1145 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001146 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001147 break;
1148 case Primitive::kPrimChar:
1149 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001150 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001151 break;
1152 case Primitive::kPrimInt:
1153 case Primitive::kPrimNot:
1154 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001155 case Primitive::kPrimFloat:
1156 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001157 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001158 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001159 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001160 case Primitive::kPrimVoid:
1161 LOG(FATAL) << "Unreachable type " << type;
1162 }
1163}
1164
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001165void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1166 CPURegister src,
1167 const MemOperand& dst) {
1168 UseScratchRegisterScope temps(GetVIXLAssembler());
1169 Register temp_base = temps.AcquireX();
1170
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001171 DCHECK(!dst.IsPreIndex());
1172 DCHECK(!dst.IsPostIndex());
1173
1174 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001175 Operand op = OperandFromMemOperand(dst);
1176 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001177 MemOperand base = MemOperand(temp_base);
1178 switch (type) {
1179 case Primitive::kPrimBoolean:
1180 case Primitive::kPrimByte:
1181 __ Stlrb(Register(src), base);
1182 break;
1183 case Primitive::kPrimChar:
1184 case Primitive::kPrimShort:
1185 __ Stlrh(Register(src), base);
1186 break;
1187 case Primitive::kPrimInt:
1188 case Primitive::kPrimNot:
1189 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001190 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001191 __ Stlr(Register(src), base);
1192 break;
1193 case Primitive::kPrimFloat:
1194 case Primitive::kPrimDouble: {
1195 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001196 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001197
1198 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1199 __ Fmov(temp, FPRegister(src));
1200 __ Stlr(temp, base);
1201 break;
1202 }
1203 case Primitive::kPrimVoid:
1204 LOG(FATAL) << "Unreachable type " << type;
1205 }
1206}
1207
Calin Juravle175dc732015-08-25 15:42:32 +01001208void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1209 HInstruction* instruction,
1210 uint32_t dex_pc,
1211 SlowPathCode* slow_path) {
1212 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1213 instruction,
1214 dex_pc,
1215 slow_path);
1216}
1217
Alexandre Rames67555f72014-11-18 10:55:16 +00001218void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1219 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001220 uint32_t dex_pc,
1221 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001222 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001223 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001224 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1225 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001226 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001227}
1228
1229void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1230 vixl::Register class_reg) {
1231 UseScratchRegisterScope temps(GetVIXLAssembler());
1232 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001233 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001234 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001235
Serban Constantinescu02164b32014-11-13 14:05:07 +00001236 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001237 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001238 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1239 __ Add(temp, class_reg, status_offset);
1240 __ Ldar(temp, HeapOperand(temp));
1241 __ Cmp(temp, mirror::Class::kStatusInitialized);
1242 __ B(lt, slow_path->GetEntryLabel());
1243 } else {
1244 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1245 __ Cmp(temp, mirror::Class::kStatusInitialized);
1246 __ B(lt, slow_path->GetEntryLabel());
1247 __ Dmb(InnerShareable, BarrierReads);
1248 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001249 __ Bind(slow_path->GetExitLabel());
1250}
Alexandre Rames5319def2014-10-23 10:03:10 +01001251
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001252void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1253 BarrierType type = BarrierAll;
1254
1255 switch (kind) {
1256 case MemBarrierKind::kAnyAny:
1257 case MemBarrierKind::kAnyStore: {
1258 type = BarrierAll;
1259 break;
1260 }
1261 case MemBarrierKind::kLoadAny: {
1262 type = BarrierReads;
1263 break;
1264 }
1265 case MemBarrierKind::kStoreStore: {
1266 type = BarrierWrites;
1267 break;
1268 }
1269 default:
1270 LOG(FATAL) << "Unexpected memory barrier " << kind;
1271 }
1272 __ Dmb(InnerShareable, type);
1273}
1274
Serban Constantinescu02164b32014-11-13 14:05:07 +00001275void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1276 HBasicBlock* successor) {
1277 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001278 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1279 if (slow_path == nullptr) {
1280 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1281 instruction->SetSlowPath(slow_path);
1282 codegen_->AddSlowPath(slow_path);
1283 if (successor != nullptr) {
1284 DCHECK(successor->IsLoopHeader());
1285 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1286 }
1287 } else {
1288 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1289 }
1290
Serban Constantinescu02164b32014-11-13 14:05:07 +00001291 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1292 Register temp = temps.AcquireW();
1293
1294 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1295 if (successor == nullptr) {
1296 __ Cbnz(temp, slow_path->GetEntryLabel());
1297 __ Bind(slow_path->GetReturnLabel());
1298 } else {
1299 __ Cbz(temp, codegen_->GetLabelOf(successor));
1300 __ B(slow_path->GetEntryLabel());
1301 // slow_path will return to GetLabelOf(successor).
1302 }
1303}
1304
Alexandre Rames5319def2014-10-23 10:03:10 +01001305InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1306 CodeGeneratorARM64* codegen)
1307 : HGraphVisitor(graph),
1308 assembler_(codegen->GetAssembler()),
1309 codegen_(codegen) {}
1310
1311#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001312 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001313
1314#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1315
1316enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001317 // Using a base helps identify when we hit such breakpoints.
1318 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001319#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1320 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1321#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1322};
1323
1324#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1325 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001326 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001327 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1328 } \
1329 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1331 locations->SetOut(Location::Any()); \
1332 }
1333 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1334#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1335
1336#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001337#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001338
Alexandre Rames67555f72014-11-18 10:55:16 +00001339void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001340 DCHECK_EQ(instr->InputCount(), 2U);
1341 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1342 Primitive::Type type = instr->GetResultType();
1343 switch (type) {
1344 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001345 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001346 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001347 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001348 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001349 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001350
1351 case Primitive::kPrimFloat:
1352 case Primitive::kPrimDouble:
1353 locations->SetInAt(0, Location::RequiresFpuRegister());
1354 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001356 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001357
Alexandre Rames5319def2014-10-23 10:03:10 +01001358 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001359 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 }
1361}
1362
Alexandre Rames09a99962015-04-15 11:47:56 +01001363void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1364 LocationSummary* locations =
1365 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1366 locations->SetInAt(0, Location::RequiresRegister());
1367 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1368 locations->SetOut(Location::RequiresFpuRegister());
1369 } else {
1370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1371 }
1372}
1373
1374void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1375 const FieldInfo& field_info) {
1376 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001377 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001378 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001379
1380 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1381 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1382
1383 if (field_info.IsVolatile()) {
1384 if (use_acquire_release) {
1385 // NB: LoadAcquire will record the pc info if needed.
1386 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1387 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001388 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001389 codegen_->MaybeRecordImplicitNullCheck(instruction);
1390 // For IRIW sequential consistency kLoadAny is not sufficient.
1391 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1392 }
1393 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001394 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001395 codegen_->MaybeRecordImplicitNullCheck(instruction);
1396 }
Roland Levillain4d027112015-07-01 15:41:14 +01001397
1398 if (field_type == Primitive::kPrimNot) {
1399 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1400 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001401}
1402
1403void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1404 LocationSummary* locations =
1405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1406 locations->SetInAt(0, Location::RequiresRegister());
1407 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1408 locations->SetInAt(1, Location::RequiresFpuRegister());
1409 } else {
1410 locations->SetInAt(1, Location::RequiresRegister());
1411 }
1412}
1413
1414void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001415 const FieldInfo& field_info,
1416 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001417 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001418 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001419
1420 Register obj = InputRegisterAt(instruction, 0);
1421 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001422 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001423 Offset offset = field_info.GetFieldOffset();
1424 Primitive::Type field_type = field_info.GetFieldType();
1425 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1426
Roland Levillain4d027112015-07-01 15:41:14 +01001427 {
1428 // We use a block to end the scratch scope before the write barrier, thus
1429 // freeing the temporary registers so they can be used in `MarkGCCard`.
1430 UseScratchRegisterScope temps(GetVIXLAssembler());
1431
1432 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1433 DCHECK(value.IsW());
1434 Register temp = temps.AcquireW();
1435 __ Mov(temp, value.W());
1436 GetAssembler()->PoisonHeapReference(temp.W());
1437 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001438 }
Roland Levillain4d027112015-07-01 15:41:14 +01001439
1440 if (field_info.IsVolatile()) {
1441 if (use_acquire_release) {
1442 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1443 codegen_->MaybeRecordImplicitNullCheck(instruction);
1444 } else {
1445 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1446 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1447 codegen_->MaybeRecordImplicitNullCheck(instruction);
1448 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1449 }
1450 } else {
1451 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1452 codegen_->MaybeRecordImplicitNullCheck(instruction);
1453 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001454 }
1455
1456 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001457 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001458 }
1459}
1460
Alexandre Rames67555f72014-11-18 10:55:16 +00001461void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001462 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001463
1464 switch (type) {
1465 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001466 case Primitive::kPrimLong: {
1467 Register dst = OutputRegister(instr);
1468 Register lhs = InputRegisterAt(instr, 0);
1469 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001470 if (instr->IsAdd()) {
1471 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001472 } else if (instr->IsAnd()) {
1473 __ And(dst, lhs, rhs);
1474 } else if (instr->IsOr()) {
1475 __ Orr(dst, lhs, rhs);
1476 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001477 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001478 } else {
1479 DCHECK(instr->IsXor());
1480 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001481 }
1482 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001483 }
1484 case Primitive::kPrimFloat:
1485 case Primitive::kPrimDouble: {
1486 FPRegister dst = OutputFPRegister(instr);
1487 FPRegister lhs = InputFPRegisterAt(instr, 0);
1488 FPRegister rhs = InputFPRegisterAt(instr, 1);
1489 if (instr->IsAdd()) {
1490 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001491 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001492 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001493 } else {
1494 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001495 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001496 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001497 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001498 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001499 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001500 }
1501}
1502
Serban Constantinescu02164b32014-11-13 14:05:07 +00001503void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1504 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1505
1506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1507 Primitive::Type type = instr->GetResultType();
1508 switch (type) {
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimLong: {
1511 locations->SetInAt(0, Location::RequiresRegister());
1512 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1513 locations->SetOut(Location::RequiresRegister());
1514 break;
1515 }
1516 default:
1517 LOG(FATAL) << "Unexpected shift type " << type;
1518 }
1519}
1520
1521void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1522 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1523
1524 Primitive::Type type = instr->GetType();
1525 switch (type) {
1526 case Primitive::kPrimInt:
1527 case Primitive::kPrimLong: {
1528 Register dst = OutputRegister(instr);
1529 Register lhs = InputRegisterAt(instr, 0);
1530 Operand rhs = InputOperandAt(instr, 1);
1531 if (rhs.IsImmediate()) {
1532 uint32_t shift_value = (type == Primitive::kPrimInt)
1533 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1534 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1535 if (instr->IsShl()) {
1536 __ Lsl(dst, lhs, shift_value);
1537 } else if (instr->IsShr()) {
1538 __ Asr(dst, lhs, shift_value);
1539 } else {
1540 __ Lsr(dst, lhs, shift_value);
1541 }
1542 } else {
1543 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1544
1545 if (instr->IsShl()) {
1546 __ Lsl(dst, lhs, rhs_reg);
1547 } else if (instr->IsShr()) {
1548 __ Asr(dst, lhs, rhs_reg);
1549 } else {
1550 __ Lsr(dst, lhs, rhs_reg);
1551 }
1552 }
1553 break;
1554 }
1555 default:
1556 LOG(FATAL) << "Unexpected shift operation type " << type;
1557 }
1558}
1559
Alexandre Rames5319def2014-10-23 10:03:10 +01001560void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001561 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001562}
1563
1564void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001565 HandleBinaryOp(instruction);
1566}
1567
1568void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1569 HandleBinaryOp(instruction);
1570}
1571
1572void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1573 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001574}
1575
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001576void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1577 LocationSummary* locations =
1578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1579 locations->SetInAt(0, Location::RequiresRegister());
1580 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001581 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1582 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1583 } else {
1584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1585 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001586}
1587
1588void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1589 LocationSummary* locations = instruction->GetLocations();
1590 Primitive::Type type = instruction->GetType();
1591 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001592 Location index = locations->InAt(1);
1593 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001594 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001595 MacroAssembler* masm = GetVIXLAssembler();
1596 UseScratchRegisterScope temps(masm);
1597 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001598
1599 if (index.IsConstant()) {
1600 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001601 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001602 } else {
1603 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001604 __ Add(temp, obj, offset);
1605 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001606 }
1607
Alexandre Rames67555f72014-11-18 10:55:16 +00001608 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001609 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001610
1611 if (type == Primitive::kPrimNot) {
1612 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1613 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001614}
1615
Alexandre Rames5319def2014-10-23 10:03:10 +01001616void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1618 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001620}
1621
1622void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001623 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001624 __ Ldr(OutputRegister(instruction),
1625 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001626 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001627}
1628
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001629void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1631 instruction,
1632 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1633 locations->SetInAt(0, Location::RequiresRegister());
1634 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1635 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1636 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001637 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001638 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001639 }
1640}
1641
1642void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1643 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001644 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001645 bool may_need_runtime_call = locations->CanCall();
1646 bool needs_write_barrier =
1647 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001648
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001649 Register array = InputRegisterAt(instruction, 0);
1650 CPURegister value = InputCPURegisterAt(instruction, 2);
1651 CPURegister source = value;
1652 Location index = locations->InAt(1);
1653 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1654 MemOperand destination = HeapOperand(array);
1655 MacroAssembler* masm = GetVIXLAssembler();
1656 BlockPoolsScope block_pools(masm);
1657
1658 if (!needs_write_barrier) {
1659 DCHECK(!may_need_runtime_call);
1660 if (index.IsConstant()) {
1661 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1662 destination = HeapOperand(array, offset);
1663 } else {
1664 UseScratchRegisterScope temps(masm);
1665 Register temp = temps.AcquireSameSizeAs(array);
1666 __ Add(temp, array, offset);
1667 destination = HeapOperand(temp,
1668 XRegisterFrom(index),
1669 LSL,
1670 Primitive::ComponentSizeShift(value_type));
1671 }
1672 codegen_->Store(value_type, value, destination);
1673 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001674 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001675 DCHECK(needs_write_barrier);
1676 vixl::Label done;
1677 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001678 {
1679 // We use a block to end the scratch scope before the write barrier, thus
1680 // freeing the temporary registers so they can be used in `MarkGCCard`.
1681 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001682 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001683 if (index.IsConstant()) {
1684 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001685 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001686 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001687 destination = HeapOperand(temp,
1688 XRegisterFrom(index),
1689 LSL,
1690 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001691 }
1692
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001693 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1694 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1695 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1696
1697 if (may_need_runtime_call) {
1698 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1699 codegen_->AddSlowPath(slow_path);
1700 if (instruction->GetValueCanBeNull()) {
1701 vixl::Label non_zero;
1702 __ Cbnz(Register(value), &non_zero);
1703 if (!index.IsConstant()) {
1704 __ Add(temp, array, offset);
1705 }
1706 __ Str(wzr, destination);
1707 codegen_->MaybeRecordImplicitNullCheck(instruction);
1708 __ B(&done);
1709 __ Bind(&non_zero);
1710 }
1711
1712 Register temp2 = temps.AcquireSameSizeAs(array);
1713 __ Ldr(temp, HeapOperand(array, class_offset));
1714 codegen_->MaybeRecordImplicitNullCheck(instruction);
1715 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1716 __ Ldr(temp, HeapOperand(temp, component_offset));
1717 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1718 // No need to poison/unpoison, we're comparing two poisoned references.
1719 __ Cmp(temp, temp2);
1720 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1721 vixl::Label do_put;
1722 __ B(eq, &do_put);
1723 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1724 __ Ldr(temp, HeapOperand(temp, super_offset));
1725 // No need to unpoison, we're comparing against null.
1726 __ Cbnz(temp, slow_path->GetEntryLabel());
1727 __ Bind(&do_put);
1728 } else {
1729 __ B(ne, slow_path->GetEntryLabel());
1730 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001731 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001732 }
1733
1734 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001735 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001736 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001737 __ Mov(temp2, value.W());
1738 GetAssembler()->PoisonHeapReference(temp2);
1739 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001740 }
1741
1742 if (!index.IsConstant()) {
1743 __ Add(temp, array, offset);
1744 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001745 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001746
1747 if (!may_need_runtime_call) {
1748 codegen_->MaybeRecordImplicitNullCheck(instruction);
1749 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001750 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001751
1752 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1753
1754 if (done.IsLinked()) {
1755 __ Bind(&done);
1756 }
1757
1758 if (slow_path != nullptr) {
1759 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001760 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001761 }
1762}
1763
Alexandre Rames67555f72014-11-18 10:55:16 +00001764void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001765 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1766 ? LocationSummary::kCallOnSlowPath
1767 : LocationSummary::kNoCall;
1768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001769 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001770 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001771 if (instruction->HasUses()) {
1772 locations->SetOut(Location::SameAsFirstInput());
1773 }
1774}
1775
1776void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001777 BoundsCheckSlowPathARM64* slow_path =
1778 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001779 codegen_->AddSlowPath(slow_path);
1780
1781 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1782 __ B(slow_path->GetEntryLabel(), hs);
1783}
1784
Alexandre Rames67555f72014-11-18 10:55:16 +00001785void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1786 LocationSummary* locations =
1787 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1788 locations->SetInAt(0, Location::RequiresRegister());
1789 if (check->HasUses()) {
1790 locations->SetOut(Location::SameAsFirstInput());
1791 }
1792}
1793
1794void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1795 // We assume the class is not null.
1796 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1797 check->GetLoadClass(), check, check->GetDexPc(), true);
1798 codegen_->AddSlowPath(slow_path);
1799 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1800}
1801
Roland Levillain7f63c522015-07-13 15:54:55 +00001802static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1803 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1804 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1805}
1806
Serban Constantinescu02164b32014-11-13 14:05:07 +00001807void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001808 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001809 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1810 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001811 switch (in_type) {
1812 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001813 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001814 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1816 break;
1817 }
1818 case Primitive::kPrimFloat:
1819 case Primitive::kPrimDouble: {
1820 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001821 locations->SetInAt(1,
1822 IsFloatingPointZeroConstant(compare->InputAt(1))
1823 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1824 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001825 locations->SetOut(Location::RequiresRegister());
1826 break;
1827 }
1828 default:
1829 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1830 }
1831}
1832
1833void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1834 Primitive::Type in_type = compare->InputAt(0)->GetType();
1835
1836 // 0 if: left == right
1837 // 1 if: left > right
1838 // -1 if: left < right
1839 switch (in_type) {
1840 case Primitive::kPrimLong: {
1841 Register result = OutputRegister(compare);
1842 Register left = InputRegisterAt(compare, 0);
1843 Operand right = InputOperandAt(compare, 1);
1844
1845 __ Cmp(left, right);
1846 __ Cset(result, ne);
1847 __ Cneg(result, result, lt);
1848 break;
1849 }
1850 case Primitive::kPrimFloat:
1851 case Primitive::kPrimDouble: {
1852 Register result = OutputRegister(compare);
1853 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001854 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001855 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1856 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001857 __ Fcmp(left, 0.0);
1858 } else {
1859 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1860 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001861 if (compare->IsGtBias()) {
1862 __ Cset(result, ne);
1863 } else {
1864 __ Csetm(result, ne);
1865 }
1866 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001867 break;
1868 }
1869 default:
1870 LOG(FATAL) << "Unimplemented compare type " << in_type;
1871 }
1872}
1873
1874void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001876
1877 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1878 locations->SetInAt(0, Location::RequiresFpuRegister());
1879 locations->SetInAt(1,
1880 IsFloatingPointZeroConstant(instruction->InputAt(1))
1881 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1882 : Location::RequiresFpuRegister());
1883 } else {
1884 // Integer cases.
1885 locations->SetInAt(0, Location::RequiresRegister());
1886 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1887 }
1888
Alexandre Rames5319def2014-10-23 10:03:10 +01001889 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001890 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001891 }
1892}
1893
1894void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1895 if (!instruction->NeedsMaterialization()) {
1896 return;
1897 }
1898
1899 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001900 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001901 IfCondition if_cond = instruction->GetCondition();
1902 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001903
Roland Levillain7f63c522015-07-13 15:54:55 +00001904 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1905 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1906 if (locations->InAt(1).IsConstant()) {
1907 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1908 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1909 __ Fcmp(lhs, 0.0);
1910 } else {
1911 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1912 }
1913 __ Cset(res, arm64_cond);
1914 if (instruction->IsFPConditionTrueIfNaN()) {
1915 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1916 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1917 } else if (instruction->IsFPConditionFalseIfNaN()) {
1918 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1919 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1920 }
1921 } else {
1922 // Integer cases.
1923 Register lhs = InputRegisterAt(instruction, 0);
1924 Operand rhs = InputOperandAt(instruction, 1);
1925 __ Cmp(lhs, rhs);
1926 __ Cset(res, arm64_cond);
1927 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001928}
1929
1930#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1931 M(Equal) \
1932 M(NotEqual) \
1933 M(LessThan) \
1934 M(LessThanOrEqual) \
1935 M(GreaterThan) \
1936 M(GreaterThanOrEqual)
1937#define DEFINE_CONDITION_VISITORS(Name) \
1938void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1939void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1940FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001941#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001942#undef FOR_EACH_CONDITION_INSTRUCTION
1943
Zheng Xuc6667102015-05-15 16:08:45 +08001944void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1945 DCHECK(instruction->IsDiv() || instruction->IsRem());
1946
1947 LocationSummary* locations = instruction->GetLocations();
1948 Location second = locations->InAt(1);
1949 DCHECK(second.IsConstant());
1950
1951 Register out = OutputRegister(instruction);
1952 Register dividend = InputRegisterAt(instruction, 0);
1953 int64_t imm = Int64FromConstant(second.GetConstant());
1954 DCHECK(imm == 1 || imm == -1);
1955
1956 if (instruction->IsRem()) {
1957 __ Mov(out, 0);
1958 } else {
1959 if (imm == 1) {
1960 __ Mov(out, dividend);
1961 } else {
1962 __ Neg(out, dividend);
1963 }
1964 }
1965}
1966
1967void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1968 DCHECK(instruction->IsDiv() || instruction->IsRem());
1969
1970 LocationSummary* locations = instruction->GetLocations();
1971 Location second = locations->InAt(1);
1972 DCHECK(second.IsConstant());
1973
1974 Register out = OutputRegister(instruction);
1975 Register dividend = InputRegisterAt(instruction, 0);
1976 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001977 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001978 DCHECK(IsPowerOfTwo(abs_imm));
1979 int ctz_imm = CTZ(abs_imm);
1980
1981 UseScratchRegisterScope temps(GetVIXLAssembler());
1982 Register temp = temps.AcquireSameSizeAs(out);
1983
1984 if (instruction->IsDiv()) {
1985 __ Add(temp, dividend, abs_imm - 1);
1986 __ Cmp(dividend, 0);
1987 __ Csel(out, temp, dividend, lt);
1988 if (imm > 0) {
1989 __ Asr(out, out, ctz_imm);
1990 } else {
1991 __ Neg(out, Operand(out, ASR, ctz_imm));
1992 }
1993 } else {
1994 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1995 __ Asr(temp, dividend, bits - 1);
1996 __ Lsr(temp, temp, bits - ctz_imm);
1997 __ Add(out, dividend, temp);
1998 __ And(out, out, abs_imm - 1);
1999 __ Sub(out, out, temp);
2000 }
2001}
2002
2003void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2004 DCHECK(instruction->IsDiv() || instruction->IsRem());
2005
2006 LocationSummary* locations = instruction->GetLocations();
2007 Location second = locations->InAt(1);
2008 DCHECK(second.IsConstant());
2009
2010 Register out = OutputRegister(instruction);
2011 Register dividend = InputRegisterAt(instruction, 0);
2012 int64_t imm = Int64FromConstant(second.GetConstant());
2013
2014 Primitive::Type type = instruction->GetResultType();
2015 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2016
2017 int64_t magic;
2018 int shift;
2019 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2020
2021 UseScratchRegisterScope temps(GetVIXLAssembler());
2022 Register temp = temps.AcquireSameSizeAs(out);
2023
2024 // temp = get_high(dividend * magic)
2025 __ Mov(temp, magic);
2026 if (type == Primitive::kPrimLong) {
2027 __ Smulh(temp, dividend, temp);
2028 } else {
2029 __ Smull(temp.X(), dividend, temp);
2030 __ Lsr(temp.X(), temp.X(), 32);
2031 }
2032
2033 if (imm > 0 && magic < 0) {
2034 __ Add(temp, temp, dividend);
2035 } else if (imm < 0 && magic > 0) {
2036 __ Sub(temp, temp, dividend);
2037 }
2038
2039 if (shift != 0) {
2040 __ Asr(temp, temp, shift);
2041 }
2042
2043 if (instruction->IsDiv()) {
2044 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2045 } else {
2046 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2047 // TODO: Strength reduction for msub.
2048 Register temp_imm = temps.AcquireSameSizeAs(out);
2049 __ Mov(temp_imm, imm);
2050 __ Msub(out, temp, temp_imm, dividend);
2051 }
2052}
2053
2054void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2055 DCHECK(instruction->IsDiv() || instruction->IsRem());
2056 Primitive::Type type = instruction->GetResultType();
2057 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2058
2059 LocationSummary* locations = instruction->GetLocations();
2060 Register out = OutputRegister(instruction);
2061 Location second = locations->InAt(1);
2062
2063 if (second.IsConstant()) {
2064 int64_t imm = Int64FromConstant(second.GetConstant());
2065
2066 if (imm == 0) {
2067 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2068 } else if (imm == 1 || imm == -1) {
2069 DivRemOneOrMinusOne(instruction);
2070 } else if (IsPowerOfTwo(std::abs(imm))) {
2071 DivRemByPowerOfTwo(instruction);
2072 } else {
2073 DCHECK(imm <= -2 || imm >= 2);
2074 GenerateDivRemWithAnyConstant(instruction);
2075 }
2076 } else {
2077 Register dividend = InputRegisterAt(instruction, 0);
2078 Register divisor = InputRegisterAt(instruction, 1);
2079 if (instruction->IsDiv()) {
2080 __ Sdiv(out, dividend, divisor);
2081 } else {
2082 UseScratchRegisterScope temps(GetVIXLAssembler());
2083 Register temp = temps.AcquireSameSizeAs(out);
2084 __ Sdiv(temp, dividend, divisor);
2085 __ Msub(out, temp, divisor, dividend);
2086 }
2087 }
2088}
2089
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002090void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2091 LocationSummary* locations =
2092 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2093 switch (div->GetResultType()) {
2094 case Primitive::kPrimInt:
2095 case Primitive::kPrimLong:
2096 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002097 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2099 break;
2100
2101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble:
2103 locations->SetInAt(0, Location::RequiresFpuRegister());
2104 locations->SetInAt(1, Location::RequiresFpuRegister());
2105 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2106 break;
2107
2108 default:
2109 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2110 }
2111}
2112
2113void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2114 Primitive::Type type = div->GetResultType();
2115 switch (type) {
2116 case Primitive::kPrimInt:
2117 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002118 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002119 break;
2120
2121 case Primitive::kPrimFloat:
2122 case Primitive::kPrimDouble:
2123 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected div type " << type;
2128 }
2129}
2130
Alexandre Rames67555f72014-11-18 10:55:16 +00002131void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002132 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2133 ? LocationSummary::kCallOnSlowPath
2134 : LocationSummary::kNoCall;
2135 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002136 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2137 if (instruction->HasUses()) {
2138 locations->SetOut(Location::SameAsFirstInput());
2139 }
2140}
2141
2142void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2143 SlowPathCodeARM64* slow_path =
2144 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2145 codegen_->AddSlowPath(slow_path);
2146 Location value = instruction->GetLocations()->InAt(0);
2147
Alexandre Rames3e69f162014-12-10 10:36:50 +00002148 Primitive::Type type = instruction->GetType();
2149
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002150 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2151 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002152 return;
2153 }
2154
Alexandre Rames67555f72014-11-18 10:55:16 +00002155 if (value.IsConstant()) {
2156 int64_t divisor = Int64ConstantFrom(value);
2157 if (divisor == 0) {
2158 __ B(slow_path->GetEntryLabel());
2159 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002160 // A division by a non-null constant is valid. We don't need to perform
2161 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002162 }
2163 } else {
2164 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2165 }
2166}
2167
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002168void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2171 locations->SetOut(Location::ConstantLocation(constant));
2172}
2173
2174void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2175 UNUSED(constant);
2176 // Will be generated at use site.
2177}
2178
Alexandre Rames5319def2014-10-23 10:03:10 +01002179void LocationsBuilderARM64::VisitExit(HExit* exit) {
2180 exit->SetLocations(nullptr);
2181}
2182
2183void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002184 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002185}
2186
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002187void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2188 LocationSummary* locations =
2189 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2190 locations->SetOut(Location::ConstantLocation(constant));
2191}
2192
2193void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2194 UNUSED(constant);
2195 // Will be generated at use site.
2196}
2197
David Brazdilfc6a86a2015-06-26 10:33:45 +00002198void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002199 DCHECK(!successor->IsExitBlock());
2200 HBasicBlock* block = got->GetBlock();
2201 HInstruction* previous = got->GetPrevious();
2202 HLoopInformation* info = block->GetLoopInformation();
2203
David Brazdil46e2a392015-03-16 17:31:52 +00002204 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002205 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2206 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2207 return;
2208 }
2209 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2210 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2211 }
2212 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002213 __ B(codegen_->GetLabelOf(successor));
2214 }
2215}
2216
David Brazdilfc6a86a2015-06-26 10:33:45 +00002217void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2218 got->SetLocations(nullptr);
2219}
2220
2221void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2222 HandleGoto(got, got->GetSuccessor());
2223}
2224
2225void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2226 try_boundary->SetLocations(nullptr);
2227}
2228
2229void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2230 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2231 if (!successor->IsExitBlock()) {
2232 HandleGoto(try_boundary, successor);
2233 }
2234}
2235
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002236void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2237 vixl::Label* true_target,
2238 vixl::Label* false_target,
2239 vixl::Label* always_true_target) {
2240 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002241 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002242
Serban Constantinescu02164b32014-11-13 14:05:07 +00002243 if (cond->IsIntConstant()) {
2244 int32_t cond_value = cond->AsIntConstant()->GetValue();
2245 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002246 if (always_true_target != nullptr) {
2247 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002248 }
2249 return;
2250 } else {
2251 DCHECK_EQ(cond_value, 0);
2252 }
2253 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002254 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002255 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002256 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002257 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 } else {
2259 // The condition instruction has not been materialized, use its inputs as
2260 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002261 Primitive::Type type =
2262 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2263
2264 if (Primitive::IsFloatingPointType(type)) {
2265 // FP compares don't like null false_targets.
2266 if (false_target == nullptr) {
2267 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002268 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002269 FPRegister lhs = InputFPRegisterAt(condition, 0);
2270 if (condition->GetLocations()->InAt(1).IsConstant()) {
2271 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2272 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2273 __ Fcmp(lhs, 0.0);
2274 } else {
2275 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2276 }
2277 if (condition->IsFPConditionTrueIfNaN()) {
2278 __ B(vs, true_target); // VS for unordered.
2279 } else if (condition->IsFPConditionFalseIfNaN()) {
2280 __ B(vs, false_target); // VS for unordered.
2281 }
2282 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002283 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002284 // Integer cases.
2285 Register lhs = InputRegisterAt(condition, 0);
2286 Operand rhs = InputOperandAt(condition, 1);
2287 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2288 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2289 switch (arm64_cond) {
2290 case eq:
2291 __ Cbz(lhs, true_target);
2292 break;
2293 case ne:
2294 __ Cbnz(lhs, true_target);
2295 break;
2296 case lt:
2297 // Test the sign bit and branch accordingly.
2298 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2299 break;
2300 case ge:
2301 // Test the sign bit and branch accordingly.
2302 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2303 break;
2304 default:
2305 // Without the `static_cast` the compiler throws an error for
2306 // `-Werror=sign-promo`.
2307 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2308 }
2309 } else {
2310 __ Cmp(lhs, rhs);
2311 __ B(arm64_cond, true_target);
2312 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002313 }
2314 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002315 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002316 __ B(false_target);
2317 }
2318}
2319
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002320void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2322 HInstruction* cond = if_instr->InputAt(0);
2323 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2324 locations->SetInAt(0, Location::RequiresRegister());
2325 }
2326}
2327
2328void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2329 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2330 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2331 vixl::Label* always_true_target = true_target;
2332 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2333 if_instr->IfTrueSuccessor())) {
2334 always_true_target = nullptr;
2335 }
2336 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2337 if_instr->IfFalseSuccessor())) {
2338 false_target = nullptr;
2339 }
2340 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2341}
2342
2343void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2344 LocationSummary* locations = new (GetGraph()->GetArena())
2345 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2346 HInstruction* cond = deoptimize->InputAt(0);
2347 DCHECK(cond->IsCondition());
2348 if (cond->AsCondition()->NeedsMaterialization()) {
2349 locations->SetInAt(0, Location::RequiresRegister());
2350 }
2351}
2352
2353void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2354 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2355 DeoptimizationSlowPathARM64(deoptimize);
2356 codegen_->AddSlowPath(slow_path);
2357 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2358 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2359}
2360
Alexandre Rames5319def2014-10-23 10:03:10 +01002361void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002362 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002363}
2364
2365void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002366 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002367}
2368
2369void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002370 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002371}
2372
2373void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002374 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002375}
2376
Alexandre Rames67555f72014-11-18 10:55:16 +00002377void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002378 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2379 switch (instruction->GetTypeCheckKind()) {
2380 case TypeCheckKind::kExactCheck:
2381 case TypeCheckKind::kAbstractClassCheck:
2382 case TypeCheckKind::kClassHierarchyCheck:
2383 case TypeCheckKind::kArrayObjectCheck:
2384 call_kind = LocationSummary::kNoCall;
2385 break;
2386 case TypeCheckKind::kInterfaceCheck:
2387 call_kind = LocationSummary::kCall;
2388 break;
2389 case TypeCheckKind::kArrayCheck:
2390 call_kind = LocationSummary::kCallOnSlowPath;
2391 break;
2392 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002394 if (call_kind != LocationSummary::kCall) {
2395 locations->SetInAt(0, Location::RequiresRegister());
2396 locations->SetInAt(1, Location::RequiresRegister());
2397 // The out register is used as a temporary, so it overlaps with the inputs.
2398 // Note that TypeCheckSlowPathARM64 uses this register too.
2399 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2400 } else {
2401 InvokeRuntimeCallingConvention calling_convention;
2402 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2403 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2404 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2405 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002406}
2407
2408void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2409 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002410 Register obj = InputRegisterAt(instruction, 0);
2411 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002412 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002413 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2414 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2415 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2416 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002417
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002418 vixl::Label done, zero;
2419 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002420
2421 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002422 // Avoid null check if we know `obj` is not null.
2423 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002424 __ Cbz(obj, &zero);
2425 }
2426
2427 // In case of an interface check, we put the object class into the object register.
2428 // This is safe, as the register is caller-save, and the object must be in another
2429 // register if it survives the runtime call.
2430 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2431 ? obj
2432 : out;
2433 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2434 GetAssembler()->MaybeUnpoisonHeapReference(target);
2435
2436 switch (instruction->GetTypeCheckKind()) {
2437 case TypeCheckKind::kExactCheck: {
2438 __ Cmp(out, cls);
2439 __ Cset(out, eq);
2440 if (zero.IsLinked()) {
2441 __ B(&done);
2442 }
2443 break;
2444 }
2445 case TypeCheckKind::kAbstractClassCheck: {
2446 // If the class is abstract, we eagerly fetch the super class of the
2447 // object to avoid doing a comparison we know will fail.
2448 vixl::Label loop, success;
2449 __ Bind(&loop);
2450 __ Ldr(out, HeapOperand(out, super_offset));
2451 GetAssembler()->MaybeUnpoisonHeapReference(out);
2452 // If `out` is null, we use it for the result, and jump to `done`.
2453 __ Cbz(out, &done);
2454 __ Cmp(out, cls);
2455 __ B(ne, &loop);
2456 __ Mov(out, 1);
2457 if (zero.IsLinked()) {
2458 __ B(&done);
2459 }
2460 break;
2461 }
2462 case TypeCheckKind::kClassHierarchyCheck: {
2463 // Walk over the class hierarchy to find a match.
2464 vixl::Label loop, success;
2465 __ Bind(&loop);
2466 __ Cmp(out, cls);
2467 __ B(eq, &success);
2468 __ Ldr(out, HeapOperand(out, super_offset));
2469 GetAssembler()->MaybeUnpoisonHeapReference(out);
2470 __ Cbnz(out, &loop);
2471 // If `out` is null, we use it for the result, and jump to `done`.
2472 __ B(&done);
2473 __ Bind(&success);
2474 __ Mov(out, 1);
2475 if (zero.IsLinked()) {
2476 __ B(&done);
2477 }
2478 break;
2479 }
2480 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002481 // Do an exact check.
2482 vixl::Label exact_check;
2483 __ Cmp(out, cls);
2484 __ B(eq, &exact_check);
2485 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002486 __ Ldr(out, HeapOperand(out, component_offset));
2487 GetAssembler()->MaybeUnpoisonHeapReference(out);
2488 // If `out` is null, we use it for the result, and jump to `done`.
2489 __ Cbz(out, &done);
2490 __ Ldrh(out, HeapOperand(out, primitive_offset));
2491 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2492 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002493 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002494 __ Mov(out, 1);
2495 __ B(&done);
2496 break;
2497 }
2498 case TypeCheckKind::kArrayCheck: {
2499 __ Cmp(out, cls);
2500 DCHECK(locations->OnlyCallsOnSlowPath());
2501 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2502 instruction, /* is_fatal */ false);
2503 codegen_->AddSlowPath(slow_path);
2504 __ B(ne, slow_path->GetEntryLabel());
2505 __ Mov(out, 1);
2506 if (zero.IsLinked()) {
2507 __ B(&done);
2508 }
2509 break;
2510 }
2511
2512 case TypeCheckKind::kInterfaceCheck:
2513 default: {
2514 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2515 instruction,
2516 instruction->GetDexPc(),
2517 nullptr);
2518 if (zero.IsLinked()) {
2519 __ B(&done);
2520 }
2521 break;
2522 }
2523 }
2524
2525 if (zero.IsLinked()) {
2526 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002527 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002528 }
2529
2530 if (done.IsLinked()) {
2531 __ Bind(&done);
2532 }
2533
2534 if (slow_path != nullptr) {
2535 __ Bind(slow_path->GetExitLabel());
2536 }
2537}
2538
2539void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2540 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2541 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2542
2543 switch (instruction->GetTypeCheckKind()) {
2544 case TypeCheckKind::kExactCheck:
2545 case TypeCheckKind::kAbstractClassCheck:
2546 case TypeCheckKind::kClassHierarchyCheck:
2547 case TypeCheckKind::kArrayObjectCheck:
2548 call_kind = throws_into_catch
2549 ? LocationSummary::kCallOnSlowPath
2550 : LocationSummary::kNoCall;
2551 break;
2552 case TypeCheckKind::kInterfaceCheck:
2553 call_kind = LocationSummary::kCall;
2554 break;
2555 case TypeCheckKind::kArrayCheck:
2556 call_kind = LocationSummary::kCallOnSlowPath;
2557 break;
2558 }
2559
2560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2561 instruction, call_kind);
2562 if (call_kind != LocationSummary::kCall) {
2563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetInAt(1, Location::RequiresRegister());
2565 // Note that TypeCheckSlowPathARM64 uses this register too.
2566 locations->AddTemp(Location::RequiresRegister());
2567 } else {
2568 InvokeRuntimeCallingConvention calling_convention;
2569 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2570 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2571 }
2572}
2573
2574void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2575 LocationSummary* locations = instruction->GetLocations();
2576 Register obj = InputRegisterAt(instruction, 0);
2577 Register cls = InputRegisterAt(instruction, 1);
2578 Register temp;
2579 if (!locations->WillCall()) {
2580 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2581 }
2582
2583 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2584 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2585 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2586 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2587 SlowPathCodeARM64* slow_path = nullptr;
2588
2589 if (!locations->WillCall()) {
2590 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2591 instruction, !locations->CanCall());
2592 codegen_->AddSlowPath(slow_path);
2593 }
2594
2595 vixl::Label done;
2596 // Avoid null check if we know obj is not null.
2597 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002598 __ Cbz(obj, &done);
2599 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002600
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002601 if (locations->WillCall()) {
2602 __ Ldr(obj, HeapOperand(obj, class_offset));
2603 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002604 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002605 __ Ldr(temp, HeapOperand(obj, class_offset));
2606 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002607 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002609 switch (instruction->GetTypeCheckKind()) {
2610 case TypeCheckKind::kExactCheck:
2611 case TypeCheckKind::kArrayCheck: {
2612 __ Cmp(temp, cls);
2613 // Jump to slow path for throwing the exception or doing a
2614 // more involved array check.
2615 __ B(ne, slow_path->GetEntryLabel());
2616 break;
2617 }
2618 case TypeCheckKind::kAbstractClassCheck: {
2619 // If the class is abstract, we eagerly fetch the super class of the
2620 // object to avoid doing a comparison we know will fail.
2621 vixl::Label loop;
2622 __ Bind(&loop);
2623 __ Ldr(temp, HeapOperand(temp, super_offset));
2624 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2625 // Jump to the slow path to throw the exception.
2626 __ Cbz(temp, slow_path->GetEntryLabel());
2627 __ Cmp(temp, cls);
2628 __ B(ne, &loop);
2629 break;
2630 }
2631 case TypeCheckKind::kClassHierarchyCheck: {
2632 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002633 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002634 __ Bind(&loop);
2635 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002636 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002637 __ Ldr(temp, HeapOperand(temp, super_offset));
2638 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2639 __ Cbnz(temp, &loop);
2640 // Jump to the slow path to throw the exception.
2641 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002642 break;
2643 }
2644 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002645 // Do an exact check.
2646 __ Cmp(temp, cls);
2647 __ B(eq, &done);
2648 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002649 __ Ldr(temp, HeapOperand(temp, component_offset));
2650 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2651 __ Cbz(temp, slow_path->GetEntryLabel());
2652 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2653 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2654 __ Cbnz(temp, slow_path->GetEntryLabel());
2655 break;
2656 }
2657 case TypeCheckKind::kInterfaceCheck:
2658 default:
2659 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2660 instruction,
2661 instruction->GetDexPc(),
2662 nullptr);
2663 break;
2664 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002665 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002666
2667 if (slow_path != nullptr) {
2668 __ Bind(slow_path->GetExitLabel());
2669 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002670}
2671
Alexandre Rames5319def2014-10-23 10:03:10 +01002672void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2673 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2674 locations->SetOut(Location::ConstantLocation(constant));
2675}
2676
2677void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2678 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002679 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002680}
2681
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002682void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2683 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2684 locations->SetOut(Location::ConstantLocation(constant));
2685}
2686
2687void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2688 // Will be generated at use site.
2689 UNUSED(constant);
2690}
2691
Calin Juravle175dc732015-08-25 15:42:32 +01002692void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2693 // The trampoline uses the same calling convention as dex calling conventions,
2694 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2695 // the method_idx.
2696 HandleInvoke(invoke);
2697}
2698
2699void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2700 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2701}
2702
Alexandre Rames5319def2014-10-23 10:03:10 +01002703void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002704 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002705 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002706}
2707
Alexandre Rames67555f72014-11-18 10:55:16 +00002708void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2709 HandleInvoke(invoke);
2710}
2711
2712void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2713 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002714 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2715 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2716 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002717 Location receiver = invoke->GetLocations()->InAt(0);
2718 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002719 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002720
2721 // The register ip1 is required to be used for the hidden argument in
2722 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002723 MacroAssembler* masm = GetVIXLAssembler();
2724 UseScratchRegisterScope scratch_scope(masm);
2725 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002726 scratch_scope.Exclude(ip1);
2727 __ Mov(ip1, invoke->GetDexMethodIndex());
2728
2729 // temp = object->GetClass();
2730 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002731 __ Ldr(temp.W(), StackOperandFrom(receiver));
2732 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002733 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002734 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002735 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002736 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002737 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002738 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002739 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002740 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002741 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002742 // lr();
2743 __ Blr(lr);
2744 DCHECK(!codegen_->IsLeafMethod());
2745 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2746}
2747
2748void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002749 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2750 if (intrinsic.TryDispatch(invoke)) {
2751 return;
2752 }
2753
Alexandre Rames67555f72014-11-18 10:55:16 +00002754 HandleInvoke(invoke);
2755}
2756
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002757void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002758 // When we do not run baseline, explicit clinit checks triggered by static
2759 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2760 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002761
Andreas Gampe878d58c2015-01-15 23:24:00 -08002762 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2763 if (intrinsic.TryDispatch(invoke)) {
2764 return;
2765 }
2766
Alexandre Rames67555f72014-11-18 10:55:16 +00002767 HandleInvoke(invoke);
2768}
2769
Andreas Gampe878d58c2015-01-15 23:24:00 -08002770static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2771 if (invoke->GetLocations()->Intrinsified()) {
2772 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2773 intrinsic.Dispatch(invoke);
2774 return true;
2775 }
2776 return false;
2777}
2778
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002779void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002780 // For better instruction scheduling we load the direct code pointer before the method pointer.
2781 bool direct_code_loaded = false;
2782 switch (invoke->GetCodePtrLocation()) {
2783 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2784 // LR = code address from literal pool with link-time patch.
2785 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2786 direct_code_loaded = true;
2787 break;
2788 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2789 // LR = invoke->GetDirectCodePtr();
2790 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2791 direct_code_loaded = true;
2792 break;
2793 default:
2794 break;
2795 }
2796
Andreas Gampe878d58c2015-01-15 23:24:00 -08002797 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002798 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2799 switch (invoke->GetMethodLoadKind()) {
2800 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2801 // temp = thread->string_init_entrypoint
2802 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2803 break;
2804 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2805 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2806 break;
2807 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2808 // Load method address from literal pool.
2809 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2810 break;
2811 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2812 // Load method address from literal pool with a link-time patch.
2813 __ Ldr(XRegisterFrom(temp).X(),
2814 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2815 break;
2816 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2817 // Add ADRP with its PC-relative DexCache access patch.
2818 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2819 invoke->GetDexCacheArrayOffset());
2820 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2821 {
2822 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2823 __ adrp(XRegisterFrom(temp).X(), 0);
2824 }
2825 __ Bind(pc_insn_label); // Bind after ADRP.
2826 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2827 // Add LDR with its PC-relative DexCache access patch.
2828 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2829 invoke->GetDexCacheArrayOffset());
2830 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2831 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2832 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2833 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002834 }
Vladimir Marko58155012015-08-19 12:49:41 +00002835 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2836 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2837 Register reg = XRegisterFrom(temp);
2838 Register method_reg;
2839 if (current_method.IsRegister()) {
2840 method_reg = XRegisterFrom(current_method);
2841 } else {
2842 DCHECK(invoke->GetLocations()->Intrinsified());
2843 DCHECK(!current_method.IsValid());
2844 method_reg = reg;
2845 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2846 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002847
Vladimir Marko58155012015-08-19 12:49:41 +00002848 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002849 __ Ldr(reg.X(),
2850 MemOperand(method_reg.X(),
2851 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002852 // temp = temp[index_in_cache];
2853 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2854 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2855 break;
2856 }
2857 }
2858
2859 switch (invoke->GetCodePtrLocation()) {
2860 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2861 __ Bl(&frame_entry_label_);
2862 break;
2863 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2864 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2865 vixl::Label* label = &relative_call_patches_.back().label;
2866 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2867 __ Bind(label); // Bind after BL.
2868 break;
2869 }
2870 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2871 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2872 // LR prepared above for better instruction scheduling.
2873 DCHECK(direct_code_loaded);
2874 // lr()
2875 __ Blr(lr);
2876 break;
2877 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2878 // LR = callee_method->entry_point_from_quick_compiled_code_;
2879 __ Ldr(lr, MemOperand(
2880 XRegisterFrom(callee_method).X(),
2881 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2882 // lr()
2883 __ Blr(lr);
2884 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002885 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002886
Andreas Gampe878d58c2015-01-15 23:24:00 -08002887 DCHECK(!IsLeafMethod());
2888}
2889
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002890void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2891 LocationSummary* locations = invoke->GetLocations();
2892 Location receiver = locations->InAt(0);
2893 Register temp = XRegisterFrom(temp_in);
2894 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2895 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2896 Offset class_offset = mirror::Object::ClassOffset();
2897 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2898
2899 BlockPoolsScope block_pools(GetVIXLAssembler());
2900
2901 DCHECK(receiver.IsRegister());
2902 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2903 MaybeRecordImplicitNullCheck(invoke);
2904 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2905 // temp = temp->GetMethodAt(method_offset);
2906 __ Ldr(temp, MemOperand(temp, method_offset));
2907 // lr = temp->GetEntryPoint();
2908 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2909 // lr();
2910 __ Blr(lr);
2911}
2912
Vladimir Marko58155012015-08-19 12:49:41 +00002913void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2914 DCHECK(linker_patches->empty());
2915 size_t size =
2916 method_patches_.size() +
2917 call_patches_.size() +
2918 relative_call_patches_.size() +
2919 pc_rel_dex_cache_patches_.size();
2920 linker_patches->reserve(size);
2921 for (const auto& entry : method_patches_) {
2922 const MethodReference& target_method = entry.first;
2923 vixl::Literal<uint64_t>* literal = entry.second;
2924 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2925 target_method.dex_file,
2926 target_method.dex_method_index));
2927 }
2928 for (const auto& entry : call_patches_) {
2929 const MethodReference& target_method = entry.first;
2930 vixl::Literal<uint64_t>* literal = entry.second;
2931 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2932 target_method.dex_file,
2933 target_method.dex_method_index));
2934 }
2935 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2936 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2937 info.target_method.dex_file,
2938 info.target_method.dex_method_index));
2939 }
2940 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2941 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2942 &info.target_dex_file,
2943 info.pc_insn_label->location() - 4u,
2944 info.element_offset));
2945 }
2946}
2947
2948vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2949 // Look up the literal for value.
2950 auto lb = uint64_literals_.lower_bound(value);
2951 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2952 return lb->second;
2953 }
2954 // We don't have a literal for this value, insert a new one.
2955 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2956 uint64_literals_.PutBefore(lb, value, literal);
2957 return literal;
2958}
2959
2960vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2961 MethodReference target_method,
2962 MethodToLiteralMap* map) {
2963 // Look up the literal for target_method.
2964 auto lb = map->lower_bound(target_method);
2965 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2966 return lb->second;
2967 }
2968 // We don't have a literal for this method yet, insert a new one.
2969 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2970 map->PutBefore(lb, target_method, literal);
2971 return literal;
2972}
2973
2974vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2975 MethodReference target_method) {
2976 return DeduplicateMethodLiteral(target_method, &method_patches_);
2977}
2978
2979vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2980 MethodReference target_method) {
2981 return DeduplicateMethodLiteral(target_method, &call_patches_);
2982}
2983
2984
Andreas Gampe878d58c2015-01-15 23:24:00 -08002985void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002986 // When we do not run baseline, explicit clinit checks triggered by static
2987 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2988 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002989
Andreas Gampe878d58c2015-01-15 23:24:00 -08002990 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2991 return;
2992 }
2993
Alexandre Ramesd921d642015-04-16 15:07:16 +01002994 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002995 LocationSummary* locations = invoke->GetLocations();
2996 codegen_->GenerateStaticOrDirectCall(
2997 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002998 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002999}
3000
3001void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003002 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3003 return;
3004 }
3005
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003006 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003007 DCHECK(!codegen_->IsLeafMethod());
3008 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3009}
3010
Alexandre Rames67555f72014-11-18 10:55:16 +00003011void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
3012 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
3013 : LocationSummary::kNoCall;
3014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003015 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003016 locations->SetOut(Location::RequiresRegister());
3017}
3018
3019void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
3020 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003021 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003022 if (cls->IsReferrersClass()) {
3023 DCHECK(!cls->CanCallRuntime());
3024 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003025 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003026 } else {
3027 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003028 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3029 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3030 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3031 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003032
3033 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3034 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3035 codegen_->AddSlowPath(slow_path);
3036 __ Cbz(out, slow_path->GetEntryLabel());
3037 if (cls->MustGenerateClinitCheck()) {
3038 GenerateClassInitializationCheck(slow_path, out);
3039 } else {
3040 __ Bind(slow_path->GetExitLabel());
3041 }
3042 }
3043}
3044
David Brazdilcb1c0552015-08-04 16:22:25 +01003045static MemOperand GetExceptionTlsAddress() {
3046 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3047}
3048
Alexandre Rames67555f72014-11-18 10:55:16 +00003049void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3050 LocationSummary* locations =
3051 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3052 locations->SetOut(Location::RequiresRegister());
3053}
3054
3055void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003056 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3057}
3058
3059void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3060 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3061}
3062
3063void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3064 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003065}
3066
Alexandre Rames5319def2014-10-23 10:03:10 +01003067void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3068 load->SetLocations(nullptr);
3069}
3070
3071void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3072 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003073 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003074}
3075
Alexandre Rames67555f72014-11-18 10:55:16 +00003076void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3077 LocationSummary* locations =
3078 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003079 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003080 locations->SetOut(Location::RequiresRegister());
3081}
3082
3083void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3084 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3085 codegen_->AddSlowPath(slow_path);
3086
3087 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003088 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003089 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003090 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3091 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3092 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003093 __ Cbz(out, slow_path->GetEntryLabel());
3094 __ Bind(slow_path->GetExitLabel());
3095}
3096
Alexandre Rames5319def2014-10-23 10:03:10 +01003097void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3098 local->SetLocations(nullptr);
3099}
3100
3101void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3102 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3103}
3104
3105void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3107 locations->SetOut(Location::ConstantLocation(constant));
3108}
3109
3110void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3111 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003112 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003113}
3114
Alexandre Rames67555f72014-11-18 10:55:16 +00003115void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3116 LocationSummary* locations =
3117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3118 InvokeRuntimeCallingConvention calling_convention;
3119 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3120}
3121
3122void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3123 codegen_->InvokeRuntime(instruction->IsEnter()
3124 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3125 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003126 instruction->GetDexPc(),
3127 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003128 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003129}
3130
Alexandre Rames42d641b2014-10-27 14:00:51 +00003131void LocationsBuilderARM64::VisitMul(HMul* mul) {
3132 LocationSummary* locations =
3133 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3134 switch (mul->GetResultType()) {
3135 case Primitive::kPrimInt:
3136 case Primitive::kPrimLong:
3137 locations->SetInAt(0, Location::RequiresRegister());
3138 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003140 break;
3141
3142 case Primitive::kPrimFloat:
3143 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003144 locations->SetInAt(0, Location::RequiresFpuRegister());
3145 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003146 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003147 break;
3148
3149 default:
3150 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3151 }
3152}
3153
3154void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3155 switch (mul->GetResultType()) {
3156 case Primitive::kPrimInt:
3157 case Primitive::kPrimLong:
3158 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3159 break;
3160
3161 case Primitive::kPrimFloat:
3162 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003163 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003164 break;
3165
3166 default:
3167 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3168 }
3169}
3170
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003171void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3172 LocationSummary* locations =
3173 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3174 switch (neg->GetResultType()) {
3175 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003176 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003177 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003179 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003180
3181 case Primitive::kPrimFloat:
3182 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003183 locations->SetInAt(0, Location::RequiresFpuRegister());
3184 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003185 break;
3186
3187 default:
3188 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3189 }
3190}
3191
3192void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3193 switch (neg->GetResultType()) {
3194 case Primitive::kPrimInt:
3195 case Primitive::kPrimLong:
3196 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3197 break;
3198
3199 case Primitive::kPrimFloat:
3200 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003201 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003202 break;
3203
3204 default:
3205 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3206 }
3207}
3208
3209void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3210 LocationSummary* locations =
3211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3212 InvokeRuntimeCallingConvention calling_convention;
3213 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003214 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003215 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003216 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003217 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003218 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003219}
3220
3221void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3222 LocationSummary* locations = instruction->GetLocations();
3223 InvokeRuntimeCallingConvention calling_convention;
3224 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3225 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003226 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003227 // Note: if heap poisoning is enabled, the entry point takes cares
3228 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003229 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3230 instruction,
3231 instruction->GetDexPc(),
3232 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003233 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003234}
3235
Alexandre Rames5319def2014-10-23 10:03:10 +01003236void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3237 LocationSummary* locations =
3238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3239 InvokeRuntimeCallingConvention calling_convention;
3240 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003241 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003242 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003243 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003244}
3245
3246void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3247 LocationSummary* locations = instruction->GetLocations();
3248 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3249 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003250 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003251 // Note: if heap poisoning is enabled, the entry point takes cares
3252 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003253 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3254 instruction,
3255 instruction->GetDexPc(),
3256 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003257 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003258}
3259
3260void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003262 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003264}
3265
3266void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003267 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003268 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003269 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003270 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003271 break;
3272
3273 default:
3274 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3275 }
3276}
3277
David Brazdil66d126e2015-04-03 16:02:44 +01003278void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3280 locations->SetInAt(0, Location::RequiresRegister());
3281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3282}
3283
3284void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003285 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3286}
3287
Alexandre Rames5319def2014-10-23 10:03:10 +01003288void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003289 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3290 ? LocationSummary::kCallOnSlowPath
3291 : LocationSummary::kNoCall;
3292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003293 locations->SetInAt(0, Location::RequiresRegister());
3294 if (instruction->HasUses()) {
3295 locations->SetOut(Location::SameAsFirstInput());
3296 }
3297}
3298
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003299void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003300 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3301 return;
3302 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003303
Alexandre Ramesd921d642015-04-16 15:07:16 +01003304 BlockPoolsScope block_pools(GetVIXLAssembler());
3305 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003306 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3307 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3308}
3309
3310void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003311 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3312 codegen_->AddSlowPath(slow_path);
3313
3314 LocationSummary* locations = instruction->GetLocations();
3315 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003316
3317 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003318}
3319
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003320void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003321 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003322 GenerateImplicitNullCheck(instruction);
3323 } else {
3324 GenerateExplicitNullCheck(instruction);
3325 }
3326}
3327
Alexandre Rames67555f72014-11-18 10:55:16 +00003328void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3329 HandleBinaryOp(instruction);
3330}
3331
3332void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3333 HandleBinaryOp(instruction);
3334}
3335
Alexandre Rames3e69f162014-12-10 10:36:50 +00003336void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3337 LOG(FATAL) << "Unreachable";
3338}
3339
3340void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3341 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3342}
3343
Alexandre Rames5319def2014-10-23 10:03:10 +01003344void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3346 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3347 if (location.IsStackSlot()) {
3348 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3349 } else if (location.IsDoubleStackSlot()) {
3350 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3351 }
3352 locations->SetOut(location);
3353}
3354
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003355void InstructionCodeGeneratorARM64::VisitParameterValue(
3356 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003357 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003358}
3359
3360void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3361 LocationSummary* locations =
3362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003363 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003364}
3365
3366void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3367 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3368 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003369}
3370
3371void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3373 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3374 locations->SetInAt(i, Location::Any());
3375 }
3376 locations->SetOut(Location::Any());
3377}
3378
3379void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003380 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003381 LOG(FATAL) << "Unreachable";
3382}
3383
Serban Constantinescu02164b32014-11-13 14:05:07 +00003384void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003385 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003386 LocationSummary::CallKind call_kind =
3387 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3389
3390 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003391 case Primitive::kPrimInt:
3392 case Primitive::kPrimLong:
3393 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003394 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3396 break;
3397
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003398 case Primitive::kPrimFloat:
3399 case Primitive::kPrimDouble: {
3400 InvokeRuntimeCallingConvention calling_convention;
3401 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3402 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3403 locations->SetOut(calling_convention.GetReturnLocation(type));
3404
3405 break;
3406 }
3407
Serban Constantinescu02164b32014-11-13 14:05:07 +00003408 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003409 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003410 }
3411}
3412
3413void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3414 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003415
Serban Constantinescu02164b32014-11-13 14:05:07 +00003416 switch (type) {
3417 case Primitive::kPrimInt:
3418 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003419 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003420 break;
3421 }
3422
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003423 case Primitive::kPrimFloat:
3424 case Primitive::kPrimDouble: {
3425 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3426 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003427 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003428 break;
3429 }
3430
Serban Constantinescu02164b32014-11-13 14:05:07 +00003431 default:
3432 LOG(FATAL) << "Unexpected rem type " << type;
3433 }
3434}
3435
Calin Juravle27df7582015-04-17 19:12:31 +01003436void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3437 memory_barrier->SetLocations(nullptr);
3438}
3439
3440void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3441 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3442}
3443
Alexandre Rames5319def2014-10-23 10:03:10 +01003444void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3446 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003447 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003448}
3449
3450void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003451 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003452 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003453}
3454
3455void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3456 instruction->SetLocations(nullptr);
3457}
3458
3459void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003460 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003461 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003462}
3463
Serban Constantinescu02164b32014-11-13 14:05:07 +00003464void LocationsBuilderARM64::VisitShl(HShl* shl) {
3465 HandleShift(shl);
3466}
3467
3468void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3469 HandleShift(shl);
3470}
3471
3472void LocationsBuilderARM64::VisitShr(HShr* shr) {
3473 HandleShift(shr);
3474}
3475
3476void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3477 HandleShift(shr);
3478}
3479
Alexandre Rames5319def2014-10-23 10:03:10 +01003480void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3482 Primitive::Type field_type = store->InputAt(1)->GetType();
3483 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003484 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003485 case Primitive::kPrimBoolean:
3486 case Primitive::kPrimByte:
3487 case Primitive::kPrimChar:
3488 case Primitive::kPrimShort:
3489 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003490 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003491 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3492 break;
3493
3494 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003495 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003496 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3497 break;
3498
3499 default:
3500 LOG(FATAL) << "Unimplemented local type " << field_type;
3501 }
3502}
3503
3504void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003505 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003506}
3507
3508void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003509 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003510}
3511
3512void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003513 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003514}
3515
Alexandre Rames67555f72014-11-18 10:55:16 +00003516void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003517 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003518}
3519
3520void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003521 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003522}
3523
3524void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003525 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003526}
3527
Alexandre Rames67555f72014-11-18 10:55:16 +00003528void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003529 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003530}
3531
Calin Juravlee460d1d2015-09-29 04:52:17 +01003532void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3533 HUnresolvedInstanceFieldGet* instruction) {
3534 FieldAccessCallingConventionARM64 calling_convention;
3535 codegen_->CreateUnresolvedFieldLocationSummary(
3536 instruction, instruction->GetFieldType(), calling_convention);
3537}
3538
3539void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3540 HUnresolvedInstanceFieldGet* instruction) {
3541 FieldAccessCallingConventionARM64 calling_convention;
3542 codegen_->GenerateUnresolvedFieldAccess(instruction,
3543 instruction->GetFieldType(),
3544 instruction->GetFieldIndex(),
3545 instruction->GetDexPc(),
3546 calling_convention);
3547}
3548
3549void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3550 HUnresolvedInstanceFieldSet* instruction) {
3551 FieldAccessCallingConventionARM64 calling_convention;
3552 codegen_->CreateUnresolvedFieldLocationSummary(
3553 instruction, instruction->GetFieldType(), calling_convention);
3554}
3555
3556void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3557 HUnresolvedInstanceFieldSet* instruction) {
3558 FieldAccessCallingConventionARM64 calling_convention;
3559 codegen_->GenerateUnresolvedFieldAccess(instruction,
3560 instruction->GetFieldType(),
3561 instruction->GetFieldIndex(),
3562 instruction->GetDexPc(),
3563 calling_convention);
3564}
3565
3566void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3567 HUnresolvedStaticFieldGet* instruction) {
3568 FieldAccessCallingConventionARM64 calling_convention;
3569 codegen_->CreateUnresolvedFieldLocationSummary(
3570 instruction, instruction->GetFieldType(), calling_convention);
3571}
3572
3573void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3574 HUnresolvedStaticFieldGet* instruction) {
3575 FieldAccessCallingConventionARM64 calling_convention;
3576 codegen_->GenerateUnresolvedFieldAccess(instruction,
3577 instruction->GetFieldType(),
3578 instruction->GetFieldIndex(),
3579 instruction->GetDexPc(),
3580 calling_convention);
3581}
3582
3583void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3584 HUnresolvedStaticFieldSet* instruction) {
3585 FieldAccessCallingConventionARM64 calling_convention;
3586 codegen_->CreateUnresolvedFieldLocationSummary(
3587 instruction, instruction->GetFieldType(), calling_convention);
3588}
3589
3590void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3591 HUnresolvedStaticFieldSet* instruction) {
3592 FieldAccessCallingConventionARM64 calling_convention;
3593 codegen_->GenerateUnresolvedFieldAccess(instruction,
3594 instruction->GetFieldType(),
3595 instruction->GetFieldIndex(),
3596 instruction->GetDexPc(),
3597 calling_convention);
3598}
3599
Alexandre Rames5319def2014-10-23 10:03:10 +01003600void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3601 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3602}
3603
3604void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003605 HBasicBlock* block = instruction->GetBlock();
3606 if (block->GetLoopInformation() != nullptr) {
3607 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3608 // The back edge will generate the suspend check.
3609 return;
3610 }
3611 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3612 // The goto will generate the suspend check.
3613 return;
3614 }
3615 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003616}
3617
3618void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3619 temp->SetLocations(nullptr);
3620}
3621
3622void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3623 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003624 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003625}
3626
Alexandre Rames67555f72014-11-18 10:55:16 +00003627void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3628 LocationSummary* locations =
3629 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3630 InvokeRuntimeCallingConvention calling_convention;
3631 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3632}
3633
3634void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3635 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003636 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003637 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003638}
3639
3640void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3641 LocationSummary* locations =
3642 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3643 Primitive::Type input_type = conversion->GetInputType();
3644 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003645 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003646 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3647 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3648 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3649 }
3650
Alexandre Rames542361f2015-01-29 16:57:31 +00003651 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003652 locations->SetInAt(0, Location::RequiresFpuRegister());
3653 } else {
3654 locations->SetInAt(0, Location::RequiresRegister());
3655 }
3656
Alexandre Rames542361f2015-01-29 16:57:31 +00003657 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3659 } else {
3660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3661 }
3662}
3663
3664void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3665 Primitive::Type result_type = conversion->GetResultType();
3666 Primitive::Type input_type = conversion->GetInputType();
3667
3668 DCHECK_NE(input_type, result_type);
3669
Alexandre Rames542361f2015-01-29 16:57:31 +00003670 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003671 int result_size = Primitive::ComponentSize(result_type);
3672 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003673 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003674 Register output = OutputRegister(conversion);
3675 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003676 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3677 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003678 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3679 // 'int' values are used directly as W registers, discarding the top
3680 // bits, so we don't need to sign-extend and can just perform a move.
3681 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3682 // top 32 bits of the target register. We theoretically could leave those
3683 // bits unchanged, but we would have to make sure that no code uses a
3684 // 32bit input value as a 64bit value assuming that the top 32 bits are
3685 // zero.
3686 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003687 } else if ((result_type == Primitive::kPrimChar) ||
3688 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3689 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003690 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003691 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003692 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003693 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003694 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003695 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003696 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3697 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003698 } else if (Primitive::IsFloatingPointType(result_type) &&
3699 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003700 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3701 } else {
3702 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3703 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003704 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003705}
Alexandre Rames67555f72014-11-18 10:55:16 +00003706
Serban Constantinescu02164b32014-11-13 14:05:07 +00003707void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3708 HandleShift(ushr);
3709}
3710
3711void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3712 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003713}
3714
3715void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3716 HandleBinaryOp(instruction);
3717}
3718
3719void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3720 HandleBinaryOp(instruction);
3721}
3722
Calin Juravleb1498f62015-02-16 13:13:29 +00003723void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3724 // Nothing to do, this should be removed during prepare for register allocator.
3725 UNUSED(instruction);
3726 LOG(FATAL) << "Unreachable";
3727}
3728
3729void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3730 // Nothing to do, this should be removed during prepare for register allocator.
3731 UNUSED(instruction);
3732 LOG(FATAL) << "Unreachable";
3733}
3734
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003735void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3736 DCHECK(codegen_->IsBaseline());
3737 LocationSummary* locations =
3738 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3739 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3740}
3741
3742void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3743 DCHECK(codegen_->IsBaseline());
3744 // Will be generated at use site.
3745}
3746
Mark Mendellfe57faa2015-09-18 09:26:15 -04003747// Simple implementation of packed switch - generate cascaded compare/jumps.
3748void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3749 LocationSummary* locations =
3750 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3751 locations->SetInAt(0, Location::RequiresRegister());
3752}
3753
3754void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3755 int32_t lower_bound = switch_instr->GetStartValue();
3756 int32_t num_entries = switch_instr->GetNumEntries();
3757 Register value_reg = InputRegisterAt(switch_instr, 0);
3758 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3759
3760 // Create a series of compare/jumps.
3761 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3762 for (int32_t i = 0; i < num_entries; i++) {
3763 int32_t case_value = lower_bound + i;
3764 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3765 if (case_value == 0) {
3766 __ Cbz(value_reg, succ);
3767 } else {
3768 __ Cmp(value_reg, vixl::Operand(case_value));
3769 __ B(eq, succ);
3770 }
3771 }
3772
3773 // And the default for any other value.
3774 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3775 __ B(codegen_->GetLabelOf(default_block));
3776 }
3777}
3778
Alexandre Rames67555f72014-11-18 10:55:16 +00003779#undef __
3780#undef QUICK_ENTRY_POINT
3781
Alexandre Rames5319def2014-10-23 10:03:10 +01003782} // namespace arm64
3783} // namespace art