blob: c94da86d2c327084d67ecb54afd785f9224518aa [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
71
Alexandre Rames5319def2014-10-23 10:03:10 +010072inline Condition ARM64Condition(IfCondition cond) {
73 switch (cond) {
74 case kCondEQ: return eq;
75 case kCondNE: return ne;
76 case kCondLT: return lt;
77 case kCondLE: return le;
78 case kCondGT: return gt;
79 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010080 }
Roland Levillain7f63c522015-07-13 15:54:55 +000081 LOG(FATAL) << "Unreachable";
82 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010083}
84
Alexandre Ramesa89086e2014-11-07 17:13:25 +000085Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
87 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
88 // but we use the exact registers for clarity.
89 if (return_type == Primitive::kPrimFloat) {
90 return LocationFrom(s0);
91 } else if (return_type == Primitive::kPrimDouble) {
92 return LocationFrom(d0);
93 } else if (return_type == Primitive::kPrimLong) {
94 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010095 } else if (return_type == Primitive::kPrimVoid) {
96 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 } else {
98 return LocationFrom(w0);
99 }
100}
101
Alexandre Rames5319def2014-10-23 10:03:10 +0100102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000103 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100104}
105
Alexandre Rames67555f72014-11-18 10:55:16 +0000106#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100108
Zheng Xuda403092015-04-24 17:35:39 +0800109// Calculate memory accessing operand for save/restore live registers.
110static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
111 RegisterSet* register_set,
112 int64_t spill_offset,
113 bool is_save) {
114 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
115 codegen->GetNumberOfCoreRegisters(),
116 register_set->GetFloatingPointRegisters(),
117 codegen->GetNumberOfFloatingPointRegisters()));
118
119 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
120 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000121 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
122 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800123
124 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
125 UseScratchRegisterScope temps(masm);
126
127 Register base = masm->StackPointer();
128 int64_t core_spill_size = core_list.TotalSizeInBytes();
129 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
130 int64_t reg_size = kXRegSizeInBytes;
131 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
132 uint32_t ls_access_size = WhichPowerOf2(reg_size);
133 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
134 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
135 // If the offset does not fit in the instruction's immediate field, use an alternate register
136 // to compute the base address(float point registers spill base address).
137 Register new_base = temps.AcquireSameSizeAs(base);
138 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
139 base = new_base;
140 spill_offset = -core_spill_size;
141 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
142 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
143 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
144 }
145
146 if (is_save) {
147 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
148 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
149 } else {
150 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
151 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
152 }
153}
154
155void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
156 RegisterSet* register_set = locations->GetLiveRegisters();
157 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
158 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
159 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
160 // If the register holds an object, update the stack mask.
161 if (locations->RegisterContainsObject(i)) {
162 locations->SetStackBit(stack_offset / kVRegSize);
163 }
164 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
165 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
166 saved_core_stack_offsets_[i] = stack_offset;
167 stack_offset += kXRegSizeInBytes;
168 }
169 }
170
171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
172 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
173 register_set->ContainsFloatingPointRegister(i)) {
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_fpu_stack_offsets_[i] = stack_offset;
177 stack_offset += kDRegSizeInBytes;
178 }
179 }
180
181 SaveRestoreLiveRegistersHelper(codegen, register_set,
182 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
183}
184
185void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 SaveRestoreLiveRegistersHelper(codegen, register_set,
188 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
189}
190
Alexandre Rames5319def2014-10-23 10:03:10 +0100191class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
192 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100193 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100194
Alexandre Rames67555f72014-11-18 10:55:16 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000197 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100198
Alexandre Rames5319def2014-10-23 10:03:10 +0100199 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000204 // We're moving two locations to locations that could overlap, so we need a parallel
205 // move resolver.
206 InvokeRuntimeCallingConvention calling_convention;
207 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100208 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
209 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000210 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800212 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100213 }
214
Alexandre Rames8158f282015-08-07 10:26:17 +0100215 bool IsFatal() const OVERRIDE { return true; }
216
Alexandre Rames9931f312015-06-19 14:47:01 +0100217 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000220 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
223};
224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
226 public:
227 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
231 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000232 if (instruction_->CanThrowIntoCatchBlock()) {
233 // Live registers will be restored in the catch block if caught.
234 SaveLiveRegisters(codegen, instruction_->GetLocations());
235 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000236 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000237 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800238 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000239 }
240
Alexandre Rames8158f282015-08-07 10:26:17 +0100241 bool IsFatal() const OVERRIDE { return true; }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
244
Alexandre Rames67555f72014-11-18 10:55:16 +0000245 private:
246 HDivZeroCheck* const instruction_;
247 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
248};
249
250class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
251 public:
252 LoadClassSlowPathARM64(HLoadClass* cls,
253 HInstruction* at,
254 uint32_t dex_pc,
255 bool do_clinit)
256 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
257 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
258 }
259
260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
261 LocationSummary* locations = at_->GetLocations();
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
268 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
270 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800272 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 // Move the class to the desired location.
279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000283 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 }
285
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000286 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 __ B(GetExitLabel());
288 }
289
Alexandre Rames9931f312015-06-19 14:47:01 +0100290 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 private:
293 // The class this slow path will load.
294 HLoadClass* const cls_;
295
296 // The instruction where this slow path is happening.
297 // (Might be the load class or an initialization check).
298 HInstruction* const at_;
299
300 // The dex PC of `at_`.
301 const uint32_t dex_pc_;
302
303 // Whether to initialize the class.
304 const bool do_clinit_;
305
306 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
307};
308
309class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
310 public:
311 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
312
313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
314 LocationSummary* locations = instruction_->GetLocations();
315 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
316 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
317
318 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000319 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320
321 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000323 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000324 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100325 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000327 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000329 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 __ B(GetExitLabel());
331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
334
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 private:
336 HLoadString* const instruction_;
337
338 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
339};
340
Alexandre Rames5319def2014-10-23 10:03:10 +0100341class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
342 public:
343 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
344
Alexandre Rames67555f72014-11-18 10:55:16 +0000345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100347 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000348 if (instruction_->CanThrowIntoCatchBlock()) {
349 // Live registers will be restored in the catch block if caught.
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
351 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800354 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 }
356
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 bool IsFatal() const OVERRIDE { return true; }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
360
Alexandre Rames5319def2014-10-23 10:03:10 +0100361 private:
362 HNullCheck* const instruction_;
363
364 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
365};
366
367class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100369 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100370 : instruction_(instruction), successor_(successor) {}
371
Alexandre Rames67555f72014-11-18 10:55:16 +0000372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000377 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800378 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000379 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 if (successor_ == nullptr) {
381 __ B(GetReturnLabel());
382 } else {
383 __ B(arm64_codegen->GetLabelOf(successor_));
384 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 }
386
387 vixl::Label* GetReturnLabel() {
388 DCHECK(successor_ == nullptr);
389 return &return_label_;
390 }
391
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100392 HBasicBlock* GetSuccessor() const {
393 return successor_;
394 }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
397
Alexandre Rames5319def2014-10-23 10:03:10 +0100398 private:
399 HSuspendCheck* const instruction_;
400 // If not null, the block to branch to after the suspend check.
401 HBasicBlock* const successor_;
402
403 // If `successor_` is null, the label to branch to after the suspend check.
404 vixl::Label return_label_;
405
406 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
407};
408
Alexandre Rames67555f72014-11-18 10:55:16 +0000409class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000411 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
412 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000415 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100416 Location class_to_check = locations->InAt(1);
417 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
418 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000419 DCHECK(instruction_->IsCheckCast()
420 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
421 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100422 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423
Alexandre Rames67555f72014-11-18 10:55:16 +0000424 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000425
426 if (instruction_->IsCheckCast()) {
427 // The codegen for the instruction overwrites `temp`, so put it back in place.
428 Register obj = InputRegisterAt(instruction_, 0);
429 Register temp = WRegisterFrom(locations->GetTemp(0));
430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
431 __ Ldr(temp, HeapOperand(obj, class_offset));
432 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
433 }
434
435 if (!is_fatal_) {
436 SaveLiveRegisters(codegen, locations);
437 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438
439 // We're moving two locations to locations that could overlap, so we need a parallel
440 // move resolver.
441 InvokeRuntimeCallingConvention calling_convention;
442 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100443 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
444 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000445
446 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000447 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100448 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449 Primitive::Type ret_type = instruction_->GetType();
450 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
451 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800452 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
453 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000454 } else {
455 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800457 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458 }
459
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000460 if (!is_fatal_) {
461 RestoreLiveRegisters(codegen, locations);
462 __ B(GetExitLabel());
463 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 }
465
Alexandre Rames9931f312015-06-19 14:47:01 +0100466 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000467 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100468
Alexandre Rames67555f72014-11-18 10:55:16 +0000469 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000470 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
474};
475
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700476class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
477 public:
478 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100479 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 __ Bind(GetEntryLabel());
483 SaveLiveRegisters(codegen, instruction_->GetLocations());
484 DCHECK(instruction_->IsDeoptimize());
485 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
486 uint32_t dex_pc = deoptimize->GetDexPc();
487 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
488 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
489 }
490
Alexandre Rames9931f312015-06-19 14:47:01 +0100491 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
492
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493 private:
494 HInstruction* const instruction_;
495 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
496};
497
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100498class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
499 public:
500 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
501
502 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
503 LocationSummary* locations = instruction_->GetLocations();
504 __ Bind(GetEntryLabel());
505 SaveLiveRegisters(codegen, locations);
506
507 InvokeRuntimeCallingConvention calling_convention;
508 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
509 parallel_move.AddMove(
510 locations->InAt(0),
511 LocationFrom(calling_convention.GetRegisterAt(0)),
512 Primitive::kPrimNot,
513 nullptr);
514 parallel_move.AddMove(
515 locations->InAt(1),
516 LocationFrom(calling_convention.GetRegisterAt(1)),
517 Primitive::kPrimInt,
518 nullptr);
519 parallel_move.AddMove(
520 locations->InAt(2),
521 LocationFrom(calling_convention.GetRegisterAt(2)),
522 Primitive::kPrimNot,
523 nullptr);
524 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
525
526 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
527 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
528 instruction_,
529 instruction_->GetDexPc(),
530 this);
531 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
532 RestoreLiveRegisters(codegen, locations);
533 __ B(GetExitLabel());
534 }
535
536 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
537
538 private:
539 HInstruction* const instruction_;
540
541 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
542};
543
Alexandre Rames5319def2014-10-23 10:03:10 +0100544#undef __
545
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100546Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100547 Location next_location;
548 if (type == Primitive::kPrimVoid) {
549 LOG(FATAL) << "Unreachable type " << type;
550 }
551
Alexandre Rames542361f2015-01-29 16:57:31 +0000552 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100553 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
554 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000555 } else if (!Primitive::IsFloatingPointType(type) &&
556 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000557 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
558 } else {
559 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000560 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
561 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100562 }
563
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000564 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000565 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100566 return next_location;
567}
568
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100569Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100570 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100571}
572
Serban Constantinescu579885a2015-02-22 20:51:33 +0000573CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
574 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100575 const CompilerOptions& compiler_options,
576 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100577 : CodeGenerator(graph,
578 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000579 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000580 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000581 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000582 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100583 compiler_options,
584 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100585 block_labels_(nullptr),
586 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000587 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000588 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000589 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100590 uint64_literals_(std::less<uint64_t>(),
591 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
592 method_patches_(MethodReferenceComparator(),
593 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
594 call_patches_(MethodReferenceComparator(),
595 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
596 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
597 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000598 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000599 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000600}
Alexandre Rames5319def2014-10-23 10:03:10 +0100601
Alexandre Rames67555f72014-11-18 10:55:16 +0000602#undef __
603#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100604
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000605void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
606 // Ensure we emit the literal pool.
607 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000608
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000609 CodeGenerator::Finalize(allocator);
610}
611
Zheng Xuad4450e2015-04-17 18:48:56 +0800612void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
613 // Note: There are 6 kinds of moves:
614 // 1. constant -> GPR/FPR (non-cycle)
615 // 2. constant -> stack (non-cycle)
616 // 3. GPR/FPR -> GPR/FPR
617 // 4. GPR/FPR -> stack
618 // 5. stack -> GPR/FPR
619 // 6. stack -> stack (non-cycle)
620 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
621 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
622 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
623 // dependency.
624 vixl_temps_.Open(GetVIXLAssembler());
625}
626
627void ParallelMoveResolverARM64::FinishEmitNativeCode() {
628 vixl_temps_.Close();
629}
630
631Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
632 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
633 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
634 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
635 Location scratch = GetScratchLocation(kind);
636 if (!scratch.Equals(Location::NoLocation())) {
637 return scratch;
638 }
639 // Allocate from VIXL temp registers.
640 if (kind == Location::kRegister) {
641 scratch = LocationFrom(vixl_temps_.AcquireX());
642 } else {
643 DCHECK(kind == Location::kFpuRegister);
644 scratch = LocationFrom(vixl_temps_.AcquireD());
645 }
646 AddScratchLocation(scratch);
647 return scratch;
648}
649
650void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
651 if (loc.IsRegister()) {
652 vixl_temps_.Release(XRegisterFrom(loc));
653 } else {
654 DCHECK(loc.IsFpuRegister());
655 vixl_temps_.Release(DRegisterFrom(loc));
656 }
657 RemoveScratchLocation(loc);
658}
659
Alexandre Rames3e69f162014-12-10 10:36:50 +0000660void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100661 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100662 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000663}
664
Alexandre Rames5319def2014-10-23 10:03:10 +0100665void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100666 MacroAssembler* masm = GetVIXLAssembler();
667 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000668 __ Bind(&frame_entry_label_);
669
Serban Constantinescu02164b32014-11-13 14:05:07 +0000670 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
671 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100672 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000673 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000674 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000675 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000676 __ Ldr(wzr, MemOperand(temp, 0));
677 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000678 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100679
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000680 if (!HasEmptyFrame()) {
681 int frame_size = GetFrameSize();
682 // Stack layout:
683 // sp[frame_size - 8] : lr.
684 // ... : other preserved core registers.
685 // ... : other preserved fp registers.
686 // ... : reserved frame space.
687 // sp[0] : current method.
688 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100689 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800690 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
691 frame_size - GetCoreSpillSize());
692 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
693 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000694 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100695}
696
697void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100698 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100699 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000700 if (!HasEmptyFrame()) {
701 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800702 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
703 frame_size - FrameEntrySpillSize());
704 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
705 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000706 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100707 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000708 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100709 __ Ret();
710 GetAssembler()->cfi().RestoreState();
711 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100712}
713
Zheng Xuda403092015-04-24 17:35:39 +0800714vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
715 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
716 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
717 core_spill_mask_);
718}
719
720vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
721 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
722 GetNumberOfFloatingPointRegisters()));
723 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
724 fpu_spill_mask_);
725}
726
Alexandre Rames5319def2014-10-23 10:03:10 +0100727void CodeGeneratorARM64::Bind(HBasicBlock* block) {
728 __ Bind(GetLabelOf(block));
729}
730
Alexandre Rames5319def2014-10-23 10:03:10 +0100731void CodeGeneratorARM64::Move(HInstruction* instruction,
732 Location location,
733 HInstruction* move_for) {
734 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100735 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000736 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100737
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100738 if (instruction->IsFakeString()) {
739 // The fake string is an alias for null.
740 DCHECK(IsBaseline());
741 instruction = locations->Out().GetConstant();
742 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
743 }
744
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100745 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100746 MoveLocation(location,
747 Location::DoubleStackSlot(kCurrentMethodStackOffset),
748 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100749 } else if (locations != nullptr && locations->Out().Equals(location)) {
750 return;
751 } else if (instruction->IsIntConstant()
752 || instruction->IsLongConstant()
753 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000754 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100755 if (location.IsRegister()) {
756 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000757 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100758 (instruction->IsLongConstant() && dst.Is64Bits()));
759 __ Mov(dst, value);
760 } else {
761 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000762 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000763 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
764 ? temps.AcquireW()
765 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100766 __ Mov(temp, value);
767 __ Str(temp, StackOperandFrom(location));
768 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000769 } else if (instruction->IsTemporary()) {
770 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000771 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 } else if (instruction->IsLoadLocal()) {
773 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000774 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000775 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000776 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000777 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100778 }
779
780 } else {
781 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000782 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100783 }
784}
785
Calin Juravle175dc732015-08-25 15:42:32 +0100786void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
787 DCHECK(location.IsRegister());
788 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
789}
790
Calin Juravlee460d1d2015-09-29 04:52:17 +0100791void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
792 if (location.IsRegister()) {
793 locations->AddTemp(location);
794 } else {
795 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
796 }
797}
798
Alexandre Rames5319def2014-10-23 10:03:10 +0100799Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
800 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000801
Alexandre Rames5319def2014-10-23 10:03:10 +0100802 switch (type) {
803 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000804 case Primitive::kPrimInt:
805 case Primitive::kPrimFloat:
806 return Location::StackSlot(GetStackSlot(load->GetLocal()));
807
808 case Primitive::kPrimLong:
809 case Primitive::kPrimDouble:
810 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
811
Alexandre Rames5319def2014-10-23 10:03:10 +0100812 case Primitive::kPrimBoolean:
813 case Primitive::kPrimByte:
814 case Primitive::kPrimChar:
815 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100816 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100817 LOG(FATAL) << "Unexpected type " << type;
818 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000819
Alexandre Rames5319def2014-10-23 10:03:10 +0100820 LOG(FATAL) << "Unreachable";
821 return Location::NoLocation();
822}
823
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100824void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000825 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100826 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000827 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100828 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100829 if (value_can_be_null) {
830 __ Cbz(value, &done);
831 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
833 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000834 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100835 if (value_can_be_null) {
836 __ Bind(&done);
837 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100838}
839
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000840void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
841 // Blocked core registers:
842 // lr : Runtime reserved.
843 // tr : Runtime reserved.
844 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
845 // ip1 : VIXL core temp.
846 // ip0 : VIXL core temp.
847 //
848 // Blocked fp registers:
849 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100850 CPURegList reserved_core_registers = vixl_reserved_core_registers;
851 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 while (!reserved_core_registers.IsEmpty()) {
853 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
854 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000855
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000856 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800857 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000858 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
859 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000860
861 if (is_baseline) {
862 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
863 while (!reserved_core_baseline_registers.IsEmpty()) {
864 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
865 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100866 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000867
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100868 if (is_baseline || GetGraph()->IsDebuggable()) {
869 // Stubs do not save callee-save floating point registers. If the graph
870 // is debuggable, we need to deal with these registers differently. For
871 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000872 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
873 while (!reserved_fp_baseline_registers.IsEmpty()) {
874 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
875 }
876 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100877}
878
879Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
880 if (type == Primitive::kPrimVoid) {
881 LOG(FATAL) << "Unreachable type " << type;
882 }
883
Alexandre Rames542361f2015-01-29 16:57:31 +0000884 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000885 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
886 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100887 return Location::FpuRegisterLocation(reg);
888 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000889 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
890 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100891 return Location::RegisterLocation(reg);
892 }
893}
894
Alexandre Rames3e69f162014-12-10 10:36:50 +0000895size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
896 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
897 __ Str(reg, MemOperand(sp, stack_index));
898 return kArm64WordSize;
899}
900
901size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
902 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
903 __ Ldr(reg, MemOperand(sp, stack_index));
904 return kArm64WordSize;
905}
906
907size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
908 FPRegister reg = FPRegister(reg_id, kDRegSize);
909 __ Str(reg, MemOperand(sp, stack_index));
910 return kArm64WordSize;
911}
912
913size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
914 FPRegister reg = FPRegister(reg_id, kDRegSize);
915 __ Ldr(reg, MemOperand(sp, stack_index));
916 return kArm64WordSize;
917}
918
Alexandre Rames5319def2014-10-23 10:03:10 +0100919void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100920 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100921}
922
923void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100924 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100925}
926
Alexandre Rames67555f72014-11-18 10:55:16 +0000927void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000928 if (constant->IsIntConstant()) {
929 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
930 } else if (constant->IsLongConstant()) {
931 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
932 } else if (constant->IsNullConstant()) {
933 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000934 } else if (constant->IsFloatConstant()) {
935 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
936 } else {
937 DCHECK(constant->IsDoubleConstant());
938 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
939 }
940}
941
Alexandre Rames3e69f162014-12-10 10:36:50 +0000942
943static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
944 DCHECK(constant.IsConstant());
945 HConstant* cst = constant.GetConstant();
946 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000947 // Null is mapped to a core W register, which we associate with kPrimInt.
948 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000949 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
950 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
951 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
952}
953
Calin Juravlee460d1d2015-09-29 04:52:17 +0100954void CodeGeneratorARM64::MoveLocation(Location destination,
955 Location source,
956 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000957 if (source.Equals(destination)) {
958 return;
959 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000960
961 // A valid move can always be inferred from the destination and source
962 // locations. When moving from and to a register, the argument type can be
963 // used to generate 32bit instead of 64bit moves. In debug mode we also
964 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100965 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000966
967 if (destination.IsRegister() || destination.IsFpuRegister()) {
968 if (unspecified_type) {
969 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
970 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000971 (src_cst != nullptr && (src_cst->IsIntConstant()
972 || src_cst->IsFloatConstant()
973 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000974 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100975 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000976 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000977 // If the source is a double stack slot or a 64bit constant, a 64bit
978 // type is appropriate. Else the source is a register, and since the
979 // type has not been specified, we chose a 64bit type to force a 64bit
980 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100981 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000982 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000983 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100984 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
985 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
986 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000987 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
988 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
989 __ Ldr(dst, StackOperandFrom(source));
990 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100991 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000992 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100993 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000994 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100995 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000996 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800997 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100998 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
999 ? Primitive::kPrimLong
1000 : Primitive::kPrimInt;
1001 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1002 }
1003 } else {
1004 DCHECK(source.IsFpuRegister());
1005 if (destination.IsRegister()) {
1006 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1007 ? Primitive::kPrimDouble
1008 : Primitive::kPrimFloat;
1009 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1010 } else {
1011 DCHECK(destination.IsFpuRegister());
1012 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001013 }
1014 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001015 } else { // The destination is not a register. It must be a stack slot.
1016 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1017 if (source.IsRegister() || source.IsFpuRegister()) {
1018 if (unspecified_type) {
1019 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001020 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001021 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001022 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001023 }
1024 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001025 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1026 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1027 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001028 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001029 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1030 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001031 UseScratchRegisterScope temps(GetVIXLAssembler());
1032 HConstant* src_cst = source.GetConstant();
1033 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001034 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001035 temp = temps.AcquireW();
1036 } else if (src_cst->IsLongConstant()) {
1037 temp = temps.AcquireX();
1038 } else if (src_cst->IsFloatConstant()) {
1039 temp = temps.AcquireS();
1040 } else {
1041 DCHECK(src_cst->IsDoubleConstant());
1042 temp = temps.AcquireD();
1043 }
1044 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001045 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001046 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001047 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001048 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001049 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001050 // There is generally less pressure on FP registers.
1051 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001052 __ Ldr(temp, StackOperandFrom(source));
1053 __ Str(temp, StackOperandFrom(destination));
1054 }
1055 }
1056}
1057
1058void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001059 CPURegister dst,
1060 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001061 switch (type) {
1062 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001063 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001064 break;
1065 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001066 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001067 break;
1068 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001069 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001070 break;
1071 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001072 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001073 break;
1074 case Primitive::kPrimInt:
1075 case Primitive::kPrimNot:
1076 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001077 case Primitive::kPrimFloat:
1078 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001079 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001080 __ Ldr(dst, src);
1081 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001082 case Primitive::kPrimVoid:
1083 LOG(FATAL) << "Unreachable type " << type;
1084 }
1085}
1086
Calin Juravle77520bc2015-01-12 18:45:46 +00001087void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001088 CPURegister dst,
1089 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001090 MacroAssembler* masm = GetVIXLAssembler();
1091 BlockPoolsScope block_pools(masm);
1092 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001093 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001094 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001095
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096 DCHECK(!src.IsPreIndex());
1097 DCHECK(!src.IsPostIndex());
1098
1099 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001100 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001101 MemOperand base = MemOperand(temp_base);
1102 switch (type) {
1103 case Primitive::kPrimBoolean:
1104 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001105 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001106 break;
1107 case Primitive::kPrimByte:
1108 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001109 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001110 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1111 break;
1112 case Primitive::kPrimChar:
1113 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001114 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001115 break;
1116 case Primitive::kPrimShort:
1117 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001118 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001119 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1120 break;
1121 case Primitive::kPrimInt:
1122 case Primitive::kPrimNot:
1123 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001124 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001126 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001127 break;
1128 case Primitive::kPrimFloat:
1129 case Primitive::kPrimDouble: {
1130 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001131 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001132
1133 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1134 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001135 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001136 __ Fmov(FPRegister(dst), temp);
1137 break;
1138 }
1139 case Primitive::kPrimVoid:
1140 LOG(FATAL) << "Unreachable type " << type;
1141 }
1142}
1143
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001144void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001145 CPURegister src,
1146 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001147 switch (type) {
1148 case Primitive::kPrimBoolean:
1149 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001150 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001151 break;
1152 case Primitive::kPrimChar:
1153 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001154 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001155 break;
1156 case Primitive::kPrimInt:
1157 case Primitive::kPrimNot:
1158 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001159 case Primitive::kPrimFloat:
1160 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001161 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001162 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001163 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001164 case Primitive::kPrimVoid:
1165 LOG(FATAL) << "Unreachable type " << type;
1166 }
1167}
1168
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001169void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1170 CPURegister src,
1171 const MemOperand& dst) {
1172 UseScratchRegisterScope temps(GetVIXLAssembler());
1173 Register temp_base = temps.AcquireX();
1174
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001175 DCHECK(!dst.IsPreIndex());
1176 DCHECK(!dst.IsPostIndex());
1177
1178 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001179 Operand op = OperandFromMemOperand(dst);
1180 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001181 MemOperand base = MemOperand(temp_base);
1182 switch (type) {
1183 case Primitive::kPrimBoolean:
1184 case Primitive::kPrimByte:
1185 __ Stlrb(Register(src), base);
1186 break;
1187 case Primitive::kPrimChar:
1188 case Primitive::kPrimShort:
1189 __ Stlrh(Register(src), base);
1190 break;
1191 case Primitive::kPrimInt:
1192 case Primitive::kPrimNot:
1193 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001194 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001195 __ Stlr(Register(src), base);
1196 break;
1197 case Primitive::kPrimFloat:
1198 case Primitive::kPrimDouble: {
1199 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001200 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001201
1202 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1203 __ Fmov(temp, FPRegister(src));
1204 __ Stlr(temp, base);
1205 break;
1206 }
1207 case Primitive::kPrimVoid:
1208 LOG(FATAL) << "Unreachable type " << type;
1209 }
1210}
1211
Calin Juravle175dc732015-08-25 15:42:32 +01001212void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1213 HInstruction* instruction,
1214 uint32_t dex_pc,
1215 SlowPathCode* slow_path) {
1216 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1217 instruction,
1218 dex_pc,
1219 slow_path);
1220}
1221
Alexandre Rames67555f72014-11-18 10:55:16 +00001222void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1223 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001224 uint32_t dex_pc,
1225 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001226 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001227 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1229 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001230 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001231}
1232
1233void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1234 vixl::Register class_reg) {
1235 UseScratchRegisterScope temps(GetVIXLAssembler());
1236 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001237 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001238 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001239
Serban Constantinescu02164b32014-11-13 14:05:07 +00001240 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001241 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001242 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1243 __ Add(temp, class_reg, status_offset);
1244 __ Ldar(temp, HeapOperand(temp));
1245 __ Cmp(temp, mirror::Class::kStatusInitialized);
1246 __ B(lt, slow_path->GetEntryLabel());
1247 } else {
1248 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1249 __ Cmp(temp, mirror::Class::kStatusInitialized);
1250 __ B(lt, slow_path->GetEntryLabel());
1251 __ Dmb(InnerShareable, BarrierReads);
1252 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001253 __ Bind(slow_path->GetExitLabel());
1254}
Alexandre Rames5319def2014-10-23 10:03:10 +01001255
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001256void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1257 BarrierType type = BarrierAll;
1258
1259 switch (kind) {
1260 case MemBarrierKind::kAnyAny:
1261 case MemBarrierKind::kAnyStore: {
1262 type = BarrierAll;
1263 break;
1264 }
1265 case MemBarrierKind::kLoadAny: {
1266 type = BarrierReads;
1267 break;
1268 }
1269 case MemBarrierKind::kStoreStore: {
1270 type = BarrierWrites;
1271 break;
1272 }
1273 default:
1274 LOG(FATAL) << "Unexpected memory barrier " << kind;
1275 }
1276 __ Dmb(InnerShareable, type);
1277}
1278
Serban Constantinescu02164b32014-11-13 14:05:07 +00001279void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1280 HBasicBlock* successor) {
1281 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001282 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1283 if (slow_path == nullptr) {
1284 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1285 instruction->SetSlowPath(slow_path);
1286 codegen_->AddSlowPath(slow_path);
1287 if (successor != nullptr) {
1288 DCHECK(successor->IsLoopHeader());
1289 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1290 }
1291 } else {
1292 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1293 }
1294
Serban Constantinescu02164b32014-11-13 14:05:07 +00001295 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1296 Register temp = temps.AcquireW();
1297
1298 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1299 if (successor == nullptr) {
1300 __ Cbnz(temp, slow_path->GetEntryLabel());
1301 __ Bind(slow_path->GetReturnLabel());
1302 } else {
1303 __ Cbz(temp, codegen_->GetLabelOf(successor));
1304 __ B(slow_path->GetEntryLabel());
1305 // slow_path will return to GetLabelOf(successor).
1306 }
1307}
1308
Alexandre Rames5319def2014-10-23 10:03:10 +01001309InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1310 CodeGeneratorARM64* codegen)
1311 : HGraphVisitor(graph),
1312 assembler_(codegen->GetAssembler()),
1313 codegen_(codegen) {}
1314
1315#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001316 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001317
1318#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1319
1320enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001321 // Using a base helps identify when we hit such breakpoints.
1322 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001323#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1324 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1325#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1326};
1327
1328#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1329 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001330 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001331 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1332 } \
1333 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1335 locations->SetOut(Location::Any()); \
1336 }
1337 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1338#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1339
1340#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001341#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001342
Alexandre Rames67555f72014-11-18 10:55:16 +00001343void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001344 DCHECK_EQ(instr->InputCount(), 2U);
1345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1346 Primitive::Type type = instr->GetResultType();
1347 switch (type) {
1348 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001349 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001351 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001353 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001354
1355 case Primitive::kPrimFloat:
1356 case Primitive::kPrimDouble:
1357 locations->SetInAt(0, Location::RequiresFpuRegister());
1358 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001359 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001360 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001361
Alexandre Rames5319def2014-10-23 10:03:10 +01001362 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001363 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001364 }
1365}
1366
Alexandre Rames09a99962015-04-15 11:47:56 +01001367void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1368 LocationSummary* locations =
1369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1370 locations->SetInAt(0, Location::RequiresRegister());
1371 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1372 locations->SetOut(Location::RequiresFpuRegister());
1373 } else {
1374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1375 }
1376}
1377
1378void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1379 const FieldInfo& field_info) {
1380 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001381 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001382 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001383
1384 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1385 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1386
1387 if (field_info.IsVolatile()) {
1388 if (use_acquire_release) {
1389 // NB: LoadAcquire will record the pc info if needed.
1390 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1391 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001392 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001393 codegen_->MaybeRecordImplicitNullCheck(instruction);
1394 // For IRIW sequential consistency kLoadAny is not sufficient.
1395 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1396 }
1397 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001398 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001399 codegen_->MaybeRecordImplicitNullCheck(instruction);
1400 }
Roland Levillain4d027112015-07-01 15:41:14 +01001401
1402 if (field_type == Primitive::kPrimNot) {
1403 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1404 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001405}
1406
1407void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1408 LocationSummary* locations =
1409 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1410 locations->SetInAt(0, Location::RequiresRegister());
1411 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1412 locations->SetInAt(1, Location::RequiresFpuRegister());
1413 } else {
1414 locations->SetInAt(1, Location::RequiresRegister());
1415 }
1416}
1417
1418void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001419 const FieldInfo& field_info,
1420 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001421 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001422 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001423
1424 Register obj = InputRegisterAt(instruction, 0);
1425 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001426 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001427 Offset offset = field_info.GetFieldOffset();
1428 Primitive::Type field_type = field_info.GetFieldType();
1429 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1430
Roland Levillain4d027112015-07-01 15:41:14 +01001431 {
1432 // We use a block to end the scratch scope before the write barrier, thus
1433 // freeing the temporary registers so they can be used in `MarkGCCard`.
1434 UseScratchRegisterScope temps(GetVIXLAssembler());
1435
1436 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1437 DCHECK(value.IsW());
1438 Register temp = temps.AcquireW();
1439 __ Mov(temp, value.W());
1440 GetAssembler()->PoisonHeapReference(temp.W());
1441 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001442 }
Roland Levillain4d027112015-07-01 15:41:14 +01001443
1444 if (field_info.IsVolatile()) {
1445 if (use_acquire_release) {
1446 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1447 codegen_->MaybeRecordImplicitNullCheck(instruction);
1448 } else {
1449 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1450 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1451 codegen_->MaybeRecordImplicitNullCheck(instruction);
1452 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1453 }
1454 } else {
1455 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1456 codegen_->MaybeRecordImplicitNullCheck(instruction);
1457 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001458 }
1459
1460 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001461 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001462 }
1463}
1464
Alexandre Rames67555f72014-11-18 10:55:16 +00001465void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001466 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001467
1468 switch (type) {
1469 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001470 case Primitive::kPrimLong: {
1471 Register dst = OutputRegister(instr);
1472 Register lhs = InputRegisterAt(instr, 0);
1473 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001474 if (instr->IsAdd()) {
1475 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001476 } else if (instr->IsAnd()) {
1477 __ And(dst, lhs, rhs);
1478 } else if (instr->IsOr()) {
1479 __ Orr(dst, lhs, rhs);
1480 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001481 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001482 } else {
1483 DCHECK(instr->IsXor());
1484 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001485 }
1486 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001487 }
1488 case Primitive::kPrimFloat:
1489 case Primitive::kPrimDouble: {
1490 FPRegister dst = OutputFPRegister(instr);
1491 FPRegister lhs = InputFPRegisterAt(instr, 0);
1492 FPRegister rhs = InputFPRegisterAt(instr, 1);
1493 if (instr->IsAdd()) {
1494 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001495 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001496 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001497 } else {
1498 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001499 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001500 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001501 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001502 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001503 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001504 }
1505}
1506
Serban Constantinescu02164b32014-11-13 14:05:07 +00001507void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1508 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1509
1510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1511 Primitive::Type type = instr->GetResultType();
1512 switch (type) {
1513 case Primitive::kPrimInt:
1514 case Primitive::kPrimLong: {
1515 locations->SetInAt(0, Location::RequiresRegister());
1516 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1517 locations->SetOut(Location::RequiresRegister());
1518 break;
1519 }
1520 default:
1521 LOG(FATAL) << "Unexpected shift type " << type;
1522 }
1523}
1524
1525void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1526 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1527
1528 Primitive::Type type = instr->GetType();
1529 switch (type) {
1530 case Primitive::kPrimInt:
1531 case Primitive::kPrimLong: {
1532 Register dst = OutputRegister(instr);
1533 Register lhs = InputRegisterAt(instr, 0);
1534 Operand rhs = InputOperandAt(instr, 1);
1535 if (rhs.IsImmediate()) {
1536 uint32_t shift_value = (type == Primitive::kPrimInt)
1537 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1538 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1539 if (instr->IsShl()) {
1540 __ Lsl(dst, lhs, shift_value);
1541 } else if (instr->IsShr()) {
1542 __ Asr(dst, lhs, shift_value);
1543 } else {
1544 __ Lsr(dst, lhs, shift_value);
1545 }
1546 } else {
1547 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1548
1549 if (instr->IsShl()) {
1550 __ Lsl(dst, lhs, rhs_reg);
1551 } else if (instr->IsShr()) {
1552 __ Asr(dst, lhs, rhs_reg);
1553 } else {
1554 __ Lsr(dst, lhs, rhs_reg);
1555 }
1556 }
1557 break;
1558 }
1559 default:
1560 LOG(FATAL) << "Unexpected shift operation type " << type;
1561 }
1562}
1563
Alexandre Rames5319def2014-10-23 10:03:10 +01001564void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001565 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001566}
1567
1568void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 HandleBinaryOp(instruction);
1570}
1571
1572void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1573 HandleBinaryOp(instruction);
1574}
1575
1576void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1577 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001578}
1579
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1581 LocationSummary* locations =
1582 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1583 locations->SetInAt(0, Location::RequiresRegister());
1584 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001585 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1586 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1587 } else {
1588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1589 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001590}
1591
1592void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1593 LocationSummary* locations = instruction->GetLocations();
1594 Primitive::Type type = instruction->GetType();
1595 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001596 Location index = locations->InAt(1);
1597 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001598 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001599 MacroAssembler* masm = GetVIXLAssembler();
1600 UseScratchRegisterScope temps(masm);
1601 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001602
1603 if (index.IsConstant()) {
1604 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001605 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001606 } else {
1607 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001608 __ Add(temp, obj, offset);
1609 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001610 }
1611
Alexandre Rames67555f72014-11-18 10:55:16 +00001612 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001613 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001614
1615 if (type == Primitive::kPrimNot) {
1616 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1617 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001618}
1619
Alexandre Rames5319def2014-10-23 10:03:10 +01001620void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1621 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1622 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001624}
1625
1626void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001627 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001628 __ Ldr(OutputRegister(instruction),
1629 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001630 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001631}
1632
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001633void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001634 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1635 instruction,
1636 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1637 locations->SetInAt(0, Location::RequiresRegister());
1638 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1639 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1640 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001641 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001642 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001643 }
1644}
1645
1646void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1647 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001648 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001649 bool may_need_runtime_call = locations->CanCall();
1650 bool needs_write_barrier =
1651 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001652
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001653 Register array = InputRegisterAt(instruction, 0);
1654 CPURegister value = InputCPURegisterAt(instruction, 2);
1655 CPURegister source = value;
1656 Location index = locations->InAt(1);
1657 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1658 MemOperand destination = HeapOperand(array);
1659 MacroAssembler* masm = GetVIXLAssembler();
1660 BlockPoolsScope block_pools(masm);
1661
1662 if (!needs_write_barrier) {
1663 DCHECK(!may_need_runtime_call);
1664 if (index.IsConstant()) {
1665 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1666 destination = HeapOperand(array, offset);
1667 } else {
1668 UseScratchRegisterScope temps(masm);
1669 Register temp = temps.AcquireSameSizeAs(array);
1670 __ Add(temp, array, offset);
1671 destination = HeapOperand(temp,
1672 XRegisterFrom(index),
1673 LSL,
1674 Primitive::ComponentSizeShift(value_type));
1675 }
1676 codegen_->Store(value_type, value, destination);
1677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001678 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001679 DCHECK(needs_write_barrier);
1680 vixl::Label done;
1681 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001682 {
1683 // We use a block to end the scratch scope before the write barrier, thus
1684 // freeing the temporary registers so they can be used in `MarkGCCard`.
1685 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001686 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001687 if (index.IsConstant()) {
1688 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001689 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001690 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001691 destination = HeapOperand(temp,
1692 XRegisterFrom(index),
1693 LSL,
1694 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001695 }
1696
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001697 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1698 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1699 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1700
1701 if (may_need_runtime_call) {
1702 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1703 codegen_->AddSlowPath(slow_path);
1704 if (instruction->GetValueCanBeNull()) {
1705 vixl::Label non_zero;
1706 __ Cbnz(Register(value), &non_zero);
1707 if (!index.IsConstant()) {
1708 __ Add(temp, array, offset);
1709 }
1710 __ Str(wzr, destination);
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
1712 __ B(&done);
1713 __ Bind(&non_zero);
1714 }
1715
1716 Register temp2 = temps.AcquireSameSizeAs(array);
1717 __ Ldr(temp, HeapOperand(array, class_offset));
1718 codegen_->MaybeRecordImplicitNullCheck(instruction);
1719 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1720 __ Ldr(temp, HeapOperand(temp, component_offset));
1721 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1722 // No need to poison/unpoison, we're comparing two poisoned references.
1723 __ Cmp(temp, temp2);
1724 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1725 vixl::Label do_put;
1726 __ B(eq, &do_put);
1727 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1728 __ Ldr(temp, HeapOperand(temp, super_offset));
1729 // No need to unpoison, we're comparing against null.
1730 __ Cbnz(temp, slow_path->GetEntryLabel());
1731 __ Bind(&do_put);
1732 } else {
1733 __ B(ne, slow_path->GetEntryLabel());
1734 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001735 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001736 }
1737
1738 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001739 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001740 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001741 __ Mov(temp2, value.W());
1742 GetAssembler()->PoisonHeapReference(temp2);
1743 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001744 }
1745
1746 if (!index.IsConstant()) {
1747 __ Add(temp, array, offset);
1748 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001749 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001750
1751 if (!may_need_runtime_call) {
1752 codegen_->MaybeRecordImplicitNullCheck(instruction);
1753 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001754 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001755
1756 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1757
1758 if (done.IsLinked()) {
1759 __ Bind(&done);
1760 }
1761
1762 if (slow_path != nullptr) {
1763 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001764 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001765 }
1766}
1767
Alexandre Rames67555f72014-11-18 10:55:16 +00001768void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001769 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1770 ? LocationSummary::kCallOnSlowPath
1771 : LocationSummary::kNoCall;
1772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001773 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001774 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001775 if (instruction->HasUses()) {
1776 locations->SetOut(Location::SameAsFirstInput());
1777 }
1778}
1779
1780void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001781 BoundsCheckSlowPathARM64* slow_path =
1782 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 codegen_->AddSlowPath(slow_path);
1784
1785 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1786 __ B(slow_path->GetEntryLabel(), hs);
1787}
1788
Alexandre Rames67555f72014-11-18 10:55:16 +00001789void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1790 LocationSummary* locations =
1791 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1792 locations->SetInAt(0, Location::RequiresRegister());
1793 if (check->HasUses()) {
1794 locations->SetOut(Location::SameAsFirstInput());
1795 }
1796}
1797
1798void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1799 // We assume the class is not null.
1800 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1801 check->GetLoadClass(), check, check->GetDexPc(), true);
1802 codegen_->AddSlowPath(slow_path);
1803 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1804}
1805
Roland Levillain7f63c522015-07-13 15:54:55 +00001806static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1807 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1808 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1809}
1810
Serban Constantinescu02164b32014-11-13 14:05:07 +00001811void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001812 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001813 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1814 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001815 switch (in_type) {
1816 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001817 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001818 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1820 break;
1821 }
1822 case Primitive::kPrimFloat:
1823 case Primitive::kPrimDouble: {
1824 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001825 locations->SetInAt(1,
1826 IsFloatingPointZeroConstant(compare->InputAt(1))
1827 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1828 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001829 locations->SetOut(Location::RequiresRegister());
1830 break;
1831 }
1832 default:
1833 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1834 }
1835}
1836
1837void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1838 Primitive::Type in_type = compare->InputAt(0)->GetType();
1839
1840 // 0 if: left == right
1841 // 1 if: left > right
1842 // -1 if: left < right
1843 switch (in_type) {
1844 case Primitive::kPrimLong: {
1845 Register result = OutputRegister(compare);
1846 Register left = InputRegisterAt(compare, 0);
1847 Operand right = InputOperandAt(compare, 1);
1848
1849 __ Cmp(left, right);
1850 __ Cset(result, ne);
1851 __ Cneg(result, result, lt);
1852 break;
1853 }
1854 case Primitive::kPrimFloat:
1855 case Primitive::kPrimDouble: {
1856 Register result = OutputRegister(compare);
1857 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001858 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001859 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1860 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001861 __ Fcmp(left, 0.0);
1862 } else {
1863 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1864 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001865 if (compare->IsGtBias()) {
1866 __ Cset(result, ne);
1867 } else {
1868 __ Csetm(result, ne);
1869 }
1870 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001871 break;
1872 }
1873 default:
1874 LOG(FATAL) << "Unimplemented compare type " << in_type;
1875 }
1876}
1877
1878void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001880
1881 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1882 locations->SetInAt(0, Location::RequiresFpuRegister());
1883 locations->SetInAt(1,
1884 IsFloatingPointZeroConstant(instruction->InputAt(1))
1885 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1886 : Location::RequiresFpuRegister());
1887 } else {
1888 // Integer cases.
1889 locations->SetInAt(0, Location::RequiresRegister());
1890 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1891 }
1892
Alexandre Rames5319def2014-10-23 10:03:10 +01001893 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001895 }
1896}
1897
1898void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1899 if (!instruction->NeedsMaterialization()) {
1900 return;
1901 }
1902
1903 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001904 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001905 IfCondition if_cond = instruction->GetCondition();
1906 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001907
Roland Levillain7f63c522015-07-13 15:54:55 +00001908 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1909 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1910 if (locations->InAt(1).IsConstant()) {
1911 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1912 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1913 __ Fcmp(lhs, 0.0);
1914 } else {
1915 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1916 }
1917 __ Cset(res, arm64_cond);
1918 if (instruction->IsFPConditionTrueIfNaN()) {
1919 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1920 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1921 } else if (instruction->IsFPConditionFalseIfNaN()) {
1922 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1923 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1924 }
1925 } else {
1926 // Integer cases.
1927 Register lhs = InputRegisterAt(instruction, 0);
1928 Operand rhs = InputOperandAt(instruction, 1);
1929 __ Cmp(lhs, rhs);
1930 __ Cset(res, arm64_cond);
1931 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001932}
1933
1934#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1935 M(Equal) \
1936 M(NotEqual) \
1937 M(LessThan) \
1938 M(LessThanOrEqual) \
1939 M(GreaterThan) \
1940 M(GreaterThanOrEqual)
1941#define DEFINE_CONDITION_VISITORS(Name) \
1942void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1943void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1944FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001945#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001946#undef FOR_EACH_CONDITION_INSTRUCTION
1947
Zheng Xuc6667102015-05-15 16:08:45 +08001948void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1949 DCHECK(instruction->IsDiv() || instruction->IsRem());
1950
1951 LocationSummary* locations = instruction->GetLocations();
1952 Location second = locations->InAt(1);
1953 DCHECK(second.IsConstant());
1954
1955 Register out = OutputRegister(instruction);
1956 Register dividend = InputRegisterAt(instruction, 0);
1957 int64_t imm = Int64FromConstant(second.GetConstant());
1958 DCHECK(imm == 1 || imm == -1);
1959
1960 if (instruction->IsRem()) {
1961 __ Mov(out, 0);
1962 } else {
1963 if (imm == 1) {
1964 __ Mov(out, dividend);
1965 } else {
1966 __ Neg(out, dividend);
1967 }
1968 }
1969}
1970
1971void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1972 DCHECK(instruction->IsDiv() || instruction->IsRem());
1973
1974 LocationSummary* locations = instruction->GetLocations();
1975 Location second = locations->InAt(1);
1976 DCHECK(second.IsConstant());
1977
1978 Register out = OutputRegister(instruction);
1979 Register dividend = InputRegisterAt(instruction, 0);
1980 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001981 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001982 DCHECK(IsPowerOfTwo(abs_imm));
1983 int ctz_imm = CTZ(abs_imm);
1984
1985 UseScratchRegisterScope temps(GetVIXLAssembler());
1986 Register temp = temps.AcquireSameSizeAs(out);
1987
1988 if (instruction->IsDiv()) {
1989 __ Add(temp, dividend, abs_imm - 1);
1990 __ Cmp(dividend, 0);
1991 __ Csel(out, temp, dividend, lt);
1992 if (imm > 0) {
1993 __ Asr(out, out, ctz_imm);
1994 } else {
1995 __ Neg(out, Operand(out, ASR, ctz_imm));
1996 }
1997 } else {
1998 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1999 __ Asr(temp, dividend, bits - 1);
2000 __ Lsr(temp, temp, bits - ctz_imm);
2001 __ Add(out, dividend, temp);
2002 __ And(out, out, abs_imm - 1);
2003 __ Sub(out, out, temp);
2004 }
2005}
2006
2007void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2008 DCHECK(instruction->IsDiv() || instruction->IsRem());
2009
2010 LocationSummary* locations = instruction->GetLocations();
2011 Location second = locations->InAt(1);
2012 DCHECK(second.IsConstant());
2013
2014 Register out = OutputRegister(instruction);
2015 Register dividend = InputRegisterAt(instruction, 0);
2016 int64_t imm = Int64FromConstant(second.GetConstant());
2017
2018 Primitive::Type type = instruction->GetResultType();
2019 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2020
2021 int64_t magic;
2022 int shift;
2023 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2024
2025 UseScratchRegisterScope temps(GetVIXLAssembler());
2026 Register temp = temps.AcquireSameSizeAs(out);
2027
2028 // temp = get_high(dividend * magic)
2029 __ Mov(temp, magic);
2030 if (type == Primitive::kPrimLong) {
2031 __ Smulh(temp, dividend, temp);
2032 } else {
2033 __ Smull(temp.X(), dividend, temp);
2034 __ Lsr(temp.X(), temp.X(), 32);
2035 }
2036
2037 if (imm > 0 && magic < 0) {
2038 __ Add(temp, temp, dividend);
2039 } else if (imm < 0 && magic > 0) {
2040 __ Sub(temp, temp, dividend);
2041 }
2042
2043 if (shift != 0) {
2044 __ Asr(temp, temp, shift);
2045 }
2046
2047 if (instruction->IsDiv()) {
2048 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2049 } else {
2050 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2051 // TODO: Strength reduction for msub.
2052 Register temp_imm = temps.AcquireSameSizeAs(out);
2053 __ Mov(temp_imm, imm);
2054 __ Msub(out, temp, temp_imm, dividend);
2055 }
2056}
2057
2058void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2059 DCHECK(instruction->IsDiv() || instruction->IsRem());
2060 Primitive::Type type = instruction->GetResultType();
2061 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2062
2063 LocationSummary* locations = instruction->GetLocations();
2064 Register out = OutputRegister(instruction);
2065 Location second = locations->InAt(1);
2066
2067 if (second.IsConstant()) {
2068 int64_t imm = Int64FromConstant(second.GetConstant());
2069
2070 if (imm == 0) {
2071 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2072 } else if (imm == 1 || imm == -1) {
2073 DivRemOneOrMinusOne(instruction);
2074 } else if (IsPowerOfTwo(std::abs(imm))) {
2075 DivRemByPowerOfTwo(instruction);
2076 } else {
2077 DCHECK(imm <= -2 || imm >= 2);
2078 GenerateDivRemWithAnyConstant(instruction);
2079 }
2080 } else {
2081 Register dividend = InputRegisterAt(instruction, 0);
2082 Register divisor = InputRegisterAt(instruction, 1);
2083 if (instruction->IsDiv()) {
2084 __ Sdiv(out, dividend, divisor);
2085 } else {
2086 UseScratchRegisterScope temps(GetVIXLAssembler());
2087 Register temp = temps.AcquireSameSizeAs(out);
2088 __ Sdiv(temp, dividend, divisor);
2089 __ Msub(out, temp, divisor, dividend);
2090 }
2091 }
2092}
2093
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002094void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2095 LocationSummary* locations =
2096 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2097 switch (div->GetResultType()) {
2098 case Primitive::kPrimInt:
2099 case Primitive::kPrimLong:
2100 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002101 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2103 break;
2104
2105 case Primitive::kPrimFloat:
2106 case Primitive::kPrimDouble:
2107 locations->SetInAt(0, Location::RequiresFpuRegister());
2108 locations->SetInAt(1, Location::RequiresFpuRegister());
2109 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2110 break;
2111
2112 default:
2113 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2114 }
2115}
2116
2117void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2118 Primitive::Type type = div->GetResultType();
2119 switch (type) {
2120 case Primitive::kPrimInt:
2121 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002122 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002123 break;
2124
2125 case Primitive::kPrimFloat:
2126 case Primitive::kPrimDouble:
2127 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2128 break;
2129
2130 default:
2131 LOG(FATAL) << "Unexpected div type " << type;
2132 }
2133}
2134
Alexandre Rames67555f72014-11-18 10:55:16 +00002135void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002136 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2137 ? LocationSummary::kCallOnSlowPath
2138 : LocationSummary::kNoCall;
2139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002140 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2141 if (instruction->HasUses()) {
2142 locations->SetOut(Location::SameAsFirstInput());
2143 }
2144}
2145
2146void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2147 SlowPathCodeARM64* slow_path =
2148 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2149 codegen_->AddSlowPath(slow_path);
2150 Location value = instruction->GetLocations()->InAt(0);
2151
Alexandre Rames3e69f162014-12-10 10:36:50 +00002152 Primitive::Type type = instruction->GetType();
2153
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002154 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2155 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002156 return;
2157 }
2158
Alexandre Rames67555f72014-11-18 10:55:16 +00002159 if (value.IsConstant()) {
2160 int64_t divisor = Int64ConstantFrom(value);
2161 if (divisor == 0) {
2162 __ B(slow_path->GetEntryLabel());
2163 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002164 // A division by a non-null constant is valid. We don't need to perform
2165 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002166 }
2167 } else {
2168 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2169 }
2170}
2171
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002172void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2173 LocationSummary* locations =
2174 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2175 locations->SetOut(Location::ConstantLocation(constant));
2176}
2177
2178void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2179 UNUSED(constant);
2180 // Will be generated at use site.
2181}
2182
Alexandre Rames5319def2014-10-23 10:03:10 +01002183void LocationsBuilderARM64::VisitExit(HExit* exit) {
2184 exit->SetLocations(nullptr);
2185}
2186
2187void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002188 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002189}
2190
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002191void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2192 LocationSummary* locations =
2193 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2194 locations->SetOut(Location::ConstantLocation(constant));
2195}
2196
2197void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2198 UNUSED(constant);
2199 // Will be generated at use site.
2200}
2201
David Brazdilfc6a86a2015-06-26 10:33:45 +00002202void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002203 DCHECK(!successor->IsExitBlock());
2204 HBasicBlock* block = got->GetBlock();
2205 HInstruction* previous = got->GetPrevious();
2206 HLoopInformation* info = block->GetLoopInformation();
2207
David Brazdil46e2a392015-03-16 17:31:52 +00002208 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002209 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2210 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2211 return;
2212 }
2213 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2214 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2215 }
2216 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002217 __ B(codegen_->GetLabelOf(successor));
2218 }
2219}
2220
David Brazdilfc6a86a2015-06-26 10:33:45 +00002221void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2222 got->SetLocations(nullptr);
2223}
2224
2225void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2226 HandleGoto(got, got->GetSuccessor());
2227}
2228
2229void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2230 try_boundary->SetLocations(nullptr);
2231}
2232
2233void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2234 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2235 if (!successor->IsExitBlock()) {
2236 HandleGoto(try_boundary, successor);
2237 }
2238}
2239
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002240void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2241 vixl::Label* true_target,
2242 vixl::Label* false_target,
2243 vixl::Label* always_true_target) {
2244 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002245 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002246
Serban Constantinescu02164b32014-11-13 14:05:07 +00002247 if (cond->IsIntConstant()) {
2248 int32_t cond_value = cond->AsIntConstant()->GetValue();
2249 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002250 if (always_true_target != nullptr) {
2251 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002252 }
2253 return;
2254 } else {
2255 DCHECK_EQ(cond_value, 0);
2256 }
2257 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002259 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002261 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002262 } else {
2263 // The condition instruction has not been materialized, use its inputs as
2264 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002265 Primitive::Type type =
2266 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2267
2268 if (Primitive::IsFloatingPointType(type)) {
2269 // FP compares don't like null false_targets.
2270 if (false_target == nullptr) {
2271 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002272 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002273 FPRegister lhs = InputFPRegisterAt(condition, 0);
2274 if (condition->GetLocations()->InAt(1).IsConstant()) {
2275 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2276 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2277 __ Fcmp(lhs, 0.0);
2278 } else {
2279 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2280 }
2281 if (condition->IsFPConditionTrueIfNaN()) {
2282 __ B(vs, true_target); // VS for unordered.
2283 } else if (condition->IsFPConditionFalseIfNaN()) {
2284 __ B(vs, false_target); // VS for unordered.
2285 }
2286 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002287 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002288 // Integer cases.
2289 Register lhs = InputRegisterAt(condition, 0);
2290 Operand rhs = InputOperandAt(condition, 1);
2291 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2292 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2293 switch (arm64_cond) {
2294 case eq:
2295 __ Cbz(lhs, true_target);
2296 break;
2297 case ne:
2298 __ Cbnz(lhs, true_target);
2299 break;
2300 case lt:
2301 // Test the sign bit and branch accordingly.
2302 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2303 break;
2304 case ge:
2305 // Test the sign bit and branch accordingly.
2306 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2307 break;
2308 default:
2309 // Without the `static_cast` the compiler throws an error for
2310 // `-Werror=sign-promo`.
2311 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2312 }
2313 } else {
2314 __ Cmp(lhs, rhs);
2315 __ B(arm64_cond, true_target);
2316 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002317 }
2318 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002319 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002320 __ B(false_target);
2321 }
2322}
2323
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002324void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2325 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2326 HInstruction* cond = if_instr->InputAt(0);
2327 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2328 locations->SetInAt(0, Location::RequiresRegister());
2329 }
2330}
2331
2332void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2333 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2334 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2335 vixl::Label* always_true_target = true_target;
2336 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2337 if_instr->IfTrueSuccessor())) {
2338 always_true_target = nullptr;
2339 }
2340 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2341 if_instr->IfFalseSuccessor())) {
2342 false_target = nullptr;
2343 }
2344 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2345}
2346
2347void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2348 LocationSummary* locations = new (GetGraph()->GetArena())
2349 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2350 HInstruction* cond = deoptimize->InputAt(0);
2351 DCHECK(cond->IsCondition());
2352 if (cond->AsCondition()->NeedsMaterialization()) {
2353 locations->SetInAt(0, Location::RequiresRegister());
2354 }
2355}
2356
2357void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2358 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2359 DeoptimizationSlowPathARM64(deoptimize);
2360 codegen_->AddSlowPath(slow_path);
2361 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2362 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2363}
2364
Alexandre Rames5319def2014-10-23 10:03:10 +01002365void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002366 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002367}
2368
2369void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002370 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002371}
2372
2373void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002374 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002375}
2376
2377void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002378 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002379}
2380
Alexandre Rames67555f72014-11-18 10:55:16 +00002381void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002382 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2383 switch (instruction->GetTypeCheckKind()) {
2384 case TypeCheckKind::kExactCheck:
2385 case TypeCheckKind::kAbstractClassCheck:
2386 case TypeCheckKind::kClassHierarchyCheck:
2387 case TypeCheckKind::kArrayObjectCheck:
2388 call_kind = LocationSummary::kNoCall;
2389 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002390 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002391 case TypeCheckKind::kInterfaceCheck:
2392 call_kind = LocationSummary::kCall;
2393 break;
2394 case TypeCheckKind::kArrayCheck:
2395 call_kind = LocationSummary::kCallOnSlowPath;
2396 break;
2397 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002399 if (call_kind != LocationSummary::kCall) {
2400 locations->SetInAt(0, Location::RequiresRegister());
2401 locations->SetInAt(1, Location::RequiresRegister());
2402 // The out register is used as a temporary, so it overlaps with the inputs.
2403 // Note that TypeCheckSlowPathARM64 uses this register too.
2404 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2405 } else {
2406 InvokeRuntimeCallingConvention calling_convention;
2407 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2408 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2409 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2410 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002411}
2412
2413void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2414 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002415 Register obj = InputRegisterAt(instruction, 0);
2416 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002417 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002418 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2419 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2420 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2421 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002422
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002423 vixl::Label done, zero;
2424 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002425
2426 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002427 // Avoid null check if we know `obj` is not null.
2428 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002429 __ Cbz(obj, &zero);
2430 }
2431
Calin Juravle98893e12015-10-02 21:05:03 +01002432 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002433 // This is safe, as the register is caller-save, and the object must be in another
2434 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01002435 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
2436 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002437 ? obj
2438 : out;
2439 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2440 GetAssembler()->MaybeUnpoisonHeapReference(target);
2441
2442 switch (instruction->GetTypeCheckKind()) {
2443 case TypeCheckKind::kExactCheck: {
2444 __ Cmp(out, cls);
2445 __ Cset(out, eq);
2446 if (zero.IsLinked()) {
2447 __ B(&done);
2448 }
2449 break;
2450 }
2451 case TypeCheckKind::kAbstractClassCheck: {
2452 // If the class is abstract, we eagerly fetch the super class of the
2453 // object to avoid doing a comparison we know will fail.
2454 vixl::Label loop, success;
2455 __ Bind(&loop);
2456 __ Ldr(out, HeapOperand(out, super_offset));
2457 GetAssembler()->MaybeUnpoisonHeapReference(out);
2458 // If `out` is null, we use it for the result, and jump to `done`.
2459 __ Cbz(out, &done);
2460 __ Cmp(out, cls);
2461 __ B(ne, &loop);
2462 __ Mov(out, 1);
2463 if (zero.IsLinked()) {
2464 __ B(&done);
2465 }
2466 break;
2467 }
2468 case TypeCheckKind::kClassHierarchyCheck: {
2469 // Walk over the class hierarchy to find a match.
2470 vixl::Label loop, success;
2471 __ Bind(&loop);
2472 __ Cmp(out, cls);
2473 __ B(eq, &success);
2474 __ Ldr(out, HeapOperand(out, super_offset));
2475 GetAssembler()->MaybeUnpoisonHeapReference(out);
2476 __ Cbnz(out, &loop);
2477 // If `out` is null, we use it for the result, and jump to `done`.
2478 __ B(&done);
2479 __ Bind(&success);
2480 __ Mov(out, 1);
2481 if (zero.IsLinked()) {
2482 __ B(&done);
2483 }
2484 break;
2485 }
2486 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002487 // Do an exact check.
2488 vixl::Label exact_check;
2489 __ Cmp(out, cls);
2490 __ B(eq, &exact_check);
2491 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002492 __ Ldr(out, HeapOperand(out, component_offset));
2493 GetAssembler()->MaybeUnpoisonHeapReference(out);
2494 // If `out` is null, we use it for the result, and jump to `done`.
2495 __ Cbz(out, &done);
2496 __ Ldrh(out, HeapOperand(out, primitive_offset));
2497 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2498 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002499 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002500 __ Mov(out, 1);
2501 __ B(&done);
2502 break;
2503 }
2504 case TypeCheckKind::kArrayCheck: {
2505 __ Cmp(out, cls);
2506 DCHECK(locations->OnlyCallsOnSlowPath());
2507 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2508 instruction, /* is_fatal */ false);
2509 codegen_->AddSlowPath(slow_path);
2510 __ B(ne, slow_path->GetEntryLabel());
2511 __ Mov(out, 1);
2512 if (zero.IsLinked()) {
2513 __ B(&done);
2514 }
2515 break;
2516 }
Calin Juravle98893e12015-10-02 21:05:03 +01002517 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002518 case TypeCheckKind::kInterfaceCheck:
2519 default: {
2520 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2521 instruction,
2522 instruction->GetDexPc(),
2523 nullptr);
2524 if (zero.IsLinked()) {
2525 __ B(&done);
2526 }
2527 break;
2528 }
2529 }
2530
2531 if (zero.IsLinked()) {
2532 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002533 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002534 }
2535
2536 if (done.IsLinked()) {
2537 __ Bind(&done);
2538 }
2539
2540 if (slow_path != nullptr) {
2541 __ Bind(slow_path->GetExitLabel());
2542 }
2543}
2544
2545void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2546 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2547 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2548
2549 switch (instruction->GetTypeCheckKind()) {
2550 case TypeCheckKind::kExactCheck:
2551 case TypeCheckKind::kAbstractClassCheck:
2552 case TypeCheckKind::kClassHierarchyCheck:
2553 case TypeCheckKind::kArrayObjectCheck:
2554 call_kind = throws_into_catch
2555 ? LocationSummary::kCallOnSlowPath
2556 : LocationSummary::kNoCall;
2557 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002558 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002559 case TypeCheckKind::kInterfaceCheck:
2560 call_kind = LocationSummary::kCall;
2561 break;
2562 case TypeCheckKind::kArrayCheck:
2563 call_kind = LocationSummary::kCallOnSlowPath;
2564 break;
2565 }
2566
2567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2568 instruction, call_kind);
2569 if (call_kind != LocationSummary::kCall) {
2570 locations->SetInAt(0, Location::RequiresRegister());
2571 locations->SetInAt(1, Location::RequiresRegister());
2572 // Note that TypeCheckSlowPathARM64 uses this register too.
2573 locations->AddTemp(Location::RequiresRegister());
2574 } else {
2575 InvokeRuntimeCallingConvention calling_convention;
2576 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2577 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2578 }
2579}
2580
2581void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2582 LocationSummary* locations = instruction->GetLocations();
2583 Register obj = InputRegisterAt(instruction, 0);
2584 Register cls = InputRegisterAt(instruction, 1);
2585 Register temp;
2586 if (!locations->WillCall()) {
2587 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2588 }
2589
2590 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2591 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2592 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2593 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2594 SlowPathCodeARM64* slow_path = nullptr;
2595
2596 if (!locations->WillCall()) {
2597 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2598 instruction, !locations->CanCall());
2599 codegen_->AddSlowPath(slow_path);
2600 }
2601
2602 vixl::Label done;
2603 // Avoid null check if we know obj is not null.
2604 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002605 __ Cbz(obj, &done);
2606 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002607
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002608 if (locations->WillCall()) {
2609 __ Ldr(obj, HeapOperand(obj, class_offset));
2610 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002611 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002612 __ Ldr(temp, HeapOperand(obj, class_offset));
2613 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002614 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002615
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002616 switch (instruction->GetTypeCheckKind()) {
2617 case TypeCheckKind::kExactCheck:
2618 case TypeCheckKind::kArrayCheck: {
2619 __ Cmp(temp, cls);
2620 // Jump to slow path for throwing the exception or doing a
2621 // more involved array check.
2622 __ B(ne, slow_path->GetEntryLabel());
2623 break;
2624 }
2625 case TypeCheckKind::kAbstractClassCheck: {
2626 // If the class is abstract, we eagerly fetch the super class of the
2627 // object to avoid doing a comparison we know will fail.
2628 vixl::Label loop;
2629 __ Bind(&loop);
2630 __ Ldr(temp, HeapOperand(temp, super_offset));
2631 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2632 // Jump to the slow path to throw the exception.
2633 __ Cbz(temp, slow_path->GetEntryLabel());
2634 __ Cmp(temp, cls);
2635 __ B(ne, &loop);
2636 break;
2637 }
2638 case TypeCheckKind::kClassHierarchyCheck: {
2639 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002640 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002641 __ Bind(&loop);
2642 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002643 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002644 __ Ldr(temp, HeapOperand(temp, super_offset));
2645 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2646 __ Cbnz(temp, &loop);
2647 // Jump to the slow path to throw the exception.
2648 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002649 break;
2650 }
2651 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002652 // Do an exact check.
2653 __ Cmp(temp, cls);
2654 __ B(eq, &done);
2655 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002656 __ Ldr(temp, HeapOperand(temp, component_offset));
2657 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2658 __ Cbz(temp, slow_path->GetEntryLabel());
2659 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2660 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2661 __ Cbnz(temp, slow_path->GetEntryLabel());
2662 break;
2663 }
Calin Juravle98893e12015-10-02 21:05:03 +01002664 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002665 case TypeCheckKind::kInterfaceCheck:
2666 default:
2667 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2668 instruction,
2669 instruction->GetDexPc(),
2670 nullptr);
2671 break;
2672 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002673 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002674
2675 if (slow_path != nullptr) {
2676 __ Bind(slow_path->GetExitLabel());
2677 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002678}
2679
Alexandre Rames5319def2014-10-23 10:03:10 +01002680void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2681 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2682 locations->SetOut(Location::ConstantLocation(constant));
2683}
2684
2685void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2686 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002687 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002688}
2689
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002690void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2692 locations->SetOut(Location::ConstantLocation(constant));
2693}
2694
2695void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2696 // Will be generated at use site.
2697 UNUSED(constant);
2698}
2699
Calin Juravle175dc732015-08-25 15:42:32 +01002700void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2701 // The trampoline uses the same calling convention as dex calling conventions,
2702 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2703 // the method_idx.
2704 HandleInvoke(invoke);
2705}
2706
2707void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2708 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2709}
2710
Alexandre Rames5319def2014-10-23 10:03:10 +01002711void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002712 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002713 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002714}
2715
Alexandre Rames67555f72014-11-18 10:55:16 +00002716void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2717 HandleInvoke(invoke);
2718}
2719
2720void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2721 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002722 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2723 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2724 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002725 Location receiver = invoke->GetLocations()->InAt(0);
2726 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002727 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002728
2729 // The register ip1 is required to be used for the hidden argument in
2730 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002731 MacroAssembler* masm = GetVIXLAssembler();
2732 UseScratchRegisterScope scratch_scope(masm);
2733 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002734 scratch_scope.Exclude(ip1);
2735 __ Mov(ip1, invoke->GetDexMethodIndex());
2736
2737 // temp = object->GetClass();
2738 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002739 __ Ldr(temp.W(), StackOperandFrom(receiver));
2740 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002741 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002742 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002743 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002744 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002745 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002746 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002747 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002748 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002749 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002750 // lr();
2751 __ Blr(lr);
2752 DCHECK(!codegen_->IsLeafMethod());
2753 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2754}
2755
2756void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002757 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2758 if (intrinsic.TryDispatch(invoke)) {
2759 return;
2760 }
2761
Alexandre Rames67555f72014-11-18 10:55:16 +00002762 HandleInvoke(invoke);
2763}
2764
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002765void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002766 // When we do not run baseline, explicit clinit checks triggered by static
2767 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2768 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002769
Andreas Gampe878d58c2015-01-15 23:24:00 -08002770 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2771 if (intrinsic.TryDispatch(invoke)) {
2772 return;
2773 }
2774
Alexandre Rames67555f72014-11-18 10:55:16 +00002775 HandleInvoke(invoke);
2776}
2777
Andreas Gampe878d58c2015-01-15 23:24:00 -08002778static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2779 if (invoke->GetLocations()->Intrinsified()) {
2780 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2781 intrinsic.Dispatch(invoke);
2782 return true;
2783 }
2784 return false;
2785}
2786
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002787void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002788 // For better instruction scheduling we load the direct code pointer before the method pointer.
2789 bool direct_code_loaded = false;
2790 switch (invoke->GetCodePtrLocation()) {
2791 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2792 // LR = code address from literal pool with link-time patch.
2793 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2794 direct_code_loaded = true;
2795 break;
2796 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2797 // LR = invoke->GetDirectCodePtr();
2798 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2799 direct_code_loaded = true;
2800 break;
2801 default:
2802 break;
2803 }
2804
Andreas Gampe878d58c2015-01-15 23:24:00 -08002805 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002806 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2807 switch (invoke->GetMethodLoadKind()) {
2808 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2809 // temp = thread->string_init_entrypoint
2810 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2811 break;
2812 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2813 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2814 break;
2815 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2816 // Load method address from literal pool.
2817 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2818 break;
2819 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2820 // Load method address from literal pool with a link-time patch.
2821 __ Ldr(XRegisterFrom(temp).X(),
2822 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2823 break;
2824 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2825 // Add ADRP with its PC-relative DexCache access patch.
2826 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2827 invoke->GetDexCacheArrayOffset());
2828 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2829 {
2830 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2831 __ adrp(XRegisterFrom(temp).X(), 0);
2832 }
2833 __ Bind(pc_insn_label); // Bind after ADRP.
2834 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2835 // Add LDR with its PC-relative DexCache access patch.
2836 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2837 invoke->GetDexCacheArrayOffset());
2838 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2839 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2840 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2841 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002842 }
Vladimir Marko58155012015-08-19 12:49:41 +00002843 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2844 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2845 Register reg = XRegisterFrom(temp);
2846 Register method_reg;
2847 if (current_method.IsRegister()) {
2848 method_reg = XRegisterFrom(current_method);
2849 } else {
2850 DCHECK(invoke->GetLocations()->Intrinsified());
2851 DCHECK(!current_method.IsValid());
2852 method_reg = reg;
2853 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2854 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002855
Vladimir Marko58155012015-08-19 12:49:41 +00002856 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002857 __ Ldr(reg.X(),
2858 MemOperand(method_reg.X(),
2859 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002860 // temp = temp[index_in_cache];
2861 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2862 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2863 break;
2864 }
2865 }
2866
2867 switch (invoke->GetCodePtrLocation()) {
2868 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2869 __ Bl(&frame_entry_label_);
2870 break;
2871 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2872 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2873 vixl::Label* label = &relative_call_patches_.back().label;
2874 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2875 __ Bind(label); // Bind after BL.
2876 break;
2877 }
2878 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2879 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2880 // LR prepared above for better instruction scheduling.
2881 DCHECK(direct_code_loaded);
2882 // lr()
2883 __ Blr(lr);
2884 break;
2885 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2886 // LR = callee_method->entry_point_from_quick_compiled_code_;
2887 __ Ldr(lr, MemOperand(
2888 XRegisterFrom(callee_method).X(),
2889 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2890 // lr()
2891 __ Blr(lr);
2892 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002893 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002894
Andreas Gampe878d58c2015-01-15 23:24:00 -08002895 DCHECK(!IsLeafMethod());
2896}
2897
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002898void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2899 LocationSummary* locations = invoke->GetLocations();
2900 Location receiver = locations->InAt(0);
2901 Register temp = XRegisterFrom(temp_in);
2902 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2903 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2904 Offset class_offset = mirror::Object::ClassOffset();
2905 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2906
2907 BlockPoolsScope block_pools(GetVIXLAssembler());
2908
2909 DCHECK(receiver.IsRegister());
2910 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2911 MaybeRecordImplicitNullCheck(invoke);
2912 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2913 // temp = temp->GetMethodAt(method_offset);
2914 __ Ldr(temp, MemOperand(temp, method_offset));
2915 // lr = temp->GetEntryPoint();
2916 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2917 // lr();
2918 __ Blr(lr);
2919}
2920
Vladimir Marko58155012015-08-19 12:49:41 +00002921void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2922 DCHECK(linker_patches->empty());
2923 size_t size =
2924 method_patches_.size() +
2925 call_patches_.size() +
2926 relative_call_patches_.size() +
2927 pc_rel_dex_cache_patches_.size();
2928 linker_patches->reserve(size);
2929 for (const auto& entry : method_patches_) {
2930 const MethodReference& target_method = entry.first;
2931 vixl::Literal<uint64_t>* literal = entry.second;
2932 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2933 target_method.dex_file,
2934 target_method.dex_method_index));
2935 }
2936 for (const auto& entry : call_patches_) {
2937 const MethodReference& target_method = entry.first;
2938 vixl::Literal<uint64_t>* literal = entry.second;
2939 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2940 target_method.dex_file,
2941 target_method.dex_method_index));
2942 }
2943 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2944 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2945 info.target_method.dex_file,
2946 info.target_method.dex_method_index));
2947 }
2948 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2949 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2950 &info.target_dex_file,
2951 info.pc_insn_label->location() - 4u,
2952 info.element_offset));
2953 }
2954}
2955
2956vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2957 // Look up the literal for value.
2958 auto lb = uint64_literals_.lower_bound(value);
2959 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2960 return lb->second;
2961 }
2962 // We don't have a literal for this value, insert a new one.
2963 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2964 uint64_literals_.PutBefore(lb, value, literal);
2965 return literal;
2966}
2967
2968vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2969 MethodReference target_method,
2970 MethodToLiteralMap* map) {
2971 // Look up the literal for target_method.
2972 auto lb = map->lower_bound(target_method);
2973 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2974 return lb->second;
2975 }
2976 // We don't have a literal for this method yet, insert a new one.
2977 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2978 map->PutBefore(lb, target_method, literal);
2979 return literal;
2980}
2981
2982vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2983 MethodReference target_method) {
2984 return DeduplicateMethodLiteral(target_method, &method_patches_);
2985}
2986
2987vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2988 MethodReference target_method) {
2989 return DeduplicateMethodLiteral(target_method, &call_patches_);
2990}
2991
2992
Andreas Gampe878d58c2015-01-15 23:24:00 -08002993void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002994 // When we do not run baseline, explicit clinit checks triggered by static
2995 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2996 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002997
Andreas Gampe878d58c2015-01-15 23:24:00 -08002998 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2999 return;
3000 }
3001
Alexandre Ramesd921d642015-04-16 15:07:16 +01003002 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003003 LocationSummary* locations = invoke->GetLocations();
3004 codegen_->GenerateStaticOrDirectCall(
3005 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003006 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003007}
3008
3009void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003010 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3011 return;
3012 }
3013
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003014 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003015 DCHECK(!codegen_->IsLeafMethod());
3016 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3017}
3018
Alexandre Rames67555f72014-11-18 10:55:16 +00003019void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003020 InvokeRuntimeCallingConvention calling_convention;
3021 CodeGenerator::CreateLoadClassLocationSummary(
3022 cls,
3023 LocationFrom(calling_convention.GetRegisterAt(0)),
3024 LocationFrom(vixl::x0));
Alexandre Rames67555f72014-11-18 10:55:16 +00003025}
3026
3027void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003028 if (cls->NeedsAccessCheck()) {
3029 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3030 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3031 cls,
3032 cls->GetDexPc(),
3033 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01003034 return;
3035 }
3036
3037 Register out = OutputRegister(cls);
3038 Register current_method = InputRegisterAt(cls, 0);
3039 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003040 DCHECK(!cls->CanCallRuntime());
3041 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003042 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003043 } else {
3044 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003045 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3046 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3047 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3048 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003049
3050 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3051 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3052 codegen_->AddSlowPath(slow_path);
3053 __ Cbz(out, slow_path->GetEntryLabel());
3054 if (cls->MustGenerateClinitCheck()) {
3055 GenerateClassInitializationCheck(slow_path, out);
3056 } else {
3057 __ Bind(slow_path->GetExitLabel());
3058 }
3059 }
3060}
3061
David Brazdilcb1c0552015-08-04 16:22:25 +01003062static MemOperand GetExceptionTlsAddress() {
3063 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3064}
3065
Alexandre Rames67555f72014-11-18 10:55:16 +00003066void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3067 LocationSummary* locations =
3068 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3069 locations->SetOut(Location::RequiresRegister());
3070}
3071
3072void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003073 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3074}
3075
3076void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3077 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3078}
3079
3080void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3081 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003082}
3083
Alexandre Rames5319def2014-10-23 10:03:10 +01003084void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3085 load->SetLocations(nullptr);
3086}
3087
3088void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3089 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003090 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003091}
3092
Alexandre Rames67555f72014-11-18 10:55:16 +00003093void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3094 LocationSummary* locations =
3095 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003096 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003097 locations->SetOut(Location::RequiresRegister());
3098}
3099
3100void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3101 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3102 codegen_->AddSlowPath(slow_path);
3103
3104 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003105 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003106 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003107 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3108 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3109 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003110 __ Cbz(out, slow_path->GetEntryLabel());
3111 __ Bind(slow_path->GetExitLabel());
3112}
3113
Alexandre Rames5319def2014-10-23 10:03:10 +01003114void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3115 local->SetLocations(nullptr);
3116}
3117
3118void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3119 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3120}
3121
3122void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3124 locations->SetOut(Location::ConstantLocation(constant));
3125}
3126
3127void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3128 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003129 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003130}
3131
Alexandre Rames67555f72014-11-18 10:55:16 +00003132void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3133 LocationSummary* locations =
3134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3135 InvokeRuntimeCallingConvention calling_convention;
3136 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3137}
3138
3139void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3140 codegen_->InvokeRuntime(instruction->IsEnter()
3141 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3142 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003143 instruction->GetDexPc(),
3144 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003145 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003146}
3147
Alexandre Rames42d641b2014-10-27 14:00:51 +00003148void LocationsBuilderARM64::VisitMul(HMul* mul) {
3149 LocationSummary* locations =
3150 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3151 switch (mul->GetResultType()) {
3152 case Primitive::kPrimInt:
3153 case Primitive::kPrimLong:
3154 locations->SetInAt(0, Location::RequiresRegister());
3155 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003157 break;
3158
3159 case Primitive::kPrimFloat:
3160 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003161 locations->SetInAt(0, Location::RequiresFpuRegister());
3162 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003163 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003164 break;
3165
3166 default:
3167 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3168 }
3169}
3170
3171void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3172 switch (mul->GetResultType()) {
3173 case Primitive::kPrimInt:
3174 case Primitive::kPrimLong:
3175 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3176 break;
3177
3178 case Primitive::kPrimFloat:
3179 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003180 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003181 break;
3182
3183 default:
3184 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3185 }
3186}
3187
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003188void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3189 LocationSummary* locations =
3190 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3191 switch (neg->GetResultType()) {
3192 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003193 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003194 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003196 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003197
3198 case Primitive::kPrimFloat:
3199 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003200 locations->SetInAt(0, Location::RequiresFpuRegister());
3201 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003202 break;
3203
3204 default:
3205 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3206 }
3207}
3208
3209void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3210 switch (neg->GetResultType()) {
3211 case Primitive::kPrimInt:
3212 case Primitive::kPrimLong:
3213 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3214 break;
3215
3216 case Primitive::kPrimFloat:
3217 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003218 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003219 break;
3220
3221 default:
3222 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3223 }
3224}
3225
3226void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3227 LocationSummary* locations =
3228 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3229 InvokeRuntimeCallingConvention calling_convention;
3230 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003231 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003232 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003233 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003234 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003235 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003236}
3237
3238void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3239 LocationSummary* locations = instruction->GetLocations();
3240 InvokeRuntimeCallingConvention calling_convention;
3241 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3242 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003243 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003244 // Note: if heap poisoning is enabled, the entry point takes cares
3245 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003246 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3247 instruction,
3248 instruction->GetDexPc(),
3249 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003250 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003251}
3252
Alexandre Rames5319def2014-10-23 10:03:10 +01003253void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3254 LocationSummary* locations =
3255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3256 InvokeRuntimeCallingConvention calling_convention;
3257 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003258 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003259 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003260 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003261}
3262
3263void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3264 LocationSummary* locations = instruction->GetLocations();
3265 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3266 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003267 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003268 // Note: if heap poisoning is enabled, the entry point takes cares
3269 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003270 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3271 instruction,
3272 instruction->GetDexPc(),
3273 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003274 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003275}
3276
3277void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003279 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003281}
3282
3283void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003284 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003285 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003286 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003287 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003288 break;
3289
3290 default:
3291 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3292 }
3293}
3294
David Brazdil66d126e2015-04-03 16:02:44 +01003295void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3297 locations->SetInAt(0, Location::RequiresRegister());
3298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3299}
3300
3301void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003302 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3303}
3304
Alexandre Rames5319def2014-10-23 10:03:10 +01003305void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003306 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3307 ? LocationSummary::kCallOnSlowPath
3308 : LocationSummary::kNoCall;
3309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003310 locations->SetInAt(0, Location::RequiresRegister());
3311 if (instruction->HasUses()) {
3312 locations->SetOut(Location::SameAsFirstInput());
3313 }
3314}
3315
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003316void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003317 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3318 return;
3319 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003320
Alexandre Ramesd921d642015-04-16 15:07:16 +01003321 BlockPoolsScope block_pools(GetVIXLAssembler());
3322 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003323 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3324 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3325}
3326
3327void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003328 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3329 codegen_->AddSlowPath(slow_path);
3330
3331 LocationSummary* locations = instruction->GetLocations();
3332 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003333
3334 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003335}
3336
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003337void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003338 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003339 GenerateImplicitNullCheck(instruction);
3340 } else {
3341 GenerateExplicitNullCheck(instruction);
3342 }
3343}
3344
Alexandre Rames67555f72014-11-18 10:55:16 +00003345void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3346 HandleBinaryOp(instruction);
3347}
3348
3349void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3350 HandleBinaryOp(instruction);
3351}
3352
Alexandre Rames3e69f162014-12-10 10:36:50 +00003353void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3354 LOG(FATAL) << "Unreachable";
3355}
3356
3357void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3358 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3359}
3360
Alexandre Rames5319def2014-10-23 10:03:10 +01003361void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3363 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3364 if (location.IsStackSlot()) {
3365 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3366 } else if (location.IsDoubleStackSlot()) {
3367 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3368 }
3369 locations->SetOut(location);
3370}
3371
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003372void InstructionCodeGeneratorARM64::VisitParameterValue(
3373 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003374 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003375}
3376
3377void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3378 LocationSummary* locations =
3379 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003380 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003381}
3382
3383void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3384 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3385 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003386}
3387
3388void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3390 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3391 locations->SetInAt(i, Location::Any());
3392 }
3393 locations->SetOut(Location::Any());
3394}
3395
3396void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003397 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003398 LOG(FATAL) << "Unreachable";
3399}
3400
Serban Constantinescu02164b32014-11-13 14:05:07 +00003401void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003402 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003403 LocationSummary::CallKind call_kind =
3404 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3406
3407 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003408 case Primitive::kPrimInt:
3409 case Primitive::kPrimLong:
3410 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003411 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3413 break;
3414
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003415 case Primitive::kPrimFloat:
3416 case Primitive::kPrimDouble: {
3417 InvokeRuntimeCallingConvention calling_convention;
3418 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3419 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3420 locations->SetOut(calling_convention.GetReturnLocation(type));
3421
3422 break;
3423 }
3424
Serban Constantinescu02164b32014-11-13 14:05:07 +00003425 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003426 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003427 }
3428}
3429
3430void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3431 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003432
Serban Constantinescu02164b32014-11-13 14:05:07 +00003433 switch (type) {
3434 case Primitive::kPrimInt:
3435 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003436 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003437 break;
3438 }
3439
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003440 case Primitive::kPrimFloat:
3441 case Primitive::kPrimDouble: {
3442 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3443 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003444 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003445 break;
3446 }
3447
Serban Constantinescu02164b32014-11-13 14:05:07 +00003448 default:
3449 LOG(FATAL) << "Unexpected rem type " << type;
3450 }
3451}
3452
Calin Juravle27df7582015-04-17 19:12:31 +01003453void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3454 memory_barrier->SetLocations(nullptr);
3455}
3456
3457void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3458 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3459}
3460
Alexandre Rames5319def2014-10-23 10:03:10 +01003461void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3463 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003464 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003465}
3466
3467void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003468 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003469 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003470}
3471
3472void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3473 instruction->SetLocations(nullptr);
3474}
3475
3476void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003477 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003478 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003479}
3480
Serban Constantinescu02164b32014-11-13 14:05:07 +00003481void LocationsBuilderARM64::VisitShl(HShl* shl) {
3482 HandleShift(shl);
3483}
3484
3485void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3486 HandleShift(shl);
3487}
3488
3489void LocationsBuilderARM64::VisitShr(HShr* shr) {
3490 HandleShift(shr);
3491}
3492
3493void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3494 HandleShift(shr);
3495}
3496
Alexandre Rames5319def2014-10-23 10:03:10 +01003497void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3498 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3499 Primitive::Type field_type = store->InputAt(1)->GetType();
3500 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003501 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003502 case Primitive::kPrimBoolean:
3503 case Primitive::kPrimByte:
3504 case Primitive::kPrimChar:
3505 case Primitive::kPrimShort:
3506 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003507 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003508 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3509 break;
3510
3511 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003512 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003513 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3514 break;
3515
3516 default:
3517 LOG(FATAL) << "Unimplemented local type " << field_type;
3518 }
3519}
3520
3521void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003522 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003523}
3524
3525void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003526 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003527}
3528
3529void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003530 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003531}
3532
Alexandre Rames67555f72014-11-18 10:55:16 +00003533void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003534 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003535}
3536
3537void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003538 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003539}
3540
3541void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003542 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003543}
3544
Alexandre Rames67555f72014-11-18 10:55:16 +00003545void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003546 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003547}
3548
Calin Juravlee460d1d2015-09-29 04:52:17 +01003549void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3550 HUnresolvedInstanceFieldGet* instruction) {
3551 FieldAccessCallingConventionARM64 calling_convention;
3552 codegen_->CreateUnresolvedFieldLocationSummary(
3553 instruction, instruction->GetFieldType(), calling_convention);
3554}
3555
3556void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3557 HUnresolvedInstanceFieldGet* instruction) {
3558 FieldAccessCallingConventionARM64 calling_convention;
3559 codegen_->GenerateUnresolvedFieldAccess(instruction,
3560 instruction->GetFieldType(),
3561 instruction->GetFieldIndex(),
3562 instruction->GetDexPc(),
3563 calling_convention);
3564}
3565
3566void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3567 HUnresolvedInstanceFieldSet* instruction) {
3568 FieldAccessCallingConventionARM64 calling_convention;
3569 codegen_->CreateUnresolvedFieldLocationSummary(
3570 instruction, instruction->GetFieldType(), calling_convention);
3571}
3572
3573void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3574 HUnresolvedInstanceFieldSet* instruction) {
3575 FieldAccessCallingConventionARM64 calling_convention;
3576 codegen_->GenerateUnresolvedFieldAccess(instruction,
3577 instruction->GetFieldType(),
3578 instruction->GetFieldIndex(),
3579 instruction->GetDexPc(),
3580 calling_convention);
3581}
3582
3583void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3584 HUnresolvedStaticFieldGet* instruction) {
3585 FieldAccessCallingConventionARM64 calling_convention;
3586 codegen_->CreateUnresolvedFieldLocationSummary(
3587 instruction, instruction->GetFieldType(), calling_convention);
3588}
3589
3590void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3591 HUnresolvedStaticFieldGet* instruction) {
3592 FieldAccessCallingConventionARM64 calling_convention;
3593 codegen_->GenerateUnresolvedFieldAccess(instruction,
3594 instruction->GetFieldType(),
3595 instruction->GetFieldIndex(),
3596 instruction->GetDexPc(),
3597 calling_convention);
3598}
3599
3600void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3601 HUnresolvedStaticFieldSet* instruction) {
3602 FieldAccessCallingConventionARM64 calling_convention;
3603 codegen_->CreateUnresolvedFieldLocationSummary(
3604 instruction, instruction->GetFieldType(), calling_convention);
3605}
3606
3607void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3608 HUnresolvedStaticFieldSet* instruction) {
3609 FieldAccessCallingConventionARM64 calling_convention;
3610 codegen_->GenerateUnresolvedFieldAccess(instruction,
3611 instruction->GetFieldType(),
3612 instruction->GetFieldIndex(),
3613 instruction->GetDexPc(),
3614 calling_convention);
3615}
3616
Alexandre Rames5319def2014-10-23 10:03:10 +01003617void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3618 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3619}
3620
3621void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003622 HBasicBlock* block = instruction->GetBlock();
3623 if (block->GetLoopInformation() != nullptr) {
3624 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3625 // The back edge will generate the suspend check.
3626 return;
3627 }
3628 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3629 // The goto will generate the suspend check.
3630 return;
3631 }
3632 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003633}
3634
3635void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3636 temp->SetLocations(nullptr);
3637}
3638
3639void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3640 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003641 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003642}
3643
Alexandre Rames67555f72014-11-18 10:55:16 +00003644void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3645 LocationSummary* locations =
3646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3647 InvokeRuntimeCallingConvention calling_convention;
3648 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3649}
3650
3651void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3652 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003653 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003654 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003655}
3656
3657void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3658 LocationSummary* locations =
3659 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3660 Primitive::Type input_type = conversion->GetInputType();
3661 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003662 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003663 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3664 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3665 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3666 }
3667
Alexandre Rames542361f2015-01-29 16:57:31 +00003668 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003669 locations->SetInAt(0, Location::RequiresFpuRegister());
3670 } else {
3671 locations->SetInAt(0, Location::RequiresRegister());
3672 }
3673
Alexandre Rames542361f2015-01-29 16:57:31 +00003674 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003675 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3676 } else {
3677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3678 }
3679}
3680
3681void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3682 Primitive::Type result_type = conversion->GetResultType();
3683 Primitive::Type input_type = conversion->GetInputType();
3684
3685 DCHECK_NE(input_type, result_type);
3686
Alexandre Rames542361f2015-01-29 16:57:31 +00003687 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003688 int result_size = Primitive::ComponentSize(result_type);
3689 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003690 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003691 Register output = OutputRegister(conversion);
3692 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003693 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3694 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003695 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3696 // 'int' values are used directly as W registers, discarding the top
3697 // bits, so we don't need to sign-extend and can just perform a move.
3698 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3699 // top 32 bits of the target register. We theoretically could leave those
3700 // bits unchanged, but we would have to make sure that no code uses a
3701 // 32bit input value as a 64bit value assuming that the top 32 bits are
3702 // zero.
3703 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003704 } else if ((result_type == Primitive::kPrimChar) ||
3705 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3706 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003707 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003708 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003709 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003710 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003711 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003712 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003713 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3714 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003715 } else if (Primitive::IsFloatingPointType(result_type) &&
3716 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003717 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3718 } else {
3719 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3720 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003721 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003722}
Alexandre Rames67555f72014-11-18 10:55:16 +00003723
Serban Constantinescu02164b32014-11-13 14:05:07 +00003724void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3725 HandleShift(ushr);
3726}
3727
3728void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3729 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003730}
3731
3732void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3733 HandleBinaryOp(instruction);
3734}
3735
3736void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3737 HandleBinaryOp(instruction);
3738}
3739
Calin Juravleb1498f62015-02-16 13:13:29 +00003740void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3741 // Nothing to do, this should be removed during prepare for register allocator.
3742 UNUSED(instruction);
3743 LOG(FATAL) << "Unreachable";
3744}
3745
3746void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3747 // Nothing to do, this should be removed during prepare for register allocator.
3748 UNUSED(instruction);
3749 LOG(FATAL) << "Unreachable";
3750}
3751
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003752void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3753 DCHECK(codegen_->IsBaseline());
3754 LocationSummary* locations =
3755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3756 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3757}
3758
3759void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3760 DCHECK(codegen_->IsBaseline());
3761 // Will be generated at use site.
3762}
3763
Mark Mendellfe57faa2015-09-18 09:26:15 -04003764// Simple implementation of packed switch - generate cascaded compare/jumps.
3765void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3766 LocationSummary* locations =
3767 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3768 locations->SetInAt(0, Location::RequiresRegister());
3769}
3770
3771void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3772 int32_t lower_bound = switch_instr->GetStartValue();
3773 int32_t num_entries = switch_instr->GetNumEntries();
3774 Register value_reg = InputRegisterAt(switch_instr, 0);
3775 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3776
3777 // Create a series of compare/jumps.
3778 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3779 for (int32_t i = 0; i < num_entries; i++) {
3780 int32_t case_value = lower_bound + i;
Vladimir Markoec7802a2015-10-01 20:57:57 +01003781 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003782 if (case_value == 0) {
3783 __ Cbz(value_reg, succ);
3784 } else {
3785 __ Cmp(value_reg, vixl::Operand(case_value));
3786 __ B(eq, succ);
3787 }
3788 }
3789
3790 // And the default for any other value.
3791 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3792 __ B(codegen_->GetLabelOf(default_block));
3793 }
3794}
3795
Alexandre Rames67555f72014-11-18 10:55:16 +00003796#undef __
3797#undef QUICK_ENTRY_POINT
3798
Alexandre Rames5319def2014-10-23 10:03:10 +01003799} // namespace arm64
3800} // namespace art