blob: af5bbaae3d722b7b26a825d8693ece82d48e2ef9 [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 Geoffray88a95ba2015-09-30 17:18:14 +0100121 CPURegList fp_list = CPURegList(
122 CPURegister::kFPRegister,
123 kDRegSize,
124 register_set->GetFloatingPointRegisters()
125 & (~(codegen->GetGraph()->IsDebuggable() ? 0 : callee_saved_fp_registers.list())));
Zheng Xuda403092015-04-24 17:35:39 +0800126
127 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
128 UseScratchRegisterScope temps(masm);
129
130 Register base = masm->StackPointer();
131 int64_t core_spill_size = core_list.TotalSizeInBytes();
132 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
133 int64_t reg_size = kXRegSizeInBytes;
134 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
135 uint32_t ls_access_size = WhichPowerOf2(reg_size);
136 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
137 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
138 // If the offset does not fit in the instruction's immediate field, use an alternate register
139 // to compute the base address(float point registers spill base address).
140 Register new_base = temps.AcquireSameSizeAs(base);
141 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
142 base = new_base;
143 spill_offset = -core_spill_size;
144 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
145 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
146 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
147 }
148
149 if (is_save) {
150 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
151 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 } else {
153 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
154 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
155 }
156}
157
158void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
159 RegisterSet* register_set = locations->GetLiveRegisters();
160 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
161 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
162 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
163 // If the register holds an object, update the stack mask.
164 if (locations->RegisterContainsObject(i)) {
165 locations->SetStackBit(stack_offset / kVRegSize);
166 }
167 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
168 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
169 saved_core_stack_offsets_[i] = stack_offset;
170 stack_offset += kXRegSizeInBytes;
171 }
172 }
173
174 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
175 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
176 register_set->ContainsFloatingPointRegister(i)) {
177 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
178 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
179 saved_fpu_stack_offsets_[i] = stack_offset;
180 stack_offset += kDRegSizeInBytes;
181 }
182 }
183
184 SaveRestoreLiveRegistersHelper(codegen, register_set,
185 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
186}
187
188void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
189 RegisterSet* register_set = locations->GetLiveRegisters();
190 SaveRestoreLiveRegistersHelper(codegen, register_set,
191 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
192}
193
Alexandre Rames5319def2014-10-23 10:03:10 +0100194class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
195 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100197
Alexandre Rames67555f72014-11-18 10:55:16 +0000198 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000200 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100201
Alexandre Rames5319def2014-10-23 10:03:10 +0100202 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000203 if (instruction_->CanThrowIntoCatchBlock()) {
204 // Live registers will be restored in the catch block if caught.
205 SaveLiveRegisters(codegen, instruction_->GetLocations());
206 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000207 // We're moving two locations to locations that could overlap, so we need a parallel
208 // move resolver.
209 InvokeRuntimeCallingConvention calling_convention;
210 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100211 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
212 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000213 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000214 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800215 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100216 }
217
Alexandre Rames8158f282015-08-07 10:26:17 +0100218 bool IsFatal() const OVERRIDE { return true; }
219
Alexandre Rames9931f312015-06-19 14:47:01 +0100220 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000223 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000224
Alexandre Rames5319def2014-10-23 10:03:10 +0100225 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
226};
227
Alexandre Rames67555f72014-11-18 10:55:16 +0000228class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
229 public:
230 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
231
232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
233 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
234 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000235 if (instruction_->CanThrowIntoCatchBlock()) {
236 // Live registers will be restored in the catch block if caught.
237 SaveLiveRegisters(codegen, instruction_->GetLocations());
238 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000240 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000242 }
243
Alexandre Rames8158f282015-08-07 10:26:17 +0100244 bool IsFatal() const OVERRIDE { return true; }
245
Alexandre Rames9931f312015-06-19 14:47:01 +0100246 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
247
Alexandre Rames67555f72014-11-18 10:55:16 +0000248 private:
249 HDivZeroCheck* const instruction_;
250 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
251};
252
253class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
254 public:
255 LoadClassSlowPathARM64(HLoadClass* cls,
256 HInstruction* at,
257 uint32_t dex_pc,
258 bool do_clinit)
259 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
260 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
261 }
262
263 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
264 LocationSummary* locations = at_->GetLocations();
265 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
266
267 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000269
270 InvokeRuntimeCallingConvention calling_convention;
271 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000272 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
273 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000274 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800275 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100276 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800277 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100278 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800279 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000280
281 // Move the class to the desired location.
282 Location out = locations->Out();
283 if (out.IsValid()) {
284 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
285 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000286 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 }
288
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000289 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 __ B(GetExitLabel());
291 }
292
Alexandre Rames9931f312015-06-19 14:47:01 +0100293 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 private:
296 // The class this slow path will load.
297 HLoadClass* const cls_;
298
299 // The instruction where this slow path is happening.
300 // (Might be the load class or an initialization check).
301 HInstruction* const at_;
302
303 // The dex PC of `at_`.
304 const uint32_t dex_pc_;
305
306 // Whether to initialize the class.
307 const bool do_clinit_;
308
309 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
310};
311
312class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
313 public:
314 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
315
316 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
317 LocationSummary* locations = instruction_->GetLocations();
318 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
319 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
320
321 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000322 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000323
324 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800325 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000327 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100328 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000330 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000331
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000332 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 __ B(GetExitLabel());
334 }
335
Alexandre Rames9931f312015-06-19 14:47:01 +0100336 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
337
Alexandre Rames67555f72014-11-18 10:55:16 +0000338 private:
339 HLoadString* const instruction_;
340
341 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
342};
343
Alexandre Rames5319def2014-10-23 10:03:10 +0100344class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
345 public:
346 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
347
Alexandre Rames67555f72014-11-18 10:55:16 +0000348 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
349 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100350 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000351 if (instruction_->CanThrowIntoCatchBlock()) {
352 // Live registers will be restored in the catch block if caught.
353 SaveLiveRegisters(codegen, instruction_->GetLocations());
354 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000356 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800357 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
363
Alexandre Rames5319def2014-10-23 10:03:10 +0100364 private:
365 HNullCheck* const instruction_;
366
367 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
368};
369
370class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100372 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 : instruction_(instruction), successor_(successor) {}
374
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
376 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100377 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000378 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000379 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000380 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800381 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000382 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 if (successor_ == nullptr) {
384 __ B(GetReturnLabel());
385 } else {
386 __ B(arm64_codegen->GetLabelOf(successor_));
387 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100388 }
389
390 vixl::Label* GetReturnLabel() {
391 DCHECK(successor_ == nullptr);
392 return &return_label_;
393 }
394
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100395 HBasicBlock* GetSuccessor() const {
396 return successor_;
397 }
398
Alexandre Rames9931f312015-06-19 14:47:01 +0100399 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
400
Alexandre Rames5319def2014-10-23 10:03:10 +0100401 private:
402 HSuspendCheck* const instruction_;
403 // If not null, the block to branch to after the suspend check.
404 HBasicBlock* const successor_;
405
406 // If `successor_` is null, the label to branch to after the suspend check.
407 vixl::Label return_label_;
408
409 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
410};
411
Alexandre Rames67555f72014-11-18 10:55:16 +0000412class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
413 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000414 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
415 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000416
417 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000418 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100419 Location class_to_check = locations->InAt(1);
420 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
421 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000422 DCHECK(instruction_->IsCheckCast()
423 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
424 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100425 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000428
429 if (instruction_->IsCheckCast()) {
430 // The codegen for the instruction overwrites `temp`, so put it back in place.
431 Register obj = InputRegisterAt(instruction_, 0);
432 Register temp = WRegisterFrom(locations->GetTemp(0));
433 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
434 __ Ldr(temp, HeapOperand(obj, class_offset));
435 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
436 }
437
438 if (!is_fatal_) {
439 SaveLiveRegisters(codegen, locations);
440 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000441
442 // We're moving two locations to locations that could overlap, so we need a parallel
443 // move resolver.
444 InvokeRuntimeCallingConvention calling_convention;
445 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100446 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
447 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448
449 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000450 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452 Primitive::Type ret_type = instruction_->GetType();
453 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
454 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800455 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
456 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000457 } else {
458 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100459 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800460 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461 }
462
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000463 if (!is_fatal_) {
464 RestoreLiveRegisters(codegen, locations);
465 __ B(GetExitLabel());
466 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000467 }
468
Alexandre Rames9931f312015-06-19 14:47:01 +0100469 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000470 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100471
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000475
Alexandre Rames67555f72014-11-18 10:55:16 +0000476 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
477};
478
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700479class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
480 public:
481 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100482 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700483
484 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
485 __ Bind(GetEntryLabel());
486 SaveLiveRegisters(codegen, instruction_->GetLocations());
487 DCHECK(instruction_->IsDeoptimize());
488 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
489 uint32_t dex_pc = deoptimize->GetDexPc();
490 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
491 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
492 }
493
Alexandre Rames9931f312015-06-19 14:47:01 +0100494 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 private:
497 HInstruction* const instruction_;
498 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
499};
500
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100501class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
502 public:
503 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
504
505 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506 LocationSummary* locations = instruction_->GetLocations();
507 __ Bind(GetEntryLabel());
508 SaveLiveRegisters(codegen, locations);
509
510 InvokeRuntimeCallingConvention calling_convention;
511 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
512 parallel_move.AddMove(
513 locations->InAt(0),
514 LocationFrom(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 nullptr);
517 parallel_move.AddMove(
518 locations->InAt(1),
519 LocationFrom(calling_convention.GetRegisterAt(1)),
520 Primitive::kPrimInt,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(2),
524 LocationFrom(calling_convention.GetRegisterAt(2)),
525 Primitive::kPrimNot,
526 nullptr);
527 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
528
529 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
530 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
531 instruction_,
532 instruction_->GetDexPc(),
533 this);
534 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
535 RestoreLiveRegisters(codegen, locations);
536 __ B(GetExitLabel());
537 }
538
539 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
540
541 private:
542 HInstruction* const instruction_;
543
544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Alexandre Rames5319def2014-10-23 10:03:10 +0100547#undef __
548
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100549Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100550 Location next_location;
551 if (type == Primitive::kPrimVoid) {
552 LOG(FATAL) << "Unreachable type " << type;
553 }
554
Alexandre Rames542361f2015-01-29 16:57:31 +0000555 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100556 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
557 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000558 } else if (!Primitive::IsFloatingPointType(type) &&
559 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000560 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
561 } else {
562 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000563 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
564 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100565 }
566
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000567 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000568 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100569 return next_location;
570}
571
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100572Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100573 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100574}
575
Serban Constantinescu579885a2015-02-22 20:51:33 +0000576CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
577 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100578 const CompilerOptions& compiler_options,
579 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100580 : CodeGenerator(graph,
581 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000582 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000583 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000584 callee_saved_core_registers.list(),
Nicolas Geoffray88a95ba2015-09-30 17:18:14 +0100585 // If the graph is debuggable, we need to save the fpu registers ourselves,
586 // as the stubs do not do it.
587 graph->IsDebuggable() ? 0 : callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100588 compiler_options,
589 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100590 block_labels_(nullptr),
591 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000592 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000593 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000594 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100595 uint64_literals_(std::less<uint64_t>(),
596 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
597 method_patches_(MethodReferenceComparator(),
598 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
599 call_patches_(MethodReferenceComparator(),
600 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
601 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
602 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000603 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000604 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000605}
Alexandre Rames5319def2014-10-23 10:03:10 +0100606
Alexandre Rames67555f72014-11-18 10:55:16 +0000607#undef __
608#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100609
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000610void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
611 // Ensure we emit the literal pool.
612 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000613
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000614 CodeGenerator::Finalize(allocator);
615}
616
Zheng Xuad4450e2015-04-17 18:48:56 +0800617void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
618 // Note: There are 6 kinds of moves:
619 // 1. constant -> GPR/FPR (non-cycle)
620 // 2. constant -> stack (non-cycle)
621 // 3. GPR/FPR -> GPR/FPR
622 // 4. GPR/FPR -> stack
623 // 5. stack -> GPR/FPR
624 // 6. stack -> stack (non-cycle)
625 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
626 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
627 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
628 // dependency.
629 vixl_temps_.Open(GetVIXLAssembler());
630}
631
632void ParallelMoveResolverARM64::FinishEmitNativeCode() {
633 vixl_temps_.Close();
634}
635
636Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
637 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
638 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
639 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
640 Location scratch = GetScratchLocation(kind);
641 if (!scratch.Equals(Location::NoLocation())) {
642 return scratch;
643 }
644 // Allocate from VIXL temp registers.
645 if (kind == Location::kRegister) {
646 scratch = LocationFrom(vixl_temps_.AcquireX());
647 } else {
648 DCHECK(kind == Location::kFpuRegister);
649 scratch = LocationFrom(vixl_temps_.AcquireD());
650 }
651 AddScratchLocation(scratch);
652 return scratch;
653}
654
655void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
656 if (loc.IsRegister()) {
657 vixl_temps_.Release(XRegisterFrom(loc));
658 } else {
659 DCHECK(loc.IsFpuRegister());
660 vixl_temps_.Release(DRegisterFrom(loc));
661 }
662 RemoveScratchLocation(loc);
663}
664
Alexandre Rames3e69f162014-12-10 10:36:50 +0000665void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100666 DCHECK_LT(index, moves_.size());
667 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000669}
670
Alexandre Rames5319def2014-10-23 10:03:10 +0100671void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100672 MacroAssembler* masm = GetVIXLAssembler();
673 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000674 __ Bind(&frame_entry_label_);
675
Serban Constantinescu02164b32014-11-13 14:05:07 +0000676 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
677 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100678 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000679 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000680 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000681 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000682 __ Ldr(wzr, MemOperand(temp, 0));
683 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000684 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100685
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000686 if (!HasEmptyFrame()) {
687 int frame_size = GetFrameSize();
688 // Stack layout:
689 // sp[frame_size - 8] : lr.
690 // ... : other preserved core registers.
691 // ... : other preserved fp registers.
692 // ... : reserved frame space.
693 // sp[0] : current method.
694 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100695 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800696 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
697 frame_size - GetCoreSpillSize());
698 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
699 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000700 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100701}
702
703void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100704 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100705 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000706 if (!HasEmptyFrame()) {
707 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800708 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
709 frame_size - FrameEntrySpillSize());
710 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
711 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000712 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100713 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000714 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100715 __ Ret();
716 GetAssembler()->cfi().RestoreState();
717 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100718}
719
Zheng Xuda403092015-04-24 17:35:39 +0800720vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
721 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
722 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
723 core_spill_mask_);
724}
725
726vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
727 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
728 GetNumberOfFloatingPointRegisters()));
729 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
730 fpu_spill_mask_);
731}
732
Alexandre Rames5319def2014-10-23 10:03:10 +0100733void CodeGeneratorARM64::Bind(HBasicBlock* block) {
734 __ Bind(GetLabelOf(block));
735}
736
Alexandre Rames5319def2014-10-23 10:03:10 +0100737void CodeGeneratorARM64::Move(HInstruction* instruction,
738 Location location,
739 HInstruction* move_for) {
740 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100741 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000742 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100743
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100744 if (instruction->IsFakeString()) {
745 // The fake string is an alias for null.
746 DCHECK(IsBaseline());
747 instruction = locations->Out().GetConstant();
748 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
749 }
750
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100751 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100752 MoveLocation(location,
753 Location::DoubleStackSlot(kCurrentMethodStackOffset),
754 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100755 } else if (locations != nullptr && locations->Out().Equals(location)) {
756 return;
757 } else if (instruction->IsIntConstant()
758 || instruction->IsLongConstant()
759 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000760 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100761 if (location.IsRegister()) {
762 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000763 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100764 (instruction->IsLongConstant() && dst.Is64Bits()));
765 __ Mov(dst, value);
766 } else {
767 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000768 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000769 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
770 ? temps.AcquireW()
771 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 __ Mov(temp, value);
773 __ Str(temp, StackOperandFrom(location));
774 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000775 } else if (instruction->IsTemporary()) {
776 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000777 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100778 } else if (instruction->IsLoadLocal()) {
779 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000780 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000781 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000782 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000783 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100784 }
785
786 } else {
787 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000788 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100789 }
790}
791
Calin Juravle175dc732015-08-25 15:42:32 +0100792void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
793 DCHECK(location.IsRegister());
794 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
795}
796
Calin Juravlee460d1d2015-09-29 04:52:17 +0100797void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
798 if (location.IsRegister()) {
799 locations->AddTemp(location);
800 } else {
801 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
802 }
803}
804
Alexandre Rames5319def2014-10-23 10:03:10 +0100805Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
806 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000807
Alexandre Rames5319def2014-10-23 10:03:10 +0100808 switch (type) {
809 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000810 case Primitive::kPrimInt:
811 case Primitive::kPrimFloat:
812 return Location::StackSlot(GetStackSlot(load->GetLocal()));
813
814 case Primitive::kPrimLong:
815 case Primitive::kPrimDouble:
816 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
817
Alexandre Rames5319def2014-10-23 10:03:10 +0100818 case Primitive::kPrimBoolean:
819 case Primitive::kPrimByte:
820 case Primitive::kPrimChar:
821 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100822 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100823 LOG(FATAL) << "Unexpected type " << type;
824 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000825
Alexandre Rames5319def2014-10-23 10:03:10 +0100826 LOG(FATAL) << "Unreachable";
827 return Location::NoLocation();
828}
829
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100830void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000831 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000833 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100834 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100835 if (value_can_be_null) {
836 __ Cbz(value, &done);
837 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100838 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
839 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000840 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100841 if (value_can_be_null) {
842 __ Bind(&done);
843 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100844}
845
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000846void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
847 // Blocked core registers:
848 // lr : Runtime reserved.
849 // tr : Runtime reserved.
850 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
851 // ip1 : VIXL core temp.
852 // ip0 : VIXL core temp.
853 //
854 // Blocked fp registers:
855 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100856 CPURegList reserved_core_registers = vixl_reserved_core_registers;
857 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100858 while (!reserved_core_registers.IsEmpty()) {
859 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
860 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000861
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000862 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800863 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000864 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
865 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000866
867 if (is_baseline) {
868 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
869 while (!reserved_core_baseline_registers.IsEmpty()) {
870 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
871 }
872
873 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
874 while (!reserved_fp_baseline_registers.IsEmpty()) {
875 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
876 }
877 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100878}
879
880Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
881 if (type == Primitive::kPrimVoid) {
882 LOG(FATAL) << "Unreachable type " << type;
883 }
884
Alexandre Rames542361f2015-01-29 16:57:31 +0000885 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000886 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
887 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100888 return Location::FpuRegisterLocation(reg);
889 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000890 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
891 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100892 return Location::RegisterLocation(reg);
893 }
894}
895
Alexandre Rames3e69f162014-12-10 10:36:50 +0000896size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
897 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
898 __ Str(reg, MemOperand(sp, stack_index));
899 return kArm64WordSize;
900}
901
902size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
903 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
904 __ Ldr(reg, MemOperand(sp, stack_index));
905 return kArm64WordSize;
906}
907
908size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
909 FPRegister reg = FPRegister(reg_id, kDRegSize);
910 __ Str(reg, MemOperand(sp, stack_index));
911 return kArm64WordSize;
912}
913
914size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
915 FPRegister reg = FPRegister(reg_id, kDRegSize);
916 __ Ldr(reg, MemOperand(sp, stack_index));
917 return kArm64WordSize;
918}
919
Alexandre Rames5319def2014-10-23 10:03:10 +0100920void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100921 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100922}
923
924void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100925 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100926}
927
Alexandre Rames67555f72014-11-18 10:55:16 +0000928void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000929 if (constant->IsIntConstant()) {
930 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
931 } else if (constant->IsLongConstant()) {
932 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
933 } else if (constant->IsNullConstant()) {
934 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000935 } else if (constant->IsFloatConstant()) {
936 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
937 } else {
938 DCHECK(constant->IsDoubleConstant());
939 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
940 }
941}
942
Alexandre Rames3e69f162014-12-10 10:36:50 +0000943
944static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
945 DCHECK(constant.IsConstant());
946 HConstant* cst = constant.GetConstant();
947 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000948 // Null is mapped to a core W register, which we associate with kPrimInt.
949 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000950 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
951 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
952 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
953}
954
Calin Juravlee460d1d2015-09-29 04:52:17 +0100955void CodeGeneratorARM64::MoveLocation(Location destination,
956 Location source,
957 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000958 if (source.Equals(destination)) {
959 return;
960 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000961
962 // A valid move can always be inferred from the destination and source
963 // locations. When moving from and to a register, the argument type can be
964 // used to generate 32bit instead of 64bit moves. In debug mode we also
965 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100966 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000967
968 if (destination.IsRegister() || destination.IsFpuRegister()) {
969 if (unspecified_type) {
970 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
971 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000972 (src_cst != nullptr && (src_cst->IsIntConstant()
973 || src_cst->IsFloatConstant()
974 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000975 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100976 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000977 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000978 // If the source is a double stack slot or a 64bit constant, a 64bit
979 // type is appropriate. Else the source is a register, and since the
980 // type has not been specified, we chose a 64bit type to force a 64bit
981 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100982 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000983 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000984 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100985 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
986 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
987 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000988 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
989 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
990 __ Ldr(dst, StackOperandFrom(source));
991 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100992 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000993 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100994 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000995 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100996 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000997 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800998 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100999 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1000 ? Primitive::kPrimLong
1001 : Primitive::kPrimInt;
1002 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1003 }
1004 } else {
1005 DCHECK(source.IsFpuRegister());
1006 if (destination.IsRegister()) {
1007 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1008 ? Primitive::kPrimDouble
1009 : Primitive::kPrimFloat;
1010 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1011 } else {
1012 DCHECK(destination.IsFpuRegister());
1013 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001014 }
1015 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001016 } else { // The destination is not a register. It must be a stack slot.
1017 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1018 if (source.IsRegister() || source.IsFpuRegister()) {
1019 if (unspecified_type) {
1020 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001022 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001023 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024 }
1025 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001026 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1027 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1028 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001029 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001030 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1031 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001032 UseScratchRegisterScope temps(GetVIXLAssembler());
1033 HConstant* src_cst = source.GetConstant();
1034 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001035 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001036 temp = temps.AcquireW();
1037 } else if (src_cst->IsLongConstant()) {
1038 temp = temps.AcquireX();
1039 } else if (src_cst->IsFloatConstant()) {
1040 temp = temps.AcquireS();
1041 } else {
1042 DCHECK(src_cst->IsDoubleConstant());
1043 temp = temps.AcquireD();
1044 }
1045 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001046 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001047 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001048 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001049 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001050 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001051 // There is generally less pressure on FP registers.
1052 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001053 __ Ldr(temp, StackOperandFrom(source));
1054 __ Str(temp, StackOperandFrom(destination));
1055 }
1056 }
1057}
1058
1059void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 CPURegister dst,
1061 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001062 switch (type) {
1063 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001064 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001065 break;
1066 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001067 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001068 break;
1069 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001070 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001071 break;
1072 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001073 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001074 break;
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot:
1077 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001078 case Primitive::kPrimFloat:
1079 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001080 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001081 __ Ldr(dst, src);
1082 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001083 case Primitive::kPrimVoid:
1084 LOG(FATAL) << "Unreachable type " << type;
1085 }
1086}
1087
Calin Juravle77520bc2015-01-12 18:45:46 +00001088void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001089 CPURegister dst,
1090 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001091 MacroAssembler* masm = GetVIXLAssembler();
1092 BlockPoolsScope block_pools(masm);
1093 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001094 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001095 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001097 DCHECK(!src.IsPreIndex());
1098 DCHECK(!src.IsPostIndex());
1099
1100 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001101 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001102 MemOperand base = MemOperand(temp_base);
1103 switch (type) {
1104 case Primitive::kPrimBoolean:
1105 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001106 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001107 break;
1108 case Primitive::kPrimByte:
1109 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001110 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001111 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1112 break;
1113 case Primitive::kPrimChar:
1114 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001115 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001116 break;
1117 case Primitive::kPrimShort:
1118 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001119 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001120 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1121 break;
1122 case Primitive::kPrimInt:
1123 case Primitive::kPrimNot:
1124 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001125 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001126 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001127 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001128 break;
1129 case Primitive::kPrimFloat:
1130 case Primitive::kPrimDouble: {
1131 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001132 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001133
1134 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1135 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001136 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001137 __ Fmov(FPRegister(dst), temp);
1138 break;
1139 }
1140 case Primitive::kPrimVoid:
1141 LOG(FATAL) << "Unreachable type " << type;
1142 }
1143}
1144
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001145void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001146 CPURegister src,
1147 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001148 switch (type) {
1149 case Primitive::kPrimBoolean:
1150 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001151 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001152 break;
1153 case Primitive::kPrimChar:
1154 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001155 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001156 break;
1157 case Primitive::kPrimInt:
1158 case Primitive::kPrimNot:
1159 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001160 case Primitive::kPrimFloat:
1161 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001162 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001163 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001164 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001165 case Primitive::kPrimVoid:
1166 LOG(FATAL) << "Unreachable type " << type;
1167 }
1168}
1169
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001170void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1171 CPURegister src,
1172 const MemOperand& dst) {
1173 UseScratchRegisterScope temps(GetVIXLAssembler());
1174 Register temp_base = temps.AcquireX();
1175
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001176 DCHECK(!dst.IsPreIndex());
1177 DCHECK(!dst.IsPostIndex());
1178
1179 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001180 Operand op = OperandFromMemOperand(dst);
1181 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001182 MemOperand base = MemOperand(temp_base);
1183 switch (type) {
1184 case Primitive::kPrimBoolean:
1185 case Primitive::kPrimByte:
1186 __ Stlrb(Register(src), base);
1187 break;
1188 case Primitive::kPrimChar:
1189 case Primitive::kPrimShort:
1190 __ Stlrh(Register(src), base);
1191 break;
1192 case Primitive::kPrimInt:
1193 case Primitive::kPrimNot:
1194 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001195 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001196 __ Stlr(Register(src), base);
1197 break;
1198 case Primitive::kPrimFloat:
1199 case Primitive::kPrimDouble: {
1200 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001201 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001202
1203 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1204 __ Fmov(temp, FPRegister(src));
1205 __ Stlr(temp, base);
1206 break;
1207 }
1208 case Primitive::kPrimVoid:
1209 LOG(FATAL) << "Unreachable type " << type;
1210 }
1211}
1212
Calin Juravle175dc732015-08-25 15:42:32 +01001213void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1214 HInstruction* instruction,
1215 uint32_t dex_pc,
1216 SlowPathCode* slow_path) {
1217 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1218 instruction,
1219 dex_pc,
1220 slow_path);
1221}
1222
Alexandre Rames67555f72014-11-18 10:55:16 +00001223void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1224 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001225 uint32_t dex_pc,
1226 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001227 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001228 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1230 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001231 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001232}
1233
1234void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1235 vixl::Register class_reg) {
1236 UseScratchRegisterScope temps(GetVIXLAssembler());
1237 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001238 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001239 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001240
Serban Constantinescu02164b32014-11-13 14:05:07 +00001241 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001242 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001243 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1244 __ Add(temp, class_reg, status_offset);
1245 __ Ldar(temp, HeapOperand(temp));
1246 __ Cmp(temp, mirror::Class::kStatusInitialized);
1247 __ B(lt, slow_path->GetEntryLabel());
1248 } else {
1249 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1250 __ Cmp(temp, mirror::Class::kStatusInitialized);
1251 __ B(lt, slow_path->GetEntryLabel());
1252 __ Dmb(InnerShareable, BarrierReads);
1253 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001254 __ Bind(slow_path->GetExitLabel());
1255}
Alexandre Rames5319def2014-10-23 10:03:10 +01001256
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001257void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1258 BarrierType type = BarrierAll;
1259
1260 switch (kind) {
1261 case MemBarrierKind::kAnyAny:
1262 case MemBarrierKind::kAnyStore: {
1263 type = BarrierAll;
1264 break;
1265 }
1266 case MemBarrierKind::kLoadAny: {
1267 type = BarrierReads;
1268 break;
1269 }
1270 case MemBarrierKind::kStoreStore: {
1271 type = BarrierWrites;
1272 break;
1273 }
1274 default:
1275 LOG(FATAL) << "Unexpected memory barrier " << kind;
1276 }
1277 __ Dmb(InnerShareable, type);
1278}
1279
Serban Constantinescu02164b32014-11-13 14:05:07 +00001280void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1281 HBasicBlock* successor) {
1282 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001283 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1284 if (slow_path == nullptr) {
1285 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1286 instruction->SetSlowPath(slow_path);
1287 codegen_->AddSlowPath(slow_path);
1288 if (successor != nullptr) {
1289 DCHECK(successor->IsLoopHeader());
1290 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1291 }
1292 } else {
1293 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1294 }
1295
Serban Constantinescu02164b32014-11-13 14:05:07 +00001296 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1297 Register temp = temps.AcquireW();
1298
1299 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1300 if (successor == nullptr) {
1301 __ Cbnz(temp, slow_path->GetEntryLabel());
1302 __ Bind(slow_path->GetReturnLabel());
1303 } else {
1304 __ Cbz(temp, codegen_->GetLabelOf(successor));
1305 __ B(slow_path->GetEntryLabel());
1306 // slow_path will return to GetLabelOf(successor).
1307 }
1308}
1309
Alexandre Rames5319def2014-10-23 10:03:10 +01001310InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1311 CodeGeneratorARM64* codegen)
1312 : HGraphVisitor(graph),
1313 assembler_(codegen->GetAssembler()),
1314 codegen_(codegen) {}
1315
1316#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001317 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001318
1319#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1320
1321enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 // Using a base helps identify when we hit such breakpoints.
1323 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001324#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1325 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1326#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1327};
1328
1329#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1330 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001331 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001332 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1333 } \
1334 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1336 locations->SetOut(Location::Any()); \
1337 }
1338 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1339#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1340
1341#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001342#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001343
Alexandre Rames67555f72014-11-18 10:55:16 +00001344void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001345 DCHECK_EQ(instr->InputCount(), 2U);
1346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1347 Primitive::Type type = instr->GetResultType();
1348 switch (type) {
1349 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001350 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001351 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001352 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001354 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001355
1356 case Primitive::kPrimFloat:
1357 case Primitive::kPrimDouble:
1358 locations->SetInAt(0, Location::RequiresFpuRegister());
1359 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001360 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001361 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001362
Alexandre Rames5319def2014-10-23 10:03:10 +01001363 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001364 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001365 }
1366}
1367
Alexandre Rames09a99962015-04-15 11:47:56 +01001368void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1369 LocationSummary* locations =
1370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1371 locations->SetInAt(0, Location::RequiresRegister());
1372 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1373 locations->SetOut(Location::RequiresFpuRegister());
1374 } else {
1375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1376 }
1377}
1378
1379void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1380 const FieldInfo& field_info) {
1381 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001382 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001383 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001384
1385 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1386 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1387
1388 if (field_info.IsVolatile()) {
1389 if (use_acquire_release) {
1390 // NB: LoadAcquire will record the pc info if needed.
1391 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1392 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001393 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001394 codegen_->MaybeRecordImplicitNullCheck(instruction);
1395 // For IRIW sequential consistency kLoadAny is not sufficient.
1396 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1397 }
1398 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001399 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001400 codegen_->MaybeRecordImplicitNullCheck(instruction);
1401 }
Roland Levillain4d027112015-07-01 15:41:14 +01001402
1403 if (field_type == Primitive::kPrimNot) {
1404 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1405 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001406}
1407
1408void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1409 LocationSummary* locations =
1410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1411 locations->SetInAt(0, Location::RequiresRegister());
1412 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 } else {
1415 locations->SetInAt(1, Location::RequiresRegister());
1416 }
1417}
1418
1419void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001420 const FieldInfo& field_info,
1421 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001422 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001423 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001424
1425 Register obj = InputRegisterAt(instruction, 0);
1426 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001427 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001428 Offset offset = field_info.GetFieldOffset();
1429 Primitive::Type field_type = field_info.GetFieldType();
1430 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1431
Roland Levillain4d027112015-07-01 15:41:14 +01001432 {
1433 // We use a block to end the scratch scope before the write barrier, thus
1434 // freeing the temporary registers so they can be used in `MarkGCCard`.
1435 UseScratchRegisterScope temps(GetVIXLAssembler());
1436
1437 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1438 DCHECK(value.IsW());
1439 Register temp = temps.AcquireW();
1440 __ Mov(temp, value.W());
1441 GetAssembler()->PoisonHeapReference(temp.W());
1442 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001443 }
Roland Levillain4d027112015-07-01 15:41:14 +01001444
1445 if (field_info.IsVolatile()) {
1446 if (use_acquire_release) {
1447 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1448 codegen_->MaybeRecordImplicitNullCheck(instruction);
1449 } else {
1450 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1451 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1452 codegen_->MaybeRecordImplicitNullCheck(instruction);
1453 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1454 }
1455 } else {
1456 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1457 codegen_->MaybeRecordImplicitNullCheck(instruction);
1458 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001459 }
1460
1461 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001462 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001463 }
1464}
1465
Alexandre Rames67555f72014-11-18 10:55:16 +00001466void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001467 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001468
1469 switch (type) {
1470 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001471 case Primitive::kPrimLong: {
1472 Register dst = OutputRegister(instr);
1473 Register lhs = InputRegisterAt(instr, 0);
1474 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001475 if (instr->IsAdd()) {
1476 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 } else if (instr->IsAnd()) {
1478 __ And(dst, lhs, rhs);
1479 } else if (instr->IsOr()) {
1480 __ Orr(dst, lhs, rhs);
1481 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001482 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001483 } else {
1484 DCHECK(instr->IsXor());
1485 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001486 }
1487 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001488 }
1489 case Primitive::kPrimFloat:
1490 case Primitive::kPrimDouble: {
1491 FPRegister dst = OutputFPRegister(instr);
1492 FPRegister lhs = InputFPRegisterAt(instr, 0);
1493 FPRegister rhs = InputFPRegisterAt(instr, 1);
1494 if (instr->IsAdd()) {
1495 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001496 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001497 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001498 } else {
1499 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001500 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001501 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001502 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001503 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001504 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001505 }
1506}
1507
Serban Constantinescu02164b32014-11-13 14:05:07 +00001508void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1509 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1510
1511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1512 Primitive::Type type = instr->GetResultType();
1513 switch (type) {
1514 case Primitive::kPrimInt:
1515 case Primitive::kPrimLong: {
1516 locations->SetInAt(0, Location::RequiresRegister());
1517 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1518 locations->SetOut(Location::RequiresRegister());
1519 break;
1520 }
1521 default:
1522 LOG(FATAL) << "Unexpected shift type " << type;
1523 }
1524}
1525
1526void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1527 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1528
1529 Primitive::Type type = instr->GetType();
1530 switch (type) {
1531 case Primitive::kPrimInt:
1532 case Primitive::kPrimLong: {
1533 Register dst = OutputRegister(instr);
1534 Register lhs = InputRegisterAt(instr, 0);
1535 Operand rhs = InputOperandAt(instr, 1);
1536 if (rhs.IsImmediate()) {
1537 uint32_t shift_value = (type == Primitive::kPrimInt)
1538 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1539 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1540 if (instr->IsShl()) {
1541 __ Lsl(dst, lhs, shift_value);
1542 } else if (instr->IsShr()) {
1543 __ Asr(dst, lhs, shift_value);
1544 } else {
1545 __ Lsr(dst, lhs, shift_value);
1546 }
1547 } else {
1548 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1549
1550 if (instr->IsShl()) {
1551 __ Lsl(dst, lhs, rhs_reg);
1552 } else if (instr->IsShr()) {
1553 __ Asr(dst, lhs, rhs_reg);
1554 } else {
1555 __ Lsr(dst, lhs, rhs_reg);
1556 }
1557 }
1558 break;
1559 }
1560 default:
1561 LOG(FATAL) << "Unexpected shift operation type " << type;
1562 }
1563}
1564
Alexandre Rames5319def2014-10-23 10:03:10 +01001565void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001566 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001567}
1568
1569void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001570 HandleBinaryOp(instruction);
1571}
1572
1573void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1574 HandleBinaryOp(instruction);
1575}
1576
1577void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1578 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001579}
1580
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001581void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1582 LocationSummary* locations =
1583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001586 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1588 } else {
1589 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1590 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001591}
1592
1593void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1594 LocationSummary* locations = instruction->GetLocations();
1595 Primitive::Type type = instruction->GetType();
1596 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001597 Location index = locations->InAt(1);
1598 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001599 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001600 MacroAssembler* masm = GetVIXLAssembler();
1601 UseScratchRegisterScope temps(masm);
1602 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001603
1604 if (index.IsConstant()) {
1605 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001606 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001607 } else {
1608 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001609 __ Add(temp, obj, offset);
1610 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001611 }
1612
Alexandre Rames67555f72014-11-18 10:55:16 +00001613 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001614 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001615
1616 if (type == Primitive::kPrimNot) {
1617 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1618 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001619}
1620
Alexandre Rames5319def2014-10-23 10:03:10 +01001621void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1623 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001624 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001625}
1626
1627void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001628 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001629 __ Ldr(OutputRegister(instruction),
1630 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001631 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001632}
1633
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001634void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1636 instruction,
1637 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1638 locations->SetInAt(0, Location::RequiresRegister());
1639 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1640 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1641 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001642 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001643 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001644 }
1645}
1646
1647void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1648 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001649 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001650 bool may_need_runtime_call = locations->CanCall();
1651 bool needs_write_barrier =
1652 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001653
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001654 Register array = InputRegisterAt(instruction, 0);
1655 CPURegister value = InputCPURegisterAt(instruction, 2);
1656 CPURegister source = value;
1657 Location index = locations->InAt(1);
1658 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1659 MemOperand destination = HeapOperand(array);
1660 MacroAssembler* masm = GetVIXLAssembler();
1661 BlockPoolsScope block_pools(masm);
1662
1663 if (!needs_write_barrier) {
1664 DCHECK(!may_need_runtime_call);
1665 if (index.IsConstant()) {
1666 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1667 destination = HeapOperand(array, offset);
1668 } else {
1669 UseScratchRegisterScope temps(masm);
1670 Register temp = temps.AcquireSameSizeAs(array);
1671 __ Add(temp, array, offset);
1672 destination = HeapOperand(temp,
1673 XRegisterFrom(index),
1674 LSL,
1675 Primitive::ComponentSizeShift(value_type));
1676 }
1677 codegen_->Store(value_type, value, destination);
1678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001679 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001680 DCHECK(needs_write_barrier);
1681 vixl::Label done;
1682 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001683 {
1684 // We use a block to end the scratch scope before the write barrier, thus
1685 // freeing the temporary registers so they can be used in `MarkGCCard`.
1686 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001687 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001688 if (index.IsConstant()) {
1689 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001690 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001691 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001692 destination = HeapOperand(temp,
1693 XRegisterFrom(index),
1694 LSL,
1695 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001696 }
1697
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001698 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1699 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1700 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1701
1702 if (may_need_runtime_call) {
1703 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1704 codegen_->AddSlowPath(slow_path);
1705 if (instruction->GetValueCanBeNull()) {
1706 vixl::Label non_zero;
1707 __ Cbnz(Register(value), &non_zero);
1708 if (!index.IsConstant()) {
1709 __ Add(temp, array, offset);
1710 }
1711 __ Str(wzr, destination);
1712 codegen_->MaybeRecordImplicitNullCheck(instruction);
1713 __ B(&done);
1714 __ Bind(&non_zero);
1715 }
1716
1717 Register temp2 = temps.AcquireSameSizeAs(array);
1718 __ Ldr(temp, HeapOperand(array, class_offset));
1719 codegen_->MaybeRecordImplicitNullCheck(instruction);
1720 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1721 __ Ldr(temp, HeapOperand(temp, component_offset));
1722 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1723 // No need to poison/unpoison, we're comparing two poisoned references.
1724 __ Cmp(temp, temp2);
1725 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1726 vixl::Label do_put;
1727 __ B(eq, &do_put);
1728 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1729 __ Ldr(temp, HeapOperand(temp, super_offset));
1730 // No need to unpoison, we're comparing against null.
1731 __ Cbnz(temp, slow_path->GetEntryLabel());
1732 __ Bind(&do_put);
1733 } else {
1734 __ B(ne, slow_path->GetEntryLabel());
1735 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001736 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001737 }
1738
1739 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001740 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001741 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001742 __ Mov(temp2, value.W());
1743 GetAssembler()->PoisonHeapReference(temp2);
1744 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001745 }
1746
1747 if (!index.IsConstant()) {
1748 __ Add(temp, array, offset);
1749 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001750 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001751
1752 if (!may_need_runtime_call) {
1753 codegen_->MaybeRecordImplicitNullCheck(instruction);
1754 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001755 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001756
1757 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1758
1759 if (done.IsLinked()) {
1760 __ Bind(&done);
1761 }
1762
1763 if (slow_path != nullptr) {
1764 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001765 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001766 }
1767}
1768
Alexandre Rames67555f72014-11-18 10:55:16 +00001769void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001770 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1771 ? LocationSummary::kCallOnSlowPath
1772 : LocationSummary::kNoCall;
1773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001774 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001775 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001776 if (instruction->HasUses()) {
1777 locations->SetOut(Location::SameAsFirstInput());
1778 }
1779}
1780
1781void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001782 BoundsCheckSlowPathARM64* slow_path =
1783 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001784 codegen_->AddSlowPath(slow_path);
1785
1786 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1787 __ B(slow_path->GetEntryLabel(), hs);
1788}
1789
Alexandre Rames67555f72014-11-18 10:55:16 +00001790void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1791 LocationSummary* locations =
1792 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1793 locations->SetInAt(0, Location::RequiresRegister());
1794 if (check->HasUses()) {
1795 locations->SetOut(Location::SameAsFirstInput());
1796 }
1797}
1798
1799void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1800 // We assume the class is not null.
1801 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1802 check->GetLoadClass(), check, check->GetDexPc(), true);
1803 codegen_->AddSlowPath(slow_path);
1804 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1805}
1806
Roland Levillain7f63c522015-07-13 15:54:55 +00001807static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1808 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1809 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1810}
1811
Serban Constantinescu02164b32014-11-13 14:05:07 +00001812void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001813 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001814 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1815 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001816 switch (in_type) {
1817 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001818 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001819 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1821 break;
1822 }
1823 case Primitive::kPrimFloat:
1824 case Primitive::kPrimDouble: {
1825 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001826 locations->SetInAt(1,
1827 IsFloatingPointZeroConstant(compare->InputAt(1))
1828 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1829 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001830 locations->SetOut(Location::RequiresRegister());
1831 break;
1832 }
1833 default:
1834 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1835 }
1836}
1837
1838void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1839 Primitive::Type in_type = compare->InputAt(0)->GetType();
1840
1841 // 0 if: left == right
1842 // 1 if: left > right
1843 // -1 if: left < right
1844 switch (in_type) {
1845 case Primitive::kPrimLong: {
1846 Register result = OutputRegister(compare);
1847 Register left = InputRegisterAt(compare, 0);
1848 Operand right = InputOperandAt(compare, 1);
1849
1850 __ Cmp(left, right);
1851 __ Cset(result, ne);
1852 __ Cneg(result, result, lt);
1853 break;
1854 }
1855 case Primitive::kPrimFloat:
1856 case Primitive::kPrimDouble: {
1857 Register result = OutputRegister(compare);
1858 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001859 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001860 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1861 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001862 __ Fcmp(left, 0.0);
1863 } else {
1864 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1865 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001866 if (compare->IsGtBias()) {
1867 __ Cset(result, ne);
1868 } else {
1869 __ Csetm(result, ne);
1870 }
1871 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001872 break;
1873 }
1874 default:
1875 LOG(FATAL) << "Unimplemented compare type " << in_type;
1876 }
1877}
1878
1879void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1880 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001881
1882 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
1884 locations->SetInAt(1,
1885 IsFloatingPointZeroConstant(instruction->InputAt(1))
1886 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1887 : Location::RequiresFpuRegister());
1888 } else {
1889 // Integer cases.
1890 locations->SetInAt(0, Location::RequiresRegister());
1891 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1892 }
1893
Alexandre Rames5319def2014-10-23 10:03:10 +01001894 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001896 }
1897}
1898
1899void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1900 if (!instruction->NeedsMaterialization()) {
1901 return;
1902 }
1903
1904 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001905 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001906 IfCondition if_cond = instruction->GetCondition();
1907 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001908
Roland Levillain7f63c522015-07-13 15:54:55 +00001909 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1910 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1911 if (locations->InAt(1).IsConstant()) {
1912 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1913 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1914 __ Fcmp(lhs, 0.0);
1915 } else {
1916 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1917 }
1918 __ Cset(res, arm64_cond);
1919 if (instruction->IsFPConditionTrueIfNaN()) {
1920 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1921 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1922 } else if (instruction->IsFPConditionFalseIfNaN()) {
1923 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1924 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1925 }
1926 } else {
1927 // Integer cases.
1928 Register lhs = InputRegisterAt(instruction, 0);
1929 Operand rhs = InputOperandAt(instruction, 1);
1930 __ Cmp(lhs, rhs);
1931 __ Cset(res, arm64_cond);
1932 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001933}
1934
1935#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1936 M(Equal) \
1937 M(NotEqual) \
1938 M(LessThan) \
1939 M(LessThanOrEqual) \
1940 M(GreaterThan) \
1941 M(GreaterThanOrEqual)
1942#define DEFINE_CONDITION_VISITORS(Name) \
1943void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1944void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1945FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001946#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001947#undef FOR_EACH_CONDITION_INSTRUCTION
1948
Zheng Xuc6667102015-05-15 16:08:45 +08001949void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1950 DCHECK(instruction->IsDiv() || instruction->IsRem());
1951
1952 LocationSummary* locations = instruction->GetLocations();
1953 Location second = locations->InAt(1);
1954 DCHECK(second.IsConstant());
1955
1956 Register out = OutputRegister(instruction);
1957 Register dividend = InputRegisterAt(instruction, 0);
1958 int64_t imm = Int64FromConstant(second.GetConstant());
1959 DCHECK(imm == 1 || imm == -1);
1960
1961 if (instruction->IsRem()) {
1962 __ Mov(out, 0);
1963 } else {
1964 if (imm == 1) {
1965 __ Mov(out, dividend);
1966 } else {
1967 __ Neg(out, dividend);
1968 }
1969 }
1970}
1971
1972void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1973 DCHECK(instruction->IsDiv() || instruction->IsRem());
1974
1975 LocationSummary* locations = instruction->GetLocations();
1976 Location second = locations->InAt(1);
1977 DCHECK(second.IsConstant());
1978
1979 Register out = OutputRegister(instruction);
1980 Register dividend = InputRegisterAt(instruction, 0);
1981 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001982 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001983 DCHECK(IsPowerOfTwo(abs_imm));
1984 int ctz_imm = CTZ(abs_imm);
1985
1986 UseScratchRegisterScope temps(GetVIXLAssembler());
1987 Register temp = temps.AcquireSameSizeAs(out);
1988
1989 if (instruction->IsDiv()) {
1990 __ Add(temp, dividend, abs_imm - 1);
1991 __ Cmp(dividend, 0);
1992 __ Csel(out, temp, dividend, lt);
1993 if (imm > 0) {
1994 __ Asr(out, out, ctz_imm);
1995 } else {
1996 __ Neg(out, Operand(out, ASR, ctz_imm));
1997 }
1998 } else {
1999 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2000 __ Asr(temp, dividend, bits - 1);
2001 __ Lsr(temp, temp, bits - ctz_imm);
2002 __ Add(out, dividend, temp);
2003 __ And(out, out, abs_imm - 1);
2004 __ Sub(out, out, temp);
2005 }
2006}
2007
2008void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2009 DCHECK(instruction->IsDiv() || instruction->IsRem());
2010
2011 LocationSummary* locations = instruction->GetLocations();
2012 Location second = locations->InAt(1);
2013 DCHECK(second.IsConstant());
2014
2015 Register out = OutputRegister(instruction);
2016 Register dividend = InputRegisterAt(instruction, 0);
2017 int64_t imm = Int64FromConstant(second.GetConstant());
2018
2019 Primitive::Type type = instruction->GetResultType();
2020 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2021
2022 int64_t magic;
2023 int shift;
2024 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2025
2026 UseScratchRegisterScope temps(GetVIXLAssembler());
2027 Register temp = temps.AcquireSameSizeAs(out);
2028
2029 // temp = get_high(dividend * magic)
2030 __ Mov(temp, magic);
2031 if (type == Primitive::kPrimLong) {
2032 __ Smulh(temp, dividend, temp);
2033 } else {
2034 __ Smull(temp.X(), dividend, temp);
2035 __ Lsr(temp.X(), temp.X(), 32);
2036 }
2037
2038 if (imm > 0 && magic < 0) {
2039 __ Add(temp, temp, dividend);
2040 } else if (imm < 0 && magic > 0) {
2041 __ Sub(temp, temp, dividend);
2042 }
2043
2044 if (shift != 0) {
2045 __ Asr(temp, temp, shift);
2046 }
2047
2048 if (instruction->IsDiv()) {
2049 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2050 } else {
2051 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2052 // TODO: Strength reduction for msub.
2053 Register temp_imm = temps.AcquireSameSizeAs(out);
2054 __ Mov(temp_imm, imm);
2055 __ Msub(out, temp, temp_imm, dividend);
2056 }
2057}
2058
2059void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2060 DCHECK(instruction->IsDiv() || instruction->IsRem());
2061 Primitive::Type type = instruction->GetResultType();
2062 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2063
2064 LocationSummary* locations = instruction->GetLocations();
2065 Register out = OutputRegister(instruction);
2066 Location second = locations->InAt(1);
2067
2068 if (second.IsConstant()) {
2069 int64_t imm = Int64FromConstant(second.GetConstant());
2070
2071 if (imm == 0) {
2072 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2073 } else if (imm == 1 || imm == -1) {
2074 DivRemOneOrMinusOne(instruction);
2075 } else if (IsPowerOfTwo(std::abs(imm))) {
2076 DivRemByPowerOfTwo(instruction);
2077 } else {
2078 DCHECK(imm <= -2 || imm >= 2);
2079 GenerateDivRemWithAnyConstant(instruction);
2080 }
2081 } else {
2082 Register dividend = InputRegisterAt(instruction, 0);
2083 Register divisor = InputRegisterAt(instruction, 1);
2084 if (instruction->IsDiv()) {
2085 __ Sdiv(out, dividend, divisor);
2086 } else {
2087 UseScratchRegisterScope temps(GetVIXLAssembler());
2088 Register temp = temps.AcquireSameSizeAs(out);
2089 __ Sdiv(temp, dividend, divisor);
2090 __ Msub(out, temp, divisor, dividend);
2091 }
2092 }
2093}
2094
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002095void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2098 switch (div->GetResultType()) {
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimLong:
2101 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002102 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2104 break;
2105
2106 case Primitive::kPrimFloat:
2107 case Primitive::kPrimDouble:
2108 locations->SetInAt(0, Location::RequiresFpuRegister());
2109 locations->SetInAt(1, Location::RequiresFpuRegister());
2110 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2111 break;
2112
2113 default:
2114 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2115 }
2116}
2117
2118void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2119 Primitive::Type type = div->GetResultType();
2120 switch (type) {
2121 case Primitive::kPrimInt:
2122 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002123 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002124 break;
2125
2126 case Primitive::kPrimFloat:
2127 case Primitive::kPrimDouble:
2128 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2129 break;
2130
2131 default:
2132 LOG(FATAL) << "Unexpected div type " << type;
2133 }
2134}
2135
Alexandre Rames67555f72014-11-18 10:55:16 +00002136void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002137 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2138 ? LocationSummary::kCallOnSlowPath
2139 : LocationSummary::kNoCall;
2140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002141 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2142 if (instruction->HasUses()) {
2143 locations->SetOut(Location::SameAsFirstInput());
2144 }
2145}
2146
2147void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2148 SlowPathCodeARM64* slow_path =
2149 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2150 codegen_->AddSlowPath(slow_path);
2151 Location value = instruction->GetLocations()->InAt(0);
2152
Alexandre Rames3e69f162014-12-10 10:36:50 +00002153 Primitive::Type type = instruction->GetType();
2154
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002155 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2156 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002157 return;
2158 }
2159
Alexandre Rames67555f72014-11-18 10:55:16 +00002160 if (value.IsConstant()) {
2161 int64_t divisor = Int64ConstantFrom(value);
2162 if (divisor == 0) {
2163 __ B(slow_path->GetEntryLabel());
2164 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002165 // A division by a non-null constant is valid. We don't need to perform
2166 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002167 }
2168 } else {
2169 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2170 }
2171}
2172
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002173void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2174 LocationSummary* locations =
2175 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2176 locations->SetOut(Location::ConstantLocation(constant));
2177}
2178
2179void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2180 UNUSED(constant);
2181 // Will be generated at use site.
2182}
2183
Alexandre Rames5319def2014-10-23 10:03:10 +01002184void LocationsBuilderARM64::VisitExit(HExit* exit) {
2185 exit->SetLocations(nullptr);
2186}
2187
2188void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002189 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002190}
2191
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002192void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2193 LocationSummary* locations =
2194 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2195 locations->SetOut(Location::ConstantLocation(constant));
2196}
2197
2198void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2199 UNUSED(constant);
2200 // Will be generated at use site.
2201}
2202
David Brazdilfc6a86a2015-06-26 10:33:45 +00002203void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002204 DCHECK(!successor->IsExitBlock());
2205 HBasicBlock* block = got->GetBlock();
2206 HInstruction* previous = got->GetPrevious();
2207 HLoopInformation* info = block->GetLoopInformation();
2208
David Brazdil46e2a392015-03-16 17:31:52 +00002209 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002210 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2211 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2212 return;
2213 }
2214 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2215 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2216 }
2217 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002218 __ B(codegen_->GetLabelOf(successor));
2219 }
2220}
2221
David Brazdilfc6a86a2015-06-26 10:33:45 +00002222void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2223 got->SetLocations(nullptr);
2224}
2225
2226void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2227 HandleGoto(got, got->GetSuccessor());
2228}
2229
2230void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2231 try_boundary->SetLocations(nullptr);
2232}
2233
2234void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2235 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2236 if (!successor->IsExitBlock()) {
2237 HandleGoto(try_boundary, successor);
2238 }
2239}
2240
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002241void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2242 vixl::Label* true_target,
2243 vixl::Label* false_target,
2244 vixl::Label* always_true_target) {
2245 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002246 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002247
Serban Constantinescu02164b32014-11-13 14:05:07 +00002248 if (cond->IsIntConstant()) {
2249 int32_t cond_value = cond->AsIntConstant()->GetValue();
2250 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002251 if (always_true_target != nullptr) {
2252 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002253 }
2254 return;
2255 } else {
2256 DCHECK_EQ(cond_value, 0);
2257 }
2258 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002259 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002260 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002261 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002262 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 } else {
2264 // The condition instruction has not been materialized, use its inputs as
2265 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002266 Primitive::Type type =
2267 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2268
2269 if (Primitive::IsFloatingPointType(type)) {
2270 // FP compares don't like null false_targets.
2271 if (false_target == nullptr) {
2272 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002273 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002274 FPRegister lhs = InputFPRegisterAt(condition, 0);
2275 if (condition->GetLocations()->InAt(1).IsConstant()) {
2276 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2277 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2278 __ Fcmp(lhs, 0.0);
2279 } else {
2280 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2281 }
2282 if (condition->IsFPConditionTrueIfNaN()) {
2283 __ B(vs, true_target); // VS for unordered.
2284 } else if (condition->IsFPConditionFalseIfNaN()) {
2285 __ B(vs, false_target); // VS for unordered.
2286 }
2287 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002288 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002289 // Integer cases.
2290 Register lhs = InputRegisterAt(condition, 0);
2291 Operand rhs = InputOperandAt(condition, 1);
2292 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2293 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2294 switch (arm64_cond) {
2295 case eq:
2296 __ Cbz(lhs, true_target);
2297 break;
2298 case ne:
2299 __ Cbnz(lhs, true_target);
2300 break;
2301 case lt:
2302 // Test the sign bit and branch accordingly.
2303 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2304 break;
2305 case ge:
2306 // Test the sign bit and branch accordingly.
2307 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2308 break;
2309 default:
2310 // Without the `static_cast` the compiler throws an error for
2311 // `-Werror=sign-promo`.
2312 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2313 }
2314 } else {
2315 __ Cmp(lhs, rhs);
2316 __ B(arm64_cond, true_target);
2317 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002318 }
2319 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002320 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002321 __ B(false_target);
2322 }
2323}
2324
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002325void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2327 HInstruction* cond = if_instr->InputAt(0);
2328 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2329 locations->SetInAt(0, Location::RequiresRegister());
2330 }
2331}
2332
2333void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2334 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2335 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2336 vixl::Label* always_true_target = true_target;
2337 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2338 if_instr->IfTrueSuccessor())) {
2339 always_true_target = nullptr;
2340 }
2341 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2342 if_instr->IfFalseSuccessor())) {
2343 false_target = nullptr;
2344 }
2345 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2346}
2347
2348void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2349 LocationSummary* locations = new (GetGraph()->GetArena())
2350 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2351 HInstruction* cond = deoptimize->InputAt(0);
2352 DCHECK(cond->IsCondition());
2353 if (cond->AsCondition()->NeedsMaterialization()) {
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 }
2356}
2357
2358void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2359 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2360 DeoptimizationSlowPathARM64(deoptimize);
2361 codegen_->AddSlowPath(slow_path);
2362 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2363 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2364}
2365
Alexandre Rames5319def2014-10-23 10:03:10 +01002366void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002367 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002368}
2369
2370void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002372}
2373
2374void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002375 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002376}
2377
2378void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002379 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002380}
2381
Alexandre Rames67555f72014-11-18 10:55:16 +00002382void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002383 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2384 switch (instruction->GetTypeCheckKind()) {
2385 case TypeCheckKind::kExactCheck:
2386 case TypeCheckKind::kAbstractClassCheck:
2387 case TypeCheckKind::kClassHierarchyCheck:
2388 case TypeCheckKind::kArrayObjectCheck:
2389 call_kind = LocationSummary::kNoCall;
2390 break;
2391 case TypeCheckKind::kInterfaceCheck:
2392 call_kind = LocationSummary::kCall;
2393 break;
2394 case TypeCheckKind::kArrayCheck:
2395 call_kind = LocationSummary::kCallOnSlowPath;
2396 break;
2397 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002399 if (call_kind != LocationSummary::kCall) {
2400 locations->SetInAt(0, Location::RequiresRegister());
2401 locations->SetInAt(1, Location::RequiresRegister());
2402 // The out register is used as a temporary, so it overlaps with the inputs.
2403 // Note that TypeCheckSlowPathARM64 uses this register too.
2404 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2405 } else {
2406 InvokeRuntimeCallingConvention calling_convention;
2407 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2408 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2409 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2410 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002411}
2412
2413void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2414 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002415 Register obj = InputRegisterAt(instruction, 0);
2416 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002417 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002418 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2419 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2420 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2421 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002422
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002423 vixl::Label done, zero;
2424 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002425
2426 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002427 // Avoid null check if we know `obj` is not null.
2428 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002429 __ Cbz(obj, &zero);
2430 }
2431
2432 // In case of an interface check, we put the object class into the object register.
2433 // This is safe, as the register is caller-save, and the object must be in another
2434 // register if it survives the runtime call.
2435 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2436 ? obj
2437 : out;
2438 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2439 GetAssembler()->MaybeUnpoisonHeapReference(target);
2440
2441 switch (instruction->GetTypeCheckKind()) {
2442 case TypeCheckKind::kExactCheck: {
2443 __ Cmp(out, cls);
2444 __ Cset(out, eq);
2445 if (zero.IsLinked()) {
2446 __ B(&done);
2447 }
2448 break;
2449 }
2450 case TypeCheckKind::kAbstractClassCheck: {
2451 // If the class is abstract, we eagerly fetch the super class of the
2452 // object to avoid doing a comparison we know will fail.
2453 vixl::Label loop, success;
2454 __ Bind(&loop);
2455 __ Ldr(out, HeapOperand(out, super_offset));
2456 GetAssembler()->MaybeUnpoisonHeapReference(out);
2457 // If `out` is null, we use it for the result, and jump to `done`.
2458 __ Cbz(out, &done);
2459 __ Cmp(out, cls);
2460 __ B(ne, &loop);
2461 __ Mov(out, 1);
2462 if (zero.IsLinked()) {
2463 __ B(&done);
2464 }
2465 break;
2466 }
2467 case TypeCheckKind::kClassHierarchyCheck: {
2468 // Walk over the class hierarchy to find a match.
2469 vixl::Label loop, success;
2470 __ Bind(&loop);
2471 __ Cmp(out, cls);
2472 __ B(eq, &success);
2473 __ Ldr(out, HeapOperand(out, super_offset));
2474 GetAssembler()->MaybeUnpoisonHeapReference(out);
2475 __ Cbnz(out, &loop);
2476 // If `out` is null, we use it for the result, and jump to `done`.
2477 __ B(&done);
2478 __ Bind(&success);
2479 __ Mov(out, 1);
2480 if (zero.IsLinked()) {
2481 __ B(&done);
2482 }
2483 break;
2484 }
2485 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002486 // Do an exact check.
2487 vixl::Label exact_check;
2488 __ Cmp(out, cls);
2489 __ B(eq, &exact_check);
2490 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002491 __ Ldr(out, HeapOperand(out, component_offset));
2492 GetAssembler()->MaybeUnpoisonHeapReference(out);
2493 // If `out` is null, we use it for the result, and jump to `done`.
2494 __ Cbz(out, &done);
2495 __ Ldrh(out, HeapOperand(out, primitive_offset));
2496 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2497 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002498 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002499 __ Mov(out, 1);
2500 __ B(&done);
2501 break;
2502 }
2503 case TypeCheckKind::kArrayCheck: {
2504 __ Cmp(out, cls);
2505 DCHECK(locations->OnlyCallsOnSlowPath());
2506 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2507 instruction, /* is_fatal */ false);
2508 codegen_->AddSlowPath(slow_path);
2509 __ B(ne, slow_path->GetEntryLabel());
2510 __ Mov(out, 1);
2511 if (zero.IsLinked()) {
2512 __ B(&done);
2513 }
2514 break;
2515 }
2516
2517 case TypeCheckKind::kInterfaceCheck:
2518 default: {
2519 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2520 instruction,
2521 instruction->GetDexPc(),
2522 nullptr);
2523 if (zero.IsLinked()) {
2524 __ B(&done);
2525 }
2526 break;
2527 }
2528 }
2529
2530 if (zero.IsLinked()) {
2531 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002532 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002533 }
2534
2535 if (done.IsLinked()) {
2536 __ Bind(&done);
2537 }
2538
2539 if (slow_path != nullptr) {
2540 __ Bind(slow_path->GetExitLabel());
2541 }
2542}
2543
2544void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2545 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2546 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2547
2548 switch (instruction->GetTypeCheckKind()) {
2549 case TypeCheckKind::kExactCheck:
2550 case TypeCheckKind::kAbstractClassCheck:
2551 case TypeCheckKind::kClassHierarchyCheck:
2552 case TypeCheckKind::kArrayObjectCheck:
2553 call_kind = throws_into_catch
2554 ? LocationSummary::kCallOnSlowPath
2555 : LocationSummary::kNoCall;
2556 break;
2557 case TypeCheckKind::kInterfaceCheck:
2558 call_kind = LocationSummary::kCall;
2559 break;
2560 case TypeCheckKind::kArrayCheck:
2561 call_kind = LocationSummary::kCallOnSlowPath;
2562 break;
2563 }
2564
2565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2566 instruction, call_kind);
2567 if (call_kind != LocationSummary::kCall) {
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 locations->SetInAt(1, Location::RequiresRegister());
2570 // Note that TypeCheckSlowPathARM64 uses this register too.
2571 locations->AddTemp(Location::RequiresRegister());
2572 } else {
2573 InvokeRuntimeCallingConvention calling_convention;
2574 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2575 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2576 }
2577}
2578
2579void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2580 LocationSummary* locations = instruction->GetLocations();
2581 Register obj = InputRegisterAt(instruction, 0);
2582 Register cls = InputRegisterAt(instruction, 1);
2583 Register temp;
2584 if (!locations->WillCall()) {
2585 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2586 }
2587
2588 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2589 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2590 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2591 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2592 SlowPathCodeARM64* slow_path = nullptr;
2593
2594 if (!locations->WillCall()) {
2595 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2596 instruction, !locations->CanCall());
2597 codegen_->AddSlowPath(slow_path);
2598 }
2599
2600 vixl::Label done;
2601 // Avoid null check if we know obj is not null.
2602 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002603 __ Cbz(obj, &done);
2604 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002605
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002606 if (locations->WillCall()) {
2607 __ Ldr(obj, HeapOperand(obj, class_offset));
2608 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002609 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002610 __ Ldr(temp, HeapOperand(obj, class_offset));
2611 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002612 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002613
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002614 switch (instruction->GetTypeCheckKind()) {
2615 case TypeCheckKind::kExactCheck:
2616 case TypeCheckKind::kArrayCheck: {
2617 __ Cmp(temp, cls);
2618 // Jump to slow path for throwing the exception or doing a
2619 // more involved array check.
2620 __ B(ne, slow_path->GetEntryLabel());
2621 break;
2622 }
2623 case TypeCheckKind::kAbstractClassCheck: {
2624 // If the class is abstract, we eagerly fetch the super class of the
2625 // object to avoid doing a comparison we know will fail.
2626 vixl::Label loop;
2627 __ Bind(&loop);
2628 __ Ldr(temp, HeapOperand(temp, super_offset));
2629 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2630 // Jump to the slow path to throw the exception.
2631 __ Cbz(temp, slow_path->GetEntryLabel());
2632 __ Cmp(temp, cls);
2633 __ B(ne, &loop);
2634 break;
2635 }
2636 case TypeCheckKind::kClassHierarchyCheck: {
2637 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002638 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002639 __ Bind(&loop);
2640 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002641 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002642 __ Ldr(temp, HeapOperand(temp, super_offset));
2643 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2644 __ Cbnz(temp, &loop);
2645 // Jump to the slow path to throw the exception.
2646 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002647 break;
2648 }
2649 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002650 // Do an exact check.
2651 __ Cmp(temp, cls);
2652 __ B(eq, &done);
2653 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002654 __ Ldr(temp, HeapOperand(temp, component_offset));
2655 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2656 __ Cbz(temp, slow_path->GetEntryLabel());
2657 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2658 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2659 __ Cbnz(temp, slow_path->GetEntryLabel());
2660 break;
2661 }
2662 case TypeCheckKind::kInterfaceCheck:
2663 default:
2664 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2665 instruction,
2666 instruction->GetDexPc(),
2667 nullptr);
2668 break;
2669 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002670 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002671
2672 if (slow_path != nullptr) {
2673 __ Bind(slow_path->GetExitLabel());
2674 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002675}
2676
Alexandre Rames5319def2014-10-23 10:03:10 +01002677void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2679 locations->SetOut(Location::ConstantLocation(constant));
2680}
2681
2682void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2683 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002684 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002685}
2686
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002687void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2689 locations->SetOut(Location::ConstantLocation(constant));
2690}
2691
2692void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2693 // Will be generated at use site.
2694 UNUSED(constant);
2695}
2696
Calin Juravle175dc732015-08-25 15:42:32 +01002697void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2698 // The trampoline uses the same calling convention as dex calling conventions,
2699 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2700 // the method_idx.
2701 HandleInvoke(invoke);
2702}
2703
2704void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2705 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2706}
2707
Alexandre Rames5319def2014-10-23 10:03:10 +01002708void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002709 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002710 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002711}
2712
Alexandre Rames67555f72014-11-18 10:55:16 +00002713void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2714 HandleInvoke(invoke);
2715}
2716
2717void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2718 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002719 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2720 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2721 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002722 Location receiver = invoke->GetLocations()->InAt(0);
2723 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002724 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002725
2726 // The register ip1 is required to be used for the hidden argument in
2727 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002728 MacroAssembler* masm = GetVIXLAssembler();
2729 UseScratchRegisterScope scratch_scope(masm);
2730 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002731 scratch_scope.Exclude(ip1);
2732 __ Mov(ip1, invoke->GetDexMethodIndex());
2733
2734 // temp = object->GetClass();
2735 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002736 __ Ldr(temp.W(), StackOperandFrom(receiver));
2737 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002738 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002739 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002740 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002741 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002742 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002743 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002744 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002745 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002746 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002747 // lr();
2748 __ Blr(lr);
2749 DCHECK(!codegen_->IsLeafMethod());
2750 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2751}
2752
2753void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002754 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2755 if (intrinsic.TryDispatch(invoke)) {
2756 return;
2757 }
2758
Alexandre Rames67555f72014-11-18 10:55:16 +00002759 HandleInvoke(invoke);
2760}
2761
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002762void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002763 // When we do not run baseline, explicit clinit checks triggered by static
2764 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2765 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002766
Andreas Gampe878d58c2015-01-15 23:24:00 -08002767 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2768 if (intrinsic.TryDispatch(invoke)) {
2769 return;
2770 }
2771
Alexandre Rames67555f72014-11-18 10:55:16 +00002772 HandleInvoke(invoke);
2773}
2774
Andreas Gampe878d58c2015-01-15 23:24:00 -08002775static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2776 if (invoke->GetLocations()->Intrinsified()) {
2777 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2778 intrinsic.Dispatch(invoke);
2779 return true;
2780 }
2781 return false;
2782}
2783
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002784void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002785 // For better instruction scheduling we load the direct code pointer before the method pointer.
2786 bool direct_code_loaded = false;
2787 switch (invoke->GetCodePtrLocation()) {
2788 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2789 // LR = code address from literal pool with link-time patch.
2790 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2791 direct_code_loaded = true;
2792 break;
2793 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2794 // LR = invoke->GetDirectCodePtr();
2795 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2796 direct_code_loaded = true;
2797 break;
2798 default:
2799 break;
2800 }
2801
Andreas Gampe878d58c2015-01-15 23:24:00 -08002802 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002803 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2804 switch (invoke->GetMethodLoadKind()) {
2805 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2806 // temp = thread->string_init_entrypoint
2807 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2808 break;
2809 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2810 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2811 break;
2812 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2813 // Load method address from literal pool.
2814 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2815 break;
2816 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2817 // Load method address from literal pool with a link-time patch.
2818 __ Ldr(XRegisterFrom(temp).X(),
2819 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2820 break;
2821 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2822 // Add ADRP with its PC-relative DexCache access patch.
2823 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2824 invoke->GetDexCacheArrayOffset());
2825 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2826 {
2827 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2828 __ adrp(XRegisterFrom(temp).X(), 0);
2829 }
2830 __ Bind(pc_insn_label); // Bind after ADRP.
2831 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2832 // Add LDR with its PC-relative DexCache access patch.
2833 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2834 invoke->GetDexCacheArrayOffset());
2835 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2836 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2837 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2838 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002839 }
Vladimir Marko58155012015-08-19 12:49:41 +00002840 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2841 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2842 Register reg = XRegisterFrom(temp);
2843 Register method_reg;
2844 if (current_method.IsRegister()) {
2845 method_reg = XRegisterFrom(current_method);
2846 } else {
2847 DCHECK(invoke->GetLocations()->Intrinsified());
2848 DCHECK(!current_method.IsValid());
2849 method_reg = reg;
2850 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2851 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002852
Vladimir Marko58155012015-08-19 12:49:41 +00002853 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002854 __ Ldr(reg.X(),
2855 MemOperand(method_reg.X(),
2856 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002857 // temp = temp[index_in_cache];
2858 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2859 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2860 break;
2861 }
2862 }
2863
2864 switch (invoke->GetCodePtrLocation()) {
2865 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2866 __ Bl(&frame_entry_label_);
2867 break;
2868 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2869 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2870 vixl::Label* label = &relative_call_patches_.back().label;
2871 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2872 __ Bind(label); // Bind after BL.
2873 break;
2874 }
2875 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2876 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2877 // LR prepared above for better instruction scheduling.
2878 DCHECK(direct_code_loaded);
2879 // lr()
2880 __ Blr(lr);
2881 break;
2882 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2883 // LR = callee_method->entry_point_from_quick_compiled_code_;
2884 __ Ldr(lr, MemOperand(
2885 XRegisterFrom(callee_method).X(),
2886 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2887 // lr()
2888 __ Blr(lr);
2889 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002890 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002891
Andreas Gampe878d58c2015-01-15 23:24:00 -08002892 DCHECK(!IsLeafMethod());
2893}
2894
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002895void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2896 LocationSummary* locations = invoke->GetLocations();
2897 Location receiver = locations->InAt(0);
2898 Register temp = XRegisterFrom(temp_in);
2899 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2900 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2901 Offset class_offset = mirror::Object::ClassOffset();
2902 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2903
2904 BlockPoolsScope block_pools(GetVIXLAssembler());
2905
2906 DCHECK(receiver.IsRegister());
2907 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2908 MaybeRecordImplicitNullCheck(invoke);
2909 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2910 // temp = temp->GetMethodAt(method_offset);
2911 __ Ldr(temp, MemOperand(temp, method_offset));
2912 // lr = temp->GetEntryPoint();
2913 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2914 // lr();
2915 __ Blr(lr);
2916}
2917
Vladimir Marko58155012015-08-19 12:49:41 +00002918void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2919 DCHECK(linker_patches->empty());
2920 size_t size =
2921 method_patches_.size() +
2922 call_patches_.size() +
2923 relative_call_patches_.size() +
2924 pc_rel_dex_cache_patches_.size();
2925 linker_patches->reserve(size);
2926 for (const auto& entry : method_patches_) {
2927 const MethodReference& target_method = entry.first;
2928 vixl::Literal<uint64_t>* literal = entry.second;
2929 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2930 target_method.dex_file,
2931 target_method.dex_method_index));
2932 }
2933 for (const auto& entry : call_patches_) {
2934 const MethodReference& target_method = entry.first;
2935 vixl::Literal<uint64_t>* literal = entry.second;
2936 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2937 target_method.dex_file,
2938 target_method.dex_method_index));
2939 }
2940 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2941 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2942 info.target_method.dex_file,
2943 info.target_method.dex_method_index));
2944 }
2945 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2946 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2947 &info.target_dex_file,
2948 info.pc_insn_label->location() - 4u,
2949 info.element_offset));
2950 }
2951}
2952
2953vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2954 // Look up the literal for value.
2955 auto lb = uint64_literals_.lower_bound(value);
2956 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2957 return lb->second;
2958 }
2959 // We don't have a literal for this value, insert a new one.
2960 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2961 uint64_literals_.PutBefore(lb, value, literal);
2962 return literal;
2963}
2964
2965vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2966 MethodReference target_method,
2967 MethodToLiteralMap* map) {
2968 // Look up the literal for target_method.
2969 auto lb = map->lower_bound(target_method);
2970 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2971 return lb->second;
2972 }
2973 // We don't have a literal for this method yet, insert a new one.
2974 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2975 map->PutBefore(lb, target_method, literal);
2976 return literal;
2977}
2978
2979vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2980 MethodReference target_method) {
2981 return DeduplicateMethodLiteral(target_method, &method_patches_);
2982}
2983
2984vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2985 MethodReference target_method) {
2986 return DeduplicateMethodLiteral(target_method, &call_patches_);
2987}
2988
2989
Andreas Gampe878d58c2015-01-15 23:24:00 -08002990void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002991 // When we do not run baseline, explicit clinit checks triggered by static
2992 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2993 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002994
Andreas Gampe878d58c2015-01-15 23:24:00 -08002995 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2996 return;
2997 }
2998
Alexandre Ramesd921d642015-04-16 15:07:16 +01002999 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003000 LocationSummary* locations = invoke->GetLocations();
3001 codegen_->GenerateStaticOrDirectCall(
3002 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003003 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003004}
3005
3006void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003007 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3008 return;
3009 }
3010
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003011 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003012 DCHECK(!codegen_->IsLeafMethod());
3013 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3014}
3015
Alexandre Rames67555f72014-11-18 10:55:16 +00003016void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
3017 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
3018 : LocationSummary::kNoCall;
3019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003020 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003021 locations->SetOut(Location::RequiresRegister());
3022}
3023
3024void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
3025 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003026 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003027 if (cls->IsReferrersClass()) {
3028 DCHECK(!cls->CanCallRuntime());
3029 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003030 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003031 } else {
3032 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003033 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3034 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3035 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3036 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003037
3038 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3039 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3040 codegen_->AddSlowPath(slow_path);
3041 __ Cbz(out, slow_path->GetEntryLabel());
3042 if (cls->MustGenerateClinitCheck()) {
3043 GenerateClassInitializationCheck(slow_path, out);
3044 } else {
3045 __ Bind(slow_path->GetExitLabel());
3046 }
3047 }
3048}
3049
David Brazdilcb1c0552015-08-04 16:22:25 +01003050static MemOperand GetExceptionTlsAddress() {
3051 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3052}
3053
Alexandre Rames67555f72014-11-18 10:55:16 +00003054void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3057 locations->SetOut(Location::RequiresRegister());
3058}
3059
3060void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003061 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3062}
3063
3064void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3065 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3066}
3067
3068void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3069 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003070}
3071
Alexandre Rames5319def2014-10-23 10:03:10 +01003072void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3073 load->SetLocations(nullptr);
3074}
3075
3076void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3077 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003078 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003079}
3080
Alexandre Rames67555f72014-11-18 10:55:16 +00003081void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3082 LocationSummary* locations =
3083 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003084 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003085 locations->SetOut(Location::RequiresRegister());
3086}
3087
3088void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3089 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3090 codegen_->AddSlowPath(slow_path);
3091
3092 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003093 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003094 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003095 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3096 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3097 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003098 __ Cbz(out, slow_path->GetEntryLabel());
3099 __ Bind(slow_path->GetExitLabel());
3100}
3101
Alexandre Rames5319def2014-10-23 10:03:10 +01003102void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3103 local->SetLocations(nullptr);
3104}
3105
3106void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3107 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3108}
3109
3110void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3112 locations->SetOut(Location::ConstantLocation(constant));
3113}
3114
3115void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3116 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003117 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003118}
3119
Alexandre Rames67555f72014-11-18 10:55:16 +00003120void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3121 LocationSummary* locations =
3122 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3123 InvokeRuntimeCallingConvention calling_convention;
3124 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3125}
3126
3127void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3128 codegen_->InvokeRuntime(instruction->IsEnter()
3129 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3130 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003131 instruction->GetDexPc(),
3132 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003133 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003134}
3135
Alexandre Rames42d641b2014-10-27 14:00:51 +00003136void LocationsBuilderARM64::VisitMul(HMul* mul) {
3137 LocationSummary* locations =
3138 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3139 switch (mul->GetResultType()) {
3140 case Primitive::kPrimInt:
3141 case Primitive::kPrimLong:
3142 locations->SetInAt(0, Location::RequiresRegister());
3143 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003145 break;
3146
3147 case Primitive::kPrimFloat:
3148 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003149 locations->SetInAt(0, Location::RequiresFpuRegister());
3150 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003151 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003152 break;
3153
3154 default:
3155 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3156 }
3157}
3158
3159void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3160 switch (mul->GetResultType()) {
3161 case Primitive::kPrimInt:
3162 case Primitive::kPrimLong:
3163 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3164 break;
3165
3166 case Primitive::kPrimFloat:
3167 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003168 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003169 break;
3170
3171 default:
3172 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3173 }
3174}
3175
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003176void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3177 LocationSummary* locations =
3178 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3179 switch (neg->GetResultType()) {
3180 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003181 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003182 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003184 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003185
3186 case Primitive::kPrimFloat:
3187 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003188 locations->SetInAt(0, Location::RequiresFpuRegister());
3189 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003190 break;
3191
3192 default:
3193 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3194 }
3195}
3196
3197void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3198 switch (neg->GetResultType()) {
3199 case Primitive::kPrimInt:
3200 case Primitive::kPrimLong:
3201 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3202 break;
3203
3204 case Primitive::kPrimFloat:
3205 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003206 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003207 break;
3208
3209 default:
3210 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3211 }
3212}
3213
3214void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3215 LocationSummary* locations =
3216 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3217 InvokeRuntimeCallingConvention calling_convention;
3218 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003219 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003220 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003221 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003222 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003223 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003224}
3225
3226void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3227 LocationSummary* locations = instruction->GetLocations();
3228 InvokeRuntimeCallingConvention calling_convention;
3229 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3230 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003231 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003232 // Note: if heap poisoning is enabled, the entry point takes cares
3233 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003234 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3235 instruction,
3236 instruction->GetDexPc(),
3237 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003238 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003239}
3240
Alexandre Rames5319def2014-10-23 10:03:10 +01003241void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3242 LocationSummary* locations =
3243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3244 InvokeRuntimeCallingConvention calling_convention;
3245 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003246 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003247 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003248 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003249}
3250
3251void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3252 LocationSummary* locations = instruction->GetLocations();
3253 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3254 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003255 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003256 // Note: if heap poisoning is enabled, the entry point takes cares
3257 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003258 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3259 instruction,
3260 instruction->GetDexPc(),
3261 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003262 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003263}
3264
3265void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003267 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003268 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003269}
3270
3271void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003272 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003273 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003274 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003275 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003276 break;
3277
3278 default:
3279 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3280 }
3281}
3282
David Brazdil66d126e2015-04-03 16:02:44 +01003283void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3285 locations->SetInAt(0, Location::RequiresRegister());
3286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3287}
3288
3289void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003290 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3291}
3292
Alexandre Rames5319def2014-10-23 10:03:10 +01003293void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003294 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3295 ? LocationSummary::kCallOnSlowPath
3296 : LocationSummary::kNoCall;
3297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003298 locations->SetInAt(0, Location::RequiresRegister());
3299 if (instruction->HasUses()) {
3300 locations->SetOut(Location::SameAsFirstInput());
3301 }
3302}
3303
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003304void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3306 return;
3307 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003308
Alexandre Ramesd921d642015-04-16 15:07:16 +01003309 BlockPoolsScope block_pools(GetVIXLAssembler());
3310 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003311 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3312 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3313}
3314
3315void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003316 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3317 codegen_->AddSlowPath(slow_path);
3318
3319 LocationSummary* locations = instruction->GetLocations();
3320 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003321
3322 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003323}
3324
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003325void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003326 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003327 GenerateImplicitNullCheck(instruction);
3328 } else {
3329 GenerateExplicitNullCheck(instruction);
3330 }
3331}
3332
Alexandre Rames67555f72014-11-18 10:55:16 +00003333void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3334 HandleBinaryOp(instruction);
3335}
3336
3337void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3338 HandleBinaryOp(instruction);
3339}
3340
Alexandre Rames3e69f162014-12-10 10:36:50 +00003341void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3342 LOG(FATAL) << "Unreachable";
3343}
3344
3345void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3346 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3347}
3348
Alexandre Rames5319def2014-10-23 10:03:10 +01003349void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3351 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3352 if (location.IsStackSlot()) {
3353 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3354 } else if (location.IsDoubleStackSlot()) {
3355 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3356 }
3357 locations->SetOut(location);
3358}
3359
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003360void InstructionCodeGeneratorARM64::VisitParameterValue(
3361 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003362 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003363}
3364
3365void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3366 LocationSummary* locations =
3367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003368 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003369}
3370
3371void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3372 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3373 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003374}
3375
3376void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3378 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3379 locations->SetInAt(i, Location::Any());
3380 }
3381 locations->SetOut(Location::Any());
3382}
3383
3384void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003385 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003386 LOG(FATAL) << "Unreachable";
3387}
3388
Serban Constantinescu02164b32014-11-13 14:05:07 +00003389void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003390 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003391 LocationSummary::CallKind call_kind =
3392 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3394
3395 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003396 case Primitive::kPrimInt:
3397 case Primitive::kPrimLong:
3398 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003399 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3401 break;
3402
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003403 case Primitive::kPrimFloat:
3404 case Primitive::kPrimDouble: {
3405 InvokeRuntimeCallingConvention calling_convention;
3406 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3407 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3408 locations->SetOut(calling_convention.GetReturnLocation(type));
3409
3410 break;
3411 }
3412
Serban Constantinescu02164b32014-11-13 14:05:07 +00003413 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003414 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003415 }
3416}
3417
3418void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3419 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003420
Serban Constantinescu02164b32014-11-13 14:05:07 +00003421 switch (type) {
3422 case Primitive::kPrimInt:
3423 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003424 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003425 break;
3426 }
3427
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003428 case Primitive::kPrimFloat:
3429 case Primitive::kPrimDouble: {
3430 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3431 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003432 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003433 break;
3434 }
3435
Serban Constantinescu02164b32014-11-13 14:05:07 +00003436 default:
3437 LOG(FATAL) << "Unexpected rem type " << type;
3438 }
3439}
3440
Calin Juravle27df7582015-04-17 19:12:31 +01003441void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3442 memory_barrier->SetLocations(nullptr);
3443}
3444
3445void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3446 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3447}
3448
Alexandre Rames5319def2014-10-23 10:03:10 +01003449void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3451 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003452 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003453}
3454
3455void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003456 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003457 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003458}
3459
3460void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3461 instruction->SetLocations(nullptr);
3462}
3463
3464void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003465 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003466 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003467}
3468
Serban Constantinescu02164b32014-11-13 14:05:07 +00003469void LocationsBuilderARM64::VisitShl(HShl* shl) {
3470 HandleShift(shl);
3471}
3472
3473void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3474 HandleShift(shl);
3475}
3476
3477void LocationsBuilderARM64::VisitShr(HShr* shr) {
3478 HandleShift(shr);
3479}
3480
3481void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3482 HandleShift(shr);
3483}
3484
Alexandre Rames5319def2014-10-23 10:03:10 +01003485void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3487 Primitive::Type field_type = store->InputAt(1)->GetType();
3488 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003489 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003490 case Primitive::kPrimBoolean:
3491 case Primitive::kPrimByte:
3492 case Primitive::kPrimChar:
3493 case Primitive::kPrimShort:
3494 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003495 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003496 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3497 break;
3498
3499 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003500 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003501 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3502 break;
3503
3504 default:
3505 LOG(FATAL) << "Unimplemented local type " << field_type;
3506 }
3507}
3508
3509void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003510 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003511}
3512
3513void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003514 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003515}
3516
3517void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003518 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003519}
3520
Alexandre Rames67555f72014-11-18 10:55:16 +00003521void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003522 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003523}
3524
3525void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003526 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003527}
3528
3529void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003530 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003531}
3532
Alexandre Rames67555f72014-11-18 10:55:16 +00003533void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003534 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003535}
3536
Calin Juravlee460d1d2015-09-29 04:52:17 +01003537void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3538 HUnresolvedInstanceFieldGet* instruction) {
3539 FieldAccessCallingConventionARM64 calling_convention;
3540 codegen_->CreateUnresolvedFieldLocationSummary(
3541 instruction, instruction->GetFieldType(), calling_convention);
3542}
3543
3544void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3545 HUnresolvedInstanceFieldGet* instruction) {
3546 FieldAccessCallingConventionARM64 calling_convention;
3547 codegen_->GenerateUnresolvedFieldAccess(instruction,
3548 instruction->GetFieldType(),
3549 instruction->GetFieldIndex(),
3550 instruction->GetDexPc(),
3551 calling_convention);
3552}
3553
3554void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3555 HUnresolvedInstanceFieldSet* instruction) {
3556 FieldAccessCallingConventionARM64 calling_convention;
3557 codegen_->CreateUnresolvedFieldLocationSummary(
3558 instruction, instruction->GetFieldType(), calling_convention);
3559}
3560
3561void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3562 HUnresolvedInstanceFieldSet* instruction) {
3563 FieldAccessCallingConventionARM64 calling_convention;
3564 codegen_->GenerateUnresolvedFieldAccess(instruction,
3565 instruction->GetFieldType(),
3566 instruction->GetFieldIndex(),
3567 instruction->GetDexPc(),
3568 calling_convention);
3569}
3570
3571void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3572 HUnresolvedStaticFieldGet* instruction) {
3573 FieldAccessCallingConventionARM64 calling_convention;
3574 codegen_->CreateUnresolvedFieldLocationSummary(
3575 instruction, instruction->GetFieldType(), calling_convention);
3576}
3577
3578void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3579 HUnresolvedStaticFieldGet* instruction) {
3580 FieldAccessCallingConventionARM64 calling_convention;
3581 codegen_->GenerateUnresolvedFieldAccess(instruction,
3582 instruction->GetFieldType(),
3583 instruction->GetFieldIndex(),
3584 instruction->GetDexPc(),
3585 calling_convention);
3586}
3587
3588void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3589 HUnresolvedStaticFieldSet* instruction) {
3590 FieldAccessCallingConventionARM64 calling_convention;
3591 codegen_->CreateUnresolvedFieldLocationSummary(
3592 instruction, instruction->GetFieldType(), calling_convention);
3593}
3594
3595void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3596 HUnresolvedStaticFieldSet* instruction) {
3597 FieldAccessCallingConventionARM64 calling_convention;
3598 codegen_->GenerateUnresolvedFieldAccess(instruction,
3599 instruction->GetFieldType(),
3600 instruction->GetFieldIndex(),
3601 instruction->GetDexPc(),
3602 calling_convention);
3603}
3604
Alexandre Rames5319def2014-10-23 10:03:10 +01003605void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3607}
3608
3609void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003610 HBasicBlock* block = instruction->GetBlock();
3611 if (block->GetLoopInformation() != nullptr) {
3612 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3613 // The back edge will generate the suspend check.
3614 return;
3615 }
3616 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3617 // The goto will generate the suspend check.
3618 return;
3619 }
3620 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003621}
3622
3623void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3624 temp->SetLocations(nullptr);
3625}
3626
3627void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3628 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003629 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003630}
3631
Alexandre Rames67555f72014-11-18 10:55:16 +00003632void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3633 LocationSummary* locations =
3634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3635 InvokeRuntimeCallingConvention calling_convention;
3636 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3637}
3638
3639void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3640 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003641 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003642 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003643}
3644
3645void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3646 LocationSummary* locations =
3647 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3648 Primitive::Type input_type = conversion->GetInputType();
3649 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003650 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003651 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3652 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3653 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3654 }
3655
Alexandre Rames542361f2015-01-29 16:57:31 +00003656 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003657 locations->SetInAt(0, Location::RequiresFpuRegister());
3658 } else {
3659 locations->SetInAt(0, Location::RequiresRegister());
3660 }
3661
Alexandre Rames542361f2015-01-29 16:57:31 +00003662 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003663 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3664 } else {
3665 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3666 }
3667}
3668
3669void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3670 Primitive::Type result_type = conversion->GetResultType();
3671 Primitive::Type input_type = conversion->GetInputType();
3672
3673 DCHECK_NE(input_type, result_type);
3674
Alexandre Rames542361f2015-01-29 16:57:31 +00003675 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003676 int result_size = Primitive::ComponentSize(result_type);
3677 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003678 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003679 Register output = OutputRegister(conversion);
3680 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003681 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3682 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003683 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3684 // 'int' values are used directly as W registers, discarding the top
3685 // bits, so we don't need to sign-extend and can just perform a move.
3686 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3687 // top 32 bits of the target register. We theoretically could leave those
3688 // bits unchanged, but we would have to make sure that no code uses a
3689 // 32bit input value as a 64bit value assuming that the top 32 bits are
3690 // zero.
3691 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003692 } else if ((result_type == Primitive::kPrimChar) ||
3693 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3694 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003695 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003696 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003697 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003698 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003699 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003700 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003701 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3702 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003703 } else if (Primitive::IsFloatingPointType(result_type) &&
3704 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003705 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3706 } else {
3707 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3708 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003709 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003710}
Alexandre Rames67555f72014-11-18 10:55:16 +00003711
Serban Constantinescu02164b32014-11-13 14:05:07 +00003712void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3713 HandleShift(ushr);
3714}
3715
3716void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3717 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003718}
3719
3720void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3721 HandleBinaryOp(instruction);
3722}
3723
3724void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3725 HandleBinaryOp(instruction);
3726}
3727
Calin Juravleb1498f62015-02-16 13:13:29 +00003728void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3729 // Nothing to do, this should be removed during prepare for register allocator.
3730 UNUSED(instruction);
3731 LOG(FATAL) << "Unreachable";
3732}
3733
3734void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3735 // Nothing to do, this should be removed during prepare for register allocator.
3736 UNUSED(instruction);
3737 LOG(FATAL) << "Unreachable";
3738}
3739
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003740void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3741 DCHECK(codegen_->IsBaseline());
3742 LocationSummary* locations =
3743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3744 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3745}
3746
3747void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3748 DCHECK(codegen_->IsBaseline());
3749 // Will be generated at use site.
3750}
3751
Mark Mendellfe57faa2015-09-18 09:26:15 -04003752// Simple implementation of packed switch - generate cascaded compare/jumps.
3753void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3754 LocationSummary* locations =
3755 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3756 locations->SetInAt(0, Location::RequiresRegister());
3757}
3758
3759void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3760 int32_t lower_bound = switch_instr->GetStartValue();
3761 int32_t num_entries = switch_instr->GetNumEntries();
3762 Register value_reg = InputRegisterAt(switch_instr, 0);
3763 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3764
3765 // Create a series of compare/jumps.
3766 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3767 for (int32_t i = 0; i < num_entries; i++) {
3768 int32_t case_value = lower_bound + i;
3769 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3770 if (case_value == 0) {
3771 __ Cbz(value_reg, succ);
3772 } else {
3773 __ Cmp(value_reg, vixl::Operand(case_value));
3774 __ B(eq, succ);
3775 }
3776 }
3777
3778 // And the default for any other value.
3779 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3780 __ B(codegen_->GetLabelOf(default_block));
3781 }
3782}
3783
Alexandre Rames67555f72014-11-18 10:55:16 +00003784#undef __
3785#undef QUICK_ENTRY_POINT
3786
Alexandre Rames5319def2014-10-23 10:03:10 +01003787} // namespace arm64
3788} // namespace art