blob: 5f8b8d4a86899ee437250e33ebb187fe76a7f356 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
45namespace arm64 {
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047using helpers::CPURegisterFrom;
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
51using helpers::HeapOperandFrom;
52using helpers::InputCPURegisterAt;
53using helpers::InputFPRegisterAt;
54using helpers::InputRegisterAt;
55using helpers::InputOperandAt;
56using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::LocationFrom;
58using helpers::OperandFromMemOperand;
59using helpers::OutputCPURegister;
60using helpers::OutputFPRegister;
61using helpers::OutputRegister;
62using helpers::RegisterFrom;
63using helpers::StackOperandFrom;
64using helpers::VIXLRegCodeFromART;
65using helpers::WRegisterFrom;
66using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000067using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080068using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069
Alexandre Rames5319def2014-10-23 10:03:10 +010070static constexpr int kCurrentMethodStackOffset = 0;
Zheng Xu3927c8b2015-11-18 17:46:25 +080071// The compare/jump sequence will generate about (2 * num_entries + 1) instructions. While jump
72// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
73// generates less code/data with a small num_entries.
74static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
Alexandre Rames5319def2014-10-23 10:03:10 +010075
Alexandre Rames5319def2014-10-23 10:03:10 +010076inline Condition ARM64Condition(IfCondition cond) {
77 switch (cond) {
78 case kCondEQ: return eq;
79 case kCondNE: return ne;
80 case kCondLT: return lt;
81 case kCondLE: return le;
82 case kCondGT: return gt;
83 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070084 case kCondB: return lo;
85 case kCondBE: return ls;
86 case kCondA: return hi;
87 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010088 }
Roland Levillain7f63c522015-07-13 15:54:55 +000089 LOG(FATAL) << "Unreachable";
90 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010091}
92
Alexandre Ramesa89086e2014-11-07 17:13:25 +000093Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000094 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
95 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
96 // but we use the exact registers for clarity.
97 if (return_type == Primitive::kPrimFloat) {
98 return LocationFrom(s0);
99 } else if (return_type == Primitive::kPrimDouble) {
100 return LocationFrom(d0);
101 } else if (return_type == Primitive::kPrimLong) {
102 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100103 } else if (return_type == Primitive::kPrimVoid) {
104 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000105 } else {
106 return LocationFrom(w0);
107 }
108}
109
Alexandre Rames5319def2014-10-23 10:03:10 +0100110Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000111 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100112}
113
Alexandre Rames67555f72014-11-18 10:55:16 +0000114#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
115#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100116
Zheng Xuda403092015-04-24 17:35:39 +0800117// Calculate memory accessing operand for save/restore live registers.
118static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
119 RegisterSet* register_set,
120 int64_t spill_offset,
121 bool is_save) {
122 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
123 codegen->GetNumberOfCoreRegisters(),
124 register_set->GetFloatingPointRegisters(),
125 codegen->GetNumberOfFloatingPointRegisters()));
126
127 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
128 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000129 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
130 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800131
132 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
133 UseScratchRegisterScope temps(masm);
134
135 Register base = masm->StackPointer();
136 int64_t core_spill_size = core_list.TotalSizeInBytes();
137 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
138 int64_t reg_size = kXRegSizeInBytes;
139 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
140 uint32_t ls_access_size = WhichPowerOf2(reg_size);
141 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
142 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
143 // If the offset does not fit in the instruction's immediate field, use an alternate register
144 // to compute the base address(float point registers spill base address).
145 Register new_base = temps.AcquireSameSizeAs(base);
146 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
147 base = new_base;
148 spill_offset = -core_spill_size;
149 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
150 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
151 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
152 }
153
154 if (is_save) {
155 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
156 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
157 } else {
158 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
159 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
160 }
161}
162
163void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
164 RegisterSet* register_set = locations->GetLiveRegisters();
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
167 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
168 // If the register holds an object, update the stack mask.
169 if (locations->RegisterContainsObject(i)) {
170 locations->SetStackBit(stack_offset / kVRegSize);
171 }
172 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
173 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
174 saved_core_stack_offsets_[i] = stack_offset;
175 stack_offset += kXRegSizeInBytes;
176 }
177 }
178
179 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
180 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
181 register_set->ContainsFloatingPointRegister(i)) {
182 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
183 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
184 saved_fpu_stack_offsets_[i] = stack_offset;
185 stack_offset += kDRegSizeInBytes;
186 }
187 }
188
189 SaveRestoreLiveRegistersHelper(codegen, register_set,
190 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
191}
192
193void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
194 RegisterSet* register_set = locations->GetLiveRegisters();
195 SaveRestoreLiveRegistersHelper(codegen, register_set,
196 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
197}
198
Alexandre Rames5319def2014-10-23 10:03:10 +0100199class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
200 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100201 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100202
Alexandre Rames67555f72014-11-18 10:55:16 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000205 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100206
Alexandre Rames5319def2014-10-23 10:03:10 +0100207 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000208 if (instruction_->CanThrowIntoCatchBlock()) {
209 // Live registers will be restored in the catch block if caught.
210 SaveLiveRegisters(codegen, instruction_->GetLocations());
211 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000212 // We're moving two locations to locations that could overlap, so we need a parallel
213 // move resolver.
214 InvokeRuntimeCallingConvention calling_convention;
215 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100216 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
217 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000218 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000219 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800220 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100221 }
222
Alexandre Rames8158f282015-08-07 10:26:17 +0100223 bool IsFatal() const OVERRIDE { return true; }
224
Alexandre Rames9931f312015-06-19 14:47:01 +0100225 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
231};
232
Alexandre Rames67555f72014-11-18 10:55:16 +0000233class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
234 public:
235 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
236
237 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
238 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
239 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000240 if (instruction_->CanThrowIntoCatchBlock()) {
241 // Live registers will be restored in the catch block if caught.
242 SaveLiveRegisters(codegen, instruction_->GetLocations());
243 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000244 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000245 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253 private:
254 HDivZeroCheck* const instruction_;
255 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
256};
257
258class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
259 public:
260 LoadClassSlowPathARM64(HLoadClass* cls,
261 HInstruction* at,
262 uint32_t dex_pc,
263 bool do_clinit)
264 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
265 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
266 }
267
268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 LocationSummary* locations = at_->GetLocations();
270 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
271
272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000274
275 InvokeRuntimeCallingConvention calling_convention;
276 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
278 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000279 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800280 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800282 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800284 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 // Move the class to the desired location.
287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000291 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 }
293
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 __ B(GetExitLabel());
296 }
297
Alexandre Rames9931f312015-06-19 14:47:01 +0100298 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
299
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 private:
301 // The class this slow path will load.
302 HLoadClass* const cls_;
303
304 // The instruction where this slow path is happening.
305 // (Might be the load class or an initialization check).
306 HInstruction* const at_;
307
308 // The dex PC of `at_`.
309 const uint32_t dex_pc_;
310
311 // Whether to initialize the class.
312 const bool do_clinit_;
313
314 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
315};
316
317class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
318 public:
319 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 LocationSummary* locations = instruction_->GetLocations();
323 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
324 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
325
326 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000327 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
329 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800330 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000331 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000332 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100333 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000335 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000336
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000337 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000338 __ B(GetExitLabel());
339 }
340
Alexandre Rames9931f312015-06-19 14:47:01 +0100341 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
342
Alexandre Rames67555f72014-11-18 10:55:16 +0000343 private:
344 HLoadString* const instruction_;
345
346 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
347};
348
Alexandre Rames5319def2014-10-23 10:03:10 +0100349class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
350 public:
351 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
352
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
354 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100355 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000356 if (instruction_->CanThrowIntoCatchBlock()) {
357 // Live registers will be restored in the catch block if caught.
358 SaveLiveRegisters(codegen, instruction_->GetLocations());
359 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000361 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800362 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100363 }
364
Alexandre Rames8158f282015-08-07 10:26:17 +0100365 bool IsFatal() const OVERRIDE { return true; }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369 private:
370 HNullCheck* const instruction_;
371
372 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
373};
374
375class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
376 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100377 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100378 : instruction_(instruction), successor_(successor) {}
379
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
381 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000384 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000385 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800386 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000387 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000388 if (successor_ == nullptr) {
389 __ B(GetReturnLabel());
390 } else {
391 __ B(arm64_codegen->GetLabelOf(successor_));
392 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100393 }
394
395 vixl::Label* GetReturnLabel() {
396 DCHECK(successor_ == nullptr);
397 return &return_label_;
398 }
399
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100400 HBasicBlock* GetSuccessor() const {
401 return successor_;
402 }
403
Alexandre Rames9931f312015-06-19 14:47:01 +0100404 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
405
Alexandre Rames5319def2014-10-23 10:03:10 +0100406 private:
407 HSuspendCheck* const instruction_;
408 // If not null, the block to branch to after the suspend check.
409 HBasicBlock* const successor_;
410
411 // If `successor_` is null, the label to branch to after the suspend check.
412 vixl::Label return_label_;
413
414 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
415};
416
Alexandre Rames67555f72014-11-18 10:55:16 +0000417class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
418 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000419 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
420 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000423 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100424 Location class_to_check = locations->InAt(1);
425 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
426 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000427 DCHECK(instruction_->IsCheckCast()
428 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
429 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100430 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000431
Alexandre Rames67555f72014-11-18 10:55:16 +0000432 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000433
434 if (instruction_->IsCheckCast()) {
435 // The codegen for the instruction overwrites `temp`, so put it back in place.
436 Register obj = InputRegisterAt(instruction_, 0);
437 Register temp = WRegisterFrom(locations->GetTemp(0));
438 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
439 __ Ldr(temp, HeapOperand(obj, class_offset));
440 arm64_codegen->GetAssembler()->MaybeUnpoisonHeapReference(temp);
441 }
442
443 if (!is_fatal_) {
444 SaveLiveRegisters(codegen, locations);
445 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
447 // We're moving two locations to locations that could overlap, so we need a parallel
448 // move resolver.
449 InvokeRuntimeCallingConvention calling_convention;
450 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
452 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453
454 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000455 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100456 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000457 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
458 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459 Primitive::Type ret_type = instruction_->GetType();
460 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
461 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
462 } else {
463 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100464 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800465 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000466 }
467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000468 if (!is_fatal_) {
469 RestoreLiveRegisters(codegen, locations);
470 __ B(GetExitLabel());
471 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000472 }
473
Alexandre Rames9931f312015-06-19 14:47:01 +0100474 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100476
Alexandre Rames67555f72014-11-18 10:55:16 +0000477 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000479 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000480
Alexandre Rames67555f72014-11-18 10:55:16 +0000481 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
482};
483
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700484class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
485 public:
486 explicit DeoptimizationSlowPathARM64(HInstruction* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100487 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700488
489 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
490 __ Bind(GetEntryLabel());
491 SaveLiveRegisters(codegen, instruction_->GetLocations());
492 DCHECK(instruction_->IsDeoptimize());
493 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
494 uint32_t dex_pc = deoptimize->GetDexPc();
495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
496 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000497 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 }
499
Alexandre Rames9931f312015-06-19 14:47:01 +0100500 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
501
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700502 private:
503 HInstruction* const instruction_;
504 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
505};
506
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100507class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
508 public:
509 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
510
511 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
512 LocationSummary* locations = instruction_->GetLocations();
513 __ Bind(GetEntryLabel());
514 SaveLiveRegisters(codegen, locations);
515
516 InvokeRuntimeCallingConvention calling_convention;
517 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
518 parallel_move.AddMove(
519 locations->InAt(0),
520 LocationFrom(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(1),
525 LocationFrom(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimInt,
527 nullptr);
528 parallel_move.AddMove(
529 locations->InAt(2),
530 LocationFrom(calling_convention.GetRegisterAt(2)),
531 Primitive::kPrimNot,
532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534
535 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
536 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
537 instruction_,
538 instruction_->GetDexPc(),
539 this);
540 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
541 RestoreLiveRegisters(codegen, locations);
542 __ B(GetExitLabel());
543 }
544
545 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
546
547 private:
548 HInstruction* const instruction_;
549
550 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
551};
552
Zheng Xu3927c8b2015-11-18 17:46:25 +0800553void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
554 uint32_t num_entries = switch_instr_->GetNumEntries();
555 DCHECK_GE(num_entries, kPackedSwitchJumpTableThreshold);
556
557 // We are about to use the assembler to place literals directly. Make sure we have enough
558 // underlying code buffer and we have generated the jump table with right size.
559 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
560 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
561
562 __ Bind(&table_start_);
563 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
564 for (uint32_t i = 0; i < num_entries; i++) {
565 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
566 DCHECK(target_label->IsBound());
567 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
568 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
569 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
570 Literal<int32_t> literal(jump_offset);
571 __ place(&literal);
572 }
573}
574
Alexandre Rames5319def2014-10-23 10:03:10 +0100575#undef __
576
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100577Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100578 Location next_location;
579 if (type == Primitive::kPrimVoid) {
580 LOG(FATAL) << "Unreachable type " << type;
581 }
582
Alexandre Rames542361f2015-01-29 16:57:31 +0000583 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100584 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
585 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000586 } else if (!Primitive::IsFloatingPointType(type) &&
587 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000588 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
589 } else {
590 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000591 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
592 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100593 }
594
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000595 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000596 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100597 return next_location;
598}
599
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100600Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100601 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100602}
603
Serban Constantinescu579885a2015-02-22 20:51:33 +0000604CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
605 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100606 const CompilerOptions& compiler_options,
607 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100608 : CodeGenerator(graph,
609 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000610 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000611 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000612 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000613 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100614 compiler_options,
615 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100616 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800617 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100618 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000619 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000620 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000621 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100622 uint64_literals_(std::less<uint64_t>(),
623 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
624 method_patches_(MethodReferenceComparator(),
625 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
626 call_patches_(MethodReferenceComparator(),
627 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
628 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000629 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000630 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000631 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000632}
Alexandre Rames5319def2014-10-23 10:03:10 +0100633
Alexandre Rames67555f72014-11-18 10:55:16 +0000634#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100635
Zheng Xu3927c8b2015-11-18 17:46:25 +0800636void CodeGeneratorARM64::EmitJumpTables() {
637 for (auto jump_table : jump_tables_) {
638 jump_table->EmitTable(this);
639 }
640}
641
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000642void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800643 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000644 // Ensure we emit the literal pool.
645 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000646
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000647 CodeGenerator::Finalize(allocator);
648}
649
Zheng Xuad4450e2015-04-17 18:48:56 +0800650void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
651 // Note: There are 6 kinds of moves:
652 // 1. constant -> GPR/FPR (non-cycle)
653 // 2. constant -> stack (non-cycle)
654 // 3. GPR/FPR -> GPR/FPR
655 // 4. GPR/FPR -> stack
656 // 5. stack -> GPR/FPR
657 // 6. stack -> stack (non-cycle)
658 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
659 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
660 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
661 // dependency.
662 vixl_temps_.Open(GetVIXLAssembler());
663}
664
665void ParallelMoveResolverARM64::FinishEmitNativeCode() {
666 vixl_temps_.Close();
667}
668
669Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
670 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
671 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
672 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
673 Location scratch = GetScratchLocation(kind);
674 if (!scratch.Equals(Location::NoLocation())) {
675 return scratch;
676 }
677 // Allocate from VIXL temp registers.
678 if (kind == Location::kRegister) {
679 scratch = LocationFrom(vixl_temps_.AcquireX());
680 } else {
681 DCHECK(kind == Location::kFpuRegister);
682 scratch = LocationFrom(vixl_temps_.AcquireD());
683 }
684 AddScratchLocation(scratch);
685 return scratch;
686}
687
688void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
689 if (loc.IsRegister()) {
690 vixl_temps_.Release(XRegisterFrom(loc));
691 } else {
692 DCHECK(loc.IsFpuRegister());
693 vixl_temps_.Release(DRegisterFrom(loc));
694 }
695 RemoveScratchLocation(loc);
696}
697
Alexandre Rames3e69f162014-12-10 10:36:50 +0000698void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100699 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100700 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000701}
702
Alexandre Rames5319def2014-10-23 10:03:10 +0100703void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100704 MacroAssembler* masm = GetVIXLAssembler();
705 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000706 __ Bind(&frame_entry_label_);
707
Serban Constantinescu02164b32014-11-13 14:05:07 +0000708 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
709 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100710 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000711 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000712 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000713 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000714 __ Ldr(wzr, MemOperand(temp, 0));
715 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000716 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100717
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000718 if (!HasEmptyFrame()) {
719 int frame_size = GetFrameSize();
720 // Stack layout:
721 // sp[frame_size - 8] : lr.
722 // ... : other preserved core registers.
723 // ... : other preserved fp registers.
724 // ... : reserved frame space.
725 // sp[0] : current method.
726 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100727 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800728 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
729 frame_size - GetCoreSpillSize());
730 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
731 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000732 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100733}
734
735void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100736 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100737 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000738 if (!HasEmptyFrame()) {
739 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800740 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
741 frame_size - FrameEntrySpillSize());
742 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
743 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000744 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100745 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000746 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100747 __ Ret();
748 GetAssembler()->cfi().RestoreState();
749 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100750}
751
Zheng Xuda403092015-04-24 17:35:39 +0800752vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
753 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
754 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
755 core_spill_mask_);
756}
757
758vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
759 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
760 GetNumberOfFloatingPointRegisters()));
761 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
762 fpu_spill_mask_);
763}
764
Alexandre Rames5319def2014-10-23 10:03:10 +0100765void CodeGeneratorARM64::Bind(HBasicBlock* block) {
766 __ Bind(GetLabelOf(block));
767}
768
Alexandre Rames5319def2014-10-23 10:03:10 +0100769void CodeGeneratorARM64::Move(HInstruction* instruction,
770 Location location,
771 HInstruction* move_for) {
772 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100773 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000774 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100775
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100776 if (instruction->IsFakeString()) {
777 // The fake string is an alias for null.
778 DCHECK(IsBaseline());
779 instruction = locations->Out().GetConstant();
780 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
781 }
782
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100783 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100784 MoveLocation(location,
785 Location::DoubleStackSlot(kCurrentMethodStackOffset),
786 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100787 } else if (locations != nullptr && locations->Out().Equals(location)) {
788 return;
789 } else if (instruction->IsIntConstant()
790 || instruction->IsLongConstant()
791 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000792 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100793 if (location.IsRegister()) {
794 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000795 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100796 (instruction->IsLongConstant() && dst.Is64Bits()));
797 __ Mov(dst, value);
798 } else {
799 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000800 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000801 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
802 ? temps.AcquireW()
803 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100804 __ Mov(temp, value);
805 __ Str(temp, StackOperandFrom(location));
806 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000807 } else if (instruction->IsTemporary()) {
808 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000809 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100810 } else if (instruction->IsLoadLocal()) {
811 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000812 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000813 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000814 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000815 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100816 }
817
818 } else {
819 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000820 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100821 }
822}
823
Calin Juravle175dc732015-08-25 15:42:32 +0100824void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
825 DCHECK(location.IsRegister());
826 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
827}
828
Calin Juravlee460d1d2015-09-29 04:52:17 +0100829void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
830 if (location.IsRegister()) {
831 locations->AddTemp(location);
832 } else {
833 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
834 }
835}
836
Alexandre Rames5319def2014-10-23 10:03:10 +0100837Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
838 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000839
Alexandre Rames5319def2014-10-23 10:03:10 +0100840 switch (type) {
841 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000842 case Primitive::kPrimInt:
843 case Primitive::kPrimFloat:
844 return Location::StackSlot(GetStackSlot(load->GetLocal()));
845
846 case Primitive::kPrimLong:
847 case Primitive::kPrimDouble:
848 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
849
Alexandre Rames5319def2014-10-23 10:03:10 +0100850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100854 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100855 LOG(FATAL) << "Unexpected type " << type;
856 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000857
Alexandre Rames5319def2014-10-23 10:03:10 +0100858 LOG(FATAL) << "Unreachable";
859 return Location::NoLocation();
860}
861
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100862void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000863 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100864 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000865 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100866 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100867 if (value_can_be_null) {
868 __ Cbz(value, &done);
869 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100870 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
871 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000872 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100873 if (value_can_be_null) {
874 __ Bind(&done);
875 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100876}
877
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000878void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
879 // Blocked core registers:
880 // lr : Runtime reserved.
881 // tr : Runtime reserved.
882 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
883 // ip1 : VIXL core temp.
884 // ip0 : VIXL core temp.
885 //
886 // Blocked fp registers:
887 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100888 CPURegList reserved_core_registers = vixl_reserved_core_registers;
889 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100890 while (!reserved_core_registers.IsEmpty()) {
891 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
892 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000893
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000894 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800895 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000896 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
897 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000898
899 if (is_baseline) {
900 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
901 while (!reserved_core_baseline_registers.IsEmpty()) {
902 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
903 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100904 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000905
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100906 if (is_baseline || GetGraph()->IsDebuggable()) {
907 // Stubs do not save callee-save floating point registers. If the graph
908 // is debuggable, we need to deal with these registers differently. For
909 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000910 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
911 while (!reserved_fp_baseline_registers.IsEmpty()) {
912 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
913 }
914 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100915}
916
917Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
918 if (type == Primitive::kPrimVoid) {
919 LOG(FATAL) << "Unreachable type " << type;
920 }
921
Alexandre Rames542361f2015-01-29 16:57:31 +0000922 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000923 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
924 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100925 return Location::FpuRegisterLocation(reg);
926 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000927 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
928 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100929 return Location::RegisterLocation(reg);
930 }
931}
932
Alexandre Rames3e69f162014-12-10 10:36:50 +0000933size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
934 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
935 __ Str(reg, MemOperand(sp, stack_index));
936 return kArm64WordSize;
937}
938
939size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
940 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
941 __ Ldr(reg, MemOperand(sp, stack_index));
942 return kArm64WordSize;
943}
944
945size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
946 FPRegister reg = FPRegister(reg_id, kDRegSize);
947 __ Str(reg, MemOperand(sp, stack_index));
948 return kArm64WordSize;
949}
950
951size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
952 FPRegister reg = FPRegister(reg_id, kDRegSize);
953 __ Ldr(reg, MemOperand(sp, stack_index));
954 return kArm64WordSize;
955}
956
Alexandre Rames5319def2014-10-23 10:03:10 +0100957void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100959}
960
961void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100962 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100963}
964
Alexandre Rames67555f72014-11-18 10:55:16 +0000965void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000966 if (constant->IsIntConstant()) {
967 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
968 } else if (constant->IsLongConstant()) {
969 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
970 } else if (constant->IsNullConstant()) {
971 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000972 } else if (constant->IsFloatConstant()) {
973 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
974 } else {
975 DCHECK(constant->IsDoubleConstant());
976 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
977 }
978}
979
Alexandre Rames3e69f162014-12-10 10:36:50 +0000980
981static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
982 DCHECK(constant.IsConstant());
983 HConstant* cst = constant.GetConstant();
984 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000985 // Null is mapped to a core W register, which we associate with kPrimInt.
986 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000987 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
988 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
989 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
990}
991
Calin Juravlee460d1d2015-09-29 04:52:17 +0100992void CodeGeneratorARM64::MoveLocation(Location destination,
993 Location source,
994 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000995 if (source.Equals(destination)) {
996 return;
997 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000998
999 // A valid move can always be inferred from the destination and source
1000 // locations. When moving from and to a register, the argument type can be
1001 // used to generate 32bit instead of 64bit moves. In debug mode we also
1002 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001003 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001004
1005 if (destination.IsRegister() || destination.IsFpuRegister()) {
1006 if (unspecified_type) {
1007 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1008 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001009 (src_cst != nullptr && (src_cst->IsIntConstant()
1010 || src_cst->IsFloatConstant()
1011 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001012 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001013 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001014 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001015 // If the source is a double stack slot or a 64bit constant, a 64bit
1016 // type is appropriate. Else the source is a register, and since the
1017 // type has not been specified, we chose a 64bit type to force a 64bit
1018 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001019 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001020 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001021 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001022 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1023 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1024 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001025 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1026 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1027 __ Ldr(dst, StackOperandFrom(source));
1028 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001029 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001030 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001031 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001032 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001033 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001034 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001035 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001036 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1037 ? Primitive::kPrimLong
1038 : Primitive::kPrimInt;
1039 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1040 }
1041 } else {
1042 DCHECK(source.IsFpuRegister());
1043 if (destination.IsRegister()) {
1044 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1045 ? Primitive::kPrimDouble
1046 : Primitive::kPrimFloat;
1047 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1048 } else {
1049 DCHECK(destination.IsFpuRegister());
1050 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001051 }
1052 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001053 } else { // The destination is not a register. It must be a stack slot.
1054 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1055 if (source.IsRegister() || source.IsFpuRegister()) {
1056 if (unspecified_type) {
1057 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001058 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001059 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001060 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001061 }
1062 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001063 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1064 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1065 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001066 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001067 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1068 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001069 UseScratchRegisterScope temps(GetVIXLAssembler());
1070 HConstant* src_cst = source.GetConstant();
1071 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001072 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001073 temp = temps.AcquireW();
1074 } else if (src_cst->IsLongConstant()) {
1075 temp = temps.AcquireX();
1076 } else if (src_cst->IsFloatConstant()) {
1077 temp = temps.AcquireS();
1078 } else {
1079 DCHECK(src_cst->IsDoubleConstant());
1080 temp = temps.AcquireD();
1081 }
1082 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001083 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001084 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001085 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001086 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001087 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001088 // There is generally less pressure on FP registers.
1089 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001090 __ Ldr(temp, StackOperandFrom(source));
1091 __ Str(temp, StackOperandFrom(destination));
1092 }
1093 }
1094}
1095
1096void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001097 CPURegister dst,
1098 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001099 switch (type) {
1100 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001102 break;
1103 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001104 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001105 break;
1106 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001107 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001108 break;
1109 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001110 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001111 break;
1112 case Primitive::kPrimInt:
1113 case Primitive::kPrimNot:
1114 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001115 case Primitive::kPrimFloat:
1116 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001117 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001118 __ Ldr(dst, src);
1119 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001120 case Primitive::kPrimVoid:
1121 LOG(FATAL) << "Unreachable type " << type;
1122 }
1123}
1124
Calin Juravle77520bc2015-01-12 18:45:46 +00001125void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001126 CPURegister dst,
1127 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001128 MacroAssembler* masm = GetVIXLAssembler();
1129 BlockPoolsScope block_pools(masm);
1130 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001131 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001132 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001133
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001134 DCHECK(!src.IsPreIndex());
1135 DCHECK(!src.IsPostIndex());
1136
1137 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001138 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001139 MemOperand base = MemOperand(temp_base);
1140 switch (type) {
1141 case Primitive::kPrimBoolean:
1142 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001143 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001144 break;
1145 case Primitive::kPrimByte:
1146 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001147 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001148 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1149 break;
1150 case Primitive::kPrimChar:
1151 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001152 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001153 break;
1154 case Primitive::kPrimShort:
1155 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001156 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001157 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1158 break;
1159 case Primitive::kPrimInt:
1160 case Primitive::kPrimNot:
1161 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001162 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001163 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001164 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001165 break;
1166 case Primitive::kPrimFloat:
1167 case Primitive::kPrimDouble: {
1168 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001169 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001170
1171 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1172 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001173 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001174 __ Fmov(FPRegister(dst), temp);
1175 break;
1176 }
1177 case Primitive::kPrimVoid:
1178 LOG(FATAL) << "Unreachable type " << type;
1179 }
1180}
1181
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001182void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001183 CPURegister src,
1184 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001185 switch (type) {
1186 case Primitive::kPrimBoolean:
1187 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001188 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001189 break;
1190 case Primitive::kPrimChar:
1191 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001192 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001193 break;
1194 case Primitive::kPrimInt:
1195 case Primitive::kPrimNot:
1196 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001197 case Primitive::kPrimFloat:
1198 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001199 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001200 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001201 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001202 case Primitive::kPrimVoid:
1203 LOG(FATAL) << "Unreachable type " << type;
1204 }
1205}
1206
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001207void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1208 CPURegister src,
1209 const MemOperand& dst) {
1210 UseScratchRegisterScope temps(GetVIXLAssembler());
1211 Register temp_base = temps.AcquireX();
1212
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001213 DCHECK(!dst.IsPreIndex());
1214 DCHECK(!dst.IsPostIndex());
1215
1216 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001217 Operand op = OperandFromMemOperand(dst);
1218 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001219 MemOperand base = MemOperand(temp_base);
1220 switch (type) {
1221 case Primitive::kPrimBoolean:
1222 case Primitive::kPrimByte:
1223 __ Stlrb(Register(src), base);
1224 break;
1225 case Primitive::kPrimChar:
1226 case Primitive::kPrimShort:
1227 __ Stlrh(Register(src), base);
1228 break;
1229 case Primitive::kPrimInt:
1230 case Primitive::kPrimNot:
1231 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001232 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001233 __ Stlr(Register(src), base);
1234 break;
1235 case Primitive::kPrimFloat:
1236 case Primitive::kPrimDouble: {
1237 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001238 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001239
1240 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1241 __ Fmov(temp, FPRegister(src));
1242 __ Stlr(temp, base);
1243 break;
1244 }
1245 case Primitive::kPrimVoid:
1246 LOG(FATAL) << "Unreachable type " << type;
1247 }
1248}
1249
Calin Juravle175dc732015-08-25 15:42:32 +01001250void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1251 HInstruction* instruction,
1252 uint32_t dex_pc,
1253 SlowPathCode* slow_path) {
1254 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1255 instruction,
1256 dex_pc,
1257 slow_path);
1258}
1259
Alexandre Rames67555f72014-11-18 10:55:16 +00001260void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1261 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001262 uint32_t dex_pc,
1263 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001264 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001265 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001266 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1267 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001268 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001269}
1270
1271void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1272 vixl::Register class_reg) {
1273 UseScratchRegisterScope temps(GetVIXLAssembler());
1274 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001275 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001276 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001277
Serban Constantinescu02164b32014-11-13 14:05:07 +00001278 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001279 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001280 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1281 __ Add(temp, class_reg, status_offset);
1282 __ Ldar(temp, HeapOperand(temp));
1283 __ Cmp(temp, mirror::Class::kStatusInitialized);
1284 __ B(lt, slow_path->GetEntryLabel());
1285 } else {
1286 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1287 __ Cmp(temp, mirror::Class::kStatusInitialized);
1288 __ B(lt, slow_path->GetEntryLabel());
1289 __ Dmb(InnerShareable, BarrierReads);
1290 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001291 __ Bind(slow_path->GetExitLabel());
1292}
Alexandre Rames5319def2014-10-23 10:03:10 +01001293
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001294void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1295 BarrierType type = BarrierAll;
1296
1297 switch (kind) {
1298 case MemBarrierKind::kAnyAny:
1299 case MemBarrierKind::kAnyStore: {
1300 type = BarrierAll;
1301 break;
1302 }
1303 case MemBarrierKind::kLoadAny: {
1304 type = BarrierReads;
1305 break;
1306 }
1307 case MemBarrierKind::kStoreStore: {
1308 type = BarrierWrites;
1309 break;
1310 }
1311 default:
1312 LOG(FATAL) << "Unexpected memory barrier " << kind;
1313 }
1314 __ Dmb(InnerShareable, type);
1315}
1316
Serban Constantinescu02164b32014-11-13 14:05:07 +00001317void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1318 HBasicBlock* successor) {
1319 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001320 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1321 if (slow_path == nullptr) {
1322 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1323 instruction->SetSlowPath(slow_path);
1324 codegen_->AddSlowPath(slow_path);
1325 if (successor != nullptr) {
1326 DCHECK(successor->IsLoopHeader());
1327 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1328 }
1329 } else {
1330 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1331 }
1332
Serban Constantinescu02164b32014-11-13 14:05:07 +00001333 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1334 Register temp = temps.AcquireW();
1335
1336 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1337 if (successor == nullptr) {
1338 __ Cbnz(temp, slow_path->GetEntryLabel());
1339 __ Bind(slow_path->GetReturnLabel());
1340 } else {
1341 __ Cbz(temp, codegen_->GetLabelOf(successor));
1342 __ B(slow_path->GetEntryLabel());
1343 // slow_path will return to GetLabelOf(successor).
1344 }
1345}
1346
Alexandre Rames5319def2014-10-23 10:03:10 +01001347InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1348 CodeGeneratorARM64* codegen)
1349 : HGraphVisitor(graph),
1350 assembler_(codegen->GetAssembler()),
1351 codegen_(codegen) {}
1352
1353#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001354 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001355
1356#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1357
1358enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001359 // Using a base helps identify when we hit such breakpoints.
1360 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001361#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1362 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1363#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1364};
1365
1366#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001367 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001368 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1369 } \
1370 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1371 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1372 locations->SetOut(Location::Any()); \
1373 }
1374 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1375#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1376
1377#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001378#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001379
Alexandre Rames67555f72014-11-18 10:55:16 +00001380void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 DCHECK_EQ(instr->InputCount(), 2U);
1382 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1383 Primitive::Type type = instr->GetResultType();
1384 switch (type) {
1385 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001386 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001387 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001388 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001390 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001391
1392 case Primitive::kPrimFloat:
1393 case Primitive::kPrimDouble:
1394 locations->SetInAt(0, Location::RequiresFpuRegister());
1395 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001396 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001397 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001398
Alexandre Rames5319def2014-10-23 10:03:10 +01001399 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001400 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001401 }
1402}
1403
Alexandre Rames09a99962015-04-15 11:47:56 +01001404void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1405 LocationSummary* locations =
1406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1407 locations->SetInAt(0, Location::RequiresRegister());
1408 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1409 locations->SetOut(Location::RequiresFpuRegister());
1410 } else {
1411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1412 }
1413}
1414
1415void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1416 const FieldInfo& field_info) {
1417 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001418 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001419 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001420
1421 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1422 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1423
1424 if (field_info.IsVolatile()) {
1425 if (use_acquire_release) {
1426 // NB: LoadAcquire will record the pc info if needed.
1427 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1428 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001429 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001430 codegen_->MaybeRecordImplicitNullCheck(instruction);
1431 // For IRIW sequential consistency kLoadAny is not sufficient.
1432 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1433 }
1434 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001435 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001436 codegen_->MaybeRecordImplicitNullCheck(instruction);
1437 }
Roland Levillain4d027112015-07-01 15:41:14 +01001438
1439 if (field_type == Primitive::kPrimNot) {
1440 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1441 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001442}
1443
1444void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1445 LocationSummary* locations =
1446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1447 locations->SetInAt(0, Location::RequiresRegister());
1448 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1449 locations->SetInAt(1, Location::RequiresFpuRegister());
1450 } else {
1451 locations->SetInAt(1, Location::RequiresRegister());
1452 }
1453}
1454
1455void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001456 const FieldInfo& field_info,
1457 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001458 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001459 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001460
1461 Register obj = InputRegisterAt(instruction, 0);
1462 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001463 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001464 Offset offset = field_info.GetFieldOffset();
1465 Primitive::Type field_type = field_info.GetFieldType();
1466 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1467
Roland Levillain4d027112015-07-01 15:41:14 +01001468 {
1469 // We use a block to end the scratch scope before the write barrier, thus
1470 // freeing the temporary registers so they can be used in `MarkGCCard`.
1471 UseScratchRegisterScope temps(GetVIXLAssembler());
1472
1473 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1474 DCHECK(value.IsW());
1475 Register temp = temps.AcquireW();
1476 __ Mov(temp, value.W());
1477 GetAssembler()->PoisonHeapReference(temp.W());
1478 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001479 }
Roland Levillain4d027112015-07-01 15:41:14 +01001480
1481 if (field_info.IsVolatile()) {
1482 if (use_acquire_release) {
1483 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1484 codegen_->MaybeRecordImplicitNullCheck(instruction);
1485 } else {
1486 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1487 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1488 codegen_->MaybeRecordImplicitNullCheck(instruction);
1489 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1490 }
1491 } else {
1492 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1493 codegen_->MaybeRecordImplicitNullCheck(instruction);
1494 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001495 }
1496
1497 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001498 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001499 }
1500}
1501
Alexandre Rames67555f72014-11-18 10:55:16 +00001502void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001503 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001504
1505 switch (type) {
1506 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001507 case Primitive::kPrimLong: {
1508 Register dst = OutputRegister(instr);
1509 Register lhs = InputRegisterAt(instr, 0);
1510 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001511 if (instr->IsAdd()) {
1512 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001513 } else if (instr->IsAnd()) {
1514 __ And(dst, lhs, rhs);
1515 } else if (instr->IsOr()) {
1516 __ Orr(dst, lhs, rhs);
1517 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001518 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001519 } else {
1520 DCHECK(instr->IsXor());
1521 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001522 }
1523 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001524 }
1525 case Primitive::kPrimFloat:
1526 case Primitive::kPrimDouble: {
1527 FPRegister dst = OutputFPRegister(instr);
1528 FPRegister lhs = InputFPRegisterAt(instr, 0);
1529 FPRegister rhs = InputFPRegisterAt(instr, 1);
1530 if (instr->IsAdd()) {
1531 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001532 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001533 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001534 } else {
1535 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001536 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001537 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001538 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001539 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001540 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001541 }
1542}
1543
Serban Constantinescu02164b32014-11-13 14:05:07 +00001544void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1545 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1546
1547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1548 Primitive::Type type = instr->GetResultType();
1549 switch (type) {
1550 case Primitive::kPrimInt:
1551 case Primitive::kPrimLong: {
1552 locations->SetInAt(0, Location::RequiresRegister());
1553 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1554 locations->SetOut(Location::RequiresRegister());
1555 break;
1556 }
1557 default:
1558 LOG(FATAL) << "Unexpected shift type " << type;
1559 }
1560}
1561
1562void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1563 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1564
1565 Primitive::Type type = instr->GetType();
1566 switch (type) {
1567 case Primitive::kPrimInt:
1568 case Primitive::kPrimLong: {
1569 Register dst = OutputRegister(instr);
1570 Register lhs = InputRegisterAt(instr, 0);
1571 Operand rhs = InputOperandAt(instr, 1);
1572 if (rhs.IsImmediate()) {
1573 uint32_t shift_value = (type == Primitive::kPrimInt)
1574 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1575 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1576 if (instr->IsShl()) {
1577 __ Lsl(dst, lhs, shift_value);
1578 } else if (instr->IsShr()) {
1579 __ Asr(dst, lhs, shift_value);
1580 } else {
1581 __ Lsr(dst, lhs, shift_value);
1582 }
1583 } else {
1584 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1585
1586 if (instr->IsShl()) {
1587 __ Lsl(dst, lhs, rhs_reg);
1588 } else if (instr->IsShr()) {
1589 __ Asr(dst, lhs, rhs_reg);
1590 } else {
1591 __ Lsr(dst, lhs, rhs_reg);
1592 }
1593 }
1594 break;
1595 }
1596 default:
1597 LOG(FATAL) << "Unexpected shift operation type " << type;
1598 }
1599}
1600
Alexandre Rames5319def2014-10-23 10:03:10 +01001601void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001602 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001603}
1604
1605void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001606 HandleBinaryOp(instruction);
1607}
1608
1609void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1610 HandleBinaryOp(instruction);
1611}
1612
1613void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1614 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001615}
1616
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001617void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
1618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1620 locations->SetInAt(0, Location::RequiresRegister());
1621 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1622 locations->SetOut(Location::RequiresRegister());
1623}
1624
1625void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1626 HArm64IntermediateAddress* instruction) {
1627 __ Add(OutputRegister(instruction),
1628 InputRegisterAt(instruction, 0),
1629 Operand(InputOperandAt(instruction, 1)));
1630}
1631
Alexandre Rames418318f2015-11-20 15:55:47 +00001632void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1633 LocationSummary* locations =
1634 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1635 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1636 Location::RequiresRegister());
1637 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1638 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1640}
1641
1642void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1643 Register res = OutputRegister(instr);
1644 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1645 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1646 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1647
1648 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1649 // This fixup should be carried out for all multiply-accumulate instructions:
1650 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1651 if (instr->GetType() == Primitive::kPrimLong &&
1652 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1653 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1654 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1655 if (prev->IsLoadOrStore()) {
1656 // Make sure we emit only exactly one nop.
1657 vixl::CodeBufferCheckScope scope(masm,
1658 vixl::kInstructionSize,
1659 vixl::CodeBufferCheckScope::kCheck,
1660 vixl::CodeBufferCheckScope::kExactSize);
1661 __ nop();
1662 }
1663 }
1664
1665 if (instr->GetOpKind() == HInstruction::kAdd) {
1666 __ Madd(res, mul_left, mul_right, accumulator);
1667 } else {
1668 DCHECK(instr->GetOpKind() == HInstruction::kSub);
1669 __ Msub(res, mul_left, mul_right, accumulator);
1670 }
1671}
1672
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001673void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1674 LocationSummary* locations =
1675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001678 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1679 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1680 } else {
1681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1682 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001683}
1684
1685void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001686 Primitive::Type type = instruction->GetType();
1687 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001688 Location index = instruction->GetLocations()->InAt(1);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001689 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001690 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001691 CPURegister dest = OutputCPURegister(instruction);
1692
Alexandre Ramesd921d642015-04-16 15:07:16 +01001693 MacroAssembler* masm = GetVIXLAssembler();
1694 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001695 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001696 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001697
1698 if (index.IsConstant()) {
1699 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001700 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001701 } else {
1702 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001703 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1704 // We do not need to compute the intermediate address from the array: the
1705 // input instruction has done it already. See the comment in
1706 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1707 if (kIsDebugBuild) {
1708 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1709 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1710 }
1711 temp = obj;
1712 } else {
1713 __ Add(temp, obj, offset);
1714 }
Alexandre Rames82000b02015-07-07 11:34:16 +01001715 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001716 }
1717
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001718 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001719 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001720
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001721 if (instruction->GetType() == Primitive::kPrimNot) {
1722 GetAssembler()->MaybeUnpoisonHeapReference(dest.W());
Roland Levillain4d027112015-07-01 15:41:14 +01001723 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001724}
1725
Alexandre Rames5319def2014-10-23 10:03:10 +01001726void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1727 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1728 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001729 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001730}
1731
1732void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001733 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001734 __ Ldr(OutputRegister(instruction),
1735 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001736 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001737}
1738
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001739void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001740 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1741 instruction,
1742 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1743 locations->SetInAt(0, Location::RequiresRegister());
1744 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1745 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1746 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001747 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001748 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001749 }
1750}
1751
1752void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1753 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001754 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001755 bool may_need_runtime_call = locations->CanCall();
1756 bool needs_write_barrier =
1757 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001758
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001759 Register array = InputRegisterAt(instruction, 0);
1760 CPURegister value = InputCPURegisterAt(instruction, 2);
1761 CPURegister source = value;
1762 Location index = locations->InAt(1);
1763 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1764 MemOperand destination = HeapOperand(array);
1765 MacroAssembler* masm = GetVIXLAssembler();
1766 BlockPoolsScope block_pools(masm);
1767
1768 if (!needs_write_barrier) {
1769 DCHECK(!may_need_runtime_call);
1770 if (index.IsConstant()) {
1771 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1772 destination = HeapOperand(array, offset);
1773 } else {
1774 UseScratchRegisterScope temps(masm);
1775 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001776 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1777 // We do not need to compute the intermediate address from the array: the
1778 // input instruction has done it already. See the comment in
1779 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1780 if (kIsDebugBuild) {
1781 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1782 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1783 }
1784 temp = array;
1785 } else {
1786 __ Add(temp, array, offset);
1787 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001788 destination = HeapOperand(temp,
1789 XRegisterFrom(index),
1790 LSL,
1791 Primitive::ComponentSizeShift(value_type));
1792 }
1793 codegen_->Store(value_type, value, destination);
1794 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001795 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001796 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001797 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001798 vixl::Label done;
1799 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001800 {
1801 // We use a block to end the scratch scope before the write barrier, thus
1802 // freeing the temporary registers so they can be used in `MarkGCCard`.
1803 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001804 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001805 if (index.IsConstant()) {
1806 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001807 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001808 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001809 destination = HeapOperand(temp,
1810 XRegisterFrom(index),
1811 LSL,
1812 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001813 }
1814
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001815 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1816 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1817 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1818
1819 if (may_need_runtime_call) {
1820 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1821 codegen_->AddSlowPath(slow_path);
1822 if (instruction->GetValueCanBeNull()) {
1823 vixl::Label non_zero;
1824 __ Cbnz(Register(value), &non_zero);
1825 if (!index.IsConstant()) {
1826 __ Add(temp, array, offset);
1827 }
1828 __ Str(wzr, destination);
1829 codegen_->MaybeRecordImplicitNullCheck(instruction);
1830 __ B(&done);
1831 __ Bind(&non_zero);
1832 }
1833
1834 Register temp2 = temps.AcquireSameSizeAs(array);
1835 __ Ldr(temp, HeapOperand(array, class_offset));
1836 codegen_->MaybeRecordImplicitNullCheck(instruction);
1837 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1838 __ Ldr(temp, HeapOperand(temp, component_offset));
1839 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1840 // No need to poison/unpoison, we're comparing two poisoned references.
1841 __ Cmp(temp, temp2);
1842 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1843 vixl::Label do_put;
1844 __ B(eq, &do_put);
1845 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1846 __ Ldr(temp, HeapOperand(temp, super_offset));
1847 // No need to unpoison, we're comparing against null.
1848 __ Cbnz(temp, slow_path->GetEntryLabel());
1849 __ Bind(&do_put);
1850 } else {
1851 __ B(ne, slow_path->GetEntryLabel());
1852 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001853 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001854 }
1855
1856 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001857 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001858 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001859 __ Mov(temp2, value.W());
1860 GetAssembler()->PoisonHeapReference(temp2);
1861 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001862 }
1863
1864 if (!index.IsConstant()) {
1865 __ Add(temp, array, offset);
1866 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001867 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001868
1869 if (!may_need_runtime_call) {
1870 codegen_->MaybeRecordImplicitNullCheck(instruction);
1871 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001872 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001873
1874 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1875
1876 if (done.IsLinked()) {
1877 __ Bind(&done);
1878 }
1879
1880 if (slow_path != nullptr) {
1881 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001882 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001883 }
1884}
1885
Alexandre Rames67555f72014-11-18 10:55:16 +00001886void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001887 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1888 ? LocationSummary::kCallOnSlowPath
1889 : LocationSummary::kNoCall;
1890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001891 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001892 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001893 if (instruction->HasUses()) {
1894 locations->SetOut(Location::SameAsFirstInput());
1895 }
1896}
1897
1898void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001899 BoundsCheckSlowPathARM64* slow_path =
1900 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001901 codegen_->AddSlowPath(slow_path);
1902
1903 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1904 __ B(slow_path->GetEntryLabel(), hs);
1905}
1906
Alexandre Rames67555f72014-11-18 10:55:16 +00001907void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1908 LocationSummary* locations =
1909 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1910 locations->SetInAt(0, Location::RequiresRegister());
1911 if (check->HasUses()) {
1912 locations->SetOut(Location::SameAsFirstInput());
1913 }
1914}
1915
1916void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1917 // We assume the class is not null.
1918 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1919 check->GetLoadClass(), check, check->GetDexPc(), true);
1920 codegen_->AddSlowPath(slow_path);
1921 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1922}
1923
Roland Levillain7f63c522015-07-13 15:54:55 +00001924static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
1925 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
1926 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
1927}
1928
Serban Constantinescu02164b32014-11-13 14:05:07 +00001929void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001930 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00001931 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
1932 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001933 switch (in_type) {
1934 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00001935 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001936 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1938 break;
1939 }
1940 case Primitive::kPrimFloat:
1941 case Primitive::kPrimDouble: {
1942 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00001943 locations->SetInAt(1,
1944 IsFloatingPointZeroConstant(compare->InputAt(1))
1945 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
1946 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00001947 locations->SetOut(Location::RequiresRegister());
1948 break;
1949 }
1950 default:
1951 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1952 }
1953}
1954
1955void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
1956 Primitive::Type in_type = compare->InputAt(0)->GetType();
1957
1958 // 0 if: left == right
1959 // 1 if: left > right
1960 // -1 if: left < right
1961 switch (in_type) {
1962 case Primitive::kPrimLong: {
1963 Register result = OutputRegister(compare);
1964 Register left = InputRegisterAt(compare, 0);
1965 Operand right = InputOperandAt(compare, 1);
1966
1967 __ Cmp(left, right);
1968 __ Cset(result, ne);
1969 __ Cneg(result, result, lt);
1970 break;
1971 }
1972 case Primitive::kPrimFloat:
1973 case Primitive::kPrimDouble: {
1974 Register result = OutputRegister(compare);
1975 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00001976 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00001977 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
1978 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00001979 __ Fcmp(left, 0.0);
1980 } else {
1981 __ Fcmp(left, InputFPRegisterAt(compare, 1));
1982 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001983 if (compare->IsGtBias()) {
1984 __ Cset(result, ne);
1985 } else {
1986 __ Csetm(result, ne);
1987 }
1988 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01001989 break;
1990 }
1991 default:
1992 LOG(FATAL) << "Unimplemented compare type " << in_type;
1993 }
1994}
1995
1996void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
1997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00001998
1999 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2000 locations->SetInAt(0, Location::RequiresFpuRegister());
2001 locations->SetInAt(1,
2002 IsFloatingPointZeroConstant(instruction->InputAt(1))
2003 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2004 : Location::RequiresFpuRegister());
2005 } else {
2006 // Integer cases.
2007 locations->SetInAt(0, Location::RequiresRegister());
2008 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2009 }
2010
Alexandre Rames5319def2014-10-23 10:03:10 +01002011 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002013 }
2014}
2015
2016void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
2017 if (!instruction->NeedsMaterialization()) {
2018 return;
2019 }
2020
2021 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002022 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002023 IfCondition if_cond = instruction->GetCondition();
2024 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002025
Roland Levillain7f63c522015-07-13 15:54:55 +00002026 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2027 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2028 if (locations->InAt(1).IsConstant()) {
2029 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2030 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2031 __ Fcmp(lhs, 0.0);
2032 } else {
2033 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2034 }
2035 __ Cset(res, arm64_cond);
2036 if (instruction->IsFPConditionTrueIfNaN()) {
2037 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
2038 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
2039 } else if (instruction->IsFPConditionFalseIfNaN()) {
2040 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
2041 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2042 }
2043 } else {
2044 // Integer cases.
2045 Register lhs = InputRegisterAt(instruction, 0);
2046 Operand rhs = InputOperandAt(instruction, 1);
2047 __ Cmp(lhs, rhs);
2048 __ Cset(res, arm64_cond);
2049 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002050}
2051
2052#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2053 M(Equal) \
2054 M(NotEqual) \
2055 M(LessThan) \
2056 M(LessThanOrEqual) \
2057 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002058 M(GreaterThanOrEqual) \
2059 M(Below) \
2060 M(BelowOrEqual) \
2061 M(Above) \
2062 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002063#define DEFINE_CONDITION_VISITORS(Name) \
2064void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
2065void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
2066FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002067#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002068#undef FOR_EACH_CONDITION_INSTRUCTION
2069
Zheng Xuc6667102015-05-15 16:08:45 +08002070void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2071 DCHECK(instruction->IsDiv() || instruction->IsRem());
2072
2073 LocationSummary* locations = instruction->GetLocations();
2074 Location second = locations->InAt(1);
2075 DCHECK(second.IsConstant());
2076
2077 Register out = OutputRegister(instruction);
2078 Register dividend = InputRegisterAt(instruction, 0);
2079 int64_t imm = Int64FromConstant(second.GetConstant());
2080 DCHECK(imm == 1 || imm == -1);
2081
2082 if (instruction->IsRem()) {
2083 __ Mov(out, 0);
2084 } else {
2085 if (imm == 1) {
2086 __ Mov(out, dividend);
2087 } else {
2088 __ Neg(out, dividend);
2089 }
2090 }
2091}
2092
2093void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2094 DCHECK(instruction->IsDiv() || instruction->IsRem());
2095
2096 LocationSummary* locations = instruction->GetLocations();
2097 Location second = locations->InAt(1);
2098 DCHECK(second.IsConstant());
2099
2100 Register out = OutputRegister(instruction);
2101 Register dividend = InputRegisterAt(instruction, 0);
2102 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01002103 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002104 DCHECK(IsPowerOfTwo(abs_imm));
2105 int ctz_imm = CTZ(abs_imm);
2106
2107 UseScratchRegisterScope temps(GetVIXLAssembler());
2108 Register temp = temps.AcquireSameSizeAs(out);
2109
2110 if (instruction->IsDiv()) {
2111 __ Add(temp, dividend, abs_imm - 1);
2112 __ Cmp(dividend, 0);
2113 __ Csel(out, temp, dividend, lt);
2114 if (imm > 0) {
2115 __ Asr(out, out, ctz_imm);
2116 } else {
2117 __ Neg(out, Operand(out, ASR, ctz_imm));
2118 }
2119 } else {
2120 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2121 __ Asr(temp, dividend, bits - 1);
2122 __ Lsr(temp, temp, bits - ctz_imm);
2123 __ Add(out, dividend, temp);
2124 __ And(out, out, abs_imm - 1);
2125 __ Sub(out, out, temp);
2126 }
2127}
2128
2129void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2130 DCHECK(instruction->IsDiv() || instruction->IsRem());
2131
2132 LocationSummary* locations = instruction->GetLocations();
2133 Location second = locations->InAt(1);
2134 DCHECK(second.IsConstant());
2135
2136 Register out = OutputRegister(instruction);
2137 Register dividend = InputRegisterAt(instruction, 0);
2138 int64_t imm = Int64FromConstant(second.GetConstant());
2139
2140 Primitive::Type type = instruction->GetResultType();
2141 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2142
2143 int64_t magic;
2144 int shift;
2145 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2146
2147 UseScratchRegisterScope temps(GetVIXLAssembler());
2148 Register temp = temps.AcquireSameSizeAs(out);
2149
2150 // temp = get_high(dividend * magic)
2151 __ Mov(temp, magic);
2152 if (type == Primitive::kPrimLong) {
2153 __ Smulh(temp, dividend, temp);
2154 } else {
2155 __ Smull(temp.X(), dividend, temp);
2156 __ Lsr(temp.X(), temp.X(), 32);
2157 }
2158
2159 if (imm > 0 && magic < 0) {
2160 __ Add(temp, temp, dividend);
2161 } else if (imm < 0 && magic > 0) {
2162 __ Sub(temp, temp, dividend);
2163 }
2164
2165 if (shift != 0) {
2166 __ Asr(temp, temp, shift);
2167 }
2168
2169 if (instruction->IsDiv()) {
2170 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2171 } else {
2172 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2173 // TODO: Strength reduction for msub.
2174 Register temp_imm = temps.AcquireSameSizeAs(out);
2175 __ Mov(temp_imm, imm);
2176 __ Msub(out, temp, temp_imm, dividend);
2177 }
2178}
2179
2180void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2181 DCHECK(instruction->IsDiv() || instruction->IsRem());
2182 Primitive::Type type = instruction->GetResultType();
2183 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2184
2185 LocationSummary* locations = instruction->GetLocations();
2186 Register out = OutputRegister(instruction);
2187 Location second = locations->InAt(1);
2188
2189 if (second.IsConstant()) {
2190 int64_t imm = Int64FromConstant(second.GetConstant());
2191
2192 if (imm == 0) {
2193 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2194 } else if (imm == 1 || imm == -1) {
2195 DivRemOneOrMinusOne(instruction);
2196 } else if (IsPowerOfTwo(std::abs(imm))) {
2197 DivRemByPowerOfTwo(instruction);
2198 } else {
2199 DCHECK(imm <= -2 || imm >= 2);
2200 GenerateDivRemWithAnyConstant(instruction);
2201 }
2202 } else {
2203 Register dividend = InputRegisterAt(instruction, 0);
2204 Register divisor = InputRegisterAt(instruction, 1);
2205 if (instruction->IsDiv()) {
2206 __ Sdiv(out, dividend, divisor);
2207 } else {
2208 UseScratchRegisterScope temps(GetVIXLAssembler());
2209 Register temp = temps.AcquireSameSizeAs(out);
2210 __ Sdiv(temp, dividend, divisor);
2211 __ Msub(out, temp, divisor, dividend);
2212 }
2213 }
2214}
2215
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002216void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2217 LocationSummary* locations =
2218 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2219 switch (div->GetResultType()) {
2220 case Primitive::kPrimInt:
2221 case Primitive::kPrimLong:
2222 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002223 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2225 break;
2226
2227 case Primitive::kPrimFloat:
2228 case Primitive::kPrimDouble:
2229 locations->SetInAt(0, Location::RequiresFpuRegister());
2230 locations->SetInAt(1, Location::RequiresFpuRegister());
2231 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2232 break;
2233
2234 default:
2235 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2236 }
2237}
2238
2239void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2240 Primitive::Type type = div->GetResultType();
2241 switch (type) {
2242 case Primitive::kPrimInt:
2243 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002244 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002245 break;
2246
2247 case Primitive::kPrimFloat:
2248 case Primitive::kPrimDouble:
2249 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2250 break;
2251
2252 default:
2253 LOG(FATAL) << "Unexpected div type " << type;
2254 }
2255}
2256
Alexandre Rames67555f72014-11-18 10:55:16 +00002257void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002258 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2259 ? LocationSummary::kCallOnSlowPath
2260 : LocationSummary::kNoCall;
2261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002262 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2263 if (instruction->HasUses()) {
2264 locations->SetOut(Location::SameAsFirstInput());
2265 }
2266}
2267
2268void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2269 SlowPathCodeARM64* slow_path =
2270 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2271 codegen_->AddSlowPath(slow_path);
2272 Location value = instruction->GetLocations()->InAt(0);
2273
Alexandre Rames3e69f162014-12-10 10:36:50 +00002274 Primitive::Type type = instruction->GetType();
2275
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002276 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2277 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002278 return;
2279 }
2280
Alexandre Rames67555f72014-11-18 10:55:16 +00002281 if (value.IsConstant()) {
2282 int64_t divisor = Int64ConstantFrom(value);
2283 if (divisor == 0) {
2284 __ B(slow_path->GetEntryLabel());
2285 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002286 // A division by a non-null constant is valid. We don't need to perform
2287 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002288 }
2289 } else {
2290 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2291 }
2292}
2293
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002294void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2297 locations->SetOut(Location::ConstantLocation(constant));
2298}
2299
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002300void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2301 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002302 // Will be generated at use site.
2303}
2304
Alexandre Rames5319def2014-10-23 10:03:10 +01002305void LocationsBuilderARM64::VisitExit(HExit* exit) {
2306 exit->SetLocations(nullptr);
2307}
2308
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002309void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002310}
2311
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002312void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2313 LocationSummary* locations =
2314 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2315 locations->SetOut(Location::ConstantLocation(constant));
2316}
2317
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002318void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002319 // Will be generated at use site.
2320}
2321
David Brazdilfc6a86a2015-06-26 10:33:45 +00002322void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002323 DCHECK(!successor->IsExitBlock());
2324 HBasicBlock* block = got->GetBlock();
2325 HInstruction* previous = got->GetPrevious();
2326 HLoopInformation* info = block->GetLoopInformation();
2327
David Brazdil46e2a392015-03-16 17:31:52 +00002328 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002329 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2330 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2331 return;
2332 }
2333 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2334 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2335 }
2336 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002337 __ B(codegen_->GetLabelOf(successor));
2338 }
2339}
2340
David Brazdilfc6a86a2015-06-26 10:33:45 +00002341void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2342 got->SetLocations(nullptr);
2343}
2344
2345void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2346 HandleGoto(got, got->GetSuccessor());
2347}
2348
2349void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2350 try_boundary->SetLocations(nullptr);
2351}
2352
2353void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2354 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2355 if (!successor->IsExitBlock()) {
2356 HandleGoto(try_boundary, successor);
2357 }
2358}
2359
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002360void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002361 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002362 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002363 vixl::Label* false_target) {
2364 // FP branching requires both targets to be explicit. If either of the targets
2365 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2366 vixl::Label fallthrough_target;
2367 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002368
David Brazdil0debae72015-11-12 18:37:00 +00002369 if (true_target == nullptr && false_target == nullptr) {
2370 // Nothing to do. The code always falls through.
2371 return;
2372 } else if (cond->IsIntConstant()) {
2373 // Constant condition, statically compared against 1.
2374 if (cond->AsIntConstant()->IsOne()) {
2375 if (true_target != nullptr) {
2376 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002377 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002378 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002379 DCHECK(cond->AsIntConstant()->IsZero());
2380 if (false_target != nullptr) {
2381 __ B(false_target);
2382 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002383 }
David Brazdil0debae72015-11-12 18:37:00 +00002384 return;
2385 }
2386
2387 // The following code generates these patterns:
2388 // (1) true_target == nullptr && false_target != nullptr
2389 // - opposite condition true => branch to false_target
2390 // (2) true_target != nullptr && false_target == nullptr
2391 // - condition true => branch to true_target
2392 // (3) true_target != nullptr && false_target != nullptr
2393 // - condition true => branch to true_target
2394 // - branch to false_target
2395 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002396 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002397 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002398 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002399 if (true_target == nullptr) {
2400 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2401 } else {
2402 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2403 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002404 } else {
2405 // The condition instruction has not been materialized, use its inputs as
2406 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002407 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002408
David Brazdil0debae72015-11-12 18:37:00 +00002409 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002410 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002411 FPRegister lhs = InputFPRegisterAt(condition, 0);
2412 if (condition->GetLocations()->InAt(1).IsConstant()) {
2413 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2414 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2415 __ Fcmp(lhs, 0.0);
2416 } else {
2417 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2418 }
2419 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002420 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002421 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002422 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002423 }
David Brazdil0debae72015-11-12 18:37:00 +00002424 if (true_target == nullptr) {
2425 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2426 } else {
2427 __ B(ARM64Condition(condition->GetCondition()), true_target);
2428 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002429 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002430 // Integer cases.
2431 Register lhs = InputRegisterAt(condition, 0);
2432 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002433
2434 Condition arm64_cond;
2435 vixl::Label* non_fallthrough_target;
2436 if (true_target == nullptr) {
2437 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2438 non_fallthrough_target = false_target;
2439 } else {
2440 arm64_cond = ARM64Condition(condition->GetCondition());
2441 non_fallthrough_target = true_target;
2442 }
2443
Roland Levillain7f63c522015-07-13 15:54:55 +00002444 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2445 switch (arm64_cond) {
2446 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002447 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002448 break;
2449 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002450 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002451 break;
2452 case lt:
2453 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002454 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002455 break;
2456 case ge:
2457 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002458 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002459 break;
2460 default:
2461 // Without the `static_cast` the compiler throws an error for
2462 // `-Werror=sign-promo`.
2463 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2464 }
2465 } else {
2466 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002467 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002468 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002469 }
2470 }
David Brazdil0debae72015-11-12 18:37:00 +00002471
2472 // If neither branch falls through (case 3), the conditional branch to `true_target`
2473 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2474 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002475 __ B(false_target);
2476 }
David Brazdil0debae72015-11-12 18:37:00 +00002477
2478 if (fallthrough_target.IsLinked()) {
2479 __ Bind(&fallthrough_target);
2480 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002481}
2482
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002483void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002485 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002486 locations->SetInAt(0, Location::RequiresRegister());
2487 }
2488}
2489
2490void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002491 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2492 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2493 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2494 nullptr : codegen_->GetLabelOf(true_successor);
2495 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2496 nullptr : codegen_->GetLabelOf(false_successor);
2497 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002498}
2499
2500void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2501 LocationSummary* locations = new (GetGraph()->GetArena())
2502 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002503 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002504 locations->SetInAt(0, Location::RequiresRegister());
2505 }
2506}
2507
2508void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2509 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2510 DeoptimizationSlowPathARM64(deoptimize);
2511 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002512 GenerateTestAndBranch(deoptimize,
2513 /* condition_input_index */ 0,
2514 slow_path->GetEntryLabel(),
2515 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002516}
2517
Alexandre Rames5319def2014-10-23 10:03:10 +01002518void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002519 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002520}
2521
2522void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002523 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002524}
2525
2526void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002527 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
2530void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002531 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002532}
2533
Alexandre Rames67555f72014-11-18 10:55:16 +00002534void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002535 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2536 switch (instruction->GetTypeCheckKind()) {
2537 case TypeCheckKind::kExactCheck:
2538 case TypeCheckKind::kAbstractClassCheck:
2539 case TypeCheckKind::kClassHierarchyCheck:
2540 case TypeCheckKind::kArrayObjectCheck:
2541 call_kind = LocationSummary::kNoCall;
2542 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002543 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002544 case TypeCheckKind::kInterfaceCheck:
2545 call_kind = LocationSummary::kCall;
2546 break;
2547 case TypeCheckKind::kArrayCheck:
2548 call_kind = LocationSummary::kCallOnSlowPath;
2549 break;
2550 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002552 if (call_kind != LocationSummary::kCall) {
2553 locations->SetInAt(0, Location::RequiresRegister());
2554 locations->SetInAt(1, Location::RequiresRegister());
2555 // The out register is used as a temporary, so it overlaps with the inputs.
2556 // Note that TypeCheckSlowPathARM64 uses this register too.
2557 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2558 } else {
2559 InvokeRuntimeCallingConvention calling_convention;
2560 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2561 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2562 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2563 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002564}
2565
2566void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2567 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002568 Register obj = InputRegisterAt(instruction, 0);
2569 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002570 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002571 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2572 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2573 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2574 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002575
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002576 vixl::Label done, zero;
2577 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002578
2579 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002580 // Avoid null check if we know `obj` is not null.
2581 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002582 __ Cbz(obj, &zero);
2583 }
2584
Calin Juravle98893e12015-10-02 21:05:03 +01002585 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002586 // This is safe, as the register is caller-save, and the object must be in another
2587 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01002588 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
2589 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002590 ? obj
2591 : out;
2592 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2593 GetAssembler()->MaybeUnpoisonHeapReference(target);
2594
2595 switch (instruction->GetTypeCheckKind()) {
2596 case TypeCheckKind::kExactCheck: {
2597 __ Cmp(out, cls);
2598 __ Cset(out, eq);
2599 if (zero.IsLinked()) {
2600 __ B(&done);
2601 }
2602 break;
2603 }
2604 case TypeCheckKind::kAbstractClassCheck: {
2605 // If the class is abstract, we eagerly fetch the super class of the
2606 // object to avoid doing a comparison we know will fail.
2607 vixl::Label loop, success;
2608 __ Bind(&loop);
2609 __ Ldr(out, HeapOperand(out, super_offset));
2610 GetAssembler()->MaybeUnpoisonHeapReference(out);
2611 // If `out` is null, we use it for the result, and jump to `done`.
2612 __ Cbz(out, &done);
2613 __ Cmp(out, cls);
2614 __ B(ne, &loop);
2615 __ Mov(out, 1);
2616 if (zero.IsLinked()) {
2617 __ B(&done);
2618 }
2619 break;
2620 }
2621 case TypeCheckKind::kClassHierarchyCheck: {
2622 // Walk over the class hierarchy to find a match.
2623 vixl::Label loop, success;
2624 __ Bind(&loop);
2625 __ Cmp(out, cls);
2626 __ B(eq, &success);
2627 __ Ldr(out, HeapOperand(out, super_offset));
2628 GetAssembler()->MaybeUnpoisonHeapReference(out);
2629 __ Cbnz(out, &loop);
2630 // If `out` is null, we use it for the result, and jump to `done`.
2631 __ B(&done);
2632 __ Bind(&success);
2633 __ Mov(out, 1);
2634 if (zero.IsLinked()) {
2635 __ B(&done);
2636 }
2637 break;
2638 }
2639 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002640 // Do an exact check.
2641 vixl::Label exact_check;
2642 __ Cmp(out, cls);
2643 __ B(eq, &exact_check);
2644 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002645 __ Ldr(out, HeapOperand(out, component_offset));
2646 GetAssembler()->MaybeUnpoisonHeapReference(out);
2647 // If `out` is null, we use it for the result, and jump to `done`.
2648 __ Cbz(out, &done);
2649 __ Ldrh(out, HeapOperand(out, primitive_offset));
2650 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2651 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002652 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002653 __ Mov(out, 1);
2654 __ B(&done);
2655 break;
2656 }
2657 case TypeCheckKind::kArrayCheck: {
2658 __ Cmp(out, cls);
2659 DCHECK(locations->OnlyCallsOnSlowPath());
2660 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2661 instruction, /* is_fatal */ false);
2662 codegen_->AddSlowPath(slow_path);
2663 __ B(ne, slow_path->GetEntryLabel());
2664 __ Mov(out, 1);
2665 if (zero.IsLinked()) {
2666 __ B(&done);
2667 }
2668 break;
2669 }
Calin Juravle98893e12015-10-02 21:05:03 +01002670 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002671 case TypeCheckKind::kInterfaceCheck:
2672 default: {
2673 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2674 instruction,
2675 instruction->GetDexPc(),
2676 nullptr);
2677 if (zero.IsLinked()) {
2678 __ B(&done);
2679 }
2680 break;
2681 }
2682 }
2683
2684 if (zero.IsLinked()) {
2685 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002686 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002687 }
2688
2689 if (done.IsLinked()) {
2690 __ Bind(&done);
2691 }
2692
2693 if (slow_path != nullptr) {
2694 __ Bind(slow_path->GetExitLabel());
2695 }
2696}
2697
2698void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2699 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2700 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2701
2702 switch (instruction->GetTypeCheckKind()) {
2703 case TypeCheckKind::kExactCheck:
2704 case TypeCheckKind::kAbstractClassCheck:
2705 case TypeCheckKind::kClassHierarchyCheck:
2706 case TypeCheckKind::kArrayObjectCheck:
2707 call_kind = throws_into_catch
2708 ? LocationSummary::kCallOnSlowPath
2709 : LocationSummary::kNoCall;
2710 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002711 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002712 case TypeCheckKind::kInterfaceCheck:
2713 call_kind = LocationSummary::kCall;
2714 break;
2715 case TypeCheckKind::kArrayCheck:
2716 call_kind = LocationSummary::kCallOnSlowPath;
2717 break;
2718 }
2719
2720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2721 instruction, call_kind);
2722 if (call_kind != LocationSummary::kCall) {
2723 locations->SetInAt(0, Location::RequiresRegister());
2724 locations->SetInAt(1, Location::RequiresRegister());
2725 // Note that TypeCheckSlowPathARM64 uses this register too.
2726 locations->AddTemp(Location::RequiresRegister());
2727 } else {
2728 InvokeRuntimeCallingConvention calling_convention;
2729 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2730 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2731 }
2732}
2733
2734void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2735 LocationSummary* locations = instruction->GetLocations();
2736 Register obj = InputRegisterAt(instruction, 0);
2737 Register cls = InputRegisterAt(instruction, 1);
2738 Register temp;
2739 if (!locations->WillCall()) {
2740 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2741 }
2742
2743 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2744 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2745 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2746 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2747 SlowPathCodeARM64* slow_path = nullptr;
2748
2749 if (!locations->WillCall()) {
2750 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2751 instruction, !locations->CanCall());
2752 codegen_->AddSlowPath(slow_path);
2753 }
2754
2755 vixl::Label done;
2756 // Avoid null check if we know obj is not null.
2757 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002758 __ Cbz(obj, &done);
2759 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002760
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002761 if (locations->WillCall()) {
2762 __ Ldr(obj, HeapOperand(obj, class_offset));
2763 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002764 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002765 __ Ldr(temp, HeapOperand(obj, class_offset));
2766 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002767 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002768
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002769 switch (instruction->GetTypeCheckKind()) {
2770 case TypeCheckKind::kExactCheck:
2771 case TypeCheckKind::kArrayCheck: {
2772 __ Cmp(temp, cls);
2773 // Jump to slow path for throwing the exception or doing a
2774 // more involved array check.
2775 __ B(ne, slow_path->GetEntryLabel());
2776 break;
2777 }
2778 case TypeCheckKind::kAbstractClassCheck: {
2779 // If the class is abstract, we eagerly fetch the super class of the
2780 // object to avoid doing a comparison we know will fail.
2781 vixl::Label loop;
2782 __ Bind(&loop);
2783 __ Ldr(temp, HeapOperand(temp, super_offset));
2784 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2785 // Jump to the slow path to throw the exception.
2786 __ Cbz(temp, slow_path->GetEntryLabel());
2787 __ Cmp(temp, cls);
2788 __ B(ne, &loop);
2789 break;
2790 }
2791 case TypeCheckKind::kClassHierarchyCheck: {
2792 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002793 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002794 __ Bind(&loop);
2795 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002796 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002797 __ Ldr(temp, HeapOperand(temp, super_offset));
2798 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2799 __ Cbnz(temp, &loop);
2800 // Jump to the slow path to throw the exception.
2801 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002802 break;
2803 }
2804 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002805 // Do an exact check.
2806 __ Cmp(temp, cls);
2807 __ B(eq, &done);
2808 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002809 __ Ldr(temp, HeapOperand(temp, component_offset));
2810 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2811 __ Cbz(temp, slow_path->GetEntryLabel());
2812 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2813 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2814 __ Cbnz(temp, slow_path->GetEntryLabel());
2815 break;
2816 }
Calin Juravle98893e12015-10-02 21:05:03 +01002817 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002818 case TypeCheckKind::kInterfaceCheck:
2819 default:
2820 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2821 instruction,
2822 instruction->GetDexPc(),
2823 nullptr);
2824 break;
2825 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002826 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002827
2828 if (slow_path != nullptr) {
2829 __ Bind(slow_path->GetExitLabel());
2830 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002831}
2832
Alexandre Rames5319def2014-10-23 10:03:10 +01002833void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2834 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2835 locations->SetOut(Location::ConstantLocation(constant));
2836}
2837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002838void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002839 // Will be generated at use site.
2840}
2841
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002842void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2843 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2844 locations->SetOut(Location::ConstantLocation(constant));
2845}
2846
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002847void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002848 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002849}
2850
Calin Juravle175dc732015-08-25 15:42:32 +01002851void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2852 // The trampoline uses the same calling convention as dex calling conventions,
2853 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2854 // the method_idx.
2855 HandleInvoke(invoke);
2856}
2857
2858void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2859 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2860}
2861
Alexandre Rames5319def2014-10-23 10:03:10 +01002862void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002863 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002864 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002865}
2866
Alexandre Rames67555f72014-11-18 10:55:16 +00002867void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2868 HandleInvoke(invoke);
2869}
2870
2871void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2872 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002873 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2874 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2875 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002876 Location receiver = invoke->GetLocations()->InAt(0);
2877 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002878 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002879
2880 // The register ip1 is required to be used for the hidden argument in
2881 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002882 MacroAssembler* masm = GetVIXLAssembler();
2883 UseScratchRegisterScope scratch_scope(masm);
2884 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002885 scratch_scope.Exclude(ip1);
2886 __ Mov(ip1, invoke->GetDexMethodIndex());
2887
2888 // temp = object->GetClass();
2889 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002890 __ Ldr(temp.W(), StackOperandFrom(receiver));
2891 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002892 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002893 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002894 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002895 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002896 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002897 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002898 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002899 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002900 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002901 // lr();
2902 __ Blr(lr);
2903 DCHECK(!codegen_->IsLeafMethod());
2904 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2905}
2906
2907void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002908 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2909 if (intrinsic.TryDispatch(invoke)) {
2910 return;
2911 }
2912
Alexandre Rames67555f72014-11-18 10:55:16 +00002913 HandleInvoke(invoke);
2914}
2915
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002916void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002917 // When we do not run baseline, explicit clinit checks triggered by static
2918 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2919 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002920
Andreas Gampe878d58c2015-01-15 23:24:00 -08002921 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2922 if (intrinsic.TryDispatch(invoke)) {
2923 return;
2924 }
2925
Alexandre Rames67555f72014-11-18 10:55:16 +00002926 HandleInvoke(invoke);
2927}
2928
Andreas Gampe878d58c2015-01-15 23:24:00 -08002929static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
2930 if (invoke->GetLocations()->Intrinsified()) {
2931 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
2932 intrinsic.Dispatch(invoke);
2933 return true;
2934 }
2935 return false;
2936}
2937
Vladimir Markodc151b22015-10-15 18:02:30 +01002938HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
2939 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
2940 MethodReference target_method ATTRIBUTE_UNUSED) {
2941 // On arm64 we support all dispatch types.
2942 return desired_dispatch_info;
2943}
2944
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002945void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00002946 // For better instruction scheduling we load the direct code pointer before the method pointer.
2947 bool direct_code_loaded = false;
2948 switch (invoke->GetCodePtrLocation()) {
2949 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2950 // LR = code address from literal pool with link-time patch.
2951 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
2952 direct_code_loaded = true;
2953 break;
2954 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2955 // LR = invoke->GetDirectCodePtr();
2956 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
2957 direct_code_loaded = true;
2958 break;
2959 default:
2960 break;
2961 }
2962
Andreas Gampe878d58c2015-01-15 23:24:00 -08002963 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00002964 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2965 switch (invoke->GetMethodLoadKind()) {
2966 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2967 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00002968 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00002969 break;
2970 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00002971 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00002972 break;
2973 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2974 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00002975 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00002976 break;
2977 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2978 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00002979 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00002980 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
2981 break;
2982 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
2983 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002984 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2985 invoke->GetDexCacheArrayOffset());
2986 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00002987 {
2988 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00002989 __ Bind(pc_insn_label);
2990 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00002991 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002992 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00002993 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002994 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
2995 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00002996 {
2997 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
2998 __ Bind(&pc_relative_dex_cache_patches_.back().label);
2999 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3000 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3001 }
Vladimir Marko58155012015-08-19 12:49:41 +00003002 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003003 }
Vladimir Marko58155012015-08-19 12:49:41 +00003004 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003005 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003006 Register reg = XRegisterFrom(temp);
3007 Register method_reg;
3008 if (current_method.IsRegister()) {
3009 method_reg = XRegisterFrom(current_method);
3010 } else {
3011 DCHECK(invoke->GetLocations()->Intrinsified());
3012 DCHECK(!current_method.IsValid());
3013 method_reg = reg;
3014 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3015 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003016
Vladimir Marko58155012015-08-19 12:49:41 +00003017 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003018 __ Ldr(reg.X(),
3019 MemOperand(method_reg.X(),
3020 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003021 // temp = temp[index_in_cache];
3022 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3023 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3024 break;
3025 }
3026 }
3027
3028 switch (invoke->GetCodePtrLocation()) {
3029 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3030 __ Bl(&frame_entry_label_);
3031 break;
3032 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3033 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3034 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003035 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3036 __ Bind(label);
3037 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003038 break;
3039 }
3040 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3041 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3042 // LR prepared above for better instruction scheduling.
3043 DCHECK(direct_code_loaded);
3044 // lr()
3045 __ Blr(lr);
3046 break;
3047 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3048 // LR = callee_method->entry_point_from_quick_compiled_code_;
3049 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003050 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003051 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3052 // lr()
3053 __ Blr(lr);
3054 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003055 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003056
Andreas Gampe878d58c2015-01-15 23:24:00 -08003057 DCHECK(!IsLeafMethod());
3058}
3059
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003060void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3061 LocationSummary* locations = invoke->GetLocations();
3062 Location receiver = locations->InAt(0);
3063 Register temp = XRegisterFrom(temp_in);
3064 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3065 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3066 Offset class_offset = mirror::Object::ClassOffset();
3067 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3068
3069 BlockPoolsScope block_pools(GetVIXLAssembler());
3070
3071 DCHECK(receiver.IsRegister());
3072 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
3073 MaybeRecordImplicitNullCheck(invoke);
3074 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3075 // temp = temp->GetMethodAt(method_offset);
3076 __ Ldr(temp, MemOperand(temp, method_offset));
3077 // lr = temp->GetEntryPoint();
3078 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3079 // lr();
3080 __ Blr(lr);
3081}
3082
Vladimir Marko58155012015-08-19 12:49:41 +00003083void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3084 DCHECK(linker_patches->empty());
3085 size_t size =
3086 method_patches_.size() +
3087 call_patches_.size() +
3088 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003089 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003090 linker_patches->reserve(size);
3091 for (const auto& entry : method_patches_) {
3092 const MethodReference& target_method = entry.first;
3093 vixl::Literal<uint64_t>* literal = entry.second;
3094 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3095 target_method.dex_file,
3096 target_method.dex_method_index));
3097 }
3098 for (const auto& entry : call_patches_) {
3099 const MethodReference& target_method = entry.first;
3100 vixl::Literal<uint64_t>* literal = entry.second;
3101 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3102 target_method.dex_file,
3103 target_method.dex_method_index));
3104 }
3105 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003106 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003107 info.target_method.dex_file,
3108 info.target_method.dex_method_index));
3109 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003110 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003111 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003112 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003113 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003114 info.element_offset));
3115 }
3116}
3117
3118vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3119 // Look up the literal for value.
3120 auto lb = uint64_literals_.lower_bound(value);
3121 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3122 return lb->second;
3123 }
3124 // We don't have a literal for this value, insert a new one.
3125 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3126 uint64_literals_.PutBefore(lb, value, literal);
3127 return literal;
3128}
3129
3130vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3131 MethodReference target_method,
3132 MethodToLiteralMap* map) {
3133 // Look up the literal for target_method.
3134 auto lb = map->lower_bound(target_method);
3135 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3136 return lb->second;
3137 }
3138 // We don't have a literal for this method yet, insert a new one.
3139 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3140 map->PutBefore(lb, target_method, literal);
3141 return literal;
3142}
3143
3144vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3145 MethodReference target_method) {
3146 return DeduplicateMethodLiteral(target_method, &method_patches_);
3147}
3148
3149vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3150 MethodReference target_method) {
3151 return DeduplicateMethodLiteral(target_method, &call_patches_);
3152}
3153
3154
Andreas Gampe878d58c2015-01-15 23:24:00 -08003155void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003156 // When we do not run baseline, explicit clinit checks triggered by static
3157 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3158 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003159
Andreas Gampe878d58c2015-01-15 23:24:00 -08003160 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3161 return;
3162 }
3163
Alexandre Ramesd921d642015-04-16 15:07:16 +01003164 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003165 LocationSummary* locations = invoke->GetLocations();
3166 codegen_->GenerateStaticOrDirectCall(
3167 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003168 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003169}
3170
3171void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003172 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3173 return;
3174 }
3175
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003176 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003177 DCHECK(!codegen_->IsLeafMethod());
3178 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3179}
3180
Alexandre Rames67555f72014-11-18 10:55:16 +00003181void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003182 InvokeRuntimeCallingConvention calling_convention;
3183 CodeGenerator::CreateLoadClassLocationSummary(
3184 cls,
3185 LocationFrom(calling_convention.GetRegisterAt(0)),
3186 LocationFrom(vixl::x0));
Alexandre Rames67555f72014-11-18 10:55:16 +00003187}
3188
3189void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003190 if (cls->NeedsAccessCheck()) {
3191 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3192 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3193 cls,
3194 cls->GetDexPc(),
3195 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003196 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003197 return;
3198 }
3199
3200 Register out = OutputRegister(cls);
3201 Register current_method = InputRegisterAt(cls, 0);
3202 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003203 DCHECK(!cls->CanCallRuntime());
3204 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003205 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003206 } else {
3207 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003208 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3209 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3210 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3211 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003212
3213 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3214 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3215 codegen_->AddSlowPath(slow_path);
3216 __ Cbz(out, slow_path->GetEntryLabel());
3217 if (cls->MustGenerateClinitCheck()) {
3218 GenerateClassInitializationCheck(slow_path, out);
3219 } else {
3220 __ Bind(slow_path->GetExitLabel());
3221 }
3222 }
3223}
3224
David Brazdilcb1c0552015-08-04 16:22:25 +01003225static MemOperand GetExceptionTlsAddress() {
3226 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3227}
3228
Alexandre Rames67555f72014-11-18 10:55:16 +00003229void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3230 LocationSummary* locations =
3231 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3232 locations->SetOut(Location::RequiresRegister());
3233}
3234
3235void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003236 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3237}
3238
3239void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3240 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3241}
3242
3243void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3244 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003245}
3246
Alexandre Rames5319def2014-10-23 10:03:10 +01003247void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3248 load->SetLocations(nullptr);
3249}
3250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003251void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003252 // Nothing to do, this is driven by the code generator.
3253}
3254
Alexandre Rames67555f72014-11-18 10:55:16 +00003255void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3256 LocationSummary* locations =
3257 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003258 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003259 locations->SetOut(Location::RequiresRegister());
3260}
3261
3262void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3263 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3264 codegen_->AddSlowPath(slow_path);
3265
3266 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003267 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003268 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003269 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3270 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3271 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003272 __ Cbz(out, slow_path->GetEntryLabel());
3273 __ Bind(slow_path->GetExitLabel());
3274}
3275
Alexandre Rames5319def2014-10-23 10:03:10 +01003276void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3277 local->SetLocations(nullptr);
3278}
3279
3280void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3281 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3282}
3283
3284void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3285 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3286 locations->SetOut(Location::ConstantLocation(constant));
3287}
3288
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003289void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003290 // Will be generated at use site.
3291}
3292
Alexandre Rames67555f72014-11-18 10:55:16 +00003293void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3294 LocationSummary* locations =
3295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3296 InvokeRuntimeCallingConvention calling_convention;
3297 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3298}
3299
3300void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3301 codegen_->InvokeRuntime(instruction->IsEnter()
3302 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3303 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003304 instruction->GetDexPc(),
3305 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003306 if (instruction->IsEnter()) {
3307 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3308 } else {
3309 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3310 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003311}
3312
Alexandre Rames42d641b2014-10-27 14:00:51 +00003313void LocationsBuilderARM64::VisitMul(HMul* mul) {
3314 LocationSummary* locations =
3315 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3316 switch (mul->GetResultType()) {
3317 case Primitive::kPrimInt:
3318 case Primitive::kPrimLong:
3319 locations->SetInAt(0, Location::RequiresRegister());
3320 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003322 break;
3323
3324 case Primitive::kPrimFloat:
3325 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003326 locations->SetInAt(0, Location::RequiresFpuRegister());
3327 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003328 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003329 break;
3330
3331 default:
3332 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3333 }
3334}
3335
3336void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3337 switch (mul->GetResultType()) {
3338 case Primitive::kPrimInt:
3339 case Primitive::kPrimLong:
3340 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3341 break;
3342
3343 case Primitive::kPrimFloat:
3344 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003345 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003346 break;
3347
3348 default:
3349 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3350 }
3351}
3352
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003353void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3354 LocationSummary* locations =
3355 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3356 switch (neg->GetResultType()) {
3357 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003358 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003359 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003361 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003362
3363 case Primitive::kPrimFloat:
3364 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003365 locations->SetInAt(0, Location::RequiresFpuRegister());
3366 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003367 break;
3368
3369 default:
3370 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3371 }
3372}
3373
3374void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3375 switch (neg->GetResultType()) {
3376 case Primitive::kPrimInt:
3377 case Primitive::kPrimLong:
3378 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3379 break;
3380
3381 case Primitive::kPrimFloat:
3382 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003383 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003384 break;
3385
3386 default:
3387 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3388 }
3389}
3390
3391void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3394 InvokeRuntimeCallingConvention calling_convention;
3395 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003396 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003397 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003398 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003399}
3400
3401void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3402 LocationSummary* locations = instruction->GetLocations();
3403 InvokeRuntimeCallingConvention calling_convention;
3404 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3405 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003406 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003407 // Note: if heap poisoning is enabled, the entry point takes cares
3408 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003409 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3410 instruction,
3411 instruction->GetDexPc(),
3412 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003413 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003414}
3415
Alexandre Rames5319def2014-10-23 10:03:10 +01003416void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3417 LocationSummary* locations =
3418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3419 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003420 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3421 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003422 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3423}
3424
3425void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003426 // Note: if heap poisoning is enabled, the entry point takes cares
3427 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003428 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3429 instruction,
3430 instruction->GetDexPc(),
3431 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003432 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003433}
3434
3435void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003437 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003439}
3440
3441void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003442 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003443 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003444 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003445 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003446 break;
3447
3448 default:
3449 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3450 }
3451}
3452
David Brazdil66d126e2015-04-03 16:02:44 +01003453void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3455 locations->SetInAt(0, Location::RequiresRegister());
3456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3457}
3458
3459void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003460 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3461}
3462
Alexandre Rames5319def2014-10-23 10:03:10 +01003463void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003464 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3465 ? LocationSummary::kCallOnSlowPath
3466 : LocationSummary::kNoCall;
3467 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003468 locations->SetInAt(0, Location::RequiresRegister());
3469 if (instruction->HasUses()) {
3470 locations->SetOut(Location::SameAsFirstInput());
3471 }
3472}
3473
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003474void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003475 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3476 return;
3477 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003478
Alexandre Ramesd921d642015-04-16 15:07:16 +01003479 BlockPoolsScope block_pools(GetVIXLAssembler());
3480 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003481 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3482 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3483}
3484
3485void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003486 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3487 codegen_->AddSlowPath(slow_path);
3488
3489 LocationSummary* locations = instruction->GetLocations();
3490 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003491
3492 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003493}
3494
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003495void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003496 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003497 GenerateImplicitNullCheck(instruction);
3498 } else {
3499 GenerateExplicitNullCheck(instruction);
3500 }
3501}
3502
Alexandre Rames67555f72014-11-18 10:55:16 +00003503void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3504 HandleBinaryOp(instruction);
3505}
3506
3507void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3508 HandleBinaryOp(instruction);
3509}
3510
Alexandre Rames3e69f162014-12-10 10:36:50 +00003511void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3512 LOG(FATAL) << "Unreachable";
3513}
3514
3515void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3516 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3517}
3518
Alexandre Rames5319def2014-10-23 10:03:10 +01003519void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3521 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3522 if (location.IsStackSlot()) {
3523 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3524 } else if (location.IsDoubleStackSlot()) {
3525 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3526 }
3527 locations->SetOut(location);
3528}
3529
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003530void InstructionCodeGeneratorARM64::VisitParameterValue(
3531 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003532 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003533}
3534
3535void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3536 LocationSummary* locations =
3537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003538 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003539}
3540
3541void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3542 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3543 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003544}
3545
3546void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3548 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3549 locations->SetInAt(i, Location::Any());
3550 }
3551 locations->SetOut(Location::Any());
3552}
3553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003554void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003555 LOG(FATAL) << "Unreachable";
3556}
3557
Serban Constantinescu02164b32014-11-13 14:05:07 +00003558void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003559 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003560 LocationSummary::CallKind call_kind =
3561 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3563
3564 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003565 case Primitive::kPrimInt:
3566 case Primitive::kPrimLong:
3567 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003568 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3570 break;
3571
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003572 case Primitive::kPrimFloat:
3573 case Primitive::kPrimDouble: {
3574 InvokeRuntimeCallingConvention calling_convention;
3575 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3576 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3577 locations->SetOut(calling_convention.GetReturnLocation(type));
3578
3579 break;
3580 }
3581
Serban Constantinescu02164b32014-11-13 14:05:07 +00003582 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003583 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003584 }
3585}
3586
3587void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3588 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003589
Serban Constantinescu02164b32014-11-13 14:05:07 +00003590 switch (type) {
3591 case Primitive::kPrimInt:
3592 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003593 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003594 break;
3595 }
3596
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003597 case Primitive::kPrimFloat:
3598 case Primitive::kPrimDouble: {
3599 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3600 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003601 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003602 if (type == Primitive::kPrimFloat) {
3603 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3604 } else {
3605 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3606 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003607 break;
3608 }
3609
Serban Constantinescu02164b32014-11-13 14:05:07 +00003610 default:
3611 LOG(FATAL) << "Unexpected rem type " << type;
3612 }
3613}
3614
Calin Juravle27df7582015-04-17 19:12:31 +01003615void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3616 memory_barrier->SetLocations(nullptr);
3617}
3618
3619void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3620 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3621}
3622
Alexandre Rames5319def2014-10-23 10:03:10 +01003623void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3624 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3625 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003626 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003627}
3628
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003629void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003630 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003631}
3632
3633void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3634 instruction->SetLocations(nullptr);
3635}
3636
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003637void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003638 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003639}
3640
Serban Constantinescu02164b32014-11-13 14:05:07 +00003641void LocationsBuilderARM64::VisitShl(HShl* shl) {
3642 HandleShift(shl);
3643}
3644
3645void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3646 HandleShift(shl);
3647}
3648
3649void LocationsBuilderARM64::VisitShr(HShr* shr) {
3650 HandleShift(shr);
3651}
3652
3653void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3654 HandleShift(shr);
3655}
3656
Alexandre Rames5319def2014-10-23 10:03:10 +01003657void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3658 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3659 Primitive::Type field_type = store->InputAt(1)->GetType();
3660 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003661 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003662 case Primitive::kPrimBoolean:
3663 case Primitive::kPrimByte:
3664 case Primitive::kPrimChar:
3665 case Primitive::kPrimShort:
3666 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003667 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003668 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3669 break;
3670
3671 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003672 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003673 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3674 break;
3675
3676 default:
3677 LOG(FATAL) << "Unimplemented local type " << field_type;
3678 }
3679}
3680
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003681void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003682}
3683
3684void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003685 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003686}
3687
3688void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003689 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003690}
3691
Alexandre Rames67555f72014-11-18 10:55:16 +00003692void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003693 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003694}
3695
3696void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003697 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003698}
3699
3700void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003701 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003702}
3703
Alexandre Rames67555f72014-11-18 10:55:16 +00003704void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003705 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003706}
3707
Calin Juravlee460d1d2015-09-29 04:52:17 +01003708void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3709 HUnresolvedInstanceFieldGet* instruction) {
3710 FieldAccessCallingConventionARM64 calling_convention;
3711 codegen_->CreateUnresolvedFieldLocationSummary(
3712 instruction, instruction->GetFieldType(), calling_convention);
3713}
3714
3715void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3716 HUnresolvedInstanceFieldGet* instruction) {
3717 FieldAccessCallingConventionARM64 calling_convention;
3718 codegen_->GenerateUnresolvedFieldAccess(instruction,
3719 instruction->GetFieldType(),
3720 instruction->GetFieldIndex(),
3721 instruction->GetDexPc(),
3722 calling_convention);
3723}
3724
3725void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3726 HUnresolvedInstanceFieldSet* instruction) {
3727 FieldAccessCallingConventionARM64 calling_convention;
3728 codegen_->CreateUnresolvedFieldLocationSummary(
3729 instruction, instruction->GetFieldType(), calling_convention);
3730}
3731
3732void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3733 HUnresolvedInstanceFieldSet* instruction) {
3734 FieldAccessCallingConventionARM64 calling_convention;
3735 codegen_->GenerateUnresolvedFieldAccess(instruction,
3736 instruction->GetFieldType(),
3737 instruction->GetFieldIndex(),
3738 instruction->GetDexPc(),
3739 calling_convention);
3740}
3741
3742void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3743 HUnresolvedStaticFieldGet* instruction) {
3744 FieldAccessCallingConventionARM64 calling_convention;
3745 codegen_->CreateUnresolvedFieldLocationSummary(
3746 instruction, instruction->GetFieldType(), calling_convention);
3747}
3748
3749void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3750 HUnresolvedStaticFieldGet* instruction) {
3751 FieldAccessCallingConventionARM64 calling_convention;
3752 codegen_->GenerateUnresolvedFieldAccess(instruction,
3753 instruction->GetFieldType(),
3754 instruction->GetFieldIndex(),
3755 instruction->GetDexPc(),
3756 calling_convention);
3757}
3758
3759void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3760 HUnresolvedStaticFieldSet* instruction) {
3761 FieldAccessCallingConventionARM64 calling_convention;
3762 codegen_->CreateUnresolvedFieldLocationSummary(
3763 instruction, instruction->GetFieldType(), calling_convention);
3764}
3765
3766void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3767 HUnresolvedStaticFieldSet* instruction) {
3768 FieldAccessCallingConventionARM64 calling_convention;
3769 codegen_->GenerateUnresolvedFieldAccess(instruction,
3770 instruction->GetFieldType(),
3771 instruction->GetFieldIndex(),
3772 instruction->GetDexPc(),
3773 calling_convention);
3774}
3775
Alexandre Rames5319def2014-10-23 10:03:10 +01003776void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3778}
3779
3780void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003781 HBasicBlock* block = instruction->GetBlock();
3782 if (block->GetLoopInformation() != nullptr) {
3783 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3784 // The back edge will generate the suspend check.
3785 return;
3786 }
3787 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3788 // The goto will generate the suspend check.
3789 return;
3790 }
3791 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003792}
3793
3794void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3795 temp->SetLocations(nullptr);
3796}
3797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003798void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003799 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01003800}
3801
Alexandre Rames67555f72014-11-18 10:55:16 +00003802void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3803 LocationSummary* locations =
3804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3805 InvokeRuntimeCallingConvention calling_convention;
3806 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3807}
3808
3809void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3810 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003811 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003812 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003813}
3814
3815void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3816 LocationSummary* locations =
3817 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3818 Primitive::Type input_type = conversion->GetInputType();
3819 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003820 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003821 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3822 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3823 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3824 }
3825
Alexandre Rames542361f2015-01-29 16:57:31 +00003826 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003827 locations->SetInAt(0, Location::RequiresFpuRegister());
3828 } else {
3829 locations->SetInAt(0, Location::RequiresRegister());
3830 }
3831
Alexandre Rames542361f2015-01-29 16:57:31 +00003832 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003833 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3834 } else {
3835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3836 }
3837}
3838
3839void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3840 Primitive::Type result_type = conversion->GetResultType();
3841 Primitive::Type input_type = conversion->GetInputType();
3842
3843 DCHECK_NE(input_type, result_type);
3844
Alexandre Rames542361f2015-01-29 16:57:31 +00003845 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003846 int result_size = Primitive::ComponentSize(result_type);
3847 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003848 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003849 Register output = OutputRegister(conversion);
3850 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003851 if ((result_type == Primitive::kPrimChar) && (input_size < result_size)) {
3852 __ Ubfx(output, source, 0, result_size * kBitsPerByte);
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003853 } else if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
3854 // 'int' values are used directly as W registers, discarding the top
3855 // bits, so we don't need to sign-extend and can just perform a move.
3856 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3857 // top 32 bits of the target register. We theoretically could leave those
3858 // bits unchanged, but we would have to make sure that no code uses a
3859 // 32bit input value as a 64bit value assuming that the top 32 bits are
3860 // zero.
3861 __ Mov(output.W(), source.W());
Alexandre Rames3e69f162014-12-10 10:36:50 +00003862 } else if ((result_type == Primitive::kPrimChar) ||
3863 ((input_type == Primitive::kPrimChar) && (result_size > input_size))) {
3864 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003865 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003866 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003867 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003868 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003869 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003870 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003871 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3872 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003873 } else if (Primitive::IsFloatingPointType(result_type) &&
3874 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003875 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3876 } else {
3877 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3878 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003879 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003880}
Alexandre Rames67555f72014-11-18 10:55:16 +00003881
Serban Constantinescu02164b32014-11-13 14:05:07 +00003882void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3883 HandleShift(ushr);
3884}
3885
3886void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3887 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003888}
3889
3890void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3891 HandleBinaryOp(instruction);
3892}
3893
3894void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3895 HandleBinaryOp(instruction);
3896}
3897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003898void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00003899 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00003900 LOG(FATAL) << "Unreachable";
3901}
3902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003903void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00003904 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00003905 LOG(FATAL) << "Unreachable";
3906}
3907
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003908void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3909 DCHECK(codegen_->IsBaseline());
3910 LocationSummary* locations =
3911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3912 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3913}
3914
3915void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3916 DCHECK(codegen_->IsBaseline());
3917 // Will be generated at use site.
3918}
3919
Mark Mendellfe57faa2015-09-18 09:26:15 -04003920// Simple implementation of packed switch - generate cascaded compare/jumps.
3921void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3922 LocationSummary* locations =
3923 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3924 locations->SetInAt(0, Location::RequiresRegister());
3925}
3926
3927void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3928 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08003929 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04003930 Register value_reg = InputRegisterAt(switch_instr, 0);
3931 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3932
Zheng Xu3927c8b2015-11-18 17:46:25 +08003933 // Roughly set 16 as max average assemblies generated per HIR in a graph.
3934 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
3935 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
3936 // make sure we don't emit it if the target may run out of range.
3937 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
3938 // ranges and emit the tables only as required.
3939 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003940
Zheng Xu3927c8b2015-11-18 17:46:25 +08003941 if (num_entries < kPackedSwitchJumpTableThreshold ||
3942 // Current instruction id is an upper bound of the number of HIRs in the graph.
3943 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
3944 // Create a series of compare/jumps.
3945 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3946 for (uint32_t i = 0; i < num_entries; i++) {
3947 int32_t case_value = lower_bound + i;
3948 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
3949 if (case_value == 0) {
3950 __ Cbz(value_reg, succ);
3951 } else {
3952 __ Cmp(value_reg, Operand(case_value));
3953 __ B(eq, succ);
3954 }
3955 }
3956
3957 // And the default for any other value.
3958 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3959 __ B(codegen_->GetLabelOf(default_block));
3960 }
3961 } else {
3962 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
3963 codegen_->AddJumpTable(jump_table);
3964
3965 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
3966
3967 // Below instructions should use at most one blocked register. Since there are two blocked
3968 // registers, we are free to block one.
3969 Register temp_w = temps.AcquireW();
3970 Register index;
3971 // Remove the bias.
3972 if (lower_bound != 0) {
3973 index = temp_w;
3974 __ Sub(index, value_reg, Operand(lower_bound));
3975 } else {
3976 index = value_reg;
3977 }
3978
3979 // Jump to default block if index is out of the range.
3980 __ Cmp(index, Operand(num_entries));
3981 __ B(hs, codegen_->GetLabelOf(default_block));
3982
3983 // In current VIXL implementation, it won't require any blocked registers to encode the
3984 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
3985 // register pressure.
3986 Register table_base = temps.AcquireX();
3987 // Load jump offset from the table.
3988 __ Adr(table_base, jump_table->GetTableStartLabel());
3989 Register jump_offset = temp_w;
3990 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
3991
3992 // Jump to target block by branching to table_base(pc related) + offset.
3993 Register target_address = table_base;
3994 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
3995 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003996 }
3997}
3998
Alexandre Rames67555f72014-11-18 10:55:16 +00003999#undef __
4000#undef QUICK_ENTRY_POINT
4001
Alexandre Rames5319def2014-10-23 10:03:10 +01004002} // namespace arm64
4003} // namespace art