blob: 78ecfdec10d93cb91e3aaaf4842ffa3121fb37e3 [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"
Calin Juravlee6f49b42015-09-17 14:04:33 +000022#include "common_arm64.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000031#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010032#include "thread.h"
33#include "utils/arm64/assembler_arm64.h"
34#include "utils/assembler.h"
35#include "utils/stack_checks.h"
36
37
38using namespace vixl; // NOLINT(build/namespaces)
39
40#ifdef __
41#error "ARM64 Codegen VIXL macro-assembler macro already defined."
42#endif
43
Alexandre Rames5319def2014-10-23 10:03:10 +010044namespace art {
45
46namespace arm64 {
47
Andreas Gampe878d58c2015-01-15 23:24:00 -080048using helpers::CPURegisterFrom;
49using helpers::DRegisterFrom;
50using helpers::FPRegisterFrom;
51using helpers::HeapOperand;
52using helpers::HeapOperandFrom;
53using helpers::InputCPURegisterAt;
54using helpers::InputFPRegisterAt;
55using helpers::InputRegisterAt;
56using helpers::InputOperandAt;
57using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::LocationFrom;
59using helpers::OperandFromMemOperand;
60using helpers::OutputCPURegister;
61using helpers::OutputFPRegister;
62using helpers::OutputRegister;
63using helpers::RegisterFrom;
64using helpers::StackOperandFrom;
65using helpers::VIXLRegCodeFromART;
66using helpers::WRegisterFrom;
67using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000068using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080069using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080070
Alexandre Rames5319def2014-10-23 10:03:10 +010071static constexpr int kCurrentMethodStackOffset = 0;
72
Alexandre Rames5319def2014-10-23 10:03:10 +010073inline Condition ARM64Condition(IfCondition cond) {
74 switch (cond) {
75 case kCondEQ: return eq;
76 case kCondNE: return ne;
77 case kCondLT: return lt;
78 case kCondLE: return le;
79 case kCondGT: return gt;
80 case kCondGE: return ge;
Alexandre Rames5319def2014-10-23 10:03:10 +010081 }
Roland Levillain7f63c522015-07-13 15:54:55 +000082 LOG(FATAL) << "Unreachable";
83 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010084}
85
Alexandre Ramesa89086e2014-11-07 17:13:25 +000086Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000087 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
88 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
89 // but we use the exact registers for clarity.
90 if (return_type == Primitive::kPrimFloat) {
91 return LocationFrom(s0);
92 } else if (return_type == Primitive::kPrimDouble) {
93 return LocationFrom(d0);
94 } else if (return_type == Primitive::kPrimLong) {
95 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +010096 } else if (return_type == Primitive::kPrimVoid) {
97 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +000098 } else {
99 return LocationFrom(w0);
100 }
101}
102
Alexandre Rames5319def2014-10-23 10:03:10 +0100103Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000104 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100105}
106
Alexandre Rames67555f72014-11-18 10:55:16 +0000107#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100109
Zheng Xuda403092015-04-24 17:35:39 +0800110// Calculate memory accessing operand for save/restore live registers.
111static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
112 RegisterSet* register_set,
113 int64_t spill_offset,
114 bool is_save) {
115 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
116 codegen->GetNumberOfCoreRegisters(),
117 register_set->GetFloatingPointRegisters(),
118 codegen->GetNumberOfFloatingPointRegisters()));
119
120 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
121 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray88a95ba2015-09-30 17:18:14 +0100122 CPURegList fp_list = CPURegList(
123 CPURegister::kFPRegister,
124 kDRegSize,
125 register_set->GetFloatingPointRegisters()
126 & (~(codegen->GetGraph()->IsDebuggable() ? 0 : callee_saved_fp_registers.list())));
Zheng Xuda403092015-04-24 17:35:39 +0800127
128 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
129 UseScratchRegisterScope temps(masm);
130
131 Register base = masm->StackPointer();
132 int64_t core_spill_size = core_list.TotalSizeInBytes();
133 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
134 int64_t reg_size = kXRegSizeInBytes;
135 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
136 uint32_t ls_access_size = WhichPowerOf2(reg_size);
137 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
138 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
139 // If the offset does not fit in the instruction's immediate field, use an alternate register
140 // to compute the base address(float point registers spill base address).
141 Register new_base = temps.AcquireSameSizeAs(base);
142 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
143 base = new_base;
144 spill_offset = -core_spill_size;
145 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
146 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
147 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
148 }
149
150 if (is_save) {
151 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
152 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
153 } else {
154 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
155 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
156 }
157}
158
159void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
160 RegisterSet* register_set = locations->GetLiveRegisters();
161 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
162 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
163 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
164 // If the register holds an object, update the stack mask.
165 if (locations->RegisterContainsObject(i)) {
166 locations->SetStackBit(stack_offset / kVRegSize);
167 }
168 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
169 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
170 saved_core_stack_offsets_[i] = stack_offset;
171 stack_offset += kXRegSizeInBytes;
172 }
173 }
174
175 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
176 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
177 register_set->ContainsFloatingPointRegister(i)) {
178 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
179 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
180 saved_fpu_stack_offsets_[i] = stack_offset;
181 stack_offset += kDRegSizeInBytes;
182 }
183 }
184
185 SaveRestoreLiveRegistersHelper(codegen, register_set,
186 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
187}
188
189void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
190 RegisterSet* register_set = locations->GetLiveRegisters();
191 SaveRestoreLiveRegistersHelper(codegen, register_set,
192 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
193}
194
Alexandre Rames5319def2014-10-23 10:03:10 +0100195class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
196 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100197 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100200 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000201 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100202
Alexandre Rames5319def2014-10-23 10:03:10 +0100203 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000204 if (instruction_->CanThrowIntoCatchBlock()) {
205 // Live registers will be restored in the catch block if caught.
206 SaveLiveRegisters(codegen, instruction_->GetLocations());
207 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000208 // We're moving two locations to locations that could overlap, so we need a parallel
209 // move resolver.
210 InvokeRuntimeCallingConvention calling_convention;
211 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100212 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
213 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000214 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000215 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800216 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100217 }
218
Alexandre Rames8158f282015-08-07 10:26:17 +0100219 bool IsFatal() const OVERRIDE { return true; }
220
Alexandre Rames9931f312015-06-19 14:47:01 +0100221 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
222
Alexandre Rames5319def2014-10-23 10:03:10 +0100223 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000224 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225
Alexandre Rames5319def2014-10-23 10:03:10 +0100226 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
227};
228
Alexandre Rames67555f72014-11-18 10:55:16 +0000229class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
230 public:
231 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
235 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000236 if (instruction_->CanThrowIntoCatchBlock()) {
237 // Live registers will be restored in the catch block if caught.
238 SaveLiveRegisters(codegen, instruction_->GetLocations());
239 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000240 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000241 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800242 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000243 }
244
Alexandre Rames8158f282015-08-07 10:26:17 +0100245 bool IsFatal() const OVERRIDE { return true; }
246
Alexandre Rames9931f312015-06-19 14:47:01 +0100247 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 private:
250 HDivZeroCheck* const instruction_;
251 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
252};
253
254class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
256 LoadClassSlowPathARM64(HLoadClass* cls,
257 HInstruction* at,
258 uint32_t dex_pc,
259 bool do_clinit)
260 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
261 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
262 }
263
264 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
265 LocationSummary* locations = at_->GetLocations();
266 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
267
268 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000269 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000270
271 InvokeRuntimeCallingConvention calling_convention;
272 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
274 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000275 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800276 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100277 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800278 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100279 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800280 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000281
282 // Move the class to the desired location.
283 Location out = locations->Out();
284 if (out.IsValid()) {
285 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
286 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000287 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000288 }
289
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 __ B(GetExitLabel());
292 }
293
Alexandre Rames9931f312015-06-19 14:47:01 +0100294 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
295
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 private:
297 // The class this slow path will load.
298 HLoadClass* const cls_;
299
300 // The instruction where this slow path is happening.
301 // (Might be the load class or an initialization check).
302 HInstruction* const at_;
303
304 // The dex PC of `at_`.
305 const uint32_t dex_pc_;
306
307 // Whether to initialize the class.
308 const bool do_clinit_;
309
310 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
311};
312
313class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
314 public:
315 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
316
317 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
318 LocationSummary* locations = instruction_->GetLocations();
319 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
320 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
321
322 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000323 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000324
325 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800326 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000327 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000328 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100329 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000331 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000332
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000333 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 __ B(GetExitLabel());
335 }
336
Alexandre Rames9931f312015-06-19 14:47:01 +0100337 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
338
Alexandre Rames67555f72014-11-18 10:55:16 +0000339 private:
340 HLoadString* const instruction_;
341
342 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
343};
344
Alexandre Rames5319def2014-10-23 10:03:10 +0100345class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
346 public:
347 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
348
Alexandre Rames67555f72014-11-18 10:55:16 +0000349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
350 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100351 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000352 if (instruction_->CanThrowIntoCatchBlock()) {
353 // Live registers will be restored in the catch block if caught.
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000357 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800358 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100359 }
360
Alexandre Rames8158f282015-08-07 10:26:17 +0100361 bool IsFatal() const OVERRIDE { return true; }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
364
Alexandre Rames5319def2014-10-23 10:03:10 +0100365 private:
366 HNullCheck* const instruction_;
367
368 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
369};
370
371class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
372 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100373 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100374 : instruction_(instruction), successor_(successor) {}
375
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
377 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100378 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000379 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000381 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800382 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000384 if (successor_ == nullptr) {
385 __ B(GetReturnLabel());
386 } else {
387 __ B(arm64_codegen->GetLabelOf(successor_));
388 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100389 }
390
391 vixl::Label* GetReturnLabel() {
392 DCHECK(successor_ == nullptr);
393 return &return_label_;
394 }
395
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100396 HBasicBlock* GetSuccessor() const {
397 return successor_;
398 }
399
Alexandre Rames9931f312015-06-19 14:47:01 +0100400 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
401
Alexandre Rames5319def2014-10-23 10:03:10 +0100402 private:
403 HSuspendCheck* const instruction_;
404 // If not null, the block to branch to after the suspend check.
405 HBasicBlock* const successor_;
406
407 // If `successor_` is null, the label to branch to after the suspend check.
408 vixl::Label return_label_;
409
410 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
411};
412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
414 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000415 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
416 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000419 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100420 Location class_to_check = locations->InAt(1);
421 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
422 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423 DCHECK(instruction_->IsCheckCast()
424 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
425 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100426 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000427
Alexandre Rames67555f72014-11-18 10:55:16 +0000428 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000429
430 if (instruction_->IsCheckCast()) {
431 // The codegen for the instruction overwrites `temp`, so put it back in place.
432 Register obj = InputRegisterAt(instruction_, 0);
433 Register temp = WRegisterFrom(locations->GetTemp(0));
434 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
435 __ Ldr(temp, HeapOperand(obj, class_offset));
436 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
437 }
438
439 if (!is_fatal_) {
440 SaveLiveRegisters(codegen, locations);
441 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442
443 // We're moving two locations to locations that could overlap, so we need a parallel
444 // move resolver.
445 InvokeRuntimeCallingConvention calling_convention;
446 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100447 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
448 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000449
450 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000451 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100452 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453 Primitive::Type ret_type = instruction_->GetType();
454 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
455 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800456 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
457 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458 } else {
459 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100460 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800461 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000462 }
463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000464 if (!is_fatal_) {
465 RestoreLiveRegisters(codegen, locations);
466 __ B(GetExitLabel());
467 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 }
469
Alexandre Rames9931f312015-06-19 14:47:01 +0100470 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
478};
479
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700480class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
481 public:
482 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100483 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700484
485 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
486 __ Bind(GetEntryLabel());
487 SaveLiveRegisters(codegen, instruction_->GetLocations());
488 DCHECK(instruction_->IsDeoptimize());
489 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
490 uint32_t dex_pc = deoptimize->GetDexPc();
491 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
492 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
493 }
494
Alexandre Rames9931f312015-06-19 14:47:01 +0100495 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
496
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700497 private:
498 HInstruction* const instruction_;
499 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
500};
501
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100502class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
503 public:
504 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
507 LocationSummary* locations = instruction_->GetLocations();
508 __ Bind(GetEntryLabel());
509 SaveLiveRegisters(codegen, locations);
510
511 InvokeRuntimeCallingConvention calling_convention;
512 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
513 parallel_move.AddMove(
514 locations->InAt(0),
515 LocationFrom(calling_convention.GetRegisterAt(0)),
516 Primitive::kPrimNot,
517 nullptr);
518 parallel_move.AddMove(
519 locations->InAt(1),
520 LocationFrom(calling_convention.GetRegisterAt(1)),
521 Primitive::kPrimInt,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(2),
525 LocationFrom(calling_convention.GetRegisterAt(2)),
526 Primitive::kPrimNot,
527 nullptr);
528 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
529
530 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
531 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
532 instruction_,
533 instruction_->GetDexPc(),
534 this);
535 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
536 RestoreLiveRegisters(codegen, locations);
537 __ B(GetExitLabel());
538 }
539
540 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
541
542 private:
543 HInstruction* const instruction_;
544
545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
546};
547
Alexandre Rames5319def2014-10-23 10:03:10 +0100548#undef __
549
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100550Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100551 Location next_location;
552 if (type == Primitive::kPrimVoid) {
553 LOG(FATAL) << "Unreachable type " << type;
554 }
555
Alexandre Rames542361f2015-01-29 16:57:31 +0000556 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100557 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
558 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000559 } else if (!Primitive::IsFloatingPointType(type) &&
560 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000561 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
562 } else {
563 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000564 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
565 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100566 }
567
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000568 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000569 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100570 return next_location;
571}
572
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100573Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100574 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100575}
576
Serban Constantinescu579885a2015-02-22 20:51:33 +0000577CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
578 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100579 const CompilerOptions& compiler_options,
580 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100581 : CodeGenerator(graph,
582 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000583 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000584 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000585 callee_saved_core_registers.list(),
Nicolas Geoffray88a95ba2015-09-30 17:18:14 +0100586 // If the graph is debuggable, we need to save the fpu registers ourselves,
587 // as the stubs do not do it.
588 graph->IsDebuggable() ? 0 : callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100589 compiler_options,
590 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100591 block_labels_(nullptr),
592 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000593 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000594 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000595 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100596 uint64_literals_(std::less<uint64_t>(),
597 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
598 method_patches_(MethodReferenceComparator(),
599 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
600 call_patches_(MethodReferenceComparator(),
601 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
602 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
603 pc_rel_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000604 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000605 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000606}
Alexandre Rames5319def2014-10-23 10:03:10 +0100607
Alexandre Rames67555f72014-11-18 10:55:16 +0000608#undef __
609#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100610
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000611void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
612 // Ensure we emit the literal pool.
613 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000614
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000615 CodeGenerator::Finalize(allocator);
616}
617
Zheng Xuad4450e2015-04-17 18:48:56 +0800618void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
619 // Note: There are 6 kinds of moves:
620 // 1. constant -> GPR/FPR (non-cycle)
621 // 2. constant -> stack (non-cycle)
622 // 3. GPR/FPR -> GPR/FPR
623 // 4. GPR/FPR -> stack
624 // 5. stack -> GPR/FPR
625 // 6. stack -> stack (non-cycle)
626 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
627 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
628 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
629 // dependency.
630 vixl_temps_.Open(GetVIXLAssembler());
631}
632
633void ParallelMoveResolverARM64::FinishEmitNativeCode() {
634 vixl_temps_.Close();
635}
636
637Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
638 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
639 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
640 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
641 Location scratch = GetScratchLocation(kind);
642 if (!scratch.Equals(Location::NoLocation())) {
643 return scratch;
644 }
645 // Allocate from VIXL temp registers.
646 if (kind == Location::kRegister) {
647 scratch = LocationFrom(vixl_temps_.AcquireX());
648 } else {
649 DCHECK(kind == Location::kFpuRegister);
650 scratch = LocationFrom(vixl_temps_.AcquireD());
651 }
652 AddScratchLocation(scratch);
653 return scratch;
654}
655
656void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
657 if (loc.IsRegister()) {
658 vixl_temps_.Release(XRegisterFrom(loc));
659 } else {
660 DCHECK(loc.IsFpuRegister());
661 vixl_temps_.Release(DRegisterFrom(loc));
662 }
663 RemoveScratchLocation(loc);
664}
665
Alexandre Rames3e69f162014-12-10 10:36:50 +0000666void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100667 DCHECK_LT(index, moves_.size());
668 MoveOperands* move = moves_[index];
Alexandre Rames3e69f162014-12-10 10:36:50 +0000669 codegen_->MoveLocation(move->GetDestination(), move->GetSource());
670}
671
Alexandre Rames5319def2014-10-23 10:03:10 +0100672void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100673 MacroAssembler* masm = GetVIXLAssembler();
674 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000675 __ Bind(&frame_entry_label_);
676
Serban Constantinescu02164b32014-11-13 14:05:07 +0000677 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
678 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100679 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000680 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000681 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000682 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000683 __ Ldr(wzr, MemOperand(temp, 0));
684 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000685 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100686
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000687 if (!HasEmptyFrame()) {
688 int frame_size = GetFrameSize();
689 // Stack layout:
690 // sp[frame_size - 8] : lr.
691 // ... : other preserved core registers.
692 // ... : other preserved fp registers.
693 // ... : reserved frame space.
694 // sp[0] : current method.
695 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100696 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800697 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
698 frame_size - GetCoreSpillSize());
699 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
700 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000701 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100702}
703
704void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100705 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100706 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000707 if (!HasEmptyFrame()) {
708 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800709 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
710 frame_size - FrameEntrySpillSize());
711 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
712 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000713 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100714 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000715 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100716 __ Ret();
717 GetAssembler()->cfi().RestoreState();
718 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100719}
720
Zheng Xuda403092015-04-24 17:35:39 +0800721vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
722 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
723 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
724 core_spill_mask_);
725}
726
727vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
728 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
729 GetNumberOfFloatingPointRegisters()));
730 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
731 fpu_spill_mask_);
732}
733
Alexandre Rames5319def2014-10-23 10:03:10 +0100734void CodeGeneratorARM64::Bind(HBasicBlock* block) {
735 __ Bind(GetLabelOf(block));
736}
737
Alexandre Rames5319def2014-10-23 10:03:10 +0100738void CodeGeneratorARM64::Move(HInstruction* instruction,
739 Location location,
740 HInstruction* move_for) {
741 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100742 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000743 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100744
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100745 if (instruction->IsFakeString()) {
746 // The fake string is an alias for null.
747 DCHECK(IsBaseline());
748 instruction = locations->Out().GetConstant();
749 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
750 }
751
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100752 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700753 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100754 } else if (locations != nullptr && locations->Out().Equals(location)) {
755 return;
756 } else if (instruction->IsIntConstant()
757 || instruction->IsLongConstant()
758 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000759 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100760 if (location.IsRegister()) {
761 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000762 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100763 (instruction->IsLongConstant() && dst.Is64Bits()));
764 __ Mov(dst, value);
765 } else {
766 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000767 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000768 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
769 ? temps.AcquireW()
770 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100771 __ Mov(temp, value);
772 __ Str(temp, StackOperandFrom(location));
773 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000774 } else if (instruction->IsTemporary()) {
775 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000776 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100777 } else if (instruction->IsLoadLocal()) {
778 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000779 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000780 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000781 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000782 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100783 }
784
785 } else {
786 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000787 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100788 }
789}
790
Calin Juravle175dc732015-08-25 15:42:32 +0100791void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
792 DCHECK(location.IsRegister());
793 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
794}
795
Alexandre Rames5319def2014-10-23 10:03:10 +0100796Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
797 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000798
Alexandre Rames5319def2014-10-23 10:03:10 +0100799 switch (type) {
800 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000801 case Primitive::kPrimInt:
802 case Primitive::kPrimFloat:
803 return Location::StackSlot(GetStackSlot(load->GetLocal()));
804
805 case Primitive::kPrimLong:
806 case Primitive::kPrimDouble:
807 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
808
Alexandre Rames5319def2014-10-23 10:03:10 +0100809 case Primitive::kPrimBoolean:
810 case Primitive::kPrimByte:
811 case Primitive::kPrimChar:
812 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100813 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100814 LOG(FATAL) << "Unexpected type " << type;
815 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000816
Alexandre Rames5319def2014-10-23 10:03:10 +0100817 LOG(FATAL) << "Unreachable";
818 return Location::NoLocation();
819}
820
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100821void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000822 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100823 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000824 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100825 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100826 if (value_can_be_null) {
827 __ Cbz(value, &done);
828 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100829 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
830 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000831 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100832 if (value_can_be_null) {
833 __ Bind(&done);
834 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100835}
836
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000837void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
838 // Blocked core registers:
839 // lr : Runtime reserved.
840 // tr : Runtime reserved.
841 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
842 // ip1 : VIXL core temp.
843 // ip0 : VIXL core temp.
844 //
845 // Blocked fp registers:
846 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100847 CPURegList reserved_core_registers = vixl_reserved_core_registers;
848 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100849 while (!reserved_core_registers.IsEmpty()) {
850 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
851 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000852
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000853 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800854 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000855 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
856 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000857
858 if (is_baseline) {
859 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
860 while (!reserved_core_baseline_registers.IsEmpty()) {
861 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
862 }
863
864 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
865 while (!reserved_fp_baseline_registers.IsEmpty()) {
866 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
867 }
868 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100869}
870
871Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
872 if (type == Primitive::kPrimVoid) {
873 LOG(FATAL) << "Unreachable type " << type;
874 }
875
Alexandre Rames542361f2015-01-29 16:57:31 +0000876 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000877 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
878 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100879 return Location::FpuRegisterLocation(reg);
880 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000881 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
882 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100883 return Location::RegisterLocation(reg);
884 }
885}
886
Alexandre Rames3e69f162014-12-10 10:36:50 +0000887size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
888 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
889 __ Str(reg, MemOperand(sp, stack_index));
890 return kArm64WordSize;
891}
892
893size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
894 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
895 __ Ldr(reg, MemOperand(sp, stack_index));
896 return kArm64WordSize;
897}
898
899size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
900 FPRegister reg = FPRegister(reg_id, kDRegSize);
901 __ Str(reg, MemOperand(sp, stack_index));
902 return kArm64WordSize;
903}
904
905size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
906 FPRegister reg = FPRegister(reg_id, kDRegSize);
907 __ Ldr(reg, MemOperand(sp, stack_index));
908 return kArm64WordSize;
909}
910
Alexandre Rames5319def2014-10-23 10:03:10 +0100911void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100912 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100913}
914
915void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100916 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100917}
918
Alexandre Rames67555f72014-11-18 10:55:16 +0000919void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000920 if (constant->IsIntConstant()) {
921 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
922 } else if (constant->IsLongConstant()) {
923 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
924 } else if (constant->IsNullConstant()) {
925 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000926 } else if (constant->IsFloatConstant()) {
927 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
928 } else {
929 DCHECK(constant->IsDoubleConstant());
930 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
931 }
932}
933
Alexandre Rames3e69f162014-12-10 10:36:50 +0000934
935static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
936 DCHECK(constant.IsConstant());
937 HConstant* cst = constant.GetConstant();
938 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000939 // Null is mapped to a core W register, which we associate with kPrimInt.
940 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000941 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
942 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
943 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
944}
945
946void CodeGeneratorARM64::MoveLocation(Location destination, Location source, Primitive::Type type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000947 if (source.Equals(destination)) {
948 return;
949 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000950
951 // A valid move can always be inferred from the destination and source
952 // locations. When moving from and to a register, the argument type can be
953 // used to generate 32bit instead of 64bit moves. In debug mode we also
954 // checks the coherency of the locations and the type.
955 bool unspecified_type = (type == Primitive::kPrimVoid);
956
957 if (destination.IsRegister() || destination.IsFpuRegister()) {
958 if (unspecified_type) {
959 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
960 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000961 (src_cst != nullptr && (src_cst->IsIntConstant()
962 || src_cst->IsFloatConstant()
963 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000964 // For stack slots and 32bit constants, a 64bit type is appropriate.
965 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +0000966 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000967 // If the source is a double stack slot or a 64bit constant, a 64bit
968 // type is appropriate. Else the source is a register, and since the
969 // type has not been specified, we chose a 64bit type to force a 64bit
970 // move.
971 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +0000972 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000973 }
Alexandre Rames542361f2015-01-29 16:57:31 +0000974 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
975 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +0000976 CPURegister dst = CPURegisterFrom(destination, type);
977 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
978 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
979 __ Ldr(dst, StackOperandFrom(source));
980 } else if (source.IsConstant()) {
981 DCHECK(CoherentConstantAndType(source, type));
982 MoveConstant(dst, source.GetConstant());
983 } else {
984 if (destination.IsRegister()) {
985 __ Mov(Register(dst), RegisterFrom(source, type));
986 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +0800987 DCHECK(destination.IsFpuRegister());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000988 __ Fmov(FPRegister(dst), FPRegisterFrom(source, type));
989 }
990 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000991 } else { // The destination is not a register. It must be a stack slot.
992 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
993 if (source.IsRegister() || source.IsFpuRegister()) {
994 if (unspecified_type) {
995 if (source.IsRegister()) {
996 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
997 } else {
998 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
999 }
1000 }
Alexandre Rames542361f2015-01-29 16:57:31 +00001001 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
1002 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001003 __ Str(CPURegisterFrom(source, type), StackOperandFrom(destination));
1004 } else if (source.IsConstant()) {
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +01001005 DCHECK(unspecified_type || CoherentConstantAndType(source, type)) << source << " " << type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001006 UseScratchRegisterScope temps(GetVIXLAssembler());
1007 HConstant* src_cst = source.GetConstant();
1008 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001009 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001010 temp = temps.AcquireW();
1011 } else if (src_cst->IsLongConstant()) {
1012 temp = temps.AcquireX();
1013 } else if (src_cst->IsFloatConstant()) {
1014 temp = temps.AcquireS();
1015 } else {
1016 DCHECK(src_cst->IsDoubleConstant());
1017 temp = temps.AcquireD();
1018 }
1019 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001020 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001021 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001022 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001023 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001024 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001025 // There is generally less pressure on FP registers.
1026 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001027 __ Ldr(temp, StackOperandFrom(source));
1028 __ Str(temp, StackOperandFrom(destination));
1029 }
1030 }
1031}
1032
1033void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001034 CPURegister dst,
1035 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001036 switch (type) {
1037 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001038 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001039 break;
1040 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001041 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001042 break;
1043 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001044 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001045 break;
1046 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001047 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001048 break;
1049 case Primitive::kPrimInt:
1050 case Primitive::kPrimNot:
1051 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001052 case Primitive::kPrimFloat:
1053 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001054 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001055 __ Ldr(dst, src);
1056 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001057 case Primitive::kPrimVoid:
1058 LOG(FATAL) << "Unreachable type " << type;
1059 }
1060}
1061
Calin Juravle77520bc2015-01-12 18:45:46 +00001062void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001063 CPURegister dst,
1064 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001065 MacroAssembler* masm = GetVIXLAssembler();
1066 BlockPoolsScope block_pools(masm);
1067 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001068 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001069 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001070
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001071 DCHECK(!src.IsPreIndex());
1072 DCHECK(!src.IsPostIndex());
1073
1074 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001075 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001076 MemOperand base = MemOperand(temp_base);
1077 switch (type) {
1078 case Primitive::kPrimBoolean:
1079 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001080 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001081 break;
1082 case Primitive::kPrimByte:
1083 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001084 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001085 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1086 break;
1087 case Primitive::kPrimChar:
1088 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001089 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001090 break;
1091 case Primitive::kPrimShort:
1092 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001093 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001094 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1095 break;
1096 case Primitive::kPrimInt:
1097 case Primitive::kPrimNot:
1098 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001099 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001100 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001101 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001102 break;
1103 case Primitive::kPrimFloat:
1104 case Primitive::kPrimDouble: {
1105 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001106 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001107
1108 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1109 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001110 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001111 __ Fmov(FPRegister(dst), temp);
1112 break;
1113 }
1114 case Primitive::kPrimVoid:
1115 LOG(FATAL) << "Unreachable type " << type;
1116 }
1117}
1118
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001119void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001120 CPURegister src,
1121 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001122 switch (type) {
1123 case Primitive::kPrimBoolean:
1124 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001126 break;
1127 case Primitive::kPrimChar:
1128 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001129 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001130 break;
1131 case Primitive::kPrimInt:
1132 case Primitive::kPrimNot:
1133 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001134 case Primitive::kPrimFloat:
1135 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001136 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001137 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001138 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001139 case Primitive::kPrimVoid:
1140 LOG(FATAL) << "Unreachable type " << type;
1141 }
1142}
1143
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001144void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1145 CPURegister src,
1146 const MemOperand& dst) {
1147 UseScratchRegisterScope temps(GetVIXLAssembler());
1148 Register temp_base = temps.AcquireX();
1149
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001150 DCHECK(!dst.IsPreIndex());
1151 DCHECK(!dst.IsPostIndex());
1152
1153 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001154 Operand op = OperandFromMemOperand(dst);
1155 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001156 MemOperand base = MemOperand(temp_base);
1157 switch (type) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 __ Stlrb(Register(src), base);
1161 break;
1162 case Primitive::kPrimChar:
1163 case Primitive::kPrimShort:
1164 __ Stlrh(Register(src), base);
1165 break;
1166 case Primitive::kPrimInt:
1167 case Primitive::kPrimNot:
1168 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001169 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001170 __ Stlr(Register(src), base);
1171 break;
1172 case Primitive::kPrimFloat:
1173 case Primitive::kPrimDouble: {
1174 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001175 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001176
1177 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1178 __ Fmov(temp, FPRegister(src));
1179 __ Stlr(temp, base);
1180 break;
1181 }
1182 case Primitive::kPrimVoid:
1183 LOG(FATAL) << "Unreachable type " << type;
1184 }
1185}
1186
Calin Juravle175dc732015-08-25 15:42:32 +01001187void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1188 HInstruction* instruction,
1189 uint32_t dex_pc,
1190 SlowPathCode* slow_path) {
1191 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1192 instruction,
1193 dex_pc,
1194 slow_path);
1195}
1196
Alexandre Rames67555f72014-11-18 10:55:16 +00001197void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1198 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001199 uint32_t dex_pc,
1200 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001201 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001202 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001203 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1204 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001205 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001206}
1207
1208void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1209 vixl::Register class_reg) {
1210 UseScratchRegisterScope temps(GetVIXLAssembler());
1211 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001212 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001213 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001214
Serban Constantinescu02164b32014-11-13 14:05:07 +00001215 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001216 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001217 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1218 __ Add(temp, class_reg, status_offset);
1219 __ Ldar(temp, HeapOperand(temp));
1220 __ Cmp(temp, mirror::Class::kStatusInitialized);
1221 __ B(lt, slow_path->GetEntryLabel());
1222 } else {
1223 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1224 __ Cmp(temp, mirror::Class::kStatusInitialized);
1225 __ B(lt, slow_path->GetEntryLabel());
1226 __ Dmb(InnerShareable, BarrierReads);
1227 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 __ Bind(slow_path->GetExitLabel());
1229}
Alexandre Rames5319def2014-10-23 10:03:10 +01001230
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001231void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1232 BarrierType type = BarrierAll;
1233
1234 switch (kind) {
1235 case MemBarrierKind::kAnyAny:
1236 case MemBarrierKind::kAnyStore: {
1237 type = BarrierAll;
1238 break;
1239 }
1240 case MemBarrierKind::kLoadAny: {
1241 type = BarrierReads;
1242 break;
1243 }
1244 case MemBarrierKind::kStoreStore: {
1245 type = BarrierWrites;
1246 break;
1247 }
1248 default:
1249 LOG(FATAL) << "Unexpected memory barrier " << kind;
1250 }
1251 __ Dmb(InnerShareable, type);
1252}
1253
Serban Constantinescu02164b32014-11-13 14:05:07 +00001254void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1255 HBasicBlock* successor) {
1256 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001257 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1258 if (slow_path == nullptr) {
1259 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1260 instruction->SetSlowPath(slow_path);
1261 codegen_->AddSlowPath(slow_path);
1262 if (successor != nullptr) {
1263 DCHECK(successor->IsLoopHeader());
1264 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1265 }
1266 } else {
1267 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1268 }
1269
Serban Constantinescu02164b32014-11-13 14:05:07 +00001270 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1271 Register temp = temps.AcquireW();
1272
1273 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1274 if (successor == nullptr) {
1275 __ Cbnz(temp, slow_path->GetEntryLabel());
1276 __ Bind(slow_path->GetReturnLabel());
1277 } else {
1278 __ Cbz(temp, codegen_->GetLabelOf(successor));
1279 __ B(slow_path->GetEntryLabel());
1280 // slow_path will return to GetLabelOf(successor).
1281 }
1282}
1283
Alexandre Rames5319def2014-10-23 10:03:10 +01001284InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1285 CodeGeneratorARM64* codegen)
1286 : HGraphVisitor(graph),
1287 assembler_(codegen->GetAssembler()),
1288 codegen_(codegen) {}
1289
1290#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001291 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001292
1293#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1294
1295enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001296 // Using a base helps identify when we hit such breakpoints.
1297 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001298#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1299 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1300#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1301};
1302
1303#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
1304 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001305 UNUSED(instr); \
Alexandre Rames5319def2014-10-23 10:03:10 +01001306 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1307 } \
1308 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1310 locations->SetOut(Location::Any()); \
1311 }
1312 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1313#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1314
1315#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001316#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001317
Alexandre Rames67555f72014-11-18 10:55:16 +00001318void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001319 DCHECK_EQ(instr->InputCount(), 2U);
1320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1321 Primitive::Type type = instr->GetResultType();
1322 switch (type) {
1323 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001324 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001325 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001326 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001328 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001329
1330 case Primitive::kPrimFloat:
1331 case Primitive::kPrimDouble:
1332 locations->SetInAt(0, Location::RequiresFpuRegister());
1333 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001335 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001336
Alexandre Rames5319def2014-10-23 10:03:10 +01001337 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001338 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001339 }
1340}
1341
Alexandre Rames09a99962015-04-15 11:47:56 +01001342void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1343 LocationSummary* locations =
1344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1345 locations->SetInAt(0, Location::RequiresRegister());
1346 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1347 locations->SetOut(Location::RequiresFpuRegister());
1348 } else {
1349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1350 }
1351}
1352
1353void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1354 const FieldInfo& field_info) {
1355 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001356 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001357 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001358
1359 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1360 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1361
1362 if (field_info.IsVolatile()) {
1363 if (use_acquire_release) {
1364 // NB: LoadAcquire will record the pc info if needed.
1365 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1366 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001367 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001368 codegen_->MaybeRecordImplicitNullCheck(instruction);
1369 // For IRIW sequential consistency kLoadAny is not sufficient.
1370 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1371 }
1372 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001373 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001374 codegen_->MaybeRecordImplicitNullCheck(instruction);
1375 }
Roland Levillain4d027112015-07-01 15:41:14 +01001376
1377 if (field_type == Primitive::kPrimNot) {
1378 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1379 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001380}
1381
1382void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1383 LocationSummary* locations =
1384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1385 locations->SetInAt(0, Location::RequiresRegister());
1386 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1387 locations->SetInAt(1, Location::RequiresFpuRegister());
1388 } else {
1389 locations->SetInAt(1, Location::RequiresRegister());
1390 }
1391}
1392
1393void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001394 const FieldInfo& field_info,
1395 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001396 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001397 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001398
1399 Register obj = InputRegisterAt(instruction, 0);
1400 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001401 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001402 Offset offset = field_info.GetFieldOffset();
1403 Primitive::Type field_type = field_info.GetFieldType();
1404 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1405
Roland Levillain4d027112015-07-01 15:41:14 +01001406 {
1407 // We use a block to end the scratch scope before the write barrier, thus
1408 // freeing the temporary registers so they can be used in `MarkGCCard`.
1409 UseScratchRegisterScope temps(GetVIXLAssembler());
1410
1411 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1412 DCHECK(value.IsW());
1413 Register temp = temps.AcquireW();
1414 __ Mov(temp, value.W());
1415 GetAssembler()->PoisonHeapReference(temp.W());
1416 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001417 }
Roland Levillain4d027112015-07-01 15:41:14 +01001418
1419 if (field_info.IsVolatile()) {
1420 if (use_acquire_release) {
1421 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1422 codegen_->MaybeRecordImplicitNullCheck(instruction);
1423 } else {
1424 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1425 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1426 codegen_->MaybeRecordImplicitNullCheck(instruction);
1427 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1428 }
1429 } else {
1430 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1431 codegen_->MaybeRecordImplicitNullCheck(instruction);
1432 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001433 }
1434
1435 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001436 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001437 }
1438}
1439
Alexandre Rames67555f72014-11-18 10:55:16 +00001440void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001441 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001442
1443 switch (type) {
1444 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001445 case Primitive::kPrimLong: {
1446 Register dst = OutputRegister(instr);
1447 Register lhs = InputRegisterAt(instr, 0);
1448 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001449 if (instr->IsAdd()) {
1450 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001451 } else if (instr->IsAnd()) {
1452 __ And(dst, lhs, rhs);
1453 } else if (instr->IsOr()) {
1454 __ Orr(dst, lhs, rhs);
1455 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001456 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001457 } else {
1458 DCHECK(instr->IsXor());
1459 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001460 }
1461 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001462 }
1463 case Primitive::kPrimFloat:
1464 case Primitive::kPrimDouble: {
1465 FPRegister dst = OutputFPRegister(instr);
1466 FPRegister lhs = InputFPRegisterAt(instr, 0);
1467 FPRegister rhs = InputFPRegisterAt(instr, 1);
1468 if (instr->IsAdd()) {
1469 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001470 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001471 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001472 } else {
1473 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001474 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001475 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001476 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001477 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001478 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001479 }
1480}
1481
Serban Constantinescu02164b32014-11-13 14:05:07 +00001482void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1483 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1484
1485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1486 Primitive::Type type = instr->GetResultType();
1487 switch (type) {
1488 case Primitive::kPrimInt:
1489 case Primitive::kPrimLong: {
1490 locations->SetInAt(0, Location::RequiresRegister());
1491 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1492 locations->SetOut(Location::RequiresRegister());
1493 break;
1494 }
1495 default:
1496 LOG(FATAL) << "Unexpected shift type " << type;
1497 }
1498}
1499
1500void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1501 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1502
1503 Primitive::Type type = instr->GetType();
1504 switch (type) {
1505 case Primitive::kPrimInt:
1506 case Primitive::kPrimLong: {
1507 Register dst = OutputRegister(instr);
1508 Register lhs = InputRegisterAt(instr, 0);
1509 Operand rhs = InputOperandAt(instr, 1);
1510 if (rhs.IsImmediate()) {
1511 uint32_t shift_value = (type == Primitive::kPrimInt)
1512 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1513 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1514 if (instr->IsShl()) {
1515 __ Lsl(dst, lhs, shift_value);
1516 } else if (instr->IsShr()) {
1517 __ Asr(dst, lhs, shift_value);
1518 } else {
1519 __ Lsr(dst, lhs, shift_value);
1520 }
1521 } else {
1522 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1523
1524 if (instr->IsShl()) {
1525 __ Lsl(dst, lhs, rhs_reg);
1526 } else if (instr->IsShr()) {
1527 __ Asr(dst, lhs, rhs_reg);
1528 } else {
1529 __ Lsr(dst, lhs, rhs_reg);
1530 }
1531 }
1532 break;
1533 }
1534 default:
1535 LOG(FATAL) << "Unexpected shift operation type " << type;
1536 }
1537}
1538
Alexandre Rames5319def2014-10-23 10:03:10 +01001539void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001540 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001541}
1542
1543void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001544 HandleBinaryOp(instruction);
1545}
1546
1547void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1548 HandleBinaryOp(instruction);
1549}
1550
1551void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1552 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001553}
1554
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001555void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1556 LocationSummary* locations =
1557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001560 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1561 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1562 } else {
1563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1564 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001565}
1566
1567void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
1568 LocationSummary* locations = instruction->GetLocations();
1569 Primitive::Type type = instruction->GetType();
1570 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001571 Location index = locations->InAt(1);
1572 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001573 MemOperand source = HeapOperand(obj);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001574 MacroAssembler* masm = GetVIXLAssembler();
1575 UseScratchRegisterScope temps(masm);
1576 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577
1578 if (index.IsConstant()) {
1579 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001580 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001581 } else {
1582 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Rames82000b02015-07-07 11:34:16 +01001583 __ Add(temp, obj, offset);
1584 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001585 }
1586
Alexandre Rames67555f72014-11-18 10:55:16 +00001587 codegen_->Load(type, OutputCPURegister(instruction), source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001588 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001589
1590 if (type == Primitive::kPrimNot) {
1591 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1592 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593}
1594
Alexandre Rames5319def2014-10-23 10:03:10 +01001595void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1597 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001599}
1600
1601void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001602 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001603 __ Ldr(OutputRegister(instruction),
1604 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001605 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001606}
1607
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001608void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1610 instruction,
1611 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1614 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1615 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001616 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001617 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001618 }
1619}
1620
1621void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1622 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001623 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001624 bool may_need_runtime_call = locations->CanCall();
1625 bool needs_write_barrier =
1626 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001627
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001628 Register array = InputRegisterAt(instruction, 0);
1629 CPURegister value = InputCPURegisterAt(instruction, 2);
1630 CPURegister source = value;
1631 Location index = locations->InAt(1);
1632 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1633 MemOperand destination = HeapOperand(array);
1634 MacroAssembler* masm = GetVIXLAssembler();
1635 BlockPoolsScope block_pools(masm);
1636
1637 if (!needs_write_barrier) {
1638 DCHECK(!may_need_runtime_call);
1639 if (index.IsConstant()) {
1640 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1641 destination = HeapOperand(array, offset);
1642 } else {
1643 UseScratchRegisterScope temps(masm);
1644 Register temp = temps.AcquireSameSizeAs(array);
1645 __ Add(temp, array, offset);
1646 destination = HeapOperand(temp,
1647 XRegisterFrom(index),
1648 LSL,
1649 Primitive::ComponentSizeShift(value_type));
1650 }
1651 codegen_->Store(value_type, value, destination);
1652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001653 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001654 DCHECK(needs_write_barrier);
1655 vixl::Label done;
1656 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001657 {
1658 // We use a block to end the scratch scope before the write barrier, thus
1659 // freeing the temporary registers so they can be used in `MarkGCCard`.
1660 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001661 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001662 if (index.IsConstant()) {
1663 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001664 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001665 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001666 destination = HeapOperand(temp,
1667 XRegisterFrom(index),
1668 LSL,
1669 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001670 }
1671
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001672 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1673 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1674 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1675
1676 if (may_need_runtime_call) {
1677 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1678 codegen_->AddSlowPath(slow_path);
1679 if (instruction->GetValueCanBeNull()) {
1680 vixl::Label non_zero;
1681 __ Cbnz(Register(value), &non_zero);
1682 if (!index.IsConstant()) {
1683 __ Add(temp, array, offset);
1684 }
1685 __ Str(wzr, destination);
1686 codegen_->MaybeRecordImplicitNullCheck(instruction);
1687 __ B(&done);
1688 __ Bind(&non_zero);
1689 }
1690
1691 Register temp2 = temps.AcquireSameSizeAs(array);
1692 __ Ldr(temp, HeapOperand(array, class_offset));
1693 codegen_->MaybeRecordImplicitNullCheck(instruction);
1694 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1695 __ Ldr(temp, HeapOperand(temp, component_offset));
1696 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1697 // No need to poison/unpoison, we're comparing two poisoned references.
1698 __ Cmp(temp, temp2);
1699 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1700 vixl::Label do_put;
1701 __ B(eq, &do_put);
1702 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1703 __ Ldr(temp, HeapOperand(temp, super_offset));
1704 // No need to unpoison, we're comparing against null.
1705 __ Cbnz(temp, slow_path->GetEntryLabel());
1706 __ Bind(&do_put);
1707 } else {
1708 __ B(ne, slow_path->GetEntryLabel());
1709 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001710 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001711 }
1712
1713 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001714 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001715 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001716 __ Mov(temp2, value.W());
1717 GetAssembler()->PoisonHeapReference(temp2);
1718 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001719 }
1720
1721 if (!index.IsConstant()) {
1722 __ Add(temp, array, offset);
1723 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001724 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001725
1726 if (!may_need_runtime_call) {
1727 codegen_->MaybeRecordImplicitNullCheck(instruction);
1728 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001729 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001730
1731 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1732
1733 if (done.IsLinked()) {
1734 __ Bind(&done);
1735 }
1736
1737 if (slow_path != nullptr) {
1738 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001739 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001740 }
1741}
1742
Alexandre Rames67555f72014-11-18 10:55:16 +00001743void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001744 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1745 ? LocationSummary::kCallOnSlowPath
1746 : LocationSummary::kNoCall;
1747 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001749 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001750 if (instruction->HasUses()) {
1751 locations->SetOut(Location::SameAsFirstInput());
1752 }
1753}
1754
1755void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001756 BoundsCheckSlowPathARM64* slow_path =
1757 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001758 codegen_->AddSlowPath(slow_path);
1759
1760 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1761 __ B(slow_path->GetEntryLabel(), hs);
1762}
1763
Alexandre Rames67555f72014-11-18 10:55:16 +00001764void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1765 LocationSummary* locations =
1766 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1767 locations->SetInAt(0, Location::RequiresRegister());
1768 if (check->HasUses()) {
1769 locations->SetOut(Location::SameAsFirstInput());
1770 }
1771}
1772
1773void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1774 // We assume the class is not null.
1775 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1776 check->GetLoadClass(), check, check->GetDexPc(), true);
1777 codegen_->AddSlowPath(slow_path);
1778 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1779}
1780
Roland Levillain7f63c522015-07-13 15:54:55 +00001781static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1782 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1783 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1784}
1785
Serban Constantinescu02164b32014-11-13 14:05:07 +00001786void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001787 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001788 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1789 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001790 switch (in_type) {
1791 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001793 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1795 break;
1796 }
1797 case Primitive::kPrimFloat:
1798 case Primitive::kPrimDouble: {
1799 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001800 locations->SetInAt(1,
1801 IsFloatingPointZeroConstant(compare->InputAt(1))
1802 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1803 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001804 locations->SetOut(Location::RequiresRegister());
1805 break;
1806 }
1807 default:
1808 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1809 }
1810}
1811
1812void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1813 Primitive::Type in_type = compare->InputAt(0)->GetType();
1814
1815 // 0 if: left == right
1816 // 1 if: left > right
1817 // -1 if: left < right
1818 switch (in_type) {
1819 case Primitive::kPrimLong: {
1820 Register result = OutputRegister(compare);
1821 Register left = InputRegisterAt(compare, 0);
1822 Operand right = InputOperandAt(compare, 1);
1823
1824 __ Cmp(left, right);
1825 __ Cset(result, ne);
1826 __ Cneg(result, result, lt);
1827 break;
1828 }
1829 case Primitive::kPrimFloat:
1830 case Primitive::kPrimDouble: {
1831 Register result = OutputRegister(compare);
1832 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001833 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001834 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1835 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001836 __ Fcmp(left, 0.0);
1837 } else {
1838 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1839 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001840 if (compare->IsGtBias()) {
1841 __ Cset(result, ne);
1842 } else {
1843 __ Csetm(result, ne);
1844 }
1845 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001846 break;
1847 }
1848 default:
1849 LOG(FATAL) << "Unimplemented compare type " << in_type;
1850 }
1851}
1852
1853void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1854 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001855
1856 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1857 locations->SetInAt(0, Location::RequiresFpuRegister());
1858 locations->SetInAt(1,
1859 IsFloatingPointZeroConstant(instruction->InputAt(1))
1860 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1861 : Location::RequiresFpuRegister());
1862 } else {
1863 // Integer cases.
1864 locations->SetInAt(0, Location::RequiresRegister());
1865 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1866 }
1867
Alexandre Rames5319def2014-10-23 10:03:10 +01001868 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001870 }
1871}
1872
1873void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1874 if (!instruction->NeedsMaterialization()) {
1875 return;
1876 }
1877
1878 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001879 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001880 IfCondition if_cond = instruction->GetCondition();
1881 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001882
Roland Levillain7f63c522015-07-13 15:54:55 +00001883 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1884 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1885 if (locations->InAt(1).IsConstant()) {
1886 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1887 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1888 __ Fcmp(lhs, 0.0);
1889 } else {
1890 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1891 }
1892 __ Cset(res, arm64_cond);
1893 if (instruction->IsFPConditionTrueIfNaN()) {
1894 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1895 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1896 } else if (instruction->IsFPConditionFalseIfNaN()) {
1897 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1898 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1899 }
1900 } else {
1901 // Integer cases.
1902 Register lhs = InputRegisterAt(instruction, 0);
1903 Operand rhs = InputOperandAt(instruction, 1);
1904 __ Cmp(lhs, rhs);
1905 __ Cset(res, arm64_cond);
1906 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001907}
1908
1909#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1910 M(Equal) \
1911 M(NotEqual) \
1912 M(LessThan) \
1913 M(LessThanOrEqual) \
1914 M(GreaterThan) \
1915 M(GreaterThanOrEqual)
1916#define DEFINE_CONDITION_VISITORS(Name) \
1917void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1918void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1919FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001920#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001921#undef FOR_EACH_CONDITION_INSTRUCTION
1922
Zheng Xuc6667102015-05-15 16:08:45 +08001923void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1924 DCHECK(instruction->IsDiv() || instruction->IsRem());
1925
1926 LocationSummary* locations = instruction->GetLocations();
1927 Location second = locations->InAt(1);
1928 DCHECK(second.IsConstant());
1929
1930 Register out = OutputRegister(instruction);
1931 Register dividend = InputRegisterAt(instruction, 0);
1932 int64_t imm = Int64FromConstant(second.GetConstant());
1933 DCHECK(imm == 1 || imm == -1);
1934
1935 if (instruction->IsRem()) {
1936 __ Mov(out, 0);
1937 } else {
1938 if (imm == 1) {
1939 __ Mov(out, dividend);
1940 } else {
1941 __ Neg(out, dividend);
1942 }
1943 }
1944}
1945
1946void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1947 DCHECK(instruction->IsDiv() || instruction->IsRem());
1948
1949 LocationSummary* locations = instruction->GetLocations();
1950 Location second = locations->InAt(1);
1951 DCHECK(second.IsConstant());
1952
1953 Register out = OutputRegister(instruction);
1954 Register dividend = InputRegisterAt(instruction, 0);
1955 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001956 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001957 DCHECK(IsPowerOfTwo(abs_imm));
1958 int ctz_imm = CTZ(abs_imm);
1959
1960 UseScratchRegisterScope temps(GetVIXLAssembler());
1961 Register temp = temps.AcquireSameSizeAs(out);
1962
1963 if (instruction->IsDiv()) {
1964 __ Add(temp, dividend, abs_imm - 1);
1965 __ Cmp(dividend, 0);
1966 __ Csel(out, temp, dividend, lt);
1967 if (imm > 0) {
1968 __ Asr(out, out, ctz_imm);
1969 } else {
1970 __ Neg(out, Operand(out, ASR, ctz_imm));
1971 }
1972 } else {
1973 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1974 __ Asr(temp, dividend, bits - 1);
1975 __ Lsr(temp, temp, bits - ctz_imm);
1976 __ Add(out, dividend, temp);
1977 __ And(out, out, abs_imm - 1);
1978 __ Sub(out, out, temp);
1979 }
1980}
1981
1982void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1983 DCHECK(instruction->IsDiv() || instruction->IsRem());
1984
1985 LocationSummary* locations = instruction->GetLocations();
1986 Location second = locations->InAt(1);
1987 DCHECK(second.IsConstant());
1988
1989 Register out = OutputRegister(instruction);
1990 Register dividend = InputRegisterAt(instruction, 0);
1991 int64_t imm = Int64FromConstant(second.GetConstant());
1992
1993 Primitive::Type type = instruction->GetResultType();
1994 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1995
1996 int64_t magic;
1997 int shift;
1998 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1999
2000 UseScratchRegisterScope temps(GetVIXLAssembler());
2001 Register temp = temps.AcquireSameSizeAs(out);
2002
2003 // temp = get_high(dividend * magic)
2004 __ Mov(temp, magic);
2005 if (type == Primitive::kPrimLong) {
2006 __ Smulh(temp, dividend, temp);
2007 } else {
2008 __ Smull(temp.X(), dividend, temp);
2009 __ Lsr(temp.X(), temp.X(), 32);
2010 }
2011
2012 if (imm > 0 && magic < 0) {
2013 __ Add(temp, temp, dividend);
2014 } else if (imm < 0 && magic > 0) {
2015 __ Sub(temp, temp, dividend);
2016 }
2017
2018 if (shift != 0) {
2019 __ Asr(temp, temp, shift);
2020 }
2021
2022 if (instruction->IsDiv()) {
2023 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2024 } else {
2025 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2026 // TODO: Strength reduction for msub.
2027 Register temp_imm = temps.AcquireSameSizeAs(out);
2028 __ Mov(temp_imm, imm);
2029 __ Msub(out, temp, temp_imm, dividend);
2030 }
2031}
2032
2033void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2034 DCHECK(instruction->IsDiv() || instruction->IsRem());
2035 Primitive::Type type = instruction->GetResultType();
2036 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2037
2038 LocationSummary* locations = instruction->GetLocations();
2039 Register out = OutputRegister(instruction);
2040 Location second = locations->InAt(1);
2041
2042 if (second.IsConstant()) {
2043 int64_t imm = Int64FromConstant(second.GetConstant());
2044
2045 if (imm == 0) {
2046 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2047 } else if (imm == 1 || imm == -1) {
2048 DivRemOneOrMinusOne(instruction);
2049 } else if (IsPowerOfTwo(std::abs(imm))) {
2050 DivRemByPowerOfTwo(instruction);
2051 } else {
2052 DCHECK(imm <= -2 || imm >= 2);
2053 GenerateDivRemWithAnyConstant(instruction);
2054 }
2055 } else {
2056 Register dividend = InputRegisterAt(instruction, 0);
2057 Register divisor = InputRegisterAt(instruction, 1);
2058 if (instruction->IsDiv()) {
2059 __ Sdiv(out, dividend, divisor);
2060 } else {
2061 UseScratchRegisterScope temps(GetVIXLAssembler());
2062 Register temp = temps.AcquireSameSizeAs(out);
2063 __ Sdiv(temp, dividend, divisor);
2064 __ Msub(out, temp, divisor, dividend);
2065 }
2066 }
2067}
2068
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002069void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2070 LocationSummary* locations =
2071 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2072 switch (div->GetResultType()) {
2073 case Primitive::kPrimInt:
2074 case Primitive::kPrimLong:
2075 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002076 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2078 break;
2079
2080 case Primitive::kPrimFloat:
2081 case Primitive::kPrimDouble:
2082 locations->SetInAt(0, Location::RequiresFpuRegister());
2083 locations->SetInAt(1, Location::RequiresFpuRegister());
2084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2089 }
2090}
2091
2092void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2093 Primitive::Type type = div->GetResultType();
2094 switch (type) {
2095 case Primitive::kPrimInt:
2096 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002097 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002098 break;
2099
2100 case Primitive::kPrimFloat:
2101 case Primitive::kPrimDouble:
2102 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2103 break;
2104
2105 default:
2106 LOG(FATAL) << "Unexpected div type " << type;
2107 }
2108}
2109
Alexandre Rames67555f72014-11-18 10:55:16 +00002110void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002111 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2112 ? LocationSummary::kCallOnSlowPath
2113 : LocationSummary::kNoCall;
2114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002115 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2116 if (instruction->HasUses()) {
2117 locations->SetOut(Location::SameAsFirstInput());
2118 }
2119}
2120
2121void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2122 SlowPathCodeARM64* slow_path =
2123 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2124 codegen_->AddSlowPath(slow_path);
2125 Location value = instruction->GetLocations()->InAt(0);
2126
Alexandre Rames3e69f162014-12-10 10:36:50 +00002127 Primitive::Type type = instruction->GetType();
2128
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002129 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2130 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002131 return;
2132 }
2133
Alexandre Rames67555f72014-11-18 10:55:16 +00002134 if (value.IsConstant()) {
2135 int64_t divisor = Int64ConstantFrom(value);
2136 if (divisor == 0) {
2137 __ B(slow_path->GetEntryLabel());
2138 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002139 // A division by a non-null constant is valid. We don't need to perform
2140 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002141 }
2142 } else {
2143 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2144 }
2145}
2146
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002147void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2148 LocationSummary* locations =
2149 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2150 locations->SetOut(Location::ConstantLocation(constant));
2151}
2152
2153void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2154 UNUSED(constant);
2155 // Will be generated at use site.
2156}
2157
Alexandre Rames5319def2014-10-23 10:03:10 +01002158void LocationsBuilderARM64::VisitExit(HExit* exit) {
2159 exit->SetLocations(nullptr);
2160}
2161
2162void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002163 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002164}
2165
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002166void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2169 locations->SetOut(Location::ConstantLocation(constant));
2170}
2171
2172void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2173 UNUSED(constant);
2174 // Will be generated at use site.
2175}
2176
David Brazdilfc6a86a2015-06-26 10:33:45 +00002177void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002178 DCHECK(!successor->IsExitBlock());
2179 HBasicBlock* block = got->GetBlock();
2180 HInstruction* previous = got->GetPrevious();
2181 HLoopInformation* info = block->GetLoopInformation();
2182
David Brazdil46e2a392015-03-16 17:31:52 +00002183 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002184 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2185 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2186 return;
2187 }
2188 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2189 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2190 }
2191 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002192 __ B(codegen_->GetLabelOf(successor));
2193 }
2194}
2195
David Brazdilfc6a86a2015-06-26 10:33:45 +00002196void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2197 got->SetLocations(nullptr);
2198}
2199
2200void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2201 HandleGoto(got, got->GetSuccessor());
2202}
2203
2204void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2205 try_boundary->SetLocations(nullptr);
2206}
2207
2208void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2209 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2210 if (!successor->IsExitBlock()) {
2211 HandleGoto(try_boundary, successor);
2212 }
2213}
2214
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002215void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2216 vixl::Label* true_target,
2217 vixl::Label* false_target,
2218 vixl::Label* always_true_target) {
2219 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002220 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002221
Serban Constantinescu02164b32014-11-13 14:05:07 +00002222 if (cond->IsIntConstant()) {
2223 int32_t cond_value = cond->AsIntConstant()->GetValue();
2224 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002225 if (always_true_target != nullptr) {
2226 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002227 }
2228 return;
2229 } else {
2230 DCHECK_EQ(cond_value, 0);
2231 }
2232 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002233 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002234 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002235 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002236 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002237 } else {
2238 // The condition instruction has not been materialized, use its inputs as
2239 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002240 Primitive::Type type =
2241 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2242
2243 if (Primitive::IsFloatingPointType(type)) {
2244 // FP compares don't like null false_targets.
2245 if (false_target == nullptr) {
2246 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002247 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002248 FPRegister lhs = InputFPRegisterAt(condition, 0);
2249 if (condition->GetLocations()->InAt(1).IsConstant()) {
2250 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2251 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2252 __ Fcmp(lhs, 0.0);
2253 } else {
2254 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2255 }
2256 if (condition->IsFPConditionTrueIfNaN()) {
2257 __ B(vs, true_target); // VS for unordered.
2258 } else if (condition->IsFPConditionFalseIfNaN()) {
2259 __ B(vs, false_target); // VS for unordered.
2260 }
2261 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002262 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002263 // Integer cases.
2264 Register lhs = InputRegisterAt(condition, 0);
2265 Operand rhs = InputOperandAt(condition, 1);
2266 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2267 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2268 switch (arm64_cond) {
2269 case eq:
2270 __ Cbz(lhs, true_target);
2271 break;
2272 case ne:
2273 __ Cbnz(lhs, true_target);
2274 break;
2275 case lt:
2276 // Test the sign bit and branch accordingly.
2277 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2278 break;
2279 case ge:
2280 // Test the sign bit and branch accordingly.
2281 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2282 break;
2283 default:
2284 // Without the `static_cast` the compiler throws an error for
2285 // `-Werror=sign-promo`.
2286 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2287 }
2288 } else {
2289 __ Cmp(lhs, rhs);
2290 __ B(arm64_cond, true_target);
2291 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002292 }
2293 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002294 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002295 __ B(false_target);
2296 }
2297}
2298
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002299void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2301 HInstruction* cond = if_instr->InputAt(0);
2302 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2303 locations->SetInAt(0, Location::RequiresRegister());
2304 }
2305}
2306
2307void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2308 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2309 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2310 vixl::Label* always_true_target = true_target;
2311 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2312 if_instr->IfTrueSuccessor())) {
2313 always_true_target = nullptr;
2314 }
2315 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2316 if_instr->IfFalseSuccessor())) {
2317 false_target = nullptr;
2318 }
2319 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2320}
2321
2322void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2323 LocationSummary* locations = new (GetGraph()->GetArena())
2324 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2325 HInstruction* cond = deoptimize->InputAt(0);
2326 DCHECK(cond->IsCondition());
2327 if (cond->AsCondition()->NeedsMaterialization()) {
2328 locations->SetInAt(0, Location::RequiresRegister());
2329 }
2330}
2331
2332void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2333 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2334 DeoptimizationSlowPathARM64(deoptimize);
2335 codegen_->AddSlowPath(slow_path);
2336 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2337 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2338}
2339
Alexandre Rames5319def2014-10-23 10:03:10 +01002340void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002342}
2343
2344void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002345 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002346}
2347
2348void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002349 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002350}
2351
2352void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002353 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002354}
2355
Alexandre Rames67555f72014-11-18 10:55:16 +00002356void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002357 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2358 switch (instruction->GetTypeCheckKind()) {
2359 case TypeCheckKind::kExactCheck:
2360 case TypeCheckKind::kAbstractClassCheck:
2361 case TypeCheckKind::kClassHierarchyCheck:
2362 case TypeCheckKind::kArrayObjectCheck:
2363 call_kind = LocationSummary::kNoCall;
2364 break;
2365 case TypeCheckKind::kInterfaceCheck:
2366 call_kind = LocationSummary::kCall;
2367 break;
2368 case TypeCheckKind::kArrayCheck:
2369 call_kind = LocationSummary::kCallOnSlowPath;
2370 break;
2371 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002373 if (call_kind != LocationSummary::kCall) {
2374 locations->SetInAt(0, Location::RequiresRegister());
2375 locations->SetInAt(1, Location::RequiresRegister());
2376 // The out register is used as a temporary, so it overlaps with the inputs.
2377 // Note that TypeCheckSlowPathARM64 uses this register too.
2378 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2379 } else {
2380 InvokeRuntimeCallingConvention calling_convention;
2381 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2382 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2383 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2384 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002385}
2386
2387void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2388 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002389 Register obj = InputRegisterAt(instruction, 0);
2390 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002391 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002392 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2393 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2394 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2395 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002397 vixl::Label done, zero;
2398 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002399
2400 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002401 // Avoid null check if we know `obj` is not null.
2402 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002403 __ Cbz(obj, &zero);
2404 }
2405
2406 // In case of an interface check, we put the object class into the object register.
2407 // This is safe, as the register is caller-save, and the object must be in another
2408 // register if it survives the runtime call.
2409 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2410 ? obj
2411 : out;
2412 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2413 GetAssembler()->MaybeUnpoisonHeapReference(target);
2414
2415 switch (instruction->GetTypeCheckKind()) {
2416 case TypeCheckKind::kExactCheck: {
2417 __ Cmp(out, cls);
2418 __ Cset(out, eq);
2419 if (zero.IsLinked()) {
2420 __ B(&done);
2421 }
2422 break;
2423 }
2424 case TypeCheckKind::kAbstractClassCheck: {
2425 // If the class is abstract, we eagerly fetch the super class of the
2426 // object to avoid doing a comparison we know will fail.
2427 vixl::Label loop, success;
2428 __ Bind(&loop);
2429 __ Ldr(out, HeapOperand(out, super_offset));
2430 GetAssembler()->MaybeUnpoisonHeapReference(out);
2431 // If `out` is null, we use it for the result, and jump to `done`.
2432 __ Cbz(out, &done);
2433 __ Cmp(out, cls);
2434 __ B(ne, &loop);
2435 __ Mov(out, 1);
2436 if (zero.IsLinked()) {
2437 __ B(&done);
2438 }
2439 break;
2440 }
2441 case TypeCheckKind::kClassHierarchyCheck: {
2442 // Walk over the class hierarchy to find a match.
2443 vixl::Label loop, success;
2444 __ Bind(&loop);
2445 __ Cmp(out, cls);
2446 __ B(eq, &success);
2447 __ Ldr(out, HeapOperand(out, super_offset));
2448 GetAssembler()->MaybeUnpoisonHeapReference(out);
2449 __ Cbnz(out, &loop);
2450 // If `out` is null, we use it for the result, and jump to `done`.
2451 __ B(&done);
2452 __ Bind(&success);
2453 __ Mov(out, 1);
2454 if (zero.IsLinked()) {
2455 __ B(&done);
2456 }
2457 break;
2458 }
2459 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002460 // Do an exact check.
2461 vixl::Label exact_check;
2462 __ Cmp(out, cls);
2463 __ B(eq, &exact_check);
2464 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002465 __ Ldr(out, HeapOperand(out, component_offset));
2466 GetAssembler()->MaybeUnpoisonHeapReference(out);
2467 // If `out` is null, we use it for the result, and jump to `done`.
2468 __ Cbz(out, &done);
2469 __ Ldrh(out, HeapOperand(out, primitive_offset));
2470 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2471 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002472 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002473 __ Mov(out, 1);
2474 __ B(&done);
2475 break;
2476 }
2477 case TypeCheckKind::kArrayCheck: {
2478 __ Cmp(out, cls);
2479 DCHECK(locations->OnlyCallsOnSlowPath());
2480 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2481 instruction, /* is_fatal */ false);
2482 codegen_->AddSlowPath(slow_path);
2483 __ B(ne, slow_path->GetEntryLabel());
2484 __ Mov(out, 1);
2485 if (zero.IsLinked()) {
2486 __ B(&done);
2487 }
2488 break;
2489 }
2490
2491 case TypeCheckKind::kInterfaceCheck:
2492 default: {
2493 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2494 instruction,
2495 instruction->GetDexPc(),
2496 nullptr);
2497 if (zero.IsLinked()) {
2498 __ B(&done);
2499 }
2500 break;
2501 }
2502 }
2503
2504 if (zero.IsLinked()) {
2505 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002506 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002507 }
2508
2509 if (done.IsLinked()) {
2510 __ Bind(&done);
2511 }
2512
2513 if (slow_path != nullptr) {
2514 __ Bind(slow_path->GetExitLabel());
2515 }
2516}
2517
2518void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2519 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2520 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2521
2522 switch (instruction->GetTypeCheckKind()) {
2523 case TypeCheckKind::kExactCheck:
2524 case TypeCheckKind::kAbstractClassCheck:
2525 case TypeCheckKind::kClassHierarchyCheck:
2526 case TypeCheckKind::kArrayObjectCheck:
2527 call_kind = throws_into_catch
2528 ? LocationSummary::kCallOnSlowPath
2529 : LocationSummary::kNoCall;
2530 break;
2531 case TypeCheckKind::kInterfaceCheck:
2532 call_kind = LocationSummary::kCall;
2533 break;
2534 case TypeCheckKind::kArrayCheck:
2535 call_kind = LocationSummary::kCallOnSlowPath;
2536 break;
2537 }
2538
2539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2540 instruction, call_kind);
2541 if (call_kind != LocationSummary::kCall) {
2542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetInAt(1, Location::RequiresRegister());
2544 // Note that TypeCheckSlowPathARM64 uses this register too.
2545 locations->AddTemp(Location::RequiresRegister());
2546 } else {
2547 InvokeRuntimeCallingConvention calling_convention;
2548 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2549 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2550 }
2551}
2552
2553void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2554 LocationSummary* locations = instruction->GetLocations();
2555 Register obj = InputRegisterAt(instruction, 0);
2556 Register cls = InputRegisterAt(instruction, 1);
2557 Register temp;
2558 if (!locations->WillCall()) {
2559 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2560 }
2561
2562 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2563 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2564 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2565 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2566 SlowPathCodeARM64* slow_path = nullptr;
2567
2568 if (!locations->WillCall()) {
2569 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2570 instruction, !locations->CanCall());
2571 codegen_->AddSlowPath(slow_path);
2572 }
2573
2574 vixl::Label done;
2575 // Avoid null check if we know obj is not null.
2576 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002577 __ Cbz(obj, &done);
2578 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002579
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002580 if (locations->WillCall()) {
2581 __ Ldr(obj, HeapOperand(obj, class_offset));
2582 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002583 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002584 __ Ldr(temp, HeapOperand(obj, class_offset));
2585 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002586 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002587
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002588 switch (instruction->GetTypeCheckKind()) {
2589 case TypeCheckKind::kExactCheck:
2590 case TypeCheckKind::kArrayCheck: {
2591 __ Cmp(temp, cls);
2592 // Jump to slow path for throwing the exception or doing a
2593 // more involved array check.
2594 __ B(ne, slow_path->GetEntryLabel());
2595 break;
2596 }
2597 case TypeCheckKind::kAbstractClassCheck: {
2598 // If the class is abstract, we eagerly fetch the super class of the
2599 // object to avoid doing a comparison we know will fail.
2600 vixl::Label loop;
2601 __ Bind(&loop);
2602 __ Ldr(temp, HeapOperand(temp, super_offset));
2603 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2604 // Jump to the slow path to throw the exception.
2605 __ Cbz(temp, slow_path->GetEntryLabel());
2606 __ Cmp(temp, cls);
2607 __ B(ne, &loop);
2608 break;
2609 }
2610 case TypeCheckKind::kClassHierarchyCheck: {
2611 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002612 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002613 __ Bind(&loop);
2614 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002615 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002616 __ Ldr(temp, HeapOperand(temp, super_offset));
2617 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2618 __ Cbnz(temp, &loop);
2619 // Jump to the slow path to throw the exception.
2620 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002621 break;
2622 }
2623 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002624 // Do an exact check.
2625 __ Cmp(temp, cls);
2626 __ B(eq, &done);
2627 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002628 __ Ldr(temp, HeapOperand(temp, component_offset));
2629 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2630 __ Cbz(temp, slow_path->GetEntryLabel());
2631 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2632 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2633 __ Cbnz(temp, slow_path->GetEntryLabel());
2634 break;
2635 }
2636 case TypeCheckKind::kInterfaceCheck:
2637 default:
2638 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2639 instruction,
2640 instruction->GetDexPc(),
2641 nullptr);
2642 break;
2643 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002644 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002645
2646 if (slow_path != nullptr) {
2647 __ Bind(slow_path->GetExitLabel());
2648 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002649}
2650
Alexandre Rames5319def2014-10-23 10:03:10 +01002651void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2653 locations->SetOut(Location::ConstantLocation(constant));
2654}
2655
2656void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2657 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002658 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002659}
2660
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002661void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2663 locations->SetOut(Location::ConstantLocation(constant));
2664}
2665
2666void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2667 // Will be generated at use site.
2668 UNUSED(constant);
2669}
2670
Calin Juravle175dc732015-08-25 15:42:32 +01002671void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2672 // The trampoline uses the same calling convention as dex calling conventions,
2673 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2674 // the method_idx.
2675 HandleInvoke(invoke);
2676}
2677
2678void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2679 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2680}
2681
Alexandre Rames5319def2014-10-23 10:03:10 +01002682void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002683 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002684 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002685}
2686
Alexandre Rames67555f72014-11-18 10:55:16 +00002687void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2688 HandleInvoke(invoke);
2689}
2690
2691void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2692 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002693 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2694 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2695 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002696 Location receiver = invoke->GetLocations()->InAt(0);
2697 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002698 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002699
2700 // The register ip1 is required to be used for the hidden argument in
2701 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002702 MacroAssembler* masm = GetVIXLAssembler();
2703 UseScratchRegisterScope scratch_scope(masm);
2704 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002705 scratch_scope.Exclude(ip1);
2706 __ Mov(ip1, invoke->GetDexMethodIndex());
2707
2708 // temp = object->GetClass();
2709 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002710 __ Ldr(temp.W(), StackOperandFrom(receiver));
2711 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002712 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002713 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002714 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002715 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002716 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002717 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002718 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002719 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002720 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002721 // lr();
2722 __ Blr(lr);
2723 DCHECK(!codegen_->IsLeafMethod());
2724 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2725}
2726
2727void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002728 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2729 if (intrinsic.TryDispatch(invoke)) {
2730 return;
2731 }
2732
Alexandre Rames67555f72014-11-18 10:55:16 +00002733 HandleInvoke(invoke);
2734}
2735
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002736void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002737 // When we do not run baseline, explicit clinit checks triggered by static
2738 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2739 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002740
Andreas Gampe878d58c2015-01-15 23:24:00 -08002741 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2742 if (intrinsic.TryDispatch(invoke)) {
2743 return;
2744 }
2745
Alexandre Rames67555f72014-11-18 10:55:16 +00002746 HandleInvoke(invoke);
2747}
2748
Andreas Gampe878d58c2015-01-15 23:24:00 -08002749static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2750 if (invoke->GetLocations()->Intrinsified()) {
2751 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2752 intrinsic.Dispatch(invoke);
2753 return true;
2754 }
2755 return false;
2756}
2757
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002758void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002759 // For better instruction scheduling we load the direct code pointer before the method pointer.
2760 bool direct_code_loaded = false;
2761 switch (invoke->GetCodePtrLocation()) {
2762 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2763 // LR = code address from literal pool with link-time patch.
2764 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2765 direct_code_loaded = true;
2766 break;
2767 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2768 // LR = invoke->GetDirectCodePtr();
2769 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2770 direct_code_loaded = true;
2771 break;
2772 default:
2773 break;
2774 }
2775
Andreas Gampe878d58c2015-01-15 23:24:00 -08002776 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002777 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2778 switch (invoke->GetMethodLoadKind()) {
2779 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2780 // temp = thread->string_init_entrypoint
2781 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2782 break;
2783 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2784 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2785 break;
2786 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2787 // Load method address from literal pool.
2788 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2789 break;
2790 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2791 // Load method address from literal pool with a link-time patch.
2792 __ Ldr(XRegisterFrom(temp).X(),
2793 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2794 break;
2795 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2796 // Add ADRP with its PC-relative DexCache access patch.
2797 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2798 invoke->GetDexCacheArrayOffset());
2799 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2800 {
2801 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2802 __ adrp(XRegisterFrom(temp).X(), 0);
2803 }
2804 __ Bind(pc_insn_label); // Bind after ADRP.
2805 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2806 // Add LDR with its PC-relative DexCache access patch.
2807 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2808 invoke->GetDexCacheArrayOffset());
2809 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2810 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2811 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2812 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002813 }
Vladimir Marko58155012015-08-19 12:49:41 +00002814 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2815 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2816 Register reg = XRegisterFrom(temp);
2817 Register method_reg;
2818 if (current_method.IsRegister()) {
2819 method_reg = XRegisterFrom(current_method);
2820 } else {
2821 DCHECK(invoke->GetLocations()->Intrinsified());
2822 DCHECK(!current_method.IsValid());
2823 method_reg = reg;
2824 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2825 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002826
Vladimir Marko58155012015-08-19 12:49:41 +00002827 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002828 __ Ldr(reg.X(),
2829 MemOperand(method_reg.X(),
2830 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002831 // temp = temp[index_in_cache];
2832 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2833 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2834 break;
2835 }
2836 }
2837
2838 switch (invoke->GetCodePtrLocation()) {
2839 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2840 __ Bl(&frame_entry_label_);
2841 break;
2842 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2843 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2844 vixl::Label* label = &relative_call_patches_.back().label;
2845 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2846 __ Bind(label); // Bind after BL.
2847 break;
2848 }
2849 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2850 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2851 // LR prepared above for better instruction scheduling.
2852 DCHECK(direct_code_loaded);
2853 // lr()
2854 __ Blr(lr);
2855 break;
2856 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2857 // LR = callee_method->entry_point_from_quick_compiled_code_;
2858 __ Ldr(lr, MemOperand(
2859 XRegisterFrom(callee_method).X(),
2860 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2861 // lr()
2862 __ Blr(lr);
2863 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002864 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002865
Andreas Gampe878d58c2015-01-15 23:24:00 -08002866 DCHECK(!IsLeafMethod());
2867}
2868
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002869void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2870 LocationSummary* locations = invoke->GetLocations();
2871 Location receiver = locations->InAt(0);
2872 Register temp = XRegisterFrom(temp_in);
2873 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2874 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2875 Offset class_offset = mirror::Object::ClassOffset();
2876 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2877
2878 BlockPoolsScope block_pools(GetVIXLAssembler());
2879
2880 DCHECK(receiver.IsRegister());
2881 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2882 MaybeRecordImplicitNullCheck(invoke);
2883 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2884 // temp = temp->GetMethodAt(method_offset);
2885 __ Ldr(temp, MemOperand(temp, method_offset));
2886 // lr = temp->GetEntryPoint();
2887 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2888 // lr();
2889 __ Blr(lr);
2890}
2891
Vladimir Marko58155012015-08-19 12:49:41 +00002892void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2893 DCHECK(linker_patches->empty());
2894 size_t size =
2895 method_patches_.size() +
2896 call_patches_.size() +
2897 relative_call_patches_.size() +
2898 pc_rel_dex_cache_patches_.size();
2899 linker_patches->reserve(size);
2900 for (const auto& entry : method_patches_) {
2901 const MethodReference& target_method = entry.first;
2902 vixl::Literal<uint64_t>* literal = entry.second;
2903 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2904 target_method.dex_file,
2905 target_method.dex_method_index));
2906 }
2907 for (const auto& entry : call_patches_) {
2908 const MethodReference& target_method = entry.first;
2909 vixl::Literal<uint64_t>* literal = entry.second;
2910 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2911 target_method.dex_file,
2912 target_method.dex_method_index));
2913 }
2914 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2915 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2916 info.target_method.dex_file,
2917 info.target_method.dex_method_index));
2918 }
2919 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2920 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2921 &info.target_dex_file,
2922 info.pc_insn_label->location() - 4u,
2923 info.element_offset));
2924 }
2925}
2926
2927vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2928 // Look up the literal for value.
2929 auto lb = uint64_literals_.lower_bound(value);
2930 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2931 return lb->second;
2932 }
2933 // We don't have a literal for this value, insert a new one.
2934 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2935 uint64_literals_.PutBefore(lb, value, literal);
2936 return literal;
2937}
2938
2939vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2940 MethodReference target_method,
2941 MethodToLiteralMap* map) {
2942 // Look up the literal for target_method.
2943 auto lb = map->lower_bound(target_method);
2944 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2945 return lb->second;
2946 }
2947 // We don't have a literal for this method yet, insert a new one.
2948 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2949 map->PutBefore(lb, target_method, literal);
2950 return literal;
2951}
2952
2953vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2954 MethodReference target_method) {
2955 return DeduplicateMethodLiteral(target_method, &method_patches_);
2956}
2957
2958vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2959 MethodReference target_method) {
2960 return DeduplicateMethodLiteral(target_method, &call_patches_);
2961}
2962
2963
Andreas Gampe878d58c2015-01-15 23:24:00 -08002964void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002965 // When we do not run baseline, explicit clinit checks triggered by static
2966 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2967 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002968
Andreas Gampe878d58c2015-01-15 23:24:00 -08002969 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2970 return;
2971 }
2972
Alexandre Ramesd921d642015-04-16 15:07:16 +01002973 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002974 LocationSummary* locations = invoke->GetLocations();
2975 codegen_->GenerateStaticOrDirectCall(
2976 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002977 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002978}
2979
2980void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002981 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2982 return;
2983 }
2984
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002985 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002986 DCHECK(!codegen_->IsLeafMethod());
2987 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2988}
2989
Alexandre Rames67555f72014-11-18 10:55:16 +00002990void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2991 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2992 : LocationSummary::kNoCall;
2993 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002994 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002995 locations->SetOut(Location::RequiresRegister());
2996}
2997
2998void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2999 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003000 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003001 if (cls->IsReferrersClass()) {
3002 DCHECK(!cls->CanCallRuntime());
3003 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003004 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003005 } else {
3006 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003007 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3008 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3009 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3010 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003011
3012 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3013 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3014 codegen_->AddSlowPath(slow_path);
3015 __ Cbz(out, slow_path->GetEntryLabel());
3016 if (cls->MustGenerateClinitCheck()) {
3017 GenerateClassInitializationCheck(slow_path, out);
3018 } else {
3019 __ Bind(slow_path->GetExitLabel());
3020 }
3021 }
3022}
3023
David Brazdilcb1c0552015-08-04 16:22:25 +01003024static MemOperand GetExceptionTlsAddress() {
3025 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3026}
3027
Alexandre Rames67555f72014-11-18 10:55:16 +00003028void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3029 LocationSummary* locations =
3030 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3031 locations->SetOut(Location::RequiresRegister());
3032}
3033
3034void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003035 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3036}
3037
3038void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3039 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3040}
3041
3042void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3043 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003044}
3045
Alexandre Rames5319def2014-10-23 10:03:10 +01003046void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3047 load->SetLocations(nullptr);
3048}
3049
3050void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3051 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003052 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003053}
3054
Alexandre Rames67555f72014-11-18 10:55:16 +00003055void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3056 LocationSummary* locations =
3057 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003058 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003059 locations->SetOut(Location::RequiresRegister());
3060}
3061
3062void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3063 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3064 codegen_->AddSlowPath(slow_path);
3065
3066 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003067 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003068 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003069 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3070 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3071 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003072 __ Cbz(out, slow_path->GetEntryLabel());
3073 __ Bind(slow_path->GetExitLabel());
3074}
3075
Alexandre Rames5319def2014-10-23 10:03:10 +01003076void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3077 local->SetLocations(nullptr);
3078}
3079
3080void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3081 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3082}
3083
3084void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3086 locations->SetOut(Location::ConstantLocation(constant));
3087}
3088
3089void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3090 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003091 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003092}
3093
Alexandre Rames67555f72014-11-18 10:55:16 +00003094void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3097 InvokeRuntimeCallingConvention calling_convention;
3098 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3099}
3100
3101void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3102 codegen_->InvokeRuntime(instruction->IsEnter()
3103 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3104 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003105 instruction->GetDexPc(),
3106 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003107 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003108}
3109
Alexandre Rames42d641b2014-10-27 14:00:51 +00003110void LocationsBuilderARM64::VisitMul(HMul* mul) {
3111 LocationSummary* locations =
3112 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3113 switch (mul->GetResultType()) {
3114 case Primitive::kPrimInt:
3115 case Primitive::kPrimLong:
3116 locations->SetInAt(0, Location::RequiresRegister());
3117 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003119 break;
3120
3121 case Primitive::kPrimFloat:
3122 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003123 locations->SetInAt(0, Location::RequiresFpuRegister());
3124 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003125 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003126 break;
3127
3128 default:
3129 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3130 }
3131}
3132
3133void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3134 switch (mul->GetResultType()) {
3135 case Primitive::kPrimInt:
3136 case Primitive::kPrimLong:
3137 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3138 break;
3139
3140 case Primitive::kPrimFloat:
3141 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003142 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003143 break;
3144
3145 default:
3146 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3147 }
3148}
3149
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003150void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3151 LocationSummary* locations =
3152 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3153 switch (neg->GetResultType()) {
3154 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003155 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003156 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003157 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003158 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003159
3160 case Primitive::kPrimFloat:
3161 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003162 locations->SetInAt(0, Location::RequiresFpuRegister());
3163 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003164 break;
3165
3166 default:
3167 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3168 }
3169}
3170
3171void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3172 switch (neg->GetResultType()) {
3173 case Primitive::kPrimInt:
3174 case Primitive::kPrimLong:
3175 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3176 break;
3177
3178 case Primitive::kPrimFloat:
3179 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003180 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003181 break;
3182
3183 default:
3184 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3185 }
3186}
3187
3188void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3189 LocationSummary* locations =
3190 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3191 InvokeRuntimeCallingConvention calling_convention;
3192 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003193 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003194 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003195 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003196 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003197 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003198}
3199
3200void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3201 LocationSummary* locations = instruction->GetLocations();
3202 InvokeRuntimeCallingConvention calling_convention;
3203 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3204 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003205 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003206 // Note: if heap poisoning is enabled, the entry point takes cares
3207 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003208 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3209 instruction,
3210 instruction->GetDexPc(),
3211 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003212 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003213}
3214
Alexandre Rames5319def2014-10-23 10:03:10 +01003215void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3216 LocationSummary* locations =
3217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3218 InvokeRuntimeCallingConvention calling_convention;
3219 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003220 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003221 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003222 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003223}
3224
3225void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3226 LocationSummary* locations = instruction->GetLocations();
3227 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3228 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003229 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003230 // Note: if heap poisoning is enabled, the entry point takes cares
3231 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003232 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3233 instruction,
3234 instruction->GetDexPc(),
3235 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003236 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003237}
3238
3239void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003241 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003242 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003243}
3244
3245void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003246 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003247 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003248 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003249 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003250 break;
3251
3252 default:
3253 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3254 }
3255}
3256
David Brazdil66d126e2015-04-03 16:02:44 +01003257void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3259 locations->SetInAt(0, Location::RequiresRegister());
3260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3261}
3262
3263void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003264 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3265}
3266
Alexandre Rames5319def2014-10-23 10:03:10 +01003267void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003268 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3269 ? LocationSummary::kCallOnSlowPath
3270 : LocationSummary::kNoCall;
3271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003272 locations->SetInAt(0, Location::RequiresRegister());
3273 if (instruction->HasUses()) {
3274 locations->SetOut(Location::SameAsFirstInput());
3275 }
3276}
3277
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003278void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003279 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3280 return;
3281 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003282
Alexandre Ramesd921d642015-04-16 15:07:16 +01003283 BlockPoolsScope block_pools(GetVIXLAssembler());
3284 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003285 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3286 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3287}
3288
3289void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003290 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3291 codegen_->AddSlowPath(slow_path);
3292
3293 LocationSummary* locations = instruction->GetLocations();
3294 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003295
3296 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003297}
3298
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003299void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003300 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003301 GenerateImplicitNullCheck(instruction);
3302 } else {
3303 GenerateExplicitNullCheck(instruction);
3304 }
3305}
3306
Alexandre Rames67555f72014-11-18 10:55:16 +00003307void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3308 HandleBinaryOp(instruction);
3309}
3310
3311void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3312 HandleBinaryOp(instruction);
3313}
3314
Alexandre Rames3e69f162014-12-10 10:36:50 +00003315void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3316 LOG(FATAL) << "Unreachable";
3317}
3318
3319void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3320 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3321}
3322
Alexandre Rames5319def2014-10-23 10:03:10 +01003323void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3324 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3325 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3326 if (location.IsStackSlot()) {
3327 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3328 } else if (location.IsDoubleStackSlot()) {
3329 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3330 }
3331 locations->SetOut(location);
3332}
3333
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003334void InstructionCodeGeneratorARM64::VisitParameterValue(
3335 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003336 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003337}
3338
3339void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3340 LocationSummary* locations =
3341 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003342 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003343}
3344
3345void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3346 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3347 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003348}
3349
3350void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3352 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3353 locations->SetInAt(i, Location::Any());
3354 }
3355 locations->SetOut(Location::Any());
3356}
3357
3358void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003359 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003360 LOG(FATAL) << "Unreachable";
3361}
3362
Serban Constantinescu02164b32014-11-13 14:05:07 +00003363void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003364 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003365 LocationSummary::CallKind call_kind =
3366 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003367 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3368
3369 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003370 case Primitive::kPrimInt:
3371 case Primitive::kPrimLong:
3372 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003373 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3375 break;
3376
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003377 case Primitive::kPrimFloat:
3378 case Primitive::kPrimDouble: {
3379 InvokeRuntimeCallingConvention calling_convention;
3380 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3381 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3382 locations->SetOut(calling_convention.GetReturnLocation(type));
3383
3384 break;
3385 }
3386
Serban Constantinescu02164b32014-11-13 14:05:07 +00003387 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003388 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003389 }
3390}
3391
3392void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3393 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003394
Serban Constantinescu02164b32014-11-13 14:05:07 +00003395 switch (type) {
3396 case Primitive::kPrimInt:
3397 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003398 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003399 break;
3400 }
3401
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003402 case Primitive::kPrimFloat:
3403 case Primitive::kPrimDouble: {
3404 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3405 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003406 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003407 break;
3408 }
3409
Serban Constantinescu02164b32014-11-13 14:05:07 +00003410 default:
3411 LOG(FATAL) << "Unexpected rem type " << type;
3412 }
3413}
3414
Calin Juravle27df7582015-04-17 19:12:31 +01003415void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3416 memory_barrier->SetLocations(nullptr);
3417}
3418
3419void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3420 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3421}
3422
Alexandre Rames5319def2014-10-23 10:03:10 +01003423void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3425 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003426 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003427}
3428
3429void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003430 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003431 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003432}
3433
3434void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3435 instruction->SetLocations(nullptr);
3436}
3437
3438void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003439 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003440 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003441}
3442
Serban Constantinescu02164b32014-11-13 14:05:07 +00003443void LocationsBuilderARM64::VisitShl(HShl* shl) {
3444 HandleShift(shl);
3445}
3446
3447void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3448 HandleShift(shl);
3449}
3450
3451void LocationsBuilderARM64::VisitShr(HShr* shr) {
3452 HandleShift(shr);
3453}
3454
3455void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3456 HandleShift(shr);
3457}
3458
Alexandre Rames5319def2014-10-23 10:03:10 +01003459void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3461 Primitive::Type field_type = store->InputAt(1)->GetType();
3462 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003463 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003464 case Primitive::kPrimBoolean:
3465 case Primitive::kPrimByte:
3466 case Primitive::kPrimChar:
3467 case Primitive::kPrimShort:
3468 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003469 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003470 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3471 break;
3472
3473 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003474 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003475 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3476 break;
3477
3478 default:
3479 LOG(FATAL) << "Unimplemented local type " << field_type;
3480 }
3481}
3482
3483void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003484 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003485}
3486
3487void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003488 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003489}
3490
3491void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003492 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003493}
3494
Alexandre Rames67555f72014-11-18 10:55:16 +00003495void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003496 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003497}
3498
3499void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003500 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003501}
3502
3503void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003504 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003505}
3506
Alexandre Rames67555f72014-11-18 10:55:16 +00003507void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003508 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003509}
3510
3511void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3512 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3513}
3514
3515void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003516 HBasicBlock* block = instruction->GetBlock();
3517 if (block->GetLoopInformation() != nullptr) {
3518 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3519 // The back edge will generate the suspend check.
3520 return;
3521 }
3522 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3523 // The goto will generate the suspend check.
3524 return;
3525 }
3526 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003527}
3528
3529void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3530 temp->SetLocations(nullptr);
3531}
3532
3533void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3534 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003535 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003536}
3537
Alexandre Rames67555f72014-11-18 10:55:16 +00003538void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3539 LocationSummary* locations =
3540 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3541 InvokeRuntimeCallingConvention calling_convention;
3542 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3543}
3544
3545void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3546 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003547 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003548 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003549}
3550
3551void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3554 Primitive::Type input_type = conversion->GetInputType();
3555 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003556 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003557 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3558 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3559 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3560 }
3561
Alexandre Rames542361f2015-01-29 16:57:31 +00003562 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003563 locations->SetInAt(0, Location::RequiresFpuRegister());
3564 } else {
3565 locations->SetInAt(0, Location::RequiresRegister());
3566 }
3567
Alexandre Rames542361f2015-01-29 16:57:31 +00003568 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003569 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3570 } else {
3571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3572 }
3573}
3574
3575void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3576 Primitive::Type result_type = conversion->GetResultType();
3577 Primitive::Type input_type = conversion->GetInputType();
3578
3579 DCHECK_NE(input_type, result_type);
3580
Alexandre Rames542361f2015-01-29 16:57:31 +00003581 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003582 int result_size = Primitive::ComponentSize(result_type);
3583 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003584 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003585 Register output = OutputRegister(conversion);
3586 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003587 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3588 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003589 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3590 // 'int' values are used directly as W registers, discarding the top
3591 // bits, so we don't need to sign-extend and can just perform a move.
3592 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3593 // top 32 bits of the target register. We theoretically could leave those
3594 // bits unchanged, but we would have to make sure that no code uses a
3595 // 32bit input value as a 64bit value assuming that the top 32 bits are
3596 // zero.
3597 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003598 } else if ((result_type == Primitive::kPrimChar) ||
3599 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3600 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003601 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003602 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003603 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003604 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003605 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003606 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003607 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3608 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003609 } else if (Primitive::IsFloatingPointType(result_type) &&
3610 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003611 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3612 } else {
3613 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3614 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003615 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003616}
Alexandre Rames67555f72014-11-18 10:55:16 +00003617
Serban Constantinescu02164b32014-11-13 14:05:07 +00003618void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3619 HandleShift(ushr);
3620}
3621
3622void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3623 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003624}
3625
3626void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3627 HandleBinaryOp(instruction);
3628}
3629
3630void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3631 HandleBinaryOp(instruction);
3632}
3633
Calin Juravleb1498f62015-02-16 13:13:29 +00003634void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3635 // Nothing to do, this should be removed during prepare for register allocator.
3636 UNUSED(instruction);
3637 LOG(FATAL) << "Unreachable";
3638}
3639
3640void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3641 // Nothing to do, this should be removed during prepare for register allocator.
3642 UNUSED(instruction);
3643 LOG(FATAL) << "Unreachable";
3644}
3645
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003646void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3647 DCHECK(codegen_->IsBaseline());
3648 LocationSummary* locations =
3649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3650 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3651}
3652
3653void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3654 DCHECK(codegen_->IsBaseline());
3655 // Will be generated at use site.
3656}
3657
Mark Mendellfe57faa2015-09-18 09:26:15 -04003658// Simple implementation of packed switch - generate cascaded compare/jumps.
3659void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3660 LocationSummary* locations =
3661 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3662 locations->SetInAt(0, Location::RequiresRegister());
3663}
3664
3665void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3666 int32_t lower_bound = switch_instr->GetStartValue();
3667 int32_t num_entries = switch_instr->GetNumEntries();
3668 Register value_reg = InputRegisterAt(switch_instr, 0);
3669 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3670
3671 // Create a series of compare/jumps.
3672 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3673 for (int32_t i = 0; i < num_entries; i++) {
3674 int32_t case_value = lower_bound + i;
3675 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3676 if (case_value == 0) {
3677 __ Cbz(value_reg, succ);
3678 } else {
3679 __ Cmp(value_reg, vixl::Operand(case_value));
3680 __ B(eq, succ);
3681 }
3682 }
3683
3684 // And the default for any other value.
3685 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3686 __ B(codegen_->GetLabelOf(default_block));
3687 }
3688}
3689
Alexandre Rames67555f72014-11-18 10:55:16 +00003690#undef __
3691#undef QUICK_ENTRY_POINT
3692
Alexandre Rames5319def2014-10-23 10:03:10 +01003693} // namespace arm64
3694} // namespace art