blob: 7c2ed7c676123f3de5bd252497f65cfb33a3f26f [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()));
121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100193 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100194
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000197 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100198
Alexandre Rames5319def2014-10-23 10:03:10 +0100199 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100208 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
223};
224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
226 public:
227 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
231 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000232 if (instruction_->CanThrowIntoCatchBlock()) {
233 // Live registers will be restored in the catch block if caught.
234 SaveLiveRegisters(codegen, instruction_->GetLocations());
235 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000237 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800238 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 }
240
Alexandre Rames8158f282015-08-07 10:26:17 +0100241 bool IsFatal() const OVERRIDE { return true; }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
244
Alexandre Rames67555f72014-11-18 10:55:16 +0000245 private:
246 HDivZeroCheck* const instruction_;
247 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
248};
249
250class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
252 LoadClassSlowPathARM64(HLoadClass* cls,
253 HInstruction* at,
254 uint32_t dex_pc,
255 bool do_clinit)
256 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
257 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
258 }
259
260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
261 LocationSummary* locations = at_->GetLocations();
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
268 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
270 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 // Move the class to the desired location.
279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000283 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 }
285
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 __ B(GetExitLabel());
288 }
289
Alexandre Rames9931f312015-06-19 14:47:01 +0100290 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 private:
293 // The class this slow path will load.
294 HLoadClass* const cls_;
295
296 // The instruction where this slow path is happening.
297 // (Might be the load class or an initialization check).
298 HInstruction* const at_;
299
300 // The dex PC of `at_`.
301 const uint32_t dex_pc_;
302
303 // Whether to initialize the class.
304 const bool do_clinit_;
305
306 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
307};
308
309class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
312
313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
314 LocationSummary* locations = instruction_->GetLocations();
315 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
316 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
317
318 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320
321 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000324 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100325 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000327 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000329 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 __ B(GetExitLabel());
331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
334
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 private:
336 HLoadString* const instruction_;
337
338 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
339};
340
Alexandre Rames5319def2014-10-23 10:03:10 +0100341class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
342 public:
343 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
344
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100347 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000348 if (instruction_->CanThrowIntoCatchBlock()) {
349 // Live registers will be restored in the catch block if caught.
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 }
356
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 bool IsFatal() const OVERRIDE { return true; }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361 private:
362 HNullCheck* const instruction_;
363
364 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
365};
366
367class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100369 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100370 : instruction_(instruction), successor_(successor) {}
371
Alexandre Rames67555f72014-11-18 10:55:16 +0000372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000377 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800378 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000379 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 if (successor_ == nullptr) {
381 __ B(GetReturnLabel());
382 } else {
383 __ B(arm64_codegen->GetLabelOf(successor_));
384 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 }
386
387 vixl::Label* GetReturnLabel() {
388 DCHECK(successor_ == nullptr);
389 return &return_label_;
390 }
391
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100392 HBasicBlock* GetSuccessor() const {
393 return successor_;
394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
397
Alexandre Rames5319def2014-10-23 10:03:10 +0100398 private:
399 HSuspendCheck* const instruction_;
400 // If not null, the block to branch to after the suspend check.
401 HBasicBlock* const successor_;
402
403 // If `successor_` is null, the label to branch to after the suspend check.
404 vixl::Label return_label_;
405
406 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
407};
408
Alexandre Rames67555f72014-11-18 10:55:16 +0000409class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100411 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
412 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100416 Location class_to_check = locations->InAt(1);
417 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
418 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000419 DCHECK(instruction_->IsCheckCast()
420 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
421 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100422 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
Alexandre Rames67555f72014-11-18 10:55:16 +0000424 __ Bind(GetEntryLabel());
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100425
426 if (instruction_->IsCheckCast()) {
427 // The codegen for the instruction overwrites `temp`, so put it back in place.
428 Register obj = InputRegisterAt(instruction_, 0);
429 Register temp = WRegisterFrom(locations->GetTemp(0));
430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
431 __ Ldr(temp, HeapOperand(obj, class_offset));
432 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
433 }
434
435 if (!is_fatal_) {
436 SaveLiveRegisters(codegen, locations);
437 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438
439 // We're moving two locations to locations that could overlap, so we need a parallel
440 // move resolver.
441 InvokeRuntimeCallingConvention calling_convention;
442 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100443 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
444 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445
446 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000447 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100448 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449 Primitive::Type ret_type = instruction_->GetType();
450 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
451 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800452 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
453 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000454 } else {
455 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800457 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458 }
459
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100460 if (!is_fatal_) {
461 RestoreLiveRegisters(codegen, locations);
462 __ B(GetExitLabel());
463 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 }
465
Alexandre Rames9931f312015-06-19 14:47:01 +0100466 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100467 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100468
Alexandre Rames67555f72014-11-18 10:55:16 +0000469 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000470 HInstruction* const instruction_;
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100471 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
474};
475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700476class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
477 public:
478 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
479 : instruction_(instruction) {}
480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 __ Bind(GetEntryLabel());
483 SaveLiveRegisters(codegen, instruction_->GetLocations());
484 DCHECK(instruction_->IsDeoptimize());
485 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
486 uint32_t dex_pc = deoptimize->GetDexPc();
487 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
488 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
489 }
490
Alexandre Rames9931f312015-06-19 14:47:01 +0100491 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
492
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493 private:
494 HInstruction* const instruction_;
495 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
496};
497
Alexandre Rames5319def2014-10-23 10:03:10 +0100498#undef __
499
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100500Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100501 Location next_location;
502 if (type == Primitive::kPrimVoid) {
503 LOG(FATAL) << "Unreachable type " << type;
504 }
505
Alexandre Rames542361f2015-01-29 16:57:31 +0000506 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100507 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
508 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000509 } else if (!Primitive::IsFloatingPointType(type) &&
510 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000511 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
512 } else {
513 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000514 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
515 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100516 }
517
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000518 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000519 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100520 return next_location;
521}
522
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100523Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100524 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100525}
526
Serban Constantinescu579885a2015-02-22 20:51:33 +0000527CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
528 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100529 const CompilerOptions& compiler_options,
530 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100531 : CodeGenerator(graph,
532 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000533 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000534 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000535 callee_saved_core_registers.list(),
536 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100537 compiler_options,
538 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100539 block_labels_(nullptr),
540 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000541 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000542 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000543 isa_features_(isa_features),
544 uint64_literals_(std::less<uint64_t>(), graph->GetArena()->Adapter()),
545 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
546 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
547 relative_call_patches_(graph->GetArena()->Adapter()),
548 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000549 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000550 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000551}
Alexandre Rames5319def2014-10-23 10:03:10 +0100552
Alexandre Rames67555f72014-11-18 10:55:16 +0000553#undef __
554#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100555
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000556void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
557 // Ensure we emit the literal pool.
558 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000559
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000560 CodeGenerator::Finalize(allocator);
561}
562
Zheng Xuad4450e2015-04-17 18:48:56 +0800563void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
564 // Note: There are 6 kinds of moves:
565 // 1. constant -> GPR/FPR (non-cycle)
566 // 2. constant -> stack (non-cycle)
567 // 3. GPR/FPR -> GPR/FPR
568 // 4. GPR/FPR -> stack
569 // 5. stack -> GPR/FPR
570 // 6. stack -> stack (non-cycle)
571 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
572 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
573 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
574 // dependency.
575 vixl_temps_.Open(GetVIXLAssembler());
576}
577
578void ParallelMoveResolverARM64::FinishEmitNativeCode() {
579 vixl_temps_.Close();
580}
581
582Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
583 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
584 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
585 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
586 Location scratch = GetScratchLocation(kind);
587 if (!scratch.Equals(Location::NoLocation())) {
588 return scratch;
589 }
590 // Allocate from VIXL temp registers.
591 if (kind == Location::kRegister) {
592 scratch = LocationFrom(vixl_temps_.AcquireX());
593 } else {
594 DCHECK(kind == Location::kFpuRegister);
595 scratch = LocationFrom(vixl_temps_.AcquireD());
596 }
597 AddScratchLocation(scratch);
598 return scratch;
599}
600
601void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
602 if (loc.IsRegister()) {
603 vixl_temps_.Release(XRegisterFrom(loc));
604 } else {
605 DCHECK(loc.IsFpuRegister());
606 vixl_temps_.Release(DRegisterFrom(loc));
607 }
608 RemoveScratchLocation(loc);
609}
610
Alexandre Rames3e69f162014-12-10 10:36:50 +0000611void ParallelMoveResolverARM64::EmitMove(size_t index) {
612 MoveOperands* move = moves_.Get(index);
613 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
614}
615
Alexandre Rames5319def2014-10-23 10:03:10 +0100616void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100617 MacroAssembler* masm = GetVIXLAssembler();
618 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000619 __ Bind(&frame_entry_label_);
620
Serban Constantinescu02164b32014-11-13 14:05:07 +0000621 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
622 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100623 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000624 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000625 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000626 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000627 __ Ldr(wzr, MemOperand(temp, 0));
628 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000629 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100630
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000631 if (!HasEmptyFrame()) {
632 int frame_size = GetFrameSize();
633 // Stack layout:
634 // sp[frame_size - 8] : lr.
635 // ... : other preserved core registers.
636 // ... : other preserved fp registers.
637 // ... : reserved frame space.
638 // sp[0] : current method.
639 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100640 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800641 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
642 frame_size - GetCoreSpillSize());
643 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
644 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000645 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100646}
647
648void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100649 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100650 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000651 if (!HasEmptyFrame()) {
652 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800653 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
654 frame_size - FrameEntrySpillSize());
655 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
656 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000657 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100658 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000659 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100660 __ Ret();
661 GetAssembler()->cfi().RestoreState();
662 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100663}
664
Zheng Xuda403092015-04-24 17:35:39 +0800665vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
666 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
667 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
668 core_spill_mask_);
669}
670
671vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
672 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
673 GetNumberOfFloatingPointRegisters()));
674 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
675 fpu_spill_mask_);
676}
677
Alexandre Rames5319def2014-10-23 10:03:10 +0100678void CodeGeneratorARM64::Bind(HBasicBlock* block) {
679 __ Bind(GetLabelOf(block));
680}
681
Alexandre Rames5319def2014-10-23 10:03:10 +0100682void CodeGeneratorARM64::Move(HInstruction* instruction,
683 Location location,
684 HInstruction* move_for) {
685 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100686 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000687 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100688
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100689 if (instruction->IsFakeString()) {
690 // The fake string is an alias for null.
691 DCHECK(IsBaseline());
692 instruction = locations->Out().GetConstant();
693 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
694 }
695
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100696 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700697 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100698 } else if (locations != nullptr && locations->Out().Equals(location)) {
699 return;
700 } else if (instruction->IsIntConstant()
701 || instruction->IsLongConstant()
702 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000703 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100704 if (location.IsRegister()) {
705 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000706 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100707 (instruction->IsLongConstant() && dst.Is64Bits()));
708 __ Mov(dst, value);
709 } else {
710 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000711 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000712 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
713 ? temps.AcquireW()
714 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100715 __ Mov(temp, value);
716 __ Str(temp, StackOperandFrom(location));
717 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000718 } else if (instruction->IsTemporary()) {
719 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000720 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100721 } else if (instruction->IsLoadLocal()) {
722 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000723 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000724 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000725 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000726 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100727 }
728
729 } else {
730 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000731 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100732 }
733}
734
Calin Juravle175dc732015-08-25 15:42:32 +0100735void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
736 DCHECK(location.IsRegister());
737 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
738}
739
Calin Juravle23a8e352015-09-08 19:56:31 +0100740void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
741 if (location.IsRegister()) {
742 locations->AddTemp(location);
743 } else {
744 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
745 }
746}
747
748void CodeGeneratorARM64::MoveLocationToTemp(Location source,
749 const LocationSummary& locations,
750 int temp_index,
751 Primitive::Type type) {
752 if (!Primitive::IsFloatingPointType(type)) {
753 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
754 }
755
756 DCHECK(source.IsFpuRegister()) << source;
757 Primitive::Type temp_type = Primitive::Is64BitType(type)
758 ? Primitive::kPrimLong
759 : Primitive::kPrimInt;
760 __ Fmov(RegisterFrom(locations.GetTemp(temp_index), temp_type),
761 FPRegisterFrom(source, type));
762}
763
764void CodeGeneratorARM64::MoveTempToLocation(const LocationSummary& locations,
765 int temp_index,
766 Location destination,
767 Primitive::Type type) {
768 if (!Primitive::IsFloatingPointType(type)) {
769 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
770 }
771
772 DCHECK(destination.IsFpuRegister()) << destination;
773 Primitive::Type temp_type = Primitive::Is64BitType(type)
774 ? Primitive::kPrimLong
775 : Primitive::kPrimInt;
776 __ Fmov(FPRegisterFrom(destination, type),
777 RegisterFrom(locations.GetTemp(temp_index), temp_type));
778}
779
Alexandre Rames5319def2014-10-23 10:03:10 +0100780Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
781 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000782
Alexandre Rames5319def2014-10-23 10:03:10 +0100783 switch (type) {
784 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000785 case Primitive::kPrimInt:
786 case Primitive::kPrimFloat:
787 return Location::StackSlot(GetStackSlot(load->GetLocal()));
788
789 case Primitive::kPrimLong:
790 case Primitive::kPrimDouble:
791 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
792
Alexandre Rames5319def2014-10-23 10:03:10 +0100793 case Primitive::kPrimBoolean:
794 case Primitive::kPrimByte:
795 case Primitive::kPrimChar:
796 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100797 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100798 LOG(FATAL) << "Unexpected type " << type;
799 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000800
Alexandre Rames5319def2014-10-23 10:03:10 +0100801 LOG(FATAL) << "Unreachable";
802 return Location::NoLocation();
803}
804
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100805void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000806 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100807 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000808 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100809 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100810 if (value_can_be_null) {
811 __ Cbz(value, &done);
812 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100813 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
814 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000815 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100816 if (value_can_be_null) {
817 __ Bind(&done);
818 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100819}
820
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000821void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
822 // Blocked core registers:
823 // lr : Runtime reserved.
824 // tr : Runtime reserved.
825 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
826 // ip1 : VIXL core temp.
827 // ip0 : VIXL core temp.
828 //
829 // Blocked fp registers:
830 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100831 CPURegList reserved_core_registers = vixl_reserved_core_registers;
832 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100833 while (!reserved_core_registers.IsEmpty()) {
834 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
835 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000836
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000837 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800838 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000839 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
840 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000841
842 if (is_baseline) {
843 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
844 while (!reserved_core_baseline_registers.IsEmpty()) {
845 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
846 }
847
848 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
849 while (!reserved_fp_baseline_registers.IsEmpty()) {
850 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
851 }
852 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100853}
854
855Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
856 if (type == Primitive::kPrimVoid) {
857 LOG(FATAL) << "Unreachable type " << type;
858 }
859
Alexandre Rames542361f2015-01-29 16:57:31 +0000860 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000861 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
862 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100863 return Location::FpuRegisterLocation(reg);
864 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000865 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
866 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 return Location::RegisterLocation(reg);
868 }
869}
870
Alexandre Rames3e69f162014-12-10 10:36:50 +0000871size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
872 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
873 __ Str(reg, MemOperand(sp, stack_index));
874 return kArm64WordSize;
875}
876
877size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
878 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
879 __ Ldr(reg, MemOperand(sp, stack_index));
880 return kArm64WordSize;
881}
882
883size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
884 FPRegister reg = FPRegister(reg_id, kDRegSize);
885 __ Str(reg, MemOperand(sp, stack_index));
886 return kArm64WordSize;
887}
888
889size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
890 FPRegister reg = FPRegister(reg_id, kDRegSize);
891 __ Ldr(reg, MemOperand(sp, stack_index));
892 return kArm64WordSize;
893}
894
Alexandre Rames5319def2014-10-23 10:03:10 +0100895void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100896 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100897}
898
899void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100900 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100901}
902
Alexandre Rames67555f72014-11-18 10:55:16 +0000903void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000904 if (constant->IsIntConstant()) {
905 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
906 } else if (constant->IsLongConstant()) {
907 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
908 } else if (constant->IsNullConstant()) {
909 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000910 } else if (constant->IsFloatConstant()) {
911 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
912 } else {
913 DCHECK(constant->IsDoubleConstant());
914 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
915 }
916}
917
Alexandre Rames3e69f162014-12-10 10:36:50 +0000918
919static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
920 DCHECK(constant.IsConstant());
921 HConstant* cst = constant.GetConstant();
922 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000923 // Null is mapped to a core W register, which we associate with kPrimInt.
924 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000925 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
926 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
927 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
928}
929
930void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000931 if (source.Equals(destination)) {
932 return;
933 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000934
935 // A valid move can always be inferred from the destination and source
936 // locations. When moving from and to a register, the argument type can be
937 // used to generate 32bit instead of 64bit moves. In debug mode we also
938 // checks the coherency of the locations and the type.
939 bool unspecified_type = (type == Primitive::kPrimVoid);
940
941 if (destination.IsRegister() || destination.IsFpuRegister()) {
942 if (unspecified_type) {
943 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
944 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000945 (src_cst != nullptr && (src_cst->IsIntConstant()
946 || src_cst->IsFloatConstant()
947 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000948 // For stack slots and 32bit constants, a 64bit type is appropriate.
949 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000950 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000951 // If the source is a double stack slot or a 64bit constant, a 64bit
952 // type is appropriate. Else the source is a register, and since the
953 // type has not been specified, we chose a 64bit type to force a 64bit
954 // move.
955 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000956 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000957 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000958 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
959 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000960 CPURegister dst = CPURegisterFrom(destination, type);
961 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
962 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
963 __ Ldr(dst, StackOperandFrom(source));
964 } else if (source.IsConstant()) {
965 DCHECK(CoherentConstantAndType(source, type));
966 MoveConstant(dst, source.GetConstant());
967 } else {
968 if (destination.IsRegister()) {
969 __ Mov(Register(dst), RegisterFrom(source, type));
970 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800971 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000972 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
973 }
974 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000975 } else { // The destination is not a register. It must be a stack slot.
976 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
977 if (source.IsRegister() || source.IsFpuRegister()) {
978 if (unspecified_type) {
979 if (source.IsRegister()) {
980 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
981 } else {
982 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
983 }
984 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000985 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
986 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000987 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
988 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100989 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000990 UseScratchRegisterScope temps(GetVIXLAssembler());
991 HConstant* src_cst = source.GetConstant();
992 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000993 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000994 temp = temps.AcquireW();
995 } else if (src_cst->IsLongConstant()) {
996 temp = temps.AcquireX();
997 } else if (src_cst->IsFloatConstant()) {
998 temp = temps.AcquireS();
999 } else {
1000 DCHECK(src_cst->IsDoubleConstant());
1001 temp = temps.AcquireD();
1002 }
1003 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001004 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001005 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001006 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001007 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001008 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001009 // There is generally less pressure on FP registers.
1010 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001011 __ Ldr(temp, StackOperandFrom(source));
1012 __ Str(temp, StackOperandFrom(destination));
1013 }
1014 }
1015}
1016
1017void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001018 CPURegister dst,
1019 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001020 switch (type) {
1021 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001022 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001023 break;
1024 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001025 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001026 break;
1027 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001028 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001029 break;
1030 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001031 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001032 break;
1033 case Primitive::kPrimInt:
1034 case Primitive::kPrimNot:
1035 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001036 case Primitive::kPrimFloat:
1037 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001038 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001039 __ Ldr(dst, src);
1040 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001041 case Primitive::kPrimVoid:
1042 LOG(FATAL) << "Unreachable type " << type;
1043 }
1044}
1045
Calin Juravle77520bc2015-01-12 18:45:46 +00001046void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001047 CPURegister dst,
1048 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001049 MacroAssembler* masm = GetVIXLAssembler();
1050 BlockPoolsScope block_pools(masm);
1051 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001052 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001053 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001054
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001055 DCHECK(!src.IsPreIndex());
1056 DCHECK(!src.IsPostIndex());
1057
1058 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001059 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001060 MemOperand base = MemOperand(temp_base);
1061 switch (type) {
1062 case Primitive::kPrimBoolean:
1063 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001064 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001065 break;
1066 case Primitive::kPrimByte:
1067 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001068 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001069 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1070 break;
1071 case Primitive::kPrimChar:
1072 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001073 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001074 break;
1075 case Primitive::kPrimShort:
1076 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001077 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001078 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1079 break;
1080 case Primitive::kPrimInt:
1081 case Primitive::kPrimNot:
1082 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001083 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001084 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001085 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001086 break;
1087 case Primitive::kPrimFloat:
1088 case Primitive::kPrimDouble: {
1089 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001090 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001091
1092 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1093 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001094 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001095 __ Fmov(FPRegister(dst), temp);
1096 break;
1097 }
1098 case Primitive::kPrimVoid:
1099 LOG(FATAL) << "Unreachable type " << type;
1100 }
1101}
1102
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001103void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001104 CPURegister src,
1105 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001106 switch (type) {
1107 case Primitive::kPrimBoolean:
1108 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001109 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001110 break;
1111 case Primitive::kPrimChar:
1112 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001113 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001114 break;
1115 case Primitive::kPrimInt:
1116 case Primitive::kPrimNot:
1117 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001118 case Primitive::kPrimFloat:
1119 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001120 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001121 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001122 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001123 case Primitive::kPrimVoid:
1124 LOG(FATAL) << "Unreachable type " << type;
1125 }
1126}
1127
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001128void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1129 CPURegister src,
1130 const MemOperand& dst) {
1131 UseScratchRegisterScope temps(GetVIXLAssembler());
1132 Register temp_base = temps.AcquireX();
1133
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001134 DCHECK(!dst.IsPreIndex());
1135 DCHECK(!dst.IsPostIndex());
1136
1137 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001138 Operand op = OperandFromMemOperand(dst);
1139 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001140 MemOperand base = MemOperand(temp_base);
1141 switch (type) {
1142 case Primitive::kPrimBoolean:
1143 case Primitive::kPrimByte:
1144 __ Stlrb(Register(src), base);
1145 break;
1146 case Primitive::kPrimChar:
1147 case Primitive::kPrimShort:
1148 __ Stlrh(Register(src), base);
1149 break;
1150 case Primitive::kPrimInt:
1151 case Primitive::kPrimNot:
1152 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001153 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001154 __ Stlr(Register(src), base);
1155 break;
1156 case Primitive::kPrimFloat:
1157 case Primitive::kPrimDouble: {
1158 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001159 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001160
1161 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1162 __ Fmov(temp, FPRegister(src));
1163 __ Stlr(temp, base);
1164 break;
1165 }
1166 case Primitive::kPrimVoid:
1167 LOG(FATAL) << "Unreachable type " << type;
1168 }
1169}
1170
Calin Juravle175dc732015-08-25 15:42:32 +01001171void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1172 HInstruction* instruction,
1173 uint32_t dex_pc,
1174 SlowPathCode* slow_path) {
1175 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1176 instruction,
1177 dex_pc,
1178 slow_path);
1179}
1180
Alexandre Rames67555f72014-11-18 10:55:16 +00001181void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1182 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001183 uint32_t dex_pc,
1184 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001185 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001186 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001187 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1188 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001189 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001190}
1191
1192void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1193 vixl::Register class_reg) {
1194 UseScratchRegisterScope temps(GetVIXLAssembler());
1195 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001196 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001197 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001198
Serban Constantinescu02164b32014-11-13 14:05:07 +00001199 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001200 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001201 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1202 __ Add(temp, class_reg, status_offset);
1203 __ Ldar(temp, HeapOperand(temp));
1204 __ Cmp(temp, mirror::Class::kStatusInitialized);
1205 __ B(lt, slow_path->GetEntryLabel());
1206 } else {
1207 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1208 __ Cmp(temp, mirror::Class::kStatusInitialized);
1209 __ B(lt, slow_path->GetEntryLabel());
1210 __ Dmb(InnerShareable, BarrierReads);
1211 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001212 __ Bind(slow_path->GetExitLabel());
1213}
Alexandre Rames5319def2014-10-23 10:03:10 +01001214
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001215void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1216 BarrierType type = BarrierAll;
1217
1218 switch (kind) {
1219 case MemBarrierKind::kAnyAny:
1220 case MemBarrierKind::kAnyStore: {
1221 type = BarrierAll;
1222 break;
1223 }
1224 case MemBarrierKind::kLoadAny: {
1225 type = BarrierReads;
1226 break;
1227 }
1228 case MemBarrierKind::kStoreStore: {
1229 type = BarrierWrites;
1230 break;
1231 }
1232 default:
1233 LOG(FATAL) << "Unexpected memory barrier " << kind;
1234 }
1235 __ Dmb(InnerShareable, type);
1236}
1237
Serban Constantinescu02164b32014-11-13 14:05:07 +00001238void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1239 HBasicBlock* successor) {
1240 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001241 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1242 if (slow_path == nullptr) {
1243 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1244 instruction->SetSlowPath(slow_path);
1245 codegen_->AddSlowPath(slow_path);
1246 if (successor != nullptr) {
1247 DCHECK(successor->IsLoopHeader());
1248 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1249 }
1250 } else {
1251 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1252 }
1253
Serban Constantinescu02164b32014-11-13 14:05:07 +00001254 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1255 Register temp = temps.AcquireW();
1256
1257 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1258 if (successor == nullptr) {
1259 __ Cbnz(temp, slow_path->GetEntryLabel());
1260 __ Bind(slow_path->GetReturnLabel());
1261 } else {
1262 __ Cbz(temp, codegen_->GetLabelOf(successor));
1263 __ B(slow_path->GetEntryLabel());
1264 // slow_path will return to GetLabelOf(successor).
1265 }
1266}
1267
Alexandre Rames5319def2014-10-23 10:03:10 +01001268InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1269 CodeGeneratorARM64* codegen)
1270 : HGraphVisitor(graph),
1271 assembler_(codegen->GetAssembler()),
1272 codegen_(codegen) {}
1273
1274#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001275 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001276
1277#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1278
1279enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001280 // Using a base helps identify when we hit such breakpoints.
1281 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001282#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1283 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1284#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1285};
1286
1287#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1288 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001289 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001290 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1291 } \
1292 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1294 locations->SetOut(Location::Any()); \
1295 }
1296 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1297#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1298
1299#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001300#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001301
Alexandre Rames67555f72014-11-18 10:55:16 +00001302void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001303 DCHECK_EQ(instr->InputCount(), 2U);
1304 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1305 Primitive::Type type = instr->GetResultType();
1306 switch (type) {
1307 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001308 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001309 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001310 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001312 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001313
1314 case Primitive::kPrimFloat:
1315 case Primitive::kPrimDouble:
1316 locations->SetInAt(0, Location::RequiresFpuRegister());
1317 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001319 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001320
Alexandre Rames5319def2014-10-23 10:03:10 +01001321 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001322 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001323 }
1324}
1325
Alexandre Rames09a99962015-04-15 11:47:56 +01001326void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1327 LocationSummary* locations =
1328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1329 locations->SetInAt(0, Location::RequiresRegister());
1330 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1331 locations->SetOut(Location::RequiresFpuRegister());
1332 } else {
1333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1334 }
1335}
1336
1337void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1338 const FieldInfo& field_info) {
1339 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001340 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001341 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001342
1343 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1344 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1345
1346 if (field_info.IsVolatile()) {
1347 if (use_acquire_release) {
1348 // NB: LoadAcquire will record the pc info if needed.
1349 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1350 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001351 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001352 codegen_->MaybeRecordImplicitNullCheck(instruction);
1353 // For IRIW sequential consistency kLoadAny is not sufficient.
1354 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1355 }
1356 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001357 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001358 codegen_->MaybeRecordImplicitNullCheck(instruction);
1359 }
Roland Levillain4d027112015-07-01 15:41:14 +01001360
1361 if (field_type == Primitive::kPrimNot) {
1362 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1363 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001364}
1365
1366void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1367 LocationSummary* locations =
1368 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1369 locations->SetInAt(0, Location::RequiresRegister());
1370 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1371 locations->SetInAt(1, Location::RequiresFpuRegister());
1372 } else {
1373 locations->SetInAt(1, Location::RequiresRegister());
1374 }
1375}
1376
1377void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001378 const FieldInfo& field_info,
1379 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001380 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001381 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001382
1383 Register obj = InputRegisterAt(instruction, 0);
1384 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001385 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001386 Offset offset = field_info.GetFieldOffset();
1387 Primitive::Type field_type = field_info.GetFieldType();
1388 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1389
Roland Levillain4d027112015-07-01 15:41:14 +01001390 {
1391 // We use a block to end the scratch scope before the write barrier, thus
1392 // freeing the temporary registers so they can be used in `MarkGCCard`.
1393 UseScratchRegisterScope temps(GetVIXLAssembler());
1394
1395 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1396 DCHECK(value.IsW());
1397 Register temp = temps.AcquireW();
1398 __ Mov(temp, value.W());
1399 GetAssembler()->PoisonHeapReference(temp.W());
1400 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001401 }
Roland Levillain4d027112015-07-01 15:41:14 +01001402
1403 if (field_info.IsVolatile()) {
1404 if (use_acquire_release) {
1405 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1406 codegen_->MaybeRecordImplicitNullCheck(instruction);
1407 } else {
1408 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1409 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1410 codegen_->MaybeRecordImplicitNullCheck(instruction);
1411 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1412 }
1413 } else {
1414 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1415 codegen_->MaybeRecordImplicitNullCheck(instruction);
1416 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001417 }
1418
1419 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001420 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001421 }
1422}
1423
Alexandre Rames67555f72014-11-18 10:55:16 +00001424void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001425 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001426
1427 switch (type) {
1428 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001429 case Primitive::kPrimLong: {
1430 Register dst = OutputRegister(instr);
1431 Register lhs = InputRegisterAt(instr, 0);
1432 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001433 if (instr->IsAdd()) {
1434 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001435 } else if (instr->IsAnd()) {
1436 __ And(dst, lhs, rhs);
1437 } else if (instr->IsOr()) {
1438 __ Orr(dst, lhs, rhs);
1439 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001440 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001441 } else {
1442 DCHECK(instr->IsXor());
1443 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001444 }
1445 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001446 }
1447 case Primitive::kPrimFloat:
1448 case Primitive::kPrimDouble: {
1449 FPRegister dst = OutputFPRegister(instr);
1450 FPRegister lhs = InputFPRegisterAt(instr, 0);
1451 FPRegister rhs = InputFPRegisterAt(instr, 1);
1452 if (instr->IsAdd()) {
1453 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001454 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001455 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001456 } else {
1457 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001458 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001459 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001460 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001461 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001462 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001463 }
1464}
1465
Serban Constantinescu02164b32014-11-13 14:05:07 +00001466void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1467 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1468
1469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1470 Primitive::Type type = instr->GetResultType();
1471 switch (type) {
1472 case Primitive::kPrimInt:
1473 case Primitive::kPrimLong: {
1474 locations->SetInAt(0, Location::RequiresRegister());
1475 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1476 locations->SetOut(Location::RequiresRegister());
1477 break;
1478 }
1479 default:
1480 LOG(FATAL) << "Unexpected shift type " << type;
1481 }
1482}
1483
1484void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1485 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1486
1487 Primitive::Type type = instr->GetType();
1488 switch (type) {
1489 case Primitive::kPrimInt:
1490 case Primitive::kPrimLong: {
1491 Register dst = OutputRegister(instr);
1492 Register lhs = InputRegisterAt(instr, 0);
1493 Operand rhs = InputOperandAt(instr, 1);
1494 if (rhs.IsImmediate()) {
1495 uint32_t shift_value = (type == Primitive::kPrimInt)
1496 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1497 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1498 if (instr->IsShl()) {
1499 __ Lsl(dst, lhs, shift_value);
1500 } else if (instr->IsShr()) {
1501 __ Asr(dst, lhs, shift_value);
1502 } else {
1503 __ Lsr(dst, lhs, shift_value);
1504 }
1505 } else {
1506 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1507
1508 if (instr->IsShl()) {
1509 __ Lsl(dst, lhs, rhs_reg);
1510 } else if (instr->IsShr()) {
1511 __ Asr(dst, lhs, rhs_reg);
1512 } else {
1513 __ Lsr(dst, lhs, rhs_reg);
1514 }
1515 }
1516 break;
1517 }
1518 default:
1519 LOG(FATAL) << "Unexpected shift operation type " << type;
1520 }
1521}
1522
Alexandre Rames5319def2014-10-23 10:03:10 +01001523void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001524 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001525}
1526
1527void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001528 HandleBinaryOp(instruction);
1529}
1530
1531void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1532 HandleBinaryOp(instruction);
1533}
1534
1535void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1536 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001537}
1538
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001539void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1540 LocationSummary* locations =
1541 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001544 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1545 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1546 } else {
1547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1548 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001549}
1550
1551void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1552 LocationSummary* locations = instruction->GetLocations();
1553 Primitive::Type type = instruction->GetType();
1554 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001555 Location index = locations->InAt(1);
1556 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001557 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001558 MacroAssembler* masm = GetVIXLAssembler();
1559 UseScratchRegisterScope temps(masm);
1560 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001561
1562 if (index.IsConstant()) {
1563 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001564 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001565 } else {
1566 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001567 __ Add(temp, obj, offset);
1568 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001569 }
1570
Alexandre Rames67555f72014-11-18 10:55:16 +00001571 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001572 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001573
1574 if (type == Primitive::kPrimNot) {
1575 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1576 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577}
1578
Alexandre Rames5319def2014-10-23 10:03:10 +01001579void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1581 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001583}
1584
1585void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001586 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001587 __ Ldr(OutputRegister(instruction),
1588 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001589 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001590}
1591
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001592void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Alexandre Rames97833a02015-04-16 15:07:12 +01001593 if (instruction->NeedsTypeCheck()) {
1594 LocationSummary* locations =
1595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001596 InvokeRuntimeCallingConvention calling_convention;
1597 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1598 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1599 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1600 } else {
Alexandre Rames97833a02015-04-16 15:07:12 +01001601 LocationSummary* locations =
1602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001603 locations->SetInAt(0, Location::RequiresRegister());
1604 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001605 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1606 locations->SetInAt(2, Location::RequiresFpuRegister());
1607 } else {
1608 locations->SetInAt(2, Location::RequiresRegister());
1609 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001610 }
1611}
1612
1613void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1614 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001615 LocationSummary* locations = instruction->GetLocations();
1616 bool needs_runtime_call = locations->WillCall();
1617
1618 if (needs_runtime_call) {
Roland Levillain4d027112015-07-01 15:41:14 +01001619 // Note: if heap poisoning is enabled, pAputObject takes cares
1620 // of poisoning the reference.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001621 codegen_->InvokeRuntime(
1622 QUICK_ENTRY_POINT(pAputObject), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08001623 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001624 } else {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001625 Register obj = InputRegisterAt(instruction, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001626 CPURegister value = InputCPURegisterAt(instruction, 2);
Roland Levillain4d027112015-07-01 15:41:14 +01001627 CPURegister source = value;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001628 Location index = locations->InAt(1);
1629 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001630 MemOperand destination = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001631 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001632 BlockPoolsScope block_pools(masm);
Alexandre Rames97833a02015-04-16 15:07:12 +01001633 {
1634 // We use a block to end the scratch scope before the write barrier, thus
1635 // freeing the temporary registers so they can be used in `MarkGCCard`.
1636 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001637
Roland Levillain4d027112015-07-01 15:41:14 +01001638 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
1639 DCHECK(value.IsW());
1640 Register temp = temps.AcquireW();
1641 __ Mov(temp, value.W());
1642 GetAssembler()->PoisonHeapReference(temp.W());
1643 source = temp;
1644 }
1645
Alexandre Rames97833a02015-04-16 15:07:12 +01001646 if (index.IsConstant()) {
1647 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1648 destination = HeapOperand(obj, offset);
1649 } else {
1650 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001651 __ Add(temp, obj, offset);
1652 destination = HeapOperand(temp,
1653 XRegisterFrom(index),
1654 LSL,
1655 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001656 }
1657
Roland Levillain4d027112015-07-01 15:41:14 +01001658 codegen_->Store(value_type, source, destination);
Alexandre Rames97833a02015-04-16 15:07:12 +01001659 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001660 }
Alexandre Rames97833a02015-04-16 15:07:12 +01001661 if (CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue())) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001662 codegen_->MarkGCCard(obj, value.W(), instruction->GetValueCanBeNull());
Alexandre Rames97833a02015-04-16 15:07:12 +01001663 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001664 }
1665}
1666
Alexandre Rames67555f72014-11-18 10:55:16 +00001667void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001668 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1669 ? LocationSummary::kCallOnSlowPath
1670 : LocationSummary::kNoCall;
1671 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001672 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001673 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001674 if (instruction->HasUses()) {
1675 locations->SetOut(Location::SameAsFirstInput());
1676 }
1677}
1678
1679void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001680 BoundsCheckSlowPathARM64* slow_path =
1681 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001682 codegen_->AddSlowPath(slow_path);
1683
1684 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1685 __ B(slow_path->GetEntryLabel(), hs);
1686}
1687
Alexandre Rames67555f72014-11-18 10:55:16 +00001688void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1689 LocationSummary* locations =
1690 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1691 locations->SetInAt(0, Location::RequiresRegister());
1692 if (check->HasUses()) {
1693 locations->SetOut(Location::SameAsFirstInput());
1694 }
1695}
1696
1697void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1698 // We assume the class is not null.
1699 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1700 check->GetLoadClass(), check, check->GetDexPc(), true);
1701 codegen_->AddSlowPath(slow_path);
1702 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1703}
1704
Roland Levillain7f63c522015-07-13 15:54:55 +00001705static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1706 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1707 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1708}
1709
Serban Constantinescu02164b32014-11-13 14:05:07 +00001710void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001711 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001712 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1713 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001714 switch (in_type) {
1715 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001716 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001717 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001718 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1719 break;
1720 }
1721 case Primitive::kPrimFloat:
1722 case Primitive::kPrimDouble: {
1723 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001724 locations->SetInAt(1,
1725 IsFloatingPointZeroConstant(compare->InputAt(1))
1726 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1727 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001728 locations->SetOut(Location::RequiresRegister());
1729 break;
1730 }
1731 default:
1732 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1733 }
1734}
1735
1736void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1737 Primitive::Type in_type = compare->InputAt(0)->GetType();
1738
1739 // 0 if: left == right
1740 // 1 if: left > right
1741 // -1 if: left < right
1742 switch (in_type) {
1743 case Primitive::kPrimLong: {
1744 Register result = OutputRegister(compare);
1745 Register left = InputRegisterAt(compare, 0);
1746 Operand right = InputOperandAt(compare, 1);
1747
1748 __ Cmp(left, right);
1749 __ Cset(result, ne);
1750 __ Cneg(result, result, lt);
1751 break;
1752 }
1753 case Primitive::kPrimFloat:
1754 case Primitive::kPrimDouble: {
1755 Register result = OutputRegister(compare);
1756 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001757 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001758 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1759 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001760 __ Fcmp(left, 0.0);
1761 } else {
1762 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1763 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001764 if (compare->IsGtBias()) {
1765 __ Cset(result, ne);
1766 } else {
1767 __ Csetm(result, ne);
1768 }
1769 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001770 break;
1771 }
1772 default:
1773 LOG(FATAL) << "Unimplemented compare type " << in_type;
1774 }
1775}
1776
1777void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001779
1780 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1781 locations->SetInAt(0, Location::RequiresFpuRegister());
1782 locations->SetInAt(1,
1783 IsFloatingPointZeroConstant(instruction->InputAt(1))
1784 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1785 : Location::RequiresFpuRegister());
1786 } else {
1787 // Integer cases.
1788 locations->SetInAt(0, Location::RequiresRegister());
1789 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1790 }
1791
Alexandre Rames5319def2014-10-23 10:03:10 +01001792 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001794 }
1795}
1796
1797void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1798 if (!instruction->NeedsMaterialization()) {
1799 return;
1800 }
1801
1802 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001803 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001804 IfCondition if_cond = instruction->GetCondition();
1805 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001806
Roland Levillain7f63c522015-07-13 15:54:55 +00001807 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1808 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1809 if (locations->InAt(1).IsConstant()) {
1810 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1811 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1812 __ Fcmp(lhs, 0.0);
1813 } else {
1814 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1815 }
1816 __ Cset(res, arm64_cond);
1817 if (instruction->IsFPConditionTrueIfNaN()) {
1818 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1819 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1820 } else if (instruction->IsFPConditionFalseIfNaN()) {
1821 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1822 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1823 }
1824 } else {
1825 // Integer cases.
1826 Register lhs = InputRegisterAt(instruction, 0);
1827 Operand rhs = InputOperandAt(instruction, 1);
1828 __ Cmp(lhs, rhs);
1829 __ Cset(res, arm64_cond);
1830 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001831}
1832
1833#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1834 M(Equal) \
1835 M(NotEqual) \
1836 M(LessThan) \
1837 M(LessThanOrEqual) \
1838 M(GreaterThan) \
1839 M(GreaterThanOrEqual)
1840#define DEFINE_CONDITION_VISITORS(Name) \
1841void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1842void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1843FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001844#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001845#undef FOR_EACH_CONDITION_INSTRUCTION
1846
Zheng Xuc6667102015-05-15 16:08:45 +08001847void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1848 DCHECK(instruction->IsDiv() || instruction->IsRem());
1849
1850 LocationSummary* locations = instruction->GetLocations();
1851 Location second = locations->InAt(1);
1852 DCHECK(second.IsConstant());
1853
1854 Register out = OutputRegister(instruction);
1855 Register dividend = InputRegisterAt(instruction, 0);
1856 int64_t imm = Int64FromConstant(second.GetConstant());
1857 DCHECK(imm == 1 || imm == -1);
1858
1859 if (instruction->IsRem()) {
1860 __ Mov(out, 0);
1861 } else {
1862 if (imm == 1) {
1863 __ Mov(out, dividend);
1864 } else {
1865 __ Neg(out, dividend);
1866 }
1867 }
1868}
1869
1870void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1871 DCHECK(instruction->IsDiv() || instruction->IsRem());
1872
1873 LocationSummary* locations = instruction->GetLocations();
1874 Location second = locations->InAt(1);
1875 DCHECK(second.IsConstant());
1876
1877 Register out = OutputRegister(instruction);
1878 Register dividend = InputRegisterAt(instruction, 0);
1879 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001880 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001881 DCHECK(IsPowerOfTwo(abs_imm));
1882 int ctz_imm = CTZ(abs_imm);
1883
1884 UseScratchRegisterScope temps(GetVIXLAssembler());
1885 Register temp = temps.AcquireSameSizeAs(out);
1886
1887 if (instruction->IsDiv()) {
1888 __ Add(temp, dividend, abs_imm - 1);
1889 __ Cmp(dividend, 0);
1890 __ Csel(out, temp, dividend, lt);
1891 if (imm > 0) {
1892 __ Asr(out, out, ctz_imm);
1893 } else {
1894 __ Neg(out, Operand(out, ASR, ctz_imm));
1895 }
1896 } else {
1897 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1898 __ Asr(temp, dividend, bits - 1);
1899 __ Lsr(temp, temp, bits - ctz_imm);
1900 __ Add(out, dividend, temp);
1901 __ And(out, out, abs_imm - 1);
1902 __ Sub(out, out, temp);
1903 }
1904}
1905
1906void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1907 DCHECK(instruction->IsDiv() || instruction->IsRem());
1908
1909 LocationSummary* locations = instruction->GetLocations();
1910 Location second = locations->InAt(1);
1911 DCHECK(second.IsConstant());
1912
1913 Register out = OutputRegister(instruction);
1914 Register dividend = InputRegisterAt(instruction, 0);
1915 int64_t imm = Int64FromConstant(second.GetConstant());
1916
1917 Primitive::Type type = instruction->GetResultType();
1918 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1919
1920 int64_t magic;
1921 int shift;
1922 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1923
1924 UseScratchRegisterScope temps(GetVIXLAssembler());
1925 Register temp = temps.AcquireSameSizeAs(out);
1926
1927 // temp = get_high(dividend * magic)
1928 __ Mov(temp, magic);
1929 if (type == Primitive::kPrimLong) {
1930 __ Smulh(temp, dividend, temp);
1931 } else {
1932 __ Smull(temp.X(), dividend, temp);
1933 __ Lsr(temp.X(), temp.X(), 32);
1934 }
1935
1936 if (imm > 0 && magic < 0) {
1937 __ Add(temp, temp, dividend);
1938 } else if (imm < 0 && magic > 0) {
1939 __ Sub(temp, temp, dividend);
1940 }
1941
1942 if (shift != 0) {
1943 __ Asr(temp, temp, shift);
1944 }
1945
1946 if (instruction->IsDiv()) {
1947 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1948 } else {
1949 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
1950 // TODO: Strength reduction for msub.
1951 Register temp_imm = temps.AcquireSameSizeAs(out);
1952 __ Mov(temp_imm, imm);
1953 __ Msub(out, temp, temp_imm, dividend);
1954 }
1955}
1956
1957void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1958 DCHECK(instruction->IsDiv() || instruction->IsRem());
1959 Primitive::Type type = instruction->GetResultType();
1960 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1961
1962 LocationSummary* locations = instruction->GetLocations();
1963 Register out = OutputRegister(instruction);
1964 Location second = locations->InAt(1);
1965
1966 if (second.IsConstant()) {
1967 int64_t imm = Int64FromConstant(second.GetConstant());
1968
1969 if (imm == 0) {
1970 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
1971 } else if (imm == 1 || imm == -1) {
1972 DivRemOneOrMinusOne(instruction);
1973 } else if (IsPowerOfTwo(std::abs(imm))) {
1974 DivRemByPowerOfTwo(instruction);
1975 } else {
1976 DCHECK(imm <= -2 || imm >= 2);
1977 GenerateDivRemWithAnyConstant(instruction);
1978 }
1979 } else {
1980 Register dividend = InputRegisterAt(instruction, 0);
1981 Register divisor = InputRegisterAt(instruction, 1);
1982 if (instruction->IsDiv()) {
1983 __ Sdiv(out, dividend, divisor);
1984 } else {
1985 UseScratchRegisterScope temps(GetVIXLAssembler());
1986 Register temp = temps.AcquireSameSizeAs(out);
1987 __ Sdiv(temp, dividend, divisor);
1988 __ Msub(out, temp, divisor, dividend);
1989 }
1990 }
1991}
1992
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001993void LocationsBuilderARM64::VisitDiv(HDiv* div) {
1994 LocationSummary* locations =
1995 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1996 switch (div->GetResultType()) {
1997 case Primitive::kPrimInt:
1998 case Primitive::kPrimLong:
1999 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002000 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2002 break;
2003
2004 case Primitive::kPrimFloat:
2005 case Primitive::kPrimDouble:
2006 locations->SetInAt(0, Location::RequiresFpuRegister());
2007 locations->SetInAt(1, Location::RequiresFpuRegister());
2008 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2009 break;
2010
2011 default:
2012 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2013 }
2014}
2015
2016void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2017 Primitive::Type type = div->GetResultType();
2018 switch (type) {
2019 case Primitive::kPrimInt:
2020 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002021 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002022 break;
2023
2024 case Primitive::kPrimFloat:
2025 case Primitive::kPrimDouble:
2026 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2027 break;
2028
2029 default:
2030 LOG(FATAL) << "Unexpected div type " << type;
2031 }
2032}
2033
Alexandre Rames67555f72014-11-18 10:55:16 +00002034void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002035 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2036 ? LocationSummary::kCallOnSlowPath
2037 : LocationSummary::kNoCall;
2038 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002039 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2040 if (instruction->HasUses()) {
2041 locations->SetOut(Location::SameAsFirstInput());
2042 }
2043}
2044
2045void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2046 SlowPathCodeARM64* slow_path =
2047 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2048 codegen_->AddSlowPath(slow_path);
2049 Location value = instruction->GetLocations()->InAt(0);
2050
Alexandre Rames3e69f162014-12-10 10:36:50 +00002051 Primitive::Type type = instruction->GetType();
2052
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002053 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2054 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002055 return;
2056 }
2057
Alexandre Rames67555f72014-11-18 10:55:16 +00002058 if (value.IsConstant()) {
2059 int64_t divisor = Int64ConstantFrom(value);
2060 if (divisor == 0) {
2061 __ B(slow_path->GetEntryLabel());
2062 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002063 // A division by a non-null constant is valid. We don't need to perform
2064 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002065 }
2066 } else {
2067 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2068 }
2069}
2070
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002071void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2072 LocationSummary* locations =
2073 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2074 locations->SetOut(Location::ConstantLocation(constant));
2075}
2076
2077void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2078 UNUSED(constant);
2079 // Will be generated at use site.
2080}
2081
Alexandre Rames5319def2014-10-23 10:03:10 +01002082void LocationsBuilderARM64::VisitExit(HExit* exit) {
2083 exit->SetLocations(nullptr);
2084}
2085
2086void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002087 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002088}
2089
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002090void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2091 LocationSummary* locations =
2092 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2093 locations->SetOut(Location::ConstantLocation(constant));
2094}
2095
2096void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2097 UNUSED(constant);
2098 // Will be generated at use site.
2099}
2100
David Brazdilfc6a86a2015-06-26 10:33:45 +00002101void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002102 DCHECK(!successor->IsExitBlock());
2103 HBasicBlock* block = got->GetBlock();
2104 HInstruction* previous = got->GetPrevious();
2105 HLoopInformation* info = block->GetLoopInformation();
2106
David Brazdil46e2a392015-03-16 17:31:52 +00002107 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002108 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2109 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2110 return;
2111 }
2112 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2113 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2114 }
2115 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002116 __ B(codegen_->GetLabelOf(successor));
2117 }
2118}
2119
David Brazdilfc6a86a2015-06-26 10:33:45 +00002120void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2121 got->SetLocations(nullptr);
2122}
2123
2124void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2125 HandleGoto(got, got->GetSuccessor());
2126}
2127
2128void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2129 try_boundary->SetLocations(nullptr);
2130}
2131
2132void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2133 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2134 if (!successor->IsExitBlock()) {
2135 HandleGoto(try_boundary, successor);
2136 }
2137}
2138
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002139void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2140 vixl::Label* true_target,
2141 vixl::Label* false_target,
2142 vixl::Label* always_true_target) {
2143 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002144 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002145
Serban Constantinescu02164b32014-11-13 14:05:07 +00002146 if (cond->IsIntConstant()) {
2147 int32_t cond_value = cond->AsIntConstant()->GetValue();
2148 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002149 if (always_true_target != nullptr) {
2150 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002151 }
2152 return;
2153 } else {
2154 DCHECK_EQ(cond_value, 0);
2155 }
2156 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002157 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002158 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002159 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002160 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002161 } else {
2162 // The condition instruction has not been materialized, use its inputs as
2163 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002164 Primitive::Type type =
2165 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2166
2167 if (Primitive::IsFloatingPointType(type)) {
2168 // FP compares don't like null false_targets.
2169 if (false_target == nullptr) {
2170 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002171 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002172 FPRegister lhs = InputFPRegisterAt(condition, 0);
2173 if (condition->GetLocations()->InAt(1).IsConstant()) {
2174 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2175 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2176 __ Fcmp(lhs, 0.0);
2177 } else {
2178 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2179 }
2180 if (condition->IsFPConditionTrueIfNaN()) {
2181 __ B(vs, true_target); // VS for unordered.
2182 } else if (condition->IsFPConditionFalseIfNaN()) {
2183 __ B(vs, false_target); // VS for unordered.
2184 }
2185 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002186 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002187 // Integer cases.
2188 Register lhs = InputRegisterAt(condition, 0);
2189 Operand rhs = InputOperandAt(condition, 1);
2190 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2191 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2192 switch (arm64_cond) {
2193 case eq:
2194 __ Cbz(lhs, true_target);
2195 break;
2196 case ne:
2197 __ Cbnz(lhs, true_target);
2198 break;
2199 case lt:
2200 // Test the sign bit and branch accordingly.
2201 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2202 break;
2203 case ge:
2204 // Test the sign bit and branch accordingly.
2205 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2206 break;
2207 default:
2208 // Without the `static_cast` the compiler throws an error for
2209 // `-Werror=sign-promo`.
2210 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2211 }
2212 } else {
2213 __ Cmp(lhs, rhs);
2214 __ B(arm64_cond, true_target);
2215 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002216 }
2217 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002218 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002219 __ B(false_target);
2220 }
2221}
2222
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002223void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2225 HInstruction* cond = if_instr->InputAt(0);
2226 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2227 locations->SetInAt(0, Location::RequiresRegister());
2228 }
2229}
2230
2231void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2232 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2233 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2234 vixl::Label* always_true_target = true_target;
2235 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2236 if_instr->IfTrueSuccessor())) {
2237 always_true_target = nullptr;
2238 }
2239 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2240 if_instr->IfFalseSuccessor())) {
2241 false_target = nullptr;
2242 }
2243 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2244}
2245
2246void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2247 LocationSummary* locations = new (GetGraph()->GetArena())
2248 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2249 HInstruction* cond = deoptimize->InputAt(0);
2250 DCHECK(cond->IsCondition());
2251 if (cond->AsCondition()->NeedsMaterialization()) {
2252 locations->SetInAt(0, Location::RequiresRegister());
2253 }
2254}
2255
2256void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2257 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2258 DeoptimizationSlowPathARM64(deoptimize);
2259 codegen_->AddSlowPath(slow_path);
2260 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2261 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2262}
2263
Alexandre Rames5319def2014-10-23 10:03:10 +01002264void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002265 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002266}
2267
2268void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002269 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002270}
2271
2272void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002273 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002274}
2275
2276void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002277 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002278}
2279
Alexandre Rames67555f72014-11-18 10:55:16 +00002280void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002281 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2282 switch (instruction->GetTypeCheckKind()) {
2283 case TypeCheckKind::kExactCheck:
2284 case TypeCheckKind::kAbstractClassCheck:
2285 case TypeCheckKind::kClassHierarchyCheck:
2286 case TypeCheckKind::kArrayObjectCheck:
2287 call_kind = LocationSummary::kNoCall;
2288 break;
2289 case TypeCheckKind::kInterfaceCheck:
2290 call_kind = LocationSummary::kCall;
2291 break;
2292 case TypeCheckKind::kArrayCheck:
2293 call_kind = LocationSummary::kCallOnSlowPath;
2294 break;
2295 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002297 if (call_kind != LocationSummary::kCall) {
2298 locations->SetInAt(0, Location::RequiresRegister());
2299 locations->SetInAt(1, Location::RequiresRegister());
2300 // The out register is used as a temporary, so it overlaps with the inputs.
2301 // Note that TypeCheckSlowPathARM64 uses this register too.
2302 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2303 } else {
2304 InvokeRuntimeCallingConvention calling_convention;
2305 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2306 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2307 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2308 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002309}
2310
2311void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2312 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002313 Register obj = InputRegisterAt(instruction, 0);
2314 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002315 Register out = OutputRegister(instruction);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002316 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2317 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2318 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2319 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002320
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002321 vixl::Label done, zero;
2322 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002323
2324 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002325 // Avoid null check if we know `obj` is not null.
2326 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002327 __ Cbz(obj, &zero);
2328 }
2329
2330 // In case of an interface check, we put the object class into the object register.
2331 // This is safe, as the register is caller-save, and the object must be in another
2332 // register if it survives the runtime call.
2333 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2334 ? obj
2335 : out;
2336 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2337 GetAssembler()->MaybeUnpoisonHeapReference(target);
2338
2339 switch (instruction->GetTypeCheckKind()) {
2340 case TypeCheckKind::kExactCheck: {
2341 __ Cmp(out, cls);
2342 __ Cset(out, eq);
2343 if (zero.IsLinked()) {
2344 __ B(&done);
2345 }
2346 break;
2347 }
2348 case TypeCheckKind::kAbstractClassCheck: {
2349 // If the class is abstract, we eagerly fetch the super class of the
2350 // object to avoid doing a comparison we know will fail.
2351 vixl::Label loop, success;
2352 __ Bind(&loop);
2353 __ Ldr(out, HeapOperand(out, super_offset));
2354 GetAssembler()->MaybeUnpoisonHeapReference(out);
2355 // If `out` is null, we use it for the result, and jump to `done`.
2356 __ Cbz(out, &done);
2357 __ Cmp(out, cls);
2358 __ B(ne, &loop);
2359 __ Mov(out, 1);
2360 if (zero.IsLinked()) {
2361 __ B(&done);
2362 }
2363 break;
2364 }
2365 case TypeCheckKind::kClassHierarchyCheck: {
2366 // Walk over the class hierarchy to find a match.
2367 vixl::Label loop, success;
2368 __ Bind(&loop);
2369 __ Cmp(out, cls);
2370 __ B(eq, &success);
2371 __ Ldr(out, HeapOperand(out, super_offset));
2372 GetAssembler()->MaybeUnpoisonHeapReference(out);
2373 __ Cbnz(out, &loop);
2374 // If `out` is null, we use it for the result, and jump to `done`.
2375 __ B(&done);
2376 __ Bind(&success);
2377 __ Mov(out, 1);
2378 if (zero.IsLinked()) {
2379 __ B(&done);
2380 }
2381 break;
2382 }
2383 case TypeCheckKind::kArrayObjectCheck: {
2384 // Just need to check that the object's class is a non primitive array.
2385 __ Ldr(out, HeapOperand(out, component_offset));
2386 GetAssembler()->MaybeUnpoisonHeapReference(out);
2387 // If `out` is null, we use it for the result, and jump to `done`.
2388 __ Cbz(out, &done);
2389 __ Ldrh(out, HeapOperand(out, primitive_offset));
2390 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2391 __ Cbnz(out, &zero);
2392 __ Mov(out, 1);
2393 __ B(&done);
2394 break;
2395 }
2396 case TypeCheckKind::kArrayCheck: {
2397 __ Cmp(out, cls);
2398 DCHECK(locations->OnlyCallsOnSlowPath());
2399 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2400 instruction, /* is_fatal */ false);
2401 codegen_->AddSlowPath(slow_path);
2402 __ B(ne, slow_path->GetEntryLabel());
2403 __ Mov(out, 1);
2404 if (zero.IsLinked()) {
2405 __ B(&done);
2406 }
2407 break;
2408 }
2409
2410 case TypeCheckKind::kInterfaceCheck:
2411 default: {
2412 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2413 instruction,
2414 instruction->GetDexPc(),
2415 nullptr);
2416 if (zero.IsLinked()) {
2417 __ B(&done);
2418 }
2419 break;
2420 }
2421 }
2422
2423 if (zero.IsLinked()) {
2424 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002425 __ Mov(out, 0);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002426 }
2427
2428 if (done.IsLinked()) {
2429 __ Bind(&done);
2430 }
2431
2432 if (slow_path != nullptr) {
2433 __ Bind(slow_path->GetExitLabel());
2434 }
2435}
2436
2437void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2438 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2439 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2440
2441 switch (instruction->GetTypeCheckKind()) {
2442 case TypeCheckKind::kExactCheck:
2443 case TypeCheckKind::kAbstractClassCheck:
2444 case TypeCheckKind::kClassHierarchyCheck:
2445 case TypeCheckKind::kArrayObjectCheck:
2446 call_kind = throws_into_catch
2447 ? LocationSummary::kCallOnSlowPath
2448 : LocationSummary::kNoCall;
2449 break;
2450 case TypeCheckKind::kInterfaceCheck:
2451 call_kind = LocationSummary::kCall;
2452 break;
2453 case TypeCheckKind::kArrayCheck:
2454 call_kind = LocationSummary::kCallOnSlowPath;
2455 break;
2456 }
2457
2458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2459 instruction, call_kind);
2460 if (call_kind != LocationSummary::kCall) {
2461 locations->SetInAt(0, Location::RequiresRegister());
2462 locations->SetInAt(1, Location::RequiresRegister());
2463 // Note that TypeCheckSlowPathARM64 uses this register too.
2464 locations->AddTemp(Location::RequiresRegister());
2465 } else {
2466 InvokeRuntimeCallingConvention calling_convention;
2467 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2468 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2469 }
2470}
2471
2472void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2473 LocationSummary* locations = instruction->GetLocations();
2474 Register obj = InputRegisterAt(instruction, 0);
2475 Register cls = InputRegisterAt(instruction, 1);
2476 Register temp;
2477 if (!locations->WillCall()) {
2478 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2479 }
2480
2481 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2482 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2483 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2484 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2485 SlowPathCodeARM64* slow_path = nullptr;
2486
2487 if (!locations->WillCall()) {
2488 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2489 instruction, !locations->CanCall());
2490 codegen_->AddSlowPath(slow_path);
2491 }
2492
2493 vixl::Label done;
2494 // Avoid null check if we know obj is not null.
2495 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002496 __ Cbz(obj, &done);
2497 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002498
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002499 if (locations->WillCall()) {
2500 __ Ldr(obj, HeapOperand(obj, class_offset));
2501 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002502 } else {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002503 __ Ldr(temp, HeapOperand(obj, class_offset));
2504 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames67555f72014-11-18 10:55:16 +00002505 }
2506
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002507 switch (instruction->GetTypeCheckKind()) {
2508 case TypeCheckKind::kExactCheck:
2509 case TypeCheckKind::kArrayCheck: {
2510 __ Cmp(temp, cls);
2511 // Jump to slow path for throwing the exception or doing a
2512 // more involved array check.
2513 __ B(ne, slow_path->GetEntryLabel());
2514 break;
2515 }
2516 case TypeCheckKind::kAbstractClassCheck: {
2517 // If the class is abstract, we eagerly fetch the super class of the
2518 // object to avoid doing a comparison we know will fail.
2519 vixl::Label loop;
2520 __ Bind(&loop);
2521 __ Ldr(temp, HeapOperand(temp, super_offset));
2522 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2523 // Jump to the slow path to throw the exception.
2524 __ Cbz(temp, slow_path->GetEntryLabel());
2525 __ Cmp(temp, cls);
2526 __ B(ne, &loop);
2527 break;
2528 }
2529 case TypeCheckKind::kClassHierarchyCheck: {
2530 // Walk over the class hierarchy to find a match.
2531 vixl::Label loop, success;
2532 __ Bind(&loop);
2533 __ Cmp(temp, cls);
2534 __ B(eq, &success);
2535 __ Ldr(temp, HeapOperand(temp, super_offset));
2536 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2537 __ Cbnz(temp, &loop);
2538 // Jump to the slow path to throw the exception.
2539 __ B(slow_path->GetEntryLabel());
2540 __ Bind(&success);
2541 break;
2542 }
2543 case TypeCheckKind::kArrayObjectCheck: {
2544 // Just need to check that the object's class is a non primitive array.
2545 __ Ldr(temp, HeapOperand(temp, component_offset));
2546 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2547 __ Cbz(temp, slow_path->GetEntryLabel());
2548 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2549 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2550 __ Cbnz(temp, slow_path->GetEntryLabel());
2551 break;
2552 }
2553 case TypeCheckKind::kInterfaceCheck:
2554 default:
2555 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2556 instruction,
2557 instruction->GetDexPc(),
2558 nullptr);
2559 break;
2560 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002561 __ Bind(&done);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002562
2563 if (slow_path != nullptr) {
2564 __ Bind(slow_path->GetExitLabel());
2565 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002566}
2567
Alexandre Rames5319def2014-10-23 10:03:10 +01002568void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2569 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2570 locations->SetOut(Location::ConstantLocation(constant));
2571}
2572
2573void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2574 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002575 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002576}
2577
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002578void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2580 locations->SetOut(Location::ConstantLocation(constant));
2581}
2582
2583void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2584 // Will be generated at use site.
2585 UNUSED(constant);
2586}
2587
Calin Juravle175dc732015-08-25 15:42:32 +01002588void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2589 // The trampoline uses the same calling convention as dex calling conventions,
2590 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2591 // the method_idx.
2592 HandleInvoke(invoke);
2593}
2594
2595void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2596 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2597}
2598
Alexandre Rames5319def2014-10-23 10:03:10 +01002599void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002600 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002601 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002602}
2603
Alexandre Rames67555f72014-11-18 10:55:16 +00002604void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2605 HandleInvoke(invoke);
2606}
2607
2608void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2609 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002610 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2611 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2612 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002613 Location receiver = invoke->GetLocations()->InAt(0);
2614 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002615 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002616
2617 // The register ip1 is required to be used for the hidden argument in
2618 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002619 MacroAssembler* masm = GetVIXLAssembler();
2620 UseScratchRegisterScope scratch_scope(masm);
2621 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002622 scratch_scope.Exclude(ip1);
2623 __ Mov(ip1, invoke->GetDexMethodIndex());
2624
2625 // temp = object->GetClass();
2626 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002627 __ Ldr(temp.W(), StackOperandFrom(receiver));
2628 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002629 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002630 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002631 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002632 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002633 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002634 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002635 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002636 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002637 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002638 // lr();
2639 __ Blr(lr);
2640 DCHECK(!codegen_->IsLeafMethod());
2641 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2642}
2643
2644void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002645 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2646 if (intrinsic.TryDispatch(invoke)) {
2647 return;
2648 }
2649
Alexandre Rames67555f72014-11-18 10:55:16 +00002650 HandleInvoke(invoke);
2651}
2652
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002653void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002654 // When we do not run baseline, explicit clinit checks triggered by static
2655 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2656 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002657
Andreas Gampe878d58c2015-01-15 23:24:00 -08002658 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2659 if (intrinsic.TryDispatch(invoke)) {
2660 return;
2661 }
2662
Alexandre Rames67555f72014-11-18 10:55:16 +00002663 HandleInvoke(invoke);
2664}
2665
Andreas Gampe878d58c2015-01-15 23:24:00 -08002666static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2667 if (invoke->GetLocations()->Intrinsified()) {
2668 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2669 intrinsic.Dispatch(invoke);
2670 return true;
2671 }
2672 return false;
2673}
2674
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002675void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002676 // For better instruction scheduling we load the direct code pointer before the method pointer.
2677 bool direct_code_loaded = false;
2678 switch (invoke->GetCodePtrLocation()) {
2679 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2680 // LR = code address from literal pool with link-time patch.
2681 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2682 direct_code_loaded = true;
2683 break;
2684 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2685 // LR = invoke->GetDirectCodePtr();
2686 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2687 direct_code_loaded = true;
2688 break;
2689 default:
2690 break;
2691 }
2692
Andreas Gampe878d58c2015-01-15 23:24:00 -08002693 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002694 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2695 switch (invoke->GetMethodLoadKind()) {
2696 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2697 // temp = thread->string_init_entrypoint
2698 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2699 break;
2700 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2701 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2702 break;
2703 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2704 // Load method address from literal pool.
2705 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2706 break;
2707 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2708 // Load method address from literal pool with a link-time patch.
2709 __ Ldr(XRegisterFrom(temp).X(),
2710 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2711 break;
2712 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2713 // Add ADRP with its PC-relative DexCache access patch.
2714 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2715 invoke->GetDexCacheArrayOffset());
2716 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2717 {
2718 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2719 __ adrp(XRegisterFrom(temp).X(), 0);
2720 }
2721 __ Bind(pc_insn_label); // Bind after ADRP.
2722 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2723 // Add LDR with its PC-relative DexCache access patch.
2724 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2725 invoke->GetDexCacheArrayOffset());
2726 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2727 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2728 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2729 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002730 }
Vladimir Marko58155012015-08-19 12:49:41 +00002731 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2732 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2733 Register reg = XRegisterFrom(temp);
2734 Register method_reg;
2735 if (current_method.IsRegister()) {
2736 method_reg = XRegisterFrom(current_method);
2737 } else {
2738 DCHECK(invoke->GetLocations()->Intrinsified());
2739 DCHECK(!current_method.IsValid());
2740 method_reg = reg;
2741 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2742 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002743
Vladimir Marko58155012015-08-19 12:49:41 +00002744 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002745 __ Ldr(reg.X(),
2746 MemOperand(method_reg.X(),
2747 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002748 // temp = temp[index_in_cache];
2749 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2750 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2751 break;
2752 }
2753 }
2754
2755 switch (invoke->GetCodePtrLocation()) {
2756 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2757 __ Bl(&frame_entry_label_);
2758 break;
2759 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2760 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2761 vixl::Label* label = &relative_call_patches_.back().label;
2762 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2763 __ Bind(label); // Bind after BL.
2764 break;
2765 }
2766 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2767 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2768 // LR prepared above for better instruction scheduling.
2769 DCHECK(direct_code_loaded);
2770 // lr()
2771 __ Blr(lr);
2772 break;
2773 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2774 // LR = callee_method->entry_point_from_quick_compiled_code_;
2775 __ Ldr(lr, MemOperand(
2776 XRegisterFrom(callee_method).X(),
2777 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2778 // lr()
2779 __ Blr(lr);
2780 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002781 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002782
Andreas Gampe878d58c2015-01-15 23:24:00 -08002783 DCHECK(!IsLeafMethod());
2784}
2785
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002786void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2787 LocationSummary* locations = invoke->GetLocations();
2788 Location receiver = locations->InAt(0);
2789 Register temp = XRegisterFrom(temp_in);
2790 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2791 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2792 Offset class_offset = mirror::Object::ClassOffset();
2793 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2794
2795 BlockPoolsScope block_pools(GetVIXLAssembler());
2796
2797 DCHECK(receiver.IsRegister());
2798 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2799 MaybeRecordImplicitNullCheck(invoke);
2800 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2801 // temp = temp->GetMethodAt(method_offset);
2802 __ Ldr(temp, MemOperand(temp, method_offset));
2803 // lr = temp->GetEntryPoint();
2804 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2805 // lr();
2806 __ Blr(lr);
2807}
2808
Vladimir Marko58155012015-08-19 12:49:41 +00002809void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2810 DCHECK(linker_patches->empty());
2811 size_t size =
2812 method_patches_.size() +
2813 call_patches_.size() +
2814 relative_call_patches_.size() +
2815 pc_rel_dex_cache_patches_.size();
2816 linker_patches->reserve(size);
2817 for (const auto& entry : method_patches_) {
2818 const MethodReference& target_method = entry.first;
2819 vixl::Literal<uint64_t>* literal = entry.second;
2820 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2821 target_method.dex_file,
2822 target_method.dex_method_index));
2823 }
2824 for (const auto& entry : call_patches_) {
2825 const MethodReference& target_method = entry.first;
2826 vixl::Literal<uint64_t>* literal = entry.second;
2827 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2828 target_method.dex_file,
2829 target_method.dex_method_index));
2830 }
2831 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2832 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2833 info.target_method.dex_file,
2834 info.target_method.dex_method_index));
2835 }
2836 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2837 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2838 &info.target_dex_file,
2839 info.pc_insn_label->location() - 4u,
2840 info.element_offset));
2841 }
2842}
2843
2844vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2845 // Look up the literal for value.
2846 auto lb = uint64_literals_.lower_bound(value);
2847 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2848 return lb->second;
2849 }
2850 // We don't have a literal for this value, insert a new one.
2851 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2852 uint64_literals_.PutBefore(lb, value, literal);
2853 return literal;
2854}
2855
2856vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2857 MethodReference target_method,
2858 MethodToLiteralMap* map) {
2859 // Look up the literal for target_method.
2860 auto lb = map->lower_bound(target_method);
2861 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2862 return lb->second;
2863 }
2864 // We don't have a literal for this method yet, insert a new one.
2865 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2866 map->PutBefore(lb, target_method, literal);
2867 return literal;
2868}
2869
2870vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2871 MethodReference target_method) {
2872 return DeduplicateMethodLiteral(target_method, &method_patches_);
2873}
2874
2875vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2876 MethodReference target_method) {
2877 return DeduplicateMethodLiteral(target_method, &call_patches_);
2878}
2879
2880
Andreas Gampe878d58c2015-01-15 23:24:00 -08002881void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002882 // When we do not run baseline, explicit clinit checks triggered by static
2883 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2884 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002885
Andreas Gampe878d58c2015-01-15 23:24:00 -08002886 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2887 return;
2888 }
2889
Alexandre Ramesd921d642015-04-16 15:07:16 +01002890 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002891 LocationSummary* locations = invoke->GetLocations();
2892 codegen_->GenerateStaticOrDirectCall(
2893 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002894 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002895}
2896
2897void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002898 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2899 return;
2900 }
2901
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002902 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002903 DCHECK(!codegen_->IsLeafMethod());
2904 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2905}
2906
Alexandre Rames67555f72014-11-18 10:55:16 +00002907void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2908 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2909 : LocationSummary::kNoCall;
2910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002911 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002912 locations->SetOut(Location::RequiresRegister());
2913}
2914
2915void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2916 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002917 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002918 if (cls->IsReferrersClass()) {
2919 DCHECK(!cls->CanCallRuntime());
2920 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002921 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002922 } else {
2923 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002924 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
2925 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
2926 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2927 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002928
2929 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2930 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2931 codegen_->AddSlowPath(slow_path);
2932 __ Cbz(out, slow_path->GetEntryLabel());
2933 if (cls->MustGenerateClinitCheck()) {
2934 GenerateClassInitializationCheck(slow_path, out);
2935 } else {
2936 __ Bind(slow_path->GetExitLabel());
2937 }
2938 }
2939}
2940
David Brazdilcb1c0552015-08-04 16:22:25 +01002941static MemOperand GetExceptionTlsAddress() {
2942 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
2943}
2944
Alexandre Rames67555f72014-11-18 10:55:16 +00002945void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
2946 LocationSummary* locations =
2947 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2948 locations->SetOut(Location::RequiresRegister());
2949}
2950
2951void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01002952 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
2953}
2954
2955void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
2956 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2957}
2958
2959void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2960 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00002961}
2962
Alexandre Rames5319def2014-10-23 10:03:10 +01002963void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
2964 load->SetLocations(nullptr);
2965}
2966
2967void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
2968 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002969 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01002970}
2971
Alexandre Rames67555f72014-11-18 10:55:16 +00002972void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
2973 LocationSummary* locations =
2974 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002975 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002976 locations->SetOut(Location::RequiresRegister());
2977}
2978
2979void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
2980 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
2981 codegen_->AddSlowPath(slow_path);
2982
2983 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01002984 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002985 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002986 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
2987 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2988 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00002989 __ Cbz(out, slow_path->GetEntryLabel());
2990 __ Bind(slow_path->GetExitLabel());
2991}
2992
Alexandre Rames5319def2014-10-23 10:03:10 +01002993void LocationsBuilderARM64::VisitLocal(HLocal* local) {
2994 local->SetLocations(nullptr);
2995}
2996
2997void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
2998 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2999}
3000
3001void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3002 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3003 locations->SetOut(Location::ConstantLocation(constant));
3004}
3005
3006void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3007 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003008 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003009}
3010
Alexandre Rames67555f72014-11-18 10:55:16 +00003011void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3012 LocationSummary* locations =
3013 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3014 InvokeRuntimeCallingConvention calling_convention;
3015 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3016}
3017
3018void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3019 codegen_->InvokeRuntime(instruction->IsEnter()
3020 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3021 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003022 instruction->GetDexPc(),
3023 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003024 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003025}
3026
Alexandre Rames42d641b2014-10-27 14:00:51 +00003027void LocationsBuilderARM64::VisitMul(HMul* mul) {
3028 LocationSummary* locations =
3029 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3030 switch (mul->GetResultType()) {
3031 case Primitive::kPrimInt:
3032 case Primitive::kPrimLong:
3033 locations->SetInAt(0, Location::RequiresRegister());
3034 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003036 break;
3037
3038 case Primitive::kPrimFloat:
3039 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003040 locations->SetInAt(0, Location::RequiresFpuRegister());
3041 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003042 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003043 break;
3044
3045 default:
3046 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3047 }
3048}
3049
3050void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3051 switch (mul->GetResultType()) {
3052 case Primitive::kPrimInt:
3053 case Primitive::kPrimLong:
3054 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3055 break;
3056
3057 case Primitive::kPrimFloat:
3058 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003059 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003060 break;
3061
3062 default:
3063 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3064 }
3065}
3066
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003067void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3068 LocationSummary* locations =
3069 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3070 switch (neg->GetResultType()) {
3071 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003072 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003073 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003075 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003076
3077 case Primitive::kPrimFloat:
3078 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003079 locations->SetInAt(0, Location::RequiresFpuRegister());
3080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003081 break;
3082
3083 default:
3084 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3085 }
3086}
3087
3088void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3089 switch (neg->GetResultType()) {
3090 case Primitive::kPrimInt:
3091 case Primitive::kPrimLong:
3092 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3093 break;
3094
3095 case Primitive::kPrimFloat:
3096 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003097 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003098 break;
3099
3100 default:
3101 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3102 }
3103}
3104
3105void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3106 LocationSummary* locations =
3107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3108 InvokeRuntimeCallingConvention calling_convention;
3109 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003110 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003111 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003112 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003113 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003114 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003115}
3116
3117void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3118 LocationSummary* locations = instruction->GetLocations();
3119 InvokeRuntimeCallingConvention calling_convention;
3120 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3121 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003122 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003123 // Note: if heap poisoning is enabled, the entry point takes cares
3124 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003125 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3126 instruction,
3127 instruction->GetDexPc(),
3128 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003129 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003130}
3131
Alexandre Rames5319def2014-10-23 10:03:10 +01003132void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3133 LocationSummary* locations =
3134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3135 InvokeRuntimeCallingConvention calling_convention;
3136 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003137 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003138 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003139 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003140}
3141
3142void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3143 LocationSummary* locations = instruction->GetLocations();
3144 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3145 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003146 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003147 // Note: if heap poisoning is enabled, the entry point takes cares
3148 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003149 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3150 instruction,
3151 instruction->GetDexPc(),
3152 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003153 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003154}
3155
3156void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003158 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003160}
3161
3162void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003163 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003164 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003165 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003166 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003167 break;
3168
3169 default:
3170 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3171 }
3172}
3173
David Brazdil66d126e2015-04-03 16:02:44 +01003174void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3175 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3176 locations->SetInAt(0, Location::RequiresRegister());
3177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3178}
3179
3180void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003181 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3182}
3183
Alexandre Rames5319def2014-10-23 10:03:10 +01003184void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003185 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3186 ? LocationSummary::kCallOnSlowPath
3187 : LocationSummary::kNoCall;
3188 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003189 locations->SetInAt(0, Location::RequiresRegister());
3190 if (instruction->HasUses()) {
3191 locations->SetOut(Location::SameAsFirstInput());
3192 }
3193}
3194
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003195void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003196 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3197 return;
3198 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003199
Alexandre Ramesd921d642015-04-16 15:07:16 +01003200 BlockPoolsScope block_pools(GetVIXLAssembler());
3201 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003202 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3203 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3204}
3205
3206void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003207 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3208 codegen_->AddSlowPath(slow_path);
3209
3210 LocationSummary* locations = instruction->GetLocations();
3211 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003212
3213 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003214}
3215
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003216void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003217 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003218 GenerateImplicitNullCheck(instruction);
3219 } else {
3220 GenerateExplicitNullCheck(instruction);
3221 }
3222}
3223
Alexandre Rames67555f72014-11-18 10:55:16 +00003224void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3225 HandleBinaryOp(instruction);
3226}
3227
3228void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3229 HandleBinaryOp(instruction);
3230}
3231
Alexandre Rames3e69f162014-12-10 10:36:50 +00003232void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3233 LOG(FATAL) << "Unreachable";
3234}
3235
3236void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3237 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3238}
3239
Alexandre Rames5319def2014-10-23 10:03:10 +01003240void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3242 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3243 if (location.IsStackSlot()) {
3244 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3245 } else if (location.IsDoubleStackSlot()) {
3246 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3247 }
3248 locations->SetOut(location);
3249}
3250
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003251void InstructionCodeGeneratorARM64::VisitParameterValue(
3252 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003253 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003254}
3255
3256void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3257 LocationSummary* locations =
3258 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003259 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003260}
3261
3262void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3263 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3264 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003265}
3266
3267void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3268 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3269 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3270 locations->SetInAt(i, Location::Any());
3271 }
3272 locations->SetOut(Location::Any());
3273}
3274
3275void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003276 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003277 LOG(FATAL) << "Unreachable";
3278}
3279
Serban Constantinescu02164b32014-11-13 14:05:07 +00003280void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003281 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003282 LocationSummary::CallKind call_kind =
3283 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3285
3286 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003287 case Primitive::kPrimInt:
3288 case Primitive::kPrimLong:
3289 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003290 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3292 break;
3293
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003294 case Primitive::kPrimFloat:
3295 case Primitive::kPrimDouble: {
3296 InvokeRuntimeCallingConvention calling_convention;
3297 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3298 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3299 locations->SetOut(calling_convention.GetReturnLocation(type));
3300
3301 break;
3302 }
3303
Serban Constantinescu02164b32014-11-13 14:05:07 +00003304 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003305 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003306 }
3307}
3308
3309void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3310 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003311
Serban Constantinescu02164b32014-11-13 14:05:07 +00003312 switch (type) {
3313 case Primitive::kPrimInt:
3314 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003315 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003316 break;
3317 }
3318
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003319 case Primitive::kPrimFloat:
3320 case Primitive::kPrimDouble: {
3321 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3322 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003323 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003324 break;
3325 }
3326
Serban Constantinescu02164b32014-11-13 14:05:07 +00003327 default:
3328 LOG(FATAL) << "Unexpected rem type " << type;
3329 }
3330}
3331
Calin Juravle27df7582015-04-17 19:12:31 +01003332void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3333 memory_barrier->SetLocations(nullptr);
3334}
3335
3336void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3337 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3338}
3339
Alexandre Rames5319def2014-10-23 10:03:10 +01003340void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3341 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3342 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003343 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003344}
3345
3346void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003347 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003348 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003349}
3350
3351void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3352 instruction->SetLocations(nullptr);
3353}
3354
3355void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003356 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003357 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003358}
3359
Serban Constantinescu02164b32014-11-13 14:05:07 +00003360void LocationsBuilderARM64::VisitShl(HShl* shl) {
3361 HandleShift(shl);
3362}
3363
3364void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3365 HandleShift(shl);
3366}
3367
3368void LocationsBuilderARM64::VisitShr(HShr* shr) {
3369 HandleShift(shr);
3370}
3371
3372void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3373 HandleShift(shr);
3374}
3375
Alexandre Rames5319def2014-10-23 10:03:10 +01003376void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3378 Primitive::Type field_type = store->InputAt(1)->GetType();
3379 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003380 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003381 case Primitive::kPrimBoolean:
3382 case Primitive::kPrimByte:
3383 case Primitive::kPrimChar:
3384 case Primitive::kPrimShort:
3385 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003386 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003387 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3388 break;
3389
3390 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003391 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003392 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3393 break;
3394
3395 default:
3396 LOG(FATAL) << "Unimplemented local type " << field_type;
3397 }
3398}
3399
3400void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003401 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003402}
3403
3404void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003405 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003406}
3407
3408void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003409 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003410}
3411
Alexandre Rames67555f72014-11-18 10:55:16 +00003412void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003413 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003414}
3415
3416void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003417 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003418}
3419
3420void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003421 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003422}
3423
Alexandre Rames67555f72014-11-18 10:55:16 +00003424void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003425 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003426}
3427
Calin Juravle23a8e352015-09-08 19:56:31 +01003428void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3429 HUnresolvedInstanceFieldGet* instruction) {
3430 FieldAccessCallingConvetionARM64 calling_convention;
3431 codegen_->CreateUnresolvedFieldLocationSummary(
3432 instruction, instruction->GetFieldType(), calling_convention);
3433}
3434
3435void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3436 HUnresolvedInstanceFieldGet* instruction) {
3437 codegen_->GenerateUnresolvedFieldAccess(instruction,
3438 instruction->GetFieldType(),
3439 instruction->GetFieldIndex(),
3440 instruction->GetDexPc());
3441}
3442
3443void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3444 HUnresolvedInstanceFieldSet* instruction) {
3445 FieldAccessCallingConvetionARM64 calling_convention;
3446 codegen_->CreateUnresolvedFieldLocationSummary(
3447 instruction, instruction->GetFieldType(), calling_convention);
3448}
3449
3450void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3451 HUnresolvedInstanceFieldSet* instruction) {
3452 codegen_->GenerateUnresolvedFieldAccess(instruction,
3453 instruction->GetFieldType(),
3454 instruction->GetFieldIndex(),
3455 instruction->GetDexPc());
3456}
3457
3458void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3459 HUnresolvedStaticFieldGet* instruction) {
3460 FieldAccessCallingConvetionARM64 calling_convention;
3461 codegen_->CreateUnresolvedFieldLocationSummary(
3462 instruction, instruction->GetFieldType(), calling_convention);
3463}
3464
3465void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3466 HUnresolvedStaticFieldGet* instruction) {
3467 codegen_->GenerateUnresolvedFieldAccess(instruction,
3468 instruction->GetFieldType(),
3469 instruction->GetFieldIndex(),
3470 instruction->GetDexPc());
3471}
3472
3473void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3474 HUnresolvedStaticFieldSet* instruction) {
3475 FieldAccessCallingConvetionARM64 calling_convention;
3476 codegen_->CreateUnresolvedFieldLocationSummary(
3477 instruction, instruction->GetFieldType(), calling_convention);
3478}
3479
3480void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3481 HUnresolvedStaticFieldSet* instruction) {
3482 codegen_->GenerateUnresolvedFieldAccess(instruction,
3483 instruction->GetFieldType(),
3484 instruction->GetFieldIndex(),
3485 instruction->GetDexPc());
3486}
3487
Alexandre Rames5319def2014-10-23 10:03:10 +01003488void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3490}
3491
3492void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003493 HBasicBlock* block = instruction->GetBlock();
3494 if (block->GetLoopInformation() != nullptr) {
3495 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3496 // The back edge will generate the suspend check.
3497 return;
3498 }
3499 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3500 // The goto will generate the suspend check.
3501 return;
3502 }
3503 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003504}
3505
3506void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3507 temp->SetLocations(nullptr);
3508}
3509
3510void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3511 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003512 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003513}
3514
Alexandre Rames67555f72014-11-18 10:55:16 +00003515void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3516 LocationSummary* locations =
3517 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3518 InvokeRuntimeCallingConvention calling_convention;
3519 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3520}
3521
3522void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3523 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003524 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003525 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003526}
3527
3528void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3529 LocationSummary* locations =
3530 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3531 Primitive::Type input_type = conversion->GetInputType();
3532 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003533 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003534 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3535 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3536 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3537 }
3538
Alexandre Rames542361f2015-01-29 16:57:31 +00003539 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003540 locations->SetInAt(0, Location::RequiresFpuRegister());
3541 } else {
3542 locations->SetInAt(0, Location::RequiresRegister());
3543 }
3544
Alexandre Rames542361f2015-01-29 16:57:31 +00003545 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003546 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3547 } else {
3548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3549 }
3550}
3551
3552void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3553 Primitive::Type result_type = conversion->GetResultType();
3554 Primitive::Type input_type = conversion->GetInputType();
3555
3556 DCHECK_NE(input_type, result_type);
3557
Alexandre Rames542361f2015-01-29 16:57:31 +00003558 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003559 int result_size = Primitive::ComponentSize(result_type);
3560 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003561 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003562 Register output = OutputRegister(conversion);
3563 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003564 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3565 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003566 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3567 // 'int' values are used directly as W registers, discarding the top
3568 // bits, so we don't need to sign-extend and can just perform a move.
3569 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3570 // top 32 bits of the target register. We theoretically could leave those
3571 // bits unchanged, but we would have to make sure that no code uses a
3572 // 32bit input value as a 64bit value assuming that the top 32 bits are
3573 // zero.
3574 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003575 } else if ((result_type == Primitive::kPrimChar) ||
3576 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3577 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003578 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003579 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003580 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003581 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003582 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003583 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003584 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3585 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003586 } else if (Primitive::IsFloatingPointType(result_type) &&
3587 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003588 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3589 } else {
3590 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3591 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003592 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003593}
Alexandre Rames67555f72014-11-18 10:55:16 +00003594
Serban Constantinescu02164b32014-11-13 14:05:07 +00003595void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3596 HandleShift(ushr);
3597}
3598
3599void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3600 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003601}
3602
3603void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3604 HandleBinaryOp(instruction);
3605}
3606
3607void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3608 HandleBinaryOp(instruction);
3609}
3610
Calin Juravleb1498f62015-02-16 13:13:29 +00003611void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3612 // Nothing to do, this should be removed during prepare for register allocator.
3613 UNUSED(instruction);
3614 LOG(FATAL) << "Unreachable";
3615}
3616
3617void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3618 // Nothing to do, this should be removed during prepare for register allocator.
3619 UNUSED(instruction);
3620 LOG(FATAL) << "Unreachable";
3621}
3622
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003623void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3624 DCHECK(codegen_->IsBaseline());
3625 LocationSummary* locations =
3626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3627 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3628}
3629
3630void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3631 DCHECK(codegen_->IsBaseline());
3632 // Will be generated at use site.
3633}
3634
Alexandre Rames67555f72014-11-18 10:55:16 +00003635#undef __
3636#undef QUICK_ENTRY_POINT
3637
Alexandre Rames5319def2014-10-23 10:03:10 +01003638} // namespace arm64
3639} // namespace art