blob: c081e11d9204311318f3a05e5977123850d40041 [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 }
1710 }
1711
1712 if (kPoisonHeapReferences) {
1713 DCHECK(value.IsW());
1714 __ Mov(temp, value.W());
1715 GetAssembler()->PoisonHeapReference(temp);
1716 source = temp;
1717 }
1718
1719 if (!index.IsConstant()) {
1720 __ Add(temp, array, offset);
1721 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001722 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001723
1724 if (!may_need_runtime_call) {
1725 codegen_->MaybeRecordImplicitNullCheck(instruction);
1726 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001727 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001728
1729 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1730
1731 if (done.IsLinked()) {
1732 __ Bind(&done);
1733 }
1734
1735 if (slow_path != nullptr) {
1736 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001737 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001738 }
1739}
1740
Alexandre Rames67555f72014-11-18 10:55:16 +00001741void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001742 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1743 ? LocationSummary::kCallOnSlowPath
1744 : LocationSummary::kNoCall;
1745 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001746 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001747 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001748 if (instruction->HasUses()) {
1749 locations->SetOut(Location::SameAsFirstInput());
1750 }
1751}
1752
1753void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001754 BoundsCheckSlowPathARM64* slow_path =
1755 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001756 codegen_->AddSlowPath(slow_path);
1757
1758 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1759 __ B(slow_path->GetEntryLabel(), hs);
1760}
1761
Alexandre Rames67555f72014-11-18 10:55:16 +00001762void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1763 LocationSummary* locations =
1764 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1765 locations->SetInAt(0, Location::RequiresRegister());
1766 if (check->HasUses()) {
1767 locations->SetOut(Location::SameAsFirstInput());
1768 }
1769}
1770
1771void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1772 // We assume the class is not null.
1773 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1774 check->GetLoadClass(), check, check->GetDexPc(), true);
1775 codegen_->AddSlowPath(slow_path);
1776 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1777}
1778
Roland Levillain7f63c522015-07-13 15:54:55 +00001779static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1780 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1781 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1782}
1783
Serban Constantinescu02164b32014-11-13 14:05:07 +00001784void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001785 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001786 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1787 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001788 switch (in_type) {
1789 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001790 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001791 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001792 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1793 break;
1794 }
1795 case Primitive::kPrimFloat:
1796 case Primitive::kPrimDouble: {
1797 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001798 locations->SetInAt(1,
1799 IsFloatingPointZeroConstant(compare->InputAt(1))
1800 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1801 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001802 locations->SetOut(Location::RequiresRegister());
1803 break;
1804 }
1805 default:
1806 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1807 }
1808}
1809
1810void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1811 Primitive::Type in_type = compare->InputAt(0)->GetType();
1812
1813 // 0 if: left == right
1814 // 1 if: left > right
1815 // -1 if: left < right
1816 switch (in_type) {
1817 case Primitive::kPrimLong: {
1818 Register result = OutputRegister(compare);
1819 Register left = InputRegisterAt(compare, 0);
1820 Operand right = InputOperandAt(compare, 1);
1821
1822 __ Cmp(left, right);
1823 __ Cset(result, ne);
1824 __ Cneg(result, result, lt);
1825 break;
1826 }
1827 case Primitive::kPrimFloat:
1828 case Primitive::kPrimDouble: {
1829 Register result = OutputRegister(compare);
1830 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001831 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001832 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1833 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001834 __ Fcmp(left, 0.0);
1835 } else {
1836 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1837 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001838 if (compare->IsGtBias()) {
1839 __ Cset(result, ne);
1840 } else {
1841 __ Csetm(result, ne);
1842 }
1843 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001844 break;
1845 }
1846 default:
1847 LOG(FATAL) << "Unimplemented compare type " << in_type;
1848 }
1849}
1850
1851void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1852 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001853
1854 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1855 locations->SetInAt(0, Location::RequiresFpuRegister());
1856 locations->SetInAt(1,
1857 IsFloatingPointZeroConstant(instruction->InputAt(1))
1858 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
1859 : Location::RequiresFpuRegister());
1860 } else {
1861 // Integer cases.
1862 locations->SetInAt(0, Location::RequiresRegister());
1863 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
1864 }
1865
Alexandre Rames5319def2014-10-23 10:03:10 +01001866 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001867 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001868 }
1869}
1870
1871void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
1872 if (!instruction->NeedsMaterialization()) {
1873 return;
1874 }
1875
1876 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001877 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00001878 IfCondition if_cond = instruction->GetCondition();
1879 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01001880
Roland Levillain7f63c522015-07-13 15:54:55 +00001881 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
1882 FPRegister lhs = InputFPRegisterAt(instruction, 0);
1883 if (locations->InAt(1).IsConstant()) {
1884 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
1885 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
1886 __ Fcmp(lhs, 0.0);
1887 } else {
1888 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
1889 }
1890 __ Cset(res, arm64_cond);
1891 if (instruction->IsFPConditionTrueIfNaN()) {
1892 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
1893 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
1894 } else if (instruction->IsFPConditionFalseIfNaN()) {
1895 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
1896 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
1897 }
1898 } else {
1899 // Integer cases.
1900 Register lhs = InputRegisterAt(instruction, 0);
1901 Operand rhs = InputOperandAt(instruction, 1);
1902 __ Cmp(lhs, rhs);
1903 __ Cset(res, arm64_cond);
1904 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001905}
1906
1907#define FOR_EACH_CONDITION_INSTRUCTION(M) \
1908 M(Equal) \
1909 M(NotEqual) \
1910 M(LessThan) \
1911 M(LessThanOrEqual) \
1912 M(GreaterThan) \
1913 M(GreaterThanOrEqual)
1914#define DEFINE_CONDITION_VISITORS(Name) \
1915void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
1916void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
1917FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00001918#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01001919#undef FOR_EACH_CONDITION_INSTRUCTION
1920
Zheng Xuc6667102015-05-15 16:08:45 +08001921void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1922 DCHECK(instruction->IsDiv() || instruction->IsRem());
1923
1924 LocationSummary* locations = instruction->GetLocations();
1925 Location second = locations->InAt(1);
1926 DCHECK(second.IsConstant());
1927
1928 Register out = OutputRegister(instruction);
1929 Register dividend = InputRegisterAt(instruction, 0);
1930 int64_t imm = Int64FromConstant(second.GetConstant());
1931 DCHECK(imm == 1 || imm == -1);
1932
1933 if (instruction->IsRem()) {
1934 __ Mov(out, 0);
1935 } else {
1936 if (imm == 1) {
1937 __ Mov(out, dividend);
1938 } else {
1939 __ Neg(out, dividend);
1940 }
1941 }
1942}
1943
1944void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1945 DCHECK(instruction->IsDiv() || instruction->IsRem());
1946
1947 LocationSummary* locations = instruction->GetLocations();
1948 Location second = locations->InAt(1);
1949 DCHECK(second.IsConstant());
1950
1951 Register out = OutputRegister(instruction);
1952 Register dividend = InputRegisterAt(instruction, 0);
1953 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01001954 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08001955 DCHECK(IsPowerOfTwo(abs_imm));
1956 int ctz_imm = CTZ(abs_imm);
1957
1958 UseScratchRegisterScope temps(GetVIXLAssembler());
1959 Register temp = temps.AcquireSameSizeAs(out);
1960
1961 if (instruction->IsDiv()) {
1962 __ Add(temp, dividend, abs_imm - 1);
1963 __ Cmp(dividend, 0);
1964 __ Csel(out, temp, dividend, lt);
1965 if (imm > 0) {
1966 __ Asr(out, out, ctz_imm);
1967 } else {
1968 __ Neg(out, Operand(out, ASR, ctz_imm));
1969 }
1970 } else {
1971 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
1972 __ Asr(temp, dividend, bits - 1);
1973 __ Lsr(temp, temp, bits - ctz_imm);
1974 __ Add(out, dividend, temp);
1975 __ And(out, out, abs_imm - 1);
1976 __ Sub(out, out, temp);
1977 }
1978}
1979
1980void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1981 DCHECK(instruction->IsDiv() || instruction->IsRem());
1982
1983 LocationSummary* locations = instruction->GetLocations();
1984 Location second = locations->InAt(1);
1985 DCHECK(second.IsConstant());
1986
1987 Register out = OutputRegister(instruction);
1988 Register dividend = InputRegisterAt(instruction, 0);
1989 int64_t imm = Int64FromConstant(second.GetConstant());
1990
1991 Primitive::Type type = instruction->GetResultType();
1992 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1993
1994 int64_t magic;
1995 int shift;
1996 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
1997
1998 UseScratchRegisterScope temps(GetVIXLAssembler());
1999 Register temp = temps.AcquireSameSizeAs(out);
2000
2001 // temp = get_high(dividend * magic)
2002 __ Mov(temp, magic);
2003 if (type == Primitive::kPrimLong) {
2004 __ Smulh(temp, dividend, temp);
2005 } else {
2006 __ Smull(temp.X(), dividend, temp);
2007 __ Lsr(temp.X(), temp.X(), 32);
2008 }
2009
2010 if (imm > 0 && magic < 0) {
2011 __ Add(temp, temp, dividend);
2012 } else if (imm < 0 && magic > 0) {
2013 __ Sub(temp, temp, dividend);
2014 }
2015
2016 if (shift != 0) {
2017 __ Asr(temp, temp, shift);
2018 }
2019
2020 if (instruction->IsDiv()) {
2021 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2022 } else {
2023 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2024 // TODO: Strength reduction for msub.
2025 Register temp_imm = temps.AcquireSameSizeAs(out);
2026 __ Mov(temp_imm, imm);
2027 __ Msub(out, temp, temp_imm, dividend);
2028 }
2029}
2030
2031void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2032 DCHECK(instruction->IsDiv() || instruction->IsRem());
2033 Primitive::Type type = instruction->GetResultType();
2034 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2035
2036 LocationSummary* locations = instruction->GetLocations();
2037 Register out = OutputRegister(instruction);
2038 Location second = locations->InAt(1);
2039
2040 if (second.IsConstant()) {
2041 int64_t imm = Int64FromConstant(second.GetConstant());
2042
2043 if (imm == 0) {
2044 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2045 } else if (imm == 1 || imm == -1) {
2046 DivRemOneOrMinusOne(instruction);
2047 } else if (IsPowerOfTwo(std::abs(imm))) {
2048 DivRemByPowerOfTwo(instruction);
2049 } else {
2050 DCHECK(imm <= -2 || imm >= 2);
2051 GenerateDivRemWithAnyConstant(instruction);
2052 }
2053 } else {
2054 Register dividend = InputRegisterAt(instruction, 0);
2055 Register divisor = InputRegisterAt(instruction, 1);
2056 if (instruction->IsDiv()) {
2057 __ Sdiv(out, dividend, divisor);
2058 } else {
2059 UseScratchRegisterScope temps(GetVIXLAssembler());
2060 Register temp = temps.AcquireSameSizeAs(out);
2061 __ Sdiv(temp, dividend, divisor);
2062 __ Msub(out, temp, divisor, dividend);
2063 }
2064 }
2065}
2066
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002067void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2068 LocationSummary* locations =
2069 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2070 switch (div->GetResultType()) {
2071 case Primitive::kPrimInt:
2072 case Primitive::kPrimLong:
2073 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002074 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2076 break;
2077
2078 case Primitive::kPrimFloat:
2079 case Primitive::kPrimDouble:
2080 locations->SetInAt(0, Location::RequiresFpuRegister());
2081 locations->SetInAt(1, Location::RequiresFpuRegister());
2082 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2083 break;
2084
2085 default:
2086 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2087 }
2088}
2089
2090void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2091 Primitive::Type type = div->GetResultType();
2092 switch (type) {
2093 case Primitive::kPrimInt:
2094 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002095 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002096 break;
2097
2098 case Primitive::kPrimFloat:
2099 case Primitive::kPrimDouble:
2100 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2101 break;
2102
2103 default:
2104 LOG(FATAL) << "Unexpected div type " << type;
2105 }
2106}
2107
Alexandre Rames67555f72014-11-18 10:55:16 +00002108void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002109 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2110 ? LocationSummary::kCallOnSlowPath
2111 : LocationSummary::kNoCall;
2112 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002113 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2114 if (instruction->HasUses()) {
2115 locations->SetOut(Location::SameAsFirstInput());
2116 }
2117}
2118
2119void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2120 SlowPathCodeARM64* slow_path =
2121 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2122 codegen_->AddSlowPath(slow_path);
2123 Location value = instruction->GetLocations()->InAt(0);
2124
Alexandre Rames3e69f162014-12-10 10:36:50 +00002125 Primitive::Type type = instruction->GetType();
2126
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002127 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2128 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002129 return;
2130 }
2131
Alexandre Rames67555f72014-11-18 10:55:16 +00002132 if (value.IsConstant()) {
2133 int64_t divisor = Int64ConstantFrom(value);
2134 if (divisor == 0) {
2135 __ B(slow_path->GetEntryLabel());
2136 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002137 // A division by a non-null constant is valid. We don't need to perform
2138 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002139 }
2140 } else {
2141 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2142 }
2143}
2144
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002145void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2146 LocationSummary* locations =
2147 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2148 locations->SetOut(Location::ConstantLocation(constant));
2149}
2150
2151void InstructionCodeGeneratorARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2152 UNUSED(constant);
2153 // Will be generated at use site.
2154}
2155
Alexandre Rames5319def2014-10-23 10:03:10 +01002156void LocationsBuilderARM64::VisitExit(HExit* exit) {
2157 exit->SetLocations(nullptr);
2158}
2159
2160void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002161 UNUSED(exit);
Alexandre Rames5319def2014-10-23 10:03:10 +01002162}
2163
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002164void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2165 LocationSummary* locations =
2166 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2167 locations->SetOut(Location::ConstantLocation(constant));
2168}
2169
2170void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant) {
2171 UNUSED(constant);
2172 // Will be generated at use site.
2173}
2174
David Brazdilfc6a86a2015-06-26 10:33:45 +00002175void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002176 DCHECK(!successor->IsExitBlock());
2177 HBasicBlock* block = got->GetBlock();
2178 HInstruction* previous = got->GetPrevious();
2179 HLoopInformation* info = block->GetLoopInformation();
2180
David Brazdil46e2a392015-03-16 17:31:52 +00002181 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002182 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2183 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2184 return;
2185 }
2186 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2187 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2188 }
2189 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002190 __ B(codegen_->GetLabelOf(successor));
2191 }
2192}
2193
David Brazdilfc6a86a2015-06-26 10:33:45 +00002194void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2195 got->SetLocations(nullptr);
2196}
2197
2198void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2199 HandleGoto(got, got->GetSuccessor());
2200}
2201
2202void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2203 try_boundary->SetLocations(nullptr);
2204}
2205
2206void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2207 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2208 if (!successor->IsExitBlock()) {
2209 HandleGoto(try_boundary, successor);
2210 }
2211}
2212
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002213void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
2214 vixl::Label* true_target,
2215 vixl::Label* false_target,
2216 vixl::Label* always_true_target) {
2217 HInstruction* cond = instruction->InputAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002218 HCondition* condition = cond->AsCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002219
Serban Constantinescu02164b32014-11-13 14:05:07 +00002220 if (cond->IsIntConstant()) {
2221 int32_t cond_value = cond->AsIntConstant()->GetValue();
2222 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002223 if (always_true_target != nullptr) {
2224 __ B(always_true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002225 }
2226 return;
2227 } else {
2228 DCHECK_EQ(cond_value, 0);
2229 }
2230 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002231 // The condition instruction has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002232 Location cond_val = instruction->GetLocations()->InAt(0);
Alexandre Rames5319def2014-10-23 10:03:10 +01002233 DCHECK(cond_val.IsRegister());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002234 __ Cbnz(InputRegisterAt(instruction, 0), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002235 } else {
2236 // The condition instruction has not been materialized, use its inputs as
2237 // the comparison and its condition as the branch condition.
Roland Levillain7f63c522015-07-13 15:54:55 +00002238 Primitive::Type type =
2239 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
2240
2241 if (Primitive::IsFloatingPointType(type)) {
2242 // FP compares don't like null false_targets.
2243 if (false_target == nullptr) {
2244 false_target = codegen_->GetLabelOf(instruction->AsIf()->IfFalseSuccessor());
Alexandre Rames5319def2014-10-23 10:03:10 +01002245 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002246 FPRegister lhs = InputFPRegisterAt(condition, 0);
2247 if (condition->GetLocations()->InAt(1).IsConstant()) {
2248 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2249 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2250 __ Fcmp(lhs, 0.0);
2251 } else {
2252 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2253 }
2254 if (condition->IsFPConditionTrueIfNaN()) {
2255 __ B(vs, true_target); // VS for unordered.
2256 } else if (condition->IsFPConditionFalseIfNaN()) {
2257 __ B(vs, false_target); // VS for unordered.
2258 }
2259 __ B(ARM64Condition(condition->GetCondition()), true_target);
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002261 // Integer cases.
2262 Register lhs = InputRegisterAt(condition, 0);
2263 Operand rhs = InputOperandAt(condition, 1);
2264 Condition arm64_cond = ARM64Condition(condition->GetCondition());
2265 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2266 switch (arm64_cond) {
2267 case eq:
2268 __ Cbz(lhs, true_target);
2269 break;
2270 case ne:
2271 __ Cbnz(lhs, true_target);
2272 break;
2273 case lt:
2274 // Test the sign bit and branch accordingly.
2275 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2276 break;
2277 case ge:
2278 // Test the sign bit and branch accordingly.
2279 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, true_target);
2280 break;
2281 default:
2282 // Without the `static_cast` the compiler throws an error for
2283 // `-Werror=sign-promo`.
2284 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2285 }
2286 } else {
2287 __ Cmp(lhs, rhs);
2288 __ B(arm64_cond, true_target);
2289 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002290 }
2291 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002292 if (false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002293 __ B(false_target);
2294 }
2295}
2296
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002297void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2299 HInstruction* cond = if_instr->InputAt(0);
2300 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2301 locations->SetInAt(0, Location::RequiresRegister());
2302 }
2303}
2304
2305void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
2306 vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2307 vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2308 vixl::Label* always_true_target = true_target;
2309 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2310 if_instr->IfTrueSuccessor())) {
2311 always_true_target = nullptr;
2312 }
2313 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2314 if_instr->IfFalseSuccessor())) {
2315 false_target = nullptr;
2316 }
2317 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2318}
2319
2320void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2321 LocationSummary* locations = new (GetGraph()->GetArena())
2322 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2323 HInstruction* cond = deoptimize->InputAt(0);
2324 DCHECK(cond->IsCondition());
2325 if (cond->AsCondition()->NeedsMaterialization()) {
2326 locations->SetInAt(0, Location::RequiresRegister());
2327 }
2328}
2329
2330void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2331 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2332 DeoptimizationSlowPathARM64(deoptimize);
2333 codegen_->AddSlowPath(slow_path);
2334 vixl::Label* slow_path_entry = slow_path->GetEntryLabel();
2335 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2336}
2337
Alexandre Rames5319def2014-10-23 10:03:10 +01002338void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002339 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002340}
2341
2342void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002343 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002344}
2345
2346void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002347 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002348}
2349
2350void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002351 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002352}
2353
Alexandre Rames67555f72014-11-18 10:55:16 +00002354void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002355 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2356 switch (instruction->GetTypeCheckKind()) {
2357 case TypeCheckKind::kExactCheck:
2358 case TypeCheckKind::kAbstractClassCheck:
2359 case TypeCheckKind::kClassHierarchyCheck:
2360 case TypeCheckKind::kArrayObjectCheck:
2361 call_kind = LocationSummary::kNoCall;
2362 break;
2363 case TypeCheckKind::kInterfaceCheck:
2364 call_kind = LocationSummary::kCall;
2365 break;
2366 case TypeCheckKind::kArrayCheck:
2367 call_kind = LocationSummary::kCallOnSlowPath;
2368 break;
2369 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002370 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002371 if (call_kind != LocationSummary::kCall) {
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 locations->SetInAt(1, Location::RequiresRegister());
2374 // The out register is used as a temporary, so it overlaps with the inputs.
2375 // Note that TypeCheckSlowPathARM64 uses this register too.
2376 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2377 } else {
2378 InvokeRuntimeCallingConvention calling_convention;
2379 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2380 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2381 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2382 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002383}
2384
2385void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2386 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002387 Register obj = InputRegisterAt(instruction, 0);
2388 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002389 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002390 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2391 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2392 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2393 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002394
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002395 vixl::Label done, zero;
2396 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002397
2398 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002399 // Avoid null check if we know `obj` is not null.
2400 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002401 __ Cbz(obj, &zero);
2402 }
2403
2404 // In case of an interface check, we put the object class into the object register.
2405 // This is safe, as the register is caller-save, and the object must be in another
2406 // register if it survives the runtime call.
2407 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
2408 ? obj
2409 : out;
2410 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2411 GetAssembler()->MaybeUnpoisonHeapReference(target);
2412
2413 switch (instruction->GetTypeCheckKind()) {
2414 case TypeCheckKind::kExactCheck: {
2415 __ Cmp(out, cls);
2416 __ Cset(out, eq);
2417 if (zero.IsLinked()) {
2418 __ B(&done);
2419 }
2420 break;
2421 }
2422 case TypeCheckKind::kAbstractClassCheck: {
2423 // If the class is abstract, we eagerly fetch the super class of the
2424 // object to avoid doing a comparison we know will fail.
2425 vixl::Label loop, success;
2426 __ Bind(&loop);
2427 __ Ldr(out, HeapOperand(out, super_offset));
2428 GetAssembler()->MaybeUnpoisonHeapReference(out);
2429 // If `out` is null, we use it for the result, and jump to `done`.
2430 __ Cbz(out, &done);
2431 __ Cmp(out, cls);
2432 __ B(ne, &loop);
2433 __ Mov(out, 1);
2434 if (zero.IsLinked()) {
2435 __ B(&done);
2436 }
2437 break;
2438 }
2439 case TypeCheckKind::kClassHierarchyCheck: {
2440 // Walk over the class hierarchy to find a match.
2441 vixl::Label loop, success;
2442 __ Bind(&loop);
2443 __ Cmp(out, cls);
2444 __ B(eq, &success);
2445 __ Ldr(out, HeapOperand(out, super_offset));
2446 GetAssembler()->MaybeUnpoisonHeapReference(out);
2447 __ Cbnz(out, &loop);
2448 // If `out` is null, we use it for the result, and jump to `done`.
2449 __ B(&done);
2450 __ Bind(&success);
2451 __ Mov(out, 1);
2452 if (zero.IsLinked()) {
2453 __ B(&done);
2454 }
2455 break;
2456 }
2457 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002458 // Do an exact check.
2459 vixl::Label exact_check;
2460 __ Cmp(out, cls);
2461 __ B(eq, &exact_check);
2462 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002463 __ Ldr(out, HeapOperand(out, component_offset));
2464 GetAssembler()->MaybeUnpoisonHeapReference(out);
2465 // If `out` is null, we use it for the result, and jump to `done`.
2466 __ Cbz(out, &done);
2467 __ Ldrh(out, HeapOperand(out, primitive_offset));
2468 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2469 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002470 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002471 __ Mov(out, 1);
2472 __ B(&done);
2473 break;
2474 }
2475 case TypeCheckKind::kArrayCheck: {
2476 __ Cmp(out, cls);
2477 DCHECK(locations->OnlyCallsOnSlowPath());
2478 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2479 instruction, /* is_fatal */ false);
2480 codegen_->AddSlowPath(slow_path);
2481 __ B(ne, slow_path->GetEntryLabel());
2482 __ Mov(out, 1);
2483 if (zero.IsLinked()) {
2484 __ B(&done);
2485 }
2486 break;
2487 }
2488
2489 case TypeCheckKind::kInterfaceCheck:
2490 default: {
2491 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2492 instruction,
2493 instruction->GetDexPc(),
2494 nullptr);
2495 if (zero.IsLinked()) {
2496 __ B(&done);
2497 }
2498 break;
2499 }
2500 }
2501
2502 if (zero.IsLinked()) {
2503 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002504 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002505 }
2506
2507 if (done.IsLinked()) {
2508 __ Bind(&done);
2509 }
2510
2511 if (slow_path != nullptr) {
2512 __ Bind(slow_path->GetExitLabel());
2513 }
2514}
2515
2516void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2517 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2518 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2519
2520 switch (instruction->GetTypeCheckKind()) {
2521 case TypeCheckKind::kExactCheck:
2522 case TypeCheckKind::kAbstractClassCheck:
2523 case TypeCheckKind::kClassHierarchyCheck:
2524 case TypeCheckKind::kArrayObjectCheck:
2525 call_kind = throws_into_catch
2526 ? LocationSummary::kCallOnSlowPath
2527 : LocationSummary::kNoCall;
2528 break;
2529 case TypeCheckKind::kInterfaceCheck:
2530 call_kind = LocationSummary::kCall;
2531 break;
2532 case TypeCheckKind::kArrayCheck:
2533 call_kind = LocationSummary::kCallOnSlowPath;
2534 break;
2535 }
2536
2537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2538 instruction, call_kind);
2539 if (call_kind != LocationSummary::kCall) {
2540 locations->SetInAt(0, Location::RequiresRegister());
2541 locations->SetInAt(1, Location::RequiresRegister());
2542 // Note that TypeCheckSlowPathARM64 uses this register too.
2543 locations->AddTemp(Location::RequiresRegister());
2544 } else {
2545 InvokeRuntimeCallingConvention calling_convention;
2546 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2547 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2548 }
2549}
2550
2551void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2552 LocationSummary* locations = instruction->GetLocations();
2553 Register obj = InputRegisterAt(instruction, 0);
2554 Register cls = InputRegisterAt(instruction, 1);
2555 Register temp;
2556 if (!locations->WillCall()) {
2557 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2558 }
2559
2560 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2561 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2562 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2563 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2564 SlowPathCodeARM64* slow_path = nullptr;
2565
2566 if (!locations->WillCall()) {
2567 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2568 instruction, !locations->CanCall());
2569 codegen_->AddSlowPath(slow_path);
2570 }
2571
2572 vixl::Label done;
2573 // Avoid null check if we know obj is not null.
2574 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002575 __ Cbz(obj, &done);
2576 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002578 if (locations->WillCall()) {
2579 __ Ldr(obj, HeapOperand(obj, class_offset));
2580 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002581 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002582 __ Ldr(temp, HeapOperand(obj, class_offset));
2583 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002584 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002585
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002586 switch (instruction->GetTypeCheckKind()) {
2587 case TypeCheckKind::kExactCheck:
2588 case TypeCheckKind::kArrayCheck: {
2589 __ Cmp(temp, cls);
2590 // Jump to slow path for throwing the exception or doing a
2591 // more involved array check.
2592 __ B(ne, slow_path->GetEntryLabel());
2593 break;
2594 }
2595 case TypeCheckKind::kAbstractClassCheck: {
2596 // If the class is abstract, we eagerly fetch the super class of the
2597 // object to avoid doing a comparison we know will fail.
2598 vixl::Label loop;
2599 __ Bind(&loop);
2600 __ Ldr(temp, HeapOperand(temp, super_offset));
2601 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2602 // Jump to the slow path to throw the exception.
2603 __ Cbz(temp, slow_path->GetEntryLabel());
2604 __ Cmp(temp, cls);
2605 __ B(ne, &loop);
2606 break;
2607 }
2608 case TypeCheckKind::kClassHierarchyCheck: {
2609 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002610 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002611 __ Bind(&loop);
2612 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002613 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002614 __ Ldr(temp, HeapOperand(temp, super_offset));
2615 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2616 __ Cbnz(temp, &loop);
2617 // Jump to the slow path to throw the exception.
2618 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002619 break;
2620 }
2621 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002622 // Do an exact check.
2623 __ Cmp(temp, cls);
2624 __ B(eq, &done);
2625 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002626 __ Ldr(temp, HeapOperand(temp, component_offset));
2627 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2628 __ Cbz(temp, slow_path->GetEntryLabel());
2629 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2630 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2631 __ Cbnz(temp, slow_path->GetEntryLabel());
2632 break;
2633 }
2634 case TypeCheckKind::kInterfaceCheck:
2635 default:
2636 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2637 instruction,
2638 instruction->GetDexPc(),
2639 nullptr);
2640 break;
2641 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002642 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002643
2644 if (slow_path != nullptr) {
2645 __ Bind(slow_path->GetExitLabel());
2646 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002647}
2648
Alexandre Rames5319def2014-10-23 10:03:10 +01002649void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2650 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2651 locations->SetOut(Location::ConstantLocation(constant));
2652}
2653
2654void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) {
2655 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002656 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01002657}
2658
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002659void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2660 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2661 locations->SetOut(Location::ConstantLocation(constant));
2662}
2663
2664void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant) {
2665 // Will be generated at use site.
2666 UNUSED(constant);
2667}
2668
Calin Juravle175dc732015-08-25 15:42:32 +01002669void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2670 // The trampoline uses the same calling convention as dex calling conventions,
2671 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2672 // the method_idx.
2673 HandleInvoke(invoke);
2674}
2675
2676void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2677 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2678}
2679
Alexandre Rames5319def2014-10-23 10:03:10 +01002680void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002681 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002682 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002683}
2684
Alexandre Rames67555f72014-11-18 10:55:16 +00002685void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2686 HandleInvoke(invoke);
2687}
2688
2689void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2690 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002691 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2692 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2693 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002694 Location receiver = invoke->GetLocations()->InAt(0);
2695 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002696 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002697
2698 // The register ip1 is required to be used for the hidden argument in
2699 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002700 MacroAssembler* masm = GetVIXLAssembler();
2701 UseScratchRegisterScope scratch_scope(masm);
2702 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002703 scratch_scope.Exclude(ip1);
2704 __ Mov(ip1, invoke->GetDexMethodIndex());
2705
2706 // temp = object->GetClass();
2707 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002708 __ Ldr(temp.W(), StackOperandFrom(receiver));
2709 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002710 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002711 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002712 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002713 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002714 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002715 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002716 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002717 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002718 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002719 // lr();
2720 __ Blr(lr);
2721 DCHECK(!codegen_->IsLeafMethod());
2722 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2723}
2724
2725void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002726 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2727 if (intrinsic.TryDispatch(invoke)) {
2728 return;
2729 }
2730
Alexandre Rames67555f72014-11-18 10:55:16 +00002731 HandleInvoke(invoke);
2732}
2733
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002734void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002735 // When we do not run baseline, explicit clinit checks triggered by static
2736 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2737 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002738
Andreas Gampe878d58c2015-01-15 23:24:00 -08002739 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2740 if (intrinsic.TryDispatch(invoke)) {
2741 return;
2742 }
2743
Alexandre Rames67555f72014-11-18 10:55:16 +00002744 HandleInvoke(invoke);
2745}
2746
Andreas Gampe878d58c2015-01-15 23:24:00 -08002747static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2748 if (invoke->GetLocations()->Intrinsified()) {
2749 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2750 intrinsic.Dispatch(invoke);
2751 return true;
2752 }
2753 return false;
2754}
2755
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002756void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002757 // For better instruction scheduling we load the direct code pointer before the method pointer.
2758 bool direct_code_loaded = false;
2759 switch (invoke->GetCodePtrLocation()) {
2760 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2761 // LR = code address from literal pool with link-time patch.
2762 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2763 direct_code_loaded = true;
2764 break;
2765 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2766 // LR = invoke->GetDirectCodePtr();
2767 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2768 direct_code_loaded = true;
2769 break;
2770 default:
2771 break;
2772 }
2773
Andreas Gampe878d58c2015-01-15 23:24:00 -08002774 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002775 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2776 switch (invoke->GetMethodLoadKind()) {
2777 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2778 // temp = thread->string_init_entrypoint
2779 __ Ldr(XRegisterFrom(temp).X(), MemOperand(tr, invoke->GetStringInitOffset()));
2780 break;
2781 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2782 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2783 break;
2784 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2785 // Load method address from literal pool.
2786 __ Ldr(XRegisterFrom(temp).X(), DeduplicateUint64Literal(invoke->GetMethodAddress()));
2787 break;
2788 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2789 // Load method address from literal pool with a link-time patch.
2790 __ Ldr(XRegisterFrom(temp).X(),
2791 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2792 break;
2793 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2794 // Add ADRP with its PC-relative DexCache access patch.
2795 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2796 invoke->GetDexCacheArrayOffset());
2797 vixl::Label* pc_insn_label = &pc_rel_dex_cache_patches_.back().label;
2798 {
2799 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2800 __ adrp(XRegisterFrom(temp).X(), 0);
2801 }
2802 __ Bind(pc_insn_label); // Bind after ADRP.
2803 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2804 // Add LDR with its PC-relative DexCache access patch.
2805 pc_rel_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2806 invoke->GetDexCacheArrayOffset());
2807 __ Ldr(XRegisterFrom(temp).X(), MemOperand(XRegisterFrom(temp).X(), 0));
2808 __ Bind(&pc_rel_dex_cache_patches_.back().label); // Bind after LDR.
2809 pc_rel_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
2810 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01002811 }
Vladimir Marko58155012015-08-19 12:49:41 +00002812 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2813 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2814 Register reg = XRegisterFrom(temp);
2815 Register method_reg;
2816 if (current_method.IsRegister()) {
2817 method_reg = XRegisterFrom(current_method);
2818 } else {
2819 DCHECK(invoke->GetLocations()->Intrinsified());
2820 DCHECK(!current_method.IsValid());
2821 method_reg = reg;
2822 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
2823 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00002824
Vladimir Marko58155012015-08-19 12:49:41 +00002825 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002826 __ Ldr(reg.X(),
2827 MemOperand(method_reg.X(),
2828 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00002829 // temp = temp[index_in_cache];
2830 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2831 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
2832 break;
2833 }
2834 }
2835
2836 switch (invoke->GetCodePtrLocation()) {
2837 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2838 __ Bl(&frame_entry_label_);
2839 break;
2840 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
2841 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
2842 vixl::Label* label = &relative_call_patches_.back().label;
2843 __ Bl(label); // Arbitrarily branch to the instruction after BL, override at link time.
2844 __ Bind(label); // Bind after BL.
2845 break;
2846 }
2847 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2848 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2849 // LR prepared above for better instruction scheduling.
2850 DCHECK(direct_code_loaded);
2851 // lr()
2852 __ Blr(lr);
2853 break;
2854 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2855 // LR = callee_method->entry_point_from_quick_compiled_code_;
2856 __ Ldr(lr, MemOperand(
2857 XRegisterFrom(callee_method).X(),
2858 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
2859 // lr()
2860 __ Blr(lr);
2861 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002862 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002863
Andreas Gampe878d58c2015-01-15 23:24:00 -08002864 DCHECK(!IsLeafMethod());
2865}
2866
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002867void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
2868 LocationSummary* locations = invoke->GetLocations();
2869 Location receiver = locations->InAt(0);
2870 Register temp = XRegisterFrom(temp_in);
2871 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2872 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
2873 Offset class_offset = mirror::Object::ClassOffset();
2874 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
2875
2876 BlockPoolsScope block_pools(GetVIXLAssembler());
2877
2878 DCHECK(receiver.IsRegister());
2879 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
2880 MaybeRecordImplicitNullCheck(invoke);
2881 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
2882 // temp = temp->GetMethodAt(method_offset);
2883 __ Ldr(temp, MemOperand(temp, method_offset));
2884 // lr = temp->GetEntryPoint();
2885 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
2886 // lr();
2887 __ Blr(lr);
2888}
2889
Vladimir Marko58155012015-08-19 12:49:41 +00002890void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
2891 DCHECK(linker_patches->empty());
2892 size_t size =
2893 method_patches_.size() +
2894 call_patches_.size() +
2895 relative_call_patches_.size() +
2896 pc_rel_dex_cache_patches_.size();
2897 linker_patches->reserve(size);
2898 for (const auto& entry : method_patches_) {
2899 const MethodReference& target_method = entry.first;
2900 vixl::Literal<uint64_t>* literal = entry.second;
2901 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
2902 target_method.dex_file,
2903 target_method.dex_method_index));
2904 }
2905 for (const auto& entry : call_patches_) {
2906 const MethodReference& target_method = entry.first;
2907 vixl::Literal<uint64_t>* literal = entry.second;
2908 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
2909 target_method.dex_file,
2910 target_method.dex_method_index));
2911 }
2912 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
2913 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location() - 4u,
2914 info.target_method.dex_file,
2915 info.target_method.dex_method_index));
2916 }
2917 for (const PcRelativeDexCacheAccessInfo& info : pc_rel_dex_cache_patches_) {
2918 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location() - 4u,
2919 &info.target_dex_file,
2920 info.pc_insn_label->location() - 4u,
2921 info.element_offset));
2922 }
2923}
2924
2925vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
2926 // Look up the literal for value.
2927 auto lb = uint64_literals_.lower_bound(value);
2928 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
2929 return lb->second;
2930 }
2931 // We don't have a literal for this value, insert a new one.
2932 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
2933 uint64_literals_.PutBefore(lb, value, literal);
2934 return literal;
2935}
2936
2937vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
2938 MethodReference target_method,
2939 MethodToLiteralMap* map) {
2940 // Look up the literal for target_method.
2941 auto lb = map->lower_bound(target_method);
2942 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
2943 return lb->second;
2944 }
2945 // We don't have a literal for this method yet, insert a new one.
2946 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
2947 map->PutBefore(lb, target_method, literal);
2948 return literal;
2949}
2950
2951vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
2952 MethodReference target_method) {
2953 return DeduplicateMethodLiteral(target_method, &method_patches_);
2954}
2955
2956vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
2957 MethodReference target_method) {
2958 return DeduplicateMethodLiteral(target_method, &call_patches_);
2959}
2960
2961
Andreas Gampe878d58c2015-01-15 23:24:00 -08002962void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002963 // When we do not run baseline, explicit clinit checks triggered by static
2964 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2965 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002966
Andreas Gampe878d58c2015-01-15 23:24:00 -08002967 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2968 return;
2969 }
2970
Alexandre Ramesd921d642015-04-16 15:07:16 +01002971 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002972 LocationSummary* locations = invoke->GetLocations();
2973 codegen_->GenerateStaticOrDirectCall(
2974 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002975 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01002976}
2977
2978void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002979 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2980 return;
2981 }
2982
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002983 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01002984 DCHECK(!codegen_->IsLeafMethod());
2985 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2986}
2987
Alexandre Rames67555f72014-11-18 10:55:16 +00002988void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
2989 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2990 : LocationSummary::kNoCall;
2991 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002992 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002993 locations->SetOut(Location::RequiresRegister());
2994}
2995
2996void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
2997 Register out = OutputRegister(cls);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002998 Register current_method = InputRegisterAt(cls, 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00002999 if (cls->IsReferrersClass()) {
3000 DCHECK(!cls->CanCallRuntime());
3001 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003002 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003003 } else {
3004 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003005 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3006 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3007 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3008 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003009
3010 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3011 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3012 codegen_->AddSlowPath(slow_path);
3013 __ Cbz(out, slow_path->GetEntryLabel());
3014 if (cls->MustGenerateClinitCheck()) {
3015 GenerateClassInitializationCheck(slow_path, out);
3016 } else {
3017 __ Bind(slow_path->GetExitLabel());
3018 }
3019 }
3020}
3021
David Brazdilcb1c0552015-08-04 16:22:25 +01003022static MemOperand GetExceptionTlsAddress() {
3023 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3024}
3025
Alexandre Rames67555f72014-11-18 10:55:16 +00003026void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3027 LocationSummary* locations =
3028 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3029 locations->SetOut(Location::RequiresRegister());
3030}
3031
3032void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003033 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3034}
3035
3036void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3037 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3038}
3039
3040void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3041 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003042}
3043
Alexandre Rames5319def2014-10-23 10:03:10 +01003044void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3045 load->SetLocations(nullptr);
3046}
3047
3048void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) {
3049 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003050 UNUSED(load);
Alexandre Rames5319def2014-10-23 10:03:10 +01003051}
3052
Alexandre Rames67555f72014-11-18 10:55:16 +00003053void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3054 LocationSummary* locations =
3055 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003056 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003057 locations->SetOut(Location::RequiresRegister());
3058}
3059
3060void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3061 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3062 codegen_->AddSlowPath(slow_path);
3063
3064 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003065 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003066 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003067 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3068 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3069 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003070 __ Cbz(out, slow_path->GetEntryLabel());
3071 __ Bind(slow_path->GetExitLabel());
3072}
3073
Alexandre Rames5319def2014-10-23 10:03:10 +01003074void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3075 local->SetLocations(nullptr);
3076}
3077
3078void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3079 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3080}
3081
3082void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3083 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3084 locations->SetOut(Location::ConstantLocation(constant));
3085}
3086
3087void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) {
3088 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003089 UNUSED(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01003090}
3091
Alexandre Rames67555f72014-11-18 10:55:16 +00003092void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3093 LocationSummary* locations =
3094 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3095 InvokeRuntimeCallingConvention calling_convention;
3096 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3097}
3098
3099void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3100 codegen_->InvokeRuntime(instruction->IsEnter()
3101 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3102 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003103 instruction->GetDexPc(),
3104 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003105 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003106}
3107
Alexandre Rames42d641b2014-10-27 14:00:51 +00003108void LocationsBuilderARM64::VisitMul(HMul* mul) {
3109 LocationSummary* locations =
3110 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3111 switch (mul->GetResultType()) {
3112 case Primitive::kPrimInt:
3113 case Primitive::kPrimLong:
3114 locations->SetInAt(0, Location::RequiresRegister());
3115 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003117 break;
3118
3119 case Primitive::kPrimFloat:
3120 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003121 locations->SetInAt(0, Location::RequiresFpuRegister());
3122 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003123 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003124 break;
3125
3126 default:
3127 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3128 }
3129}
3130
3131void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3132 switch (mul->GetResultType()) {
3133 case Primitive::kPrimInt:
3134 case Primitive::kPrimLong:
3135 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3136 break;
3137
3138 case Primitive::kPrimFloat:
3139 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003140 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003141 break;
3142
3143 default:
3144 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3145 }
3146}
3147
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003148void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3149 LocationSummary* locations =
3150 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3151 switch (neg->GetResultType()) {
3152 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003153 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003154 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003156 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003157
3158 case Primitive::kPrimFloat:
3159 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003160 locations->SetInAt(0, Location::RequiresFpuRegister());
3161 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003162 break;
3163
3164 default:
3165 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3166 }
3167}
3168
3169void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3170 switch (neg->GetResultType()) {
3171 case Primitive::kPrimInt:
3172 case Primitive::kPrimLong:
3173 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3174 break;
3175
3176 case Primitive::kPrimFloat:
3177 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003178 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003179 break;
3180
3181 default:
3182 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3183 }
3184}
3185
3186void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3187 LocationSummary* locations =
3188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3189 InvokeRuntimeCallingConvention calling_convention;
3190 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003191 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003192 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003193 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003194 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003195 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003196}
3197
3198void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3199 LocationSummary* locations = instruction->GetLocations();
3200 InvokeRuntimeCallingConvention calling_convention;
3201 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3202 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003203 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003204 // Note: if heap poisoning is enabled, the entry point takes cares
3205 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003206 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3207 instruction,
3208 instruction->GetDexPc(),
3209 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003210 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003211}
3212
Alexandre Rames5319def2014-10-23 10:03:10 +01003213void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3214 LocationSummary* locations =
3215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3216 InvokeRuntimeCallingConvention calling_convention;
3217 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003218 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003219 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003220 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003221}
3222
3223void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
3224 LocationSummary* locations = instruction->GetLocations();
3225 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3226 DCHECK(type_index.Is(w0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003227 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003228 // Note: if heap poisoning is enabled, the entry point takes cares
3229 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003230 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3231 instruction,
3232 instruction->GetDexPc(),
3233 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003234 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003235}
3236
3237void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3238 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003239 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003241}
3242
3243void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003244 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003245 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003246 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003247 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003248 break;
3249
3250 default:
3251 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3252 }
3253}
3254
David Brazdil66d126e2015-04-03 16:02:44 +01003255void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3257 locations->SetInAt(0, Location::RequiresRegister());
3258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3259}
3260
3261void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003262 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3263}
3264
Alexandre Rames5319def2014-10-23 10:03:10 +01003265void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003266 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3267 ? LocationSummary::kCallOnSlowPath
3268 : LocationSummary::kNoCall;
3269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003270 locations->SetInAt(0, Location::RequiresRegister());
3271 if (instruction->HasUses()) {
3272 locations->SetOut(Location::SameAsFirstInput());
3273 }
3274}
3275
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003276void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003277 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3278 return;
3279 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003280
Alexandre Ramesd921d642015-04-16 15:07:16 +01003281 BlockPoolsScope block_pools(GetVIXLAssembler());
3282 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003283 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3284 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3285}
3286
3287void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003288 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3289 codegen_->AddSlowPath(slow_path);
3290
3291 LocationSummary* locations = instruction->GetLocations();
3292 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003293
3294 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003295}
3296
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003297void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003298 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003299 GenerateImplicitNullCheck(instruction);
3300 } else {
3301 GenerateExplicitNullCheck(instruction);
3302 }
3303}
3304
Alexandre Rames67555f72014-11-18 10:55:16 +00003305void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3306 HandleBinaryOp(instruction);
3307}
3308
3309void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3310 HandleBinaryOp(instruction);
3311}
3312
Alexandre Rames3e69f162014-12-10 10:36:50 +00003313void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3314 LOG(FATAL) << "Unreachable";
3315}
3316
3317void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3318 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3319}
3320
Alexandre Rames5319def2014-10-23 10:03:10 +01003321void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3322 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3323 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3324 if (location.IsStackSlot()) {
3325 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3326 } else if (location.IsDoubleStackSlot()) {
3327 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3328 }
3329 locations->SetOut(location);
3330}
3331
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003332void InstructionCodeGeneratorARM64::VisitParameterValue(
3333 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003334 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003335}
3336
3337void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3338 LocationSummary* locations =
3339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003340 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003341}
3342
3343void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3344 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3345 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003346}
3347
3348void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3350 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3351 locations->SetInAt(i, Location::Any());
3352 }
3353 locations->SetOut(Location::Any());
3354}
3355
3356void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003357 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003358 LOG(FATAL) << "Unreachable";
3359}
3360
Serban Constantinescu02164b32014-11-13 14:05:07 +00003361void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003362 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003363 LocationSummary::CallKind call_kind =
3364 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3366
3367 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003368 case Primitive::kPrimInt:
3369 case Primitive::kPrimLong:
3370 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003371 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3373 break;
3374
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003375 case Primitive::kPrimFloat:
3376 case Primitive::kPrimDouble: {
3377 InvokeRuntimeCallingConvention calling_convention;
3378 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3379 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3380 locations->SetOut(calling_convention.GetReturnLocation(type));
3381
3382 break;
3383 }
3384
Serban Constantinescu02164b32014-11-13 14:05:07 +00003385 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003386 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003387 }
3388}
3389
3390void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3391 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003392
Serban Constantinescu02164b32014-11-13 14:05:07 +00003393 switch (type) {
3394 case Primitive::kPrimInt:
3395 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003396 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003397 break;
3398 }
3399
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003400 case Primitive::kPrimFloat:
3401 case Primitive::kPrimDouble: {
3402 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3403 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003404 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003405 break;
3406 }
3407
Serban Constantinescu02164b32014-11-13 14:05:07 +00003408 default:
3409 LOG(FATAL) << "Unexpected rem type " << type;
3410 }
3411}
3412
Calin Juravle27df7582015-04-17 19:12:31 +01003413void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3414 memory_barrier->SetLocations(nullptr);
3415}
3416
3417void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3418 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3419}
3420
Alexandre Rames5319def2014-10-23 10:03:10 +01003421void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3423 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003424 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003425}
3426
3427void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003428 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003429 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003430}
3431
3432void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3433 instruction->SetLocations(nullptr);
3434}
3435
3436void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003437 UNUSED(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003438 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003439}
3440
Serban Constantinescu02164b32014-11-13 14:05:07 +00003441void LocationsBuilderARM64::VisitShl(HShl* shl) {
3442 HandleShift(shl);
3443}
3444
3445void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3446 HandleShift(shl);
3447}
3448
3449void LocationsBuilderARM64::VisitShr(HShr* shr) {
3450 HandleShift(shr);
3451}
3452
3453void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3454 HandleShift(shr);
3455}
3456
Alexandre Rames5319def2014-10-23 10:03:10 +01003457void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3459 Primitive::Type field_type = store->InputAt(1)->GetType();
3460 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003461 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003462 case Primitive::kPrimBoolean:
3463 case Primitive::kPrimByte:
3464 case Primitive::kPrimChar:
3465 case Primitive::kPrimShort:
3466 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003467 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003468 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3469 break;
3470
3471 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003472 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003473 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3474 break;
3475
3476 default:
3477 LOG(FATAL) << "Unimplemented local type " << field_type;
3478 }
3479}
3480
3481void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003482 UNUSED(store);
Alexandre Rames5319def2014-10-23 10:03:10 +01003483}
3484
3485void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003486 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003487}
3488
3489void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003490 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003491}
3492
Alexandre Rames67555f72014-11-18 10:55:16 +00003493void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003494 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003495}
3496
3497void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003498 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003499}
3500
3501void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003502 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003503}
3504
Alexandre Rames67555f72014-11-18 10:55:16 +00003505void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003506 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003507}
3508
3509void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3511}
3512
3513void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003514 HBasicBlock* block = instruction->GetBlock();
3515 if (block->GetLoopInformation() != nullptr) {
3516 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3517 // The back edge will generate the suspend check.
3518 return;
3519 }
3520 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3521 // The goto will generate the suspend check.
3522 return;
3523 }
3524 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003525}
3526
3527void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3528 temp->SetLocations(nullptr);
3529}
3530
3531void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) {
3532 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003533 UNUSED(temp);
Alexandre Rames5319def2014-10-23 10:03:10 +01003534}
3535
Alexandre Rames67555f72014-11-18 10:55:16 +00003536void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3537 LocationSummary* locations =
3538 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3539 InvokeRuntimeCallingConvention calling_convention;
3540 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3541}
3542
3543void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3544 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003545 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003546 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003547}
3548
3549void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3550 LocationSummary* locations =
3551 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3552 Primitive::Type input_type = conversion->GetInputType();
3553 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003554 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003555 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3556 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3557 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3558 }
3559
Alexandre Rames542361f2015-01-29 16:57:31 +00003560 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003561 locations->SetInAt(0, Location::RequiresFpuRegister());
3562 } else {
3563 locations->SetInAt(0, Location::RequiresRegister());
3564 }
3565
Alexandre Rames542361f2015-01-29 16:57:31 +00003566 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003567 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3568 } else {
3569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3570 }
3571}
3572
3573void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3574 Primitive::Type result_type = conversion->GetResultType();
3575 Primitive::Type input_type = conversion->GetInputType();
3576
3577 DCHECK_NE(input_type, result_type);
3578
Alexandre Rames542361f2015-01-29 16:57:31 +00003579 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003580 int result_size = Primitive::ComponentSize(result_type);
3581 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003582 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003583 Register output = OutputRegister(conversion);
3584 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003585 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3586 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003587 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3588 // 'int' values are used directly as W registers, discarding the top
3589 // bits, so we don't need to sign-extend and can just perform a move.
3590 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3591 // top 32 bits of the target register. We theoretically could leave those
3592 // bits unchanged, but we would have to make sure that no code uses a
3593 // 32bit input value as a 64bit value assuming that the top 32 bits are
3594 // zero.
3595 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003596 } else if ((result_type == Primitive::kPrimChar) ||
3597 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3598 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003599 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003600 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003601 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003602 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003603 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003604 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003605 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3606 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003607 } else if (Primitive::IsFloatingPointType(result_type) &&
3608 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003609 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3610 } else {
3611 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3612 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003613 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003614}
Alexandre Rames67555f72014-11-18 10:55:16 +00003615
Serban Constantinescu02164b32014-11-13 14:05:07 +00003616void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3617 HandleShift(ushr);
3618}
3619
3620void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3621 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003622}
3623
3624void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3625 HandleBinaryOp(instruction);
3626}
3627
3628void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3629 HandleBinaryOp(instruction);
3630}
3631
Calin Juravleb1498f62015-02-16 13:13:29 +00003632void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction) {
3633 // Nothing to do, this should be removed during prepare for register allocator.
3634 UNUSED(instruction);
3635 LOG(FATAL) << "Unreachable";
3636}
3637
3638void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction) {
3639 // Nothing to do, this should be removed during prepare for register allocator.
3640 UNUSED(instruction);
3641 LOG(FATAL) << "Unreachable";
3642}
3643
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003644void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3645 DCHECK(codegen_->IsBaseline());
3646 LocationSummary* locations =
3647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3648 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3649}
3650
3651void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3652 DCHECK(codegen_->IsBaseline());
3653 // Will be generated at use site.
3654}
3655
Mark Mendellfe57faa2015-09-18 09:26:15 -04003656// Simple implementation of packed switch - generate cascaded compare/jumps.
3657void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3658 LocationSummary* locations =
3659 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3660 locations->SetInAt(0, Location::RequiresRegister());
3661}
3662
3663void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3664 int32_t lower_bound = switch_instr->GetStartValue();
3665 int32_t num_entries = switch_instr->GetNumEntries();
3666 Register value_reg = InputRegisterAt(switch_instr, 0);
3667 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3668
3669 // Create a series of compare/jumps.
3670 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3671 for (int32_t i = 0; i < num_entries; i++) {
3672 int32_t case_value = lower_bound + i;
3673 vixl::Label* succ = codegen_->GetLabelOf(successors.at(i));
3674 if (case_value == 0) {
3675 __ Cbz(value_reg, succ);
3676 } else {
3677 __ Cmp(value_reg, vixl::Operand(case_value));
3678 __ B(eq, succ);
3679 }
3680 }
3681
3682 // And the default for any other value.
3683 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3684 __ B(codegen_->GetLabelOf(default_block));
3685 }
3686}
3687
Alexandre Rames67555f72014-11-18 10:55:16 +00003688#undef __
3689#undef QUICK_ENTRY_POINT
3690
Alexandre Rames5319def2014-10-23 10:03:10 +01003691} // namespace arm64
3692} // namespace art