blob: 072e805c7ac6bae5aae8e709b352c1fcbef8db4e [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);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000457 Primitive::Type ret_type = instruction_->GetType();
458 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
459 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800460 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
461 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000462 } 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);
497 }
498
Alexandre Rames9931f312015-06-19 14:47:01 +0100499 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
500
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700501 private:
502 HInstruction* const instruction_;
503 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
504};
505
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100506class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
507 public:
508 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 LocationSummary* locations = instruction_->GetLocations();
512 __ Bind(GetEntryLabel());
513 SaveLiveRegisters(codegen, locations);
514
515 InvokeRuntimeCallingConvention calling_convention;
516 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
517 parallel_move.AddMove(
518 locations->InAt(0),
519 LocationFrom(calling_convention.GetRegisterAt(0)),
520 Primitive::kPrimNot,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(1),
524 LocationFrom(calling_convention.GetRegisterAt(1)),
525 Primitive::kPrimInt,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(2),
529 LocationFrom(calling_convention.GetRegisterAt(2)),
530 Primitive::kPrimNot,
531 nullptr);
532 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
533
534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
535 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
536 instruction_,
537 instruction_->GetDexPc(),
538 this);
539 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
540 RestoreLiveRegisters(codegen, locations);
541 __ B(GetExitLabel());
542 }
543
544 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
545
546 private:
547 HInstruction* const instruction_;
548
549 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
550};
551
Zheng Xu3927c8b2015-11-18 17:46:25 +0800552void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
553 uint32_t num_entries = switch_instr_->GetNumEntries();
554 DCHECK_GE(num_entries, kPackedSwitchJumpTableThreshold);
555
556 // We are about to use the assembler to place literals directly. Make sure we have enough
557 // underlying code buffer and we have generated the jump table with right size.
558 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
559 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
560
561 __ Bind(&table_start_);
562 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
563 for (uint32_t i = 0; i < num_entries; i++) {
564 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
565 DCHECK(target_label->IsBound());
566 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
567 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
568 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
569 Literal<int32_t> literal(jump_offset);
570 __ place(&literal);
571 }
572}
573
Alexandre Rames5319def2014-10-23 10:03:10 +0100574#undef __
575
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100576Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100577 Location next_location;
578 if (type == Primitive::kPrimVoid) {
579 LOG(FATAL) << "Unreachable type " << type;
580 }
581
Alexandre Rames542361f2015-01-29 16:57:31 +0000582 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100583 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
584 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000585 } else if (!Primitive::IsFloatingPointType(type) &&
586 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000587 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
588 } else {
589 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000590 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
591 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100592 }
593
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000594 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000595 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100596 return next_location;
597}
598
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100599Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100600 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100601}
602
Serban Constantinescu579885a2015-02-22 20:51:33 +0000603CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
604 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100605 const CompilerOptions& compiler_options,
606 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100607 : CodeGenerator(graph,
608 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000609 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000610 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000611 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000612 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100613 compiler_options,
614 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100615 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800616 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100617 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000618 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000619 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000620 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100621 uint64_literals_(std::less<uint64_t>(),
622 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
623 method_patches_(MethodReferenceComparator(),
624 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
625 call_patches_(MethodReferenceComparator(),
626 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
627 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000628 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000629 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000630 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000631}
Alexandre Rames5319def2014-10-23 10:03:10 +0100632
Alexandre Rames67555f72014-11-18 10:55:16 +0000633#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100634
Zheng Xu3927c8b2015-11-18 17:46:25 +0800635void CodeGeneratorARM64::EmitJumpTables() {
636 for (auto jump_table : jump_tables_) {
637 jump_table->EmitTable(this);
638 }
639}
640
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000641void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800642 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000643 // Ensure we emit the literal pool.
644 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000645
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000646 CodeGenerator::Finalize(allocator);
647}
648
Zheng Xuad4450e2015-04-17 18:48:56 +0800649void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
650 // Note: There are 6 kinds of moves:
651 // 1. constant -> GPR/FPR (non-cycle)
652 // 2. constant -> stack (non-cycle)
653 // 3. GPR/FPR -> GPR/FPR
654 // 4. GPR/FPR -> stack
655 // 5. stack -> GPR/FPR
656 // 6. stack -> stack (non-cycle)
657 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
658 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
659 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
660 // dependency.
661 vixl_temps_.Open(GetVIXLAssembler());
662}
663
664void ParallelMoveResolverARM64::FinishEmitNativeCode() {
665 vixl_temps_.Close();
666}
667
668Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
669 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
670 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
671 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
672 Location scratch = GetScratchLocation(kind);
673 if (!scratch.Equals(Location::NoLocation())) {
674 return scratch;
675 }
676 // Allocate from VIXL temp registers.
677 if (kind == Location::kRegister) {
678 scratch = LocationFrom(vixl_temps_.AcquireX());
679 } else {
680 DCHECK(kind == Location::kFpuRegister);
681 scratch = LocationFrom(vixl_temps_.AcquireD());
682 }
683 AddScratchLocation(scratch);
684 return scratch;
685}
686
687void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
688 if (loc.IsRegister()) {
689 vixl_temps_.Release(XRegisterFrom(loc));
690 } else {
691 DCHECK(loc.IsFpuRegister());
692 vixl_temps_.Release(DRegisterFrom(loc));
693 }
694 RemoveScratchLocation(loc);
695}
696
Alexandre Rames3e69f162014-12-10 10:36:50 +0000697void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100698 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100699 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000700}
701
Alexandre Rames5319def2014-10-23 10:03:10 +0100702void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100703 MacroAssembler* masm = GetVIXLAssembler();
704 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000705 __ Bind(&frame_entry_label_);
706
Serban Constantinescu02164b32014-11-13 14:05:07 +0000707 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
708 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100709 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000710 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000711 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000712 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000713 __ Ldr(wzr, MemOperand(temp, 0));
714 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000715 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100716
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000717 if (!HasEmptyFrame()) {
718 int frame_size = GetFrameSize();
719 // Stack layout:
720 // sp[frame_size - 8] : lr.
721 // ... : other preserved core registers.
722 // ... : other preserved fp registers.
723 // ... : reserved frame space.
724 // sp[0] : current method.
725 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100726 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800727 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
728 frame_size - GetCoreSpillSize());
729 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
730 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000731 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100732}
733
734void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100735 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100736 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000737 if (!HasEmptyFrame()) {
738 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800739 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
740 frame_size - FrameEntrySpillSize());
741 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
742 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000743 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100744 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000745 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100746 __ Ret();
747 GetAssembler()->cfi().RestoreState();
748 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +0100749}
750
Zheng Xuda403092015-04-24 17:35:39 +0800751vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
752 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
753 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
754 core_spill_mask_);
755}
756
757vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
758 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
759 GetNumberOfFloatingPointRegisters()));
760 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
761 fpu_spill_mask_);
762}
763
Alexandre Rames5319def2014-10-23 10:03:10 +0100764void CodeGeneratorARM64::Bind(HBasicBlock* block) {
765 __ Bind(GetLabelOf(block));
766}
767
Alexandre Rames5319def2014-10-23 10:03:10 +0100768void CodeGeneratorARM64::Move(HInstruction* instruction,
769 Location location,
770 HInstruction* move_for) {
771 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +0100772 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000773 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +0100774
Nicolas Geoffray9b1eba32015-07-13 15:55:26 +0100775 if (instruction->IsFakeString()) {
776 // The fake string is an alias for null.
777 DCHECK(IsBaseline());
778 instruction = locations->Out().GetConstant();
779 DCHECK(instruction->IsNullConstant()) << instruction->DebugName();
780 }
781
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100782 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100783 MoveLocation(location,
784 Location::DoubleStackSlot(kCurrentMethodStackOffset),
785 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100786 } else if (locations != nullptr && locations->Out().Equals(location)) {
787 return;
788 } else if (instruction->IsIntConstant()
789 || instruction->IsLongConstant()
790 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000791 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +0100792 if (location.IsRegister()) {
793 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000794 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100795 (instruction->IsLongConstant() && dst.Is64Bits()));
796 __ Mov(dst, value);
797 } else {
798 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +0000799 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000800 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
801 ? temps.AcquireW()
802 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +0100803 __ Mov(temp, value);
804 __ Str(temp, StackOperandFrom(location));
805 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000806 } else if (instruction->IsTemporary()) {
807 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000808 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100809 } else if (instruction->IsLoadLocal()) {
810 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +0000811 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000812 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000813 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000814 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100815 }
816
817 } else {
818 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +0000819 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100820 }
821}
822
Calin Juravle175dc732015-08-25 15:42:32 +0100823void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
824 DCHECK(location.IsRegister());
825 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
826}
827
Calin Juravlee460d1d2015-09-29 04:52:17 +0100828void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
829 if (location.IsRegister()) {
830 locations->AddTemp(location);
831 } else {
832 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
833 }
834}
835
Alexandre Rames5319def2014-10-23 10:03:10 +0100836Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
837 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000838
Alexandre Rames5319def2014-10-23 10:03:10 +0100839 switch (type) {
840 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000841 case Primitive::kPrimInt:
842 case Primitive::kPrimFloat:
843 return Location::StackSlot(GetStackSlot(load->GetLocal()));
844
845 case Primitive::kPrimLong:
846 case Primitive::kPrimDouble:
847 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
848
Alexandre Rames5319def2014-10-23 10:03:10 +0100849 case Primitive::kPrimBoolean:
850 case Primitive::kPrimByte:
851 case Primitive::kPrimChar:
852 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +0100853 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +0100854 LOG(FATAL) << "Unexpected type " << type;
855 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000856
Alexandre Rames5319def2014-10-23 10:03:10 +0100857 LOG(FATAL) << "Unreachable";
858 return Location::NoLocation();
859}
860
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100861void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000862 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +0100863 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +0000864 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100866 if (value_can_be_null) {
867 __ Cbz(value, &done);
868 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100869 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
870 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000871 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100872 if (value_can_be_null) {
873 __ Bind(&done);
874 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100875}
876
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000877void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
878 // Blocked core registers:
879 // lr : Runtime reserved.
880 // tr : Runtime reserved.
881 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
882 // ip1 : VIXL core temp.
883 // ip0 : VIXL core temp.
884 //
885 // Blocked fp registers:
886 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +0100887 CPURegList reserved_core_registers = vixl_reserved_core_registers;
888 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +0100889 while (!reserved_core_registers.IsEmpty()) {
890 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
891 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000892
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000893 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +0800894 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
896 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000897
898 if (is_baseline) {
899 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
900 while (!reserved_core_baseline_registers.IsEmpty()) {
901 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
902 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100903 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000904
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100905 if (is_baseline || GetGraph()->IsDebuggable()) {
906 // Stubs do not save callee-save floating point registers. If the graph
907 // is debuggable, we need to deal with these registers differently. For
908 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000909 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
910 while (!reserved_fp_baseline_registers.IsEmpty()) {
911 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
912 }
913 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100914}
915
916Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
917 if (type == Primitive::kPrimVoid) {
918 LOG(FATAL) << "Unreachable type " << type;
919 }
920
Alexandre Rames542361f2015-01-29 16:57:31 +0000921 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000922 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
923 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100924 return Location::FpuRegisterLocation(reg);
925 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000926 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
927 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +0100928 return Location::RegisterLocation(reg);
929 }
930}
931
Alexandre Rames3e69f162014-12-10 10:36:50 +0000932size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
933 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
934 __ Str(reg, MemOperand(sp, stack_index));
935 return kArm64WordSize;
936}
937
938size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
939 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
940 __ Ldr(reg, MemOperand(sp, stack_index));
941 return kArm64WordSize;
942}
943
944size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
945 FPRegister reg = FPRegister(reg_id, kDRegSize);
946 __ Str(reg, MemOperand(sp, stack_index));
947 return kArm64WordSize;
948}
949
950size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
951 FPRegister reg = FPRegister(reg_id, kDRegSize);
952 __ Ldr(reg, MemOperand(sp, stack_index));
953 return kArm64WordSize;
954}
955
Alexandre Rames5319def2014-10-23 10:03:10 +0100956void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100958}
959
960void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100961 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +0100962}
963
Alexandre Rames67555f72014-11-18 10:55:16 +0000964void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000965 if (constant->IsIntConstant()) {
966 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
967 } else if (constant->IsLongConstant()) {
968 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
969 } else if (constant->IsNullConstant()) {
970 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +0000971 } else if (constant->IsFloatConstant()) {
972 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
973 } else {
974 DCHECK(constant->IsDoubleConstant());
975 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
976 }
977}
978
Alexandre Rames3e69f162014-12-10 10:36:50 +0000979
980static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
981 DCHECK(constant.IsConstant());
982 HConstant* cst = constant.GetConstant();
983 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000984 // Null is mapped to a core W register, which we associate with kPrimInt.
985 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +0000986 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
987 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
988 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
989}
990
Calin Juravlee460d1d2015-09-29 04:52:17 +0100991void CodeGeneratorARM64::MoveLocation(Location destination,
992 Location source,
993 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +0000994 if (source.Equals(destination)) {
995 return;
996 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000997
998 // A valid move can always be inferred from the destination and source
999 // locations. When moving from and to a register, the argument type can be
1000 // used to generate 32bit instead of 64bit moves. In debug mode we also
1001 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001002 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001003
1004 if (destination.IsRegister() || destination.IsFpuRegister()) {
1005 if (unspecified_type) {
1006 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1007 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001008 (src_cst != nullptr && (src_cst->IsIntConstant()
1009 || src_cst->IsFloatConstant()
1010 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001011 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001012 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001013 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001014 // If the source is a double stack slot or a 64bit constant, a 64bit
1015 // type is appropriate. Else the source is a register, and since the
1016 // type has not been specified, we chose a 64bit type to force a 64bit
1017 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001018 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001019 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001020 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001021 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1022 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1023 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1025 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1026 __ Ldr(dst, StackOperandFrom(source));
1027 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001028 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001029 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001030 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001031 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001032 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001033 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001034 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001035 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1036 ? Primitive::kPrimLong
1037 : Primitive::kPrimInt;
1038 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1039 }
1040 } else {
1041 DCHECK(source.IsFpuRegister());
1042 if (destination.IsRegister()) {
1043 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1044 ? Primitive::kPrimDouble
1045 : Primitive::kPrimFloat;
1046 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1047 } else {
1048 DCHECK(destination.IsFpuRegister());
1049 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001050 }
1051 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001052 } else { // The destination is not a register. It must be a stack slot.
1053 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1054 if (source.IsRegister() || source.IsFpuRegister()) {
1055 if (unspecified_type) {
1056 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001057 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001058 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001059 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001060 }
1061 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001062 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1063 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1064 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001065 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1067 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001068 UseScratchRegisterScope temps(GetVIXLAssembler());
1069 HConstant* src_cst = source.GetConstant();
1070 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001071 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001072 temp = temps.AcquireW();
1073 } else if (src_cst->IsLongConstant()) {
1074 temp = temps.AcquireX();
1075 } else if (src_cst->IsFloatConstant()) {
1076 temp = temps.AcquireS();
1077 } else {
1078 DCHECK(src_cst->IsDoubleConstant());
1079 temp = temps.AcquireD();
1080 }
1081 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001082 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001083 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001084 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001085 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001086 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001087 // There is generally less pressure on FP registers.
1088 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001089 __ Ldr(temp, StackOperandFrom(source));
1090 __ Str(temp, StackOperandFrom(destination));
1091 }
1092 }
1093}
1094
1095void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001096 CPURegister dst,
1097 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001098 switch (type) {
1099 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001100 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001101 break;
1102 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001103 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001104 break;
1105 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001106 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001107 break;
1108 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001109 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001110 break;
1111 case Primitive::kPrimInt:
1112 case Primitive::kPrimNot:
1113 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001114 case Primitive::kPrimFloat:
1115 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001116 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001117 __ Ldr(dst, src);
1118 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001119 case Primitive::kPrimVoid:
1120 LOG(FATAL) << "Unreachable type " << type;
1121 }
1122}
1123
Calin Juravle77520bc2015-01-12 18:45:46 +00001124void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001125 CPURegister dst,
1126 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001127 MacroAssembler* masm = GetVIXLAssembler();
1128 BlockPoolsScope block_pools(masm);
1129 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001130 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001131 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001132
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001133 DCHECK(!src.IsPreIndex());
1134 DCHECK(!src.IsPostIndex());
1135
1136 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001137 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001138 MemOperand base = MemOperand(temp_base);
1139 switch (type) {
1140 case Primitive::kPrimBoolean:
1141 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001142 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001143 break;
1144 case Primitive::kPrimByte:
1145 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001146 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001147 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1148 break;
1149 case Primitive::kPrimChar:
1150 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001151 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001152 break;
1153 case Primitive::kPrimShort:
1154 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001155 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001156 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1157 break;
1158 case Primitive::kPrimInt:
1159 case Primitive::kPrimNot:
1160 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001161 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001162 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001163 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001164 break;
1165 case Primitive::kPrimFloat:
1166 case Primitive::kPrimDouble: {
1167 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001168 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001169
1170 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1171 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001172 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001173 __ Fmov(FPRegister(dst), temp);
1174 break;
1175 }
1176 case Primitive::kPrimVoid:
1177 LOG(FATAL) << "Unreachable type " << type;
1178 }
1179}
1180
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001181void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001182 CPURegister src,
1183 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001184 switch (type) {
1185 case Primitive::kPrimBoolean:
1186 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001187 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001188 break;
1189 case Primitive::kPrimChar:
1190 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001191 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001192 break;
1193 case Primitive::kPrimInt:
1194 case Primitive::kPrimNot:
1195 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001196 case Primitive::kPrimFloat:
1197 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001198 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001199 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001200 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001201 case Primitive::kPrimVoid:
1202 LOG(FATAL) << "Unreachable type " << type;
1203 }
1204}
1205
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001206void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1207 CPURegister src,
1208 const MemOperand& dst) {
1209 UseScratchRegisterScope temps(GetVIXLAssembler());
1210 Register temp_base = temps.AcquireX();
1211
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001212 DCHECK(!dst.IsPreIndex());
1213 DCHECK(!dst.IsPostIndex());
1214
1215 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001216 Operand op = OperandFromMemOperand(dst);
1217 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001218 MemOperand base = MemOperand(temp_base);
1219 switch (type) {
1220 case Primitive::kPrimBoolean:
1221 case Primitive::kPrimByte:
1222 __ Stlrb(Register(src), base);
1223 break;
1224 case Primitive::kPrimChar:
1225 case Primitive::kPrimShort:
1226 __ Stlrh(Register(src), base);
1227 break;
1228 case Primitive::kPrimInt:
1229 case Primitive::kPrimNot:
1230 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001231 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001232 __ Stlr(Register(src), base);
1233 break;
1234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble: {
1236 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001237 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001238
1239 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1240 __ Fmov(temp, FPRegister(src));
1241 __ Stlr(temp, base);
1242 break;
1243 }
1244 case Primitive::kPrimVoid:
1245 LOG(FATAL) << "Unreachable type " << type;
1246 }
1247}
1248
Calin Juravle175dc732015-08-25 15:42:32 +01001249void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1250 HInstruction* instruction,
1251 uint32_t dex_pc,
1252 SlowPathCode* slow_path) {
1253 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1254 instruction,
1255 dex_pc,
1256 slow_path);
1257}
1258
Alexandre Rames67555f72014-11-18 10:55:16 +00001259void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1260 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001261 uint32_t dex_pc,
1262 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001263 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001264 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001265 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1266 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001267 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001268}
1269
1270void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1271 vixl::Register class_reg) {
1272 UseScratchRegisterScope temps(GetVIXLAssembler());
1273 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001274 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001275 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001276
Serban Constantinescu02164b32014-11-13 14:05:07 +00001277 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001278 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001279 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1280 __ Add(temp, class_reg, status_offset);
1281 __ Ldar(temp, HeapOperand(temp));
1282 __ Cmp(temp, mirror::Class::kStatusInitialized);
1283 __ B(lt, slow_path->GetEntryLabel());
1284 } else {
1285 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1286 __ Cmp(temp, mirror::Class::kStatusInitialized);
1287 __ B(lt, slow_path->GetEntryLabel());
1288 __ Dmb(InnerShareable, BarrierReads);
1289 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001290 __ Bind(slow_path->GetExitLabel());
1291}
Alexandre Rames5319def2014-10-23 10:03:10 +01001292
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001293void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1294 BarrierType type = BarrierAll;
1295
1296 switch (kind) {
1297 case MemBarrierKind::kAnyAny:
1298 case MemBarrierKind::kAnyStore: {
1299 type = BarrierAll;
1300 break;
1301 }
1302 case MemBarrierKind::kLoadAny: {
1303 type = BarrierReads;
1304 break;
1305 }
1306 case MemBarrierKind::kStoreStore: {
1307 type = BarrierWrites;
1308 break;
1309 }
1310 default:
1311 LOG(FATAL) << "Unexpected memory barrier " << kind;
1312 }
1313 __ Dmb(InnerShareable, type);
1314}
1315
Serban Constantinescu02164b32014-11-13 14:05:07 +00001316void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1317 HBasicBlock* successor) {
1318 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001319 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1320 if (slow_path == nullptr) {
1321 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1322 instruction->SetSlowPath(slow_path);
1323 codegen_->AddSlowPath(slow_path);
1324 if (successor != nullptr) {
1325 DCHECK(successor->IsLoopHeader());
1326 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1327 }
1328 } else {
1329 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1330 }
1331
Serban Constantinescu02164b32014-11-13 14:05:07 +00001332 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1333 Register temp = temps.AcquireW();
1334
1335 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1336 if (successor == nullptr) {
1337 __ Cbnz(temp, slow_path->GetEntryLabel());
1338 __ Bind(slow_path->GetReturnLabel());
1339 } else {
1340 __ Cbz(temp, codegen_->GetLabelOf(successor));
1341 __ B(slow_path->GetEntryLabel());
1342 // slow_path will return to GetLabelOf(successor).
1343 }
1344}
1345
Alexandre Rames5319def2014-10-23 10:03:10 +01001346InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1347 CodeGeneratorARM64* codegen)
1348 : HGraphVisitor(graph),
1349 assembler_(codegen->GetAssembler()),
1350 codegen_(codegen) {}
1351
1352#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001353 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001354
1355#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1356
1357enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001358 // Using a base helps identify when we hit such breakpoints.
1359 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001360#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1361 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1362#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1363};
1364
1365#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001366 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001367 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1368 } \
1369 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1370 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1371 locations->SetOut(Location::Any()); \
1372 }
1373 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1374#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1375
1376#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001377#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001378
Alexandre Rames67555f72014-11-18 10:55:16 +00001379void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001380 DCHECK_EQ(instr->InputCount(), 2U);
1381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1382 Primitive::Type type = instr->GetResultType();
1383 switch (type) {
1384 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001385 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001386 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001387 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001389 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001390
1391 case Primitive::kPrimFloat:
1392 case Primitive::kPrimDouble:
1393 locations->SetInAt(0, Location::RequiresFpuRegister());
1394 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001395 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001396 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001397
Alexandre Rames5319def2014-10-23 10:03:10 +01001398 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001399 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001400 }
1401}
1402
Alexandre Rames09a99962015-04-15 11:47:56 +01001403void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
1404 LocationSummary* locations =
1405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1406 locations->SetInAt(0, Location::RequiresRegister());
1407 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1408 locations->SetOut(Location::RequiresFpuRegister());
1409 } else {
1410 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1411 }
1412}
1413
1414void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1415 const FieldInfo& field_info) {
1416 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001417 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001418 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001419
1420 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1421 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1422
1423 if (field_info.IsVolatile()) {
1424 if (use_acquire_release) {
1425 // NB: LoadAcquire will record the pc info if needed.
1426 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1427 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001428 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001429 codegen_->MaybeRecordImplicitNullCheck(instruction);
1430 // For IRIW sequential consistency kLoadAny is not sufficient.
1431 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1432 }
1433 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001434 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001435 codegen_->MaybeRecordImplicitNullCheck(instruction);
1436 }
Roland Levillain4d027112015-07-01 15:41:14 +01001437
1438 if (field_type == Primitive::kPrimNot) {
1439 GetAssembler()->MaybeUnpoisonHeapReference(OutputCPURegister(instruction).W());
1440 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001441}
1442
1443void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1444 LocationSummary* locations =
1445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1446 locations->SetInAt(0, Location::RequiresRegister());
1447 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1448 locations->SetInAt(1, Location::RequiresFpuRegister());
1449 } else {
1450 locations->SetInAt(1, Location::RequiresRegister());
1451 }
1452}
1453
1454void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001455 const FieldInfo& field_info,
1456 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001457 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001458 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001459
1460 Register obj = InputRegisterAt(instruction, 0);
1461 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001462 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001463 Offset offset = field_info.GetFieldOffset();
1464 Primitive::Type field_type = field_info.GetFieldType();
1465 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1466
Roland Levillain4d027112015-07-01 15:41:14 +01001467 {
1468 // We use a block to end the scratch scope before the write barrier, thus
1469 // freeing the temporary registers so they can be used in `MarkGCCard`.
1470 UseScratchRegisterScope temps(GetVIXLAssembler());
1471
1472 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1473 DCHECK(value.IsW());
1474 Register temp = temps.AcquireW();
1475 __ Mov(temp, value.W());
1476 GetAssembler()->PoisonHeapReference(temp.W());
1477 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001478 }
Roland Levillain4d027112015-07-01 15:41:14 +01001479
1480 if (field_info.IsVolatile()) {
1481 if (use_acquire_release) {
1482 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1483 codegen_->MaybeRecordImplicitNullCheck(instruction);
1484 } else {
1485 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1486 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1487 codegen_->MaybeRecordImplicitNullCheck(instruction);
1488 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1489 }
1490 } else {
1491 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1492 codegen_->MaybeRecordImplicitNullCheck(instruction);
1493 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001494 }
1495
1496 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001497 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001498 }
1499}
1500
Alexandre Rames67555f72014-11-18 10:55:16 +00001501void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001502 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001503
1504 switch (type) {
1505 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001506 case Primitive::kPrimLong: {
1507 Register dst = OutputRegister(instr);
1508 Register lhs = InputRegisterAt(instr, 0);
1509 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001510 if (instr->IsAdd()) {
1511 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001512 } else if (instr->IsAnd()) {
1513 __ And(dst, lhs, rhs);
1514 } else if (instr->IsOr()) {
1515 __ Orr(dst, lhs, rhs);
1516 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001517 __ Sub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001518 } else {
1519 DCHECK(instr->IsXor());
1520 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001521 }
1522 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001523 }
1524 case Primitive::kPrimFloat:
1525 case Primitive::kPrimDouble: {
1526 FPRegister dst = OutputFPRegister(instr);
1527 FPRegister lhs = InputFPRegisterAt(instr, 0);
1528 FPRegister rhs = InputFPRegisterAt(instr, 1);
1529 if (instr->IsAdd()) {
1530 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001531 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001532 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001533 } else {
1534 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001535 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001536 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001537 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001538 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001539 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001540 }
1541}
1542
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1544 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1545
1546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1547 Primitive::Type type = instr->GetResultType();
1548 switch (type) {
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimLong: {
1551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1553 locations->SetOut(Location::RequiresRegister());
1554 break;
1555 }
1556 default:
1557 LOG(FATAL) << "Unexpected shift type " << type;
1558 }
1559}
1560
1561void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1562 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1563
1564 Primitive::Type type = instr->GetType();
1565 switch (type) {
1566 case Primitive::kPrimInt:
1567 case Primitive::kPrimLong: {
1568 Register dst = OutputRegister(instr);
1569 Register lhs = InputRegisterAt(instr, 0);
1570 Operand rhs = InputOperandAt(instr, 1);
1571 if (rhs.IsImmediate()) {
1572 uint32_t shift_value = (type == Primitive::kPrimInt)
1573 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1574 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1575 if (instr->IsShl()) {
1576 __ Lsl(dst, lhs, shift_value);
1577 } else if (instr->IsShr()) {
1578 __ Asr(dst, lhs, shift_value);
1579 } else {
1580 __ Lsr(dst, lhs, shift_value);
1581 }
1582 } else {
1583 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1584
1585 if (instr->IsShl()) {
1586 __ Lsl(dst, lhs, rhs_reg);
1587 } else if (instr->IsShr()) {
1588 __ Asr(dst, lhs, rhs_reg);
1589 } else {
1590 __ Lsr(dst, lhs, rhs_reg);
1591 }
1592 }
1593 break;
1594 }
1595 default:
1596 LOG(FATAL) << "Unexpected shift operation type " << type;
1597 }
1598}
1599
Alexandre Rames5319def2014-10-23 10:03:10 +01001600void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001601 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001602}
1603
1604void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001605 HandleBinaryOp(instruction);
1606}
1607
1608void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1609 HandleBinaryOp(instruction);
1610}
1611
1612void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1613 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001614}
1615
Alexandre Rames8626b742015-11-25 16:28:08 +00001616void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1617 HArm64DataProcWithShifterOp* instruction) {
1618 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1619 instruction->GetType() == Primitive::kPrimLong);
1620 LocationSummary* locations =
1621 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1622 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1623 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1624 } else {
1625 locations->SetInAt(0, Location::RequiresRegister());
1626 }
1627 locations->SetInAt(1, Location::RequiresRegister());
1628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1629}
1630
1631void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1632 HArm64DataProcWithShifterOp* instruction) {
1633 Primitive::Type type = instruction->GetType();
1634 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1635 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1636 Register out = OutputRegister(instruction);
1637 Register left;
1638 if (kind != HInstruction::kNeg) {
1639 left = InputRegisterAt(instruction, 0);
1640 }
1641 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1642 // shifter operand operation, the IR generating `right_reg` (input to the type
1643 // conversion) can have a different type from the current instruction's type,
1644 // so we manually indicate the type.
1645 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1646 int64_t shift_amount = (type == Primitive::kPrimInt)
1647 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1648 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1649
1650 Operand right_operand(0);
1651
1652 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1653 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1654 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1655 } else {
1656 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1657 }
1658
1659 // Logical binary operations do not support extension operations in the
1660 // operand. Note that VIXL would still manage if it was passed by generating
1661 // the extension as a separate instruction.
1662 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1663 DCHECK(!right_operand.IsExtendedRegister() ||
1664 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1665 kind != HInstruction::kNeg));
1666 switch (kind) {
1667 case HInstruction::kAdd:
1668 __ Add(out, left, right_operand);
1669 break;
1670 case HInstruction::kAnd:
1671 __ And(out, left, right_operand);
1672 break;
1673 case HInstruction::kNeg:
1674 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1675 __ Neg(out, right_operand);
1676 break;
1677 case HInstruction::kOr:
1678 __ Orr(out, left, right_operand);
1679 break;
1680 case HInstruction::kSub:
1681 __ Sub(out, left, right_operand);
1682 break;
1683 case HInstruction::kXor:
1684 __ Eor(out, left, right_operand);
1685 break;
1686 default:
1687 LOG(FATAL) << "Unexpected operation kind: " << kind;
1688 UNREACHABLE();
1689 }
1690}
1691
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001692void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
1693 LocationSummary* locations =
1694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1697 locations->SetOut(Location::RequiresRegister());
1698}
1699
1700void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1701 HArm64IntermediateAddress* instruction) {
1702 __ Add(OutputRegister(instruction),
1703 InputRegisterAt(instruction, 0),
1704 Operand(InputOperandAt(instruction, 1)));
1705}
1706
Alexandre Rames418318f2015-11-20 15:55:47 +00001707void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1708 LocationSummary* locations =
1709 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1710 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1711 Location::RequiresRegister());
1712 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1713 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1715}
1716
1717void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1718 Register res = OutputRegister(instr);
1719 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
1720 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
1721 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
1722
1723 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1724 // This fixup should be carried out for all multiply-accumulate instructions:
1725 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1726 if (instr->GetType() == Primitive::kPrimLong &&
1727 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
1728 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
1729 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
1730 if (prev->IsLoadOrStore()) {
1731 // Make sure we emit only exactly one nop.
1732 vixl::CodeBufferCheckScope scope(masm,
1733 vixl::kInstructionSize,
1734 vixl::CodeBufferCheckScope::kCheck,
1735 vixl::CodeBufferCheckScope::kExactSize);
1736 __ nop();
1737 }
1738 }
1739
1740 if (instr->GetOpKind() == HInstruction::kAdd) {
1741 __ Madd(res, mul_left, mul_right, accumulator);
1742 } else {
1743 DCHECK(instr->GetOpKind() == HInstruction::kSub);
1744 __ Msub(res, mul_left, mul_right, accumulator);
1745 }
1746}
1747
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001748void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
1749 LocationSummary* locations =
1750 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1751 locations->SetInAt(0, Location::RequiresRegister());
1752 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01001753 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1754 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1755 } else {
1756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1757 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001758}
1759
1760void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001761 Primitive::Type type = instruction->GetType();
1762 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001763 Location index = instruction->GetLocations()->InAt(1);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001764 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001765 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001766 CPURegister dest = OutputCPURegister(instruction);
1767
Alexandre Ramesd921d642015-04-16 15:07:16 +01001768 MacroAssembler* masm = GetVIXLAssembler();
1769 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001770 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01001771 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001772
1773 if (index.IsConstant()) {
1774 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001775 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001776 } else {
1777 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001778 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1779 // We do not need to compute the intermediate address from the array: the
1780 // input instruction has done it already. See the comment in
1781 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1782 if (kIsDebugBuild) {
1783 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1784 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1785 }
1786 temp = obj;
1787 } else {
1788 __ Add(temp, obj, offset);
1789 }
Alexandre Rames82000b02015-07-07 11:34:16 +01001790 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001791 }
1792
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001793 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00001794 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001795
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001796 if (instruction->GetType() == Primitive::kPrimNot) {
1797 GetAssembler()->MaybeUnpoisonHeapReference(dest.W());
Roland Levillain4d027112015-07-01 15:41:14 +01001798 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001799}
1800
Alexandre Rames5319def2014-10-23 10:03:10 +01001801void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
1802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1803 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001804 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001805}
1806
1807void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001808 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001809 __ Ldr(OutputRegister(instruction),
1810 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00001811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001812}
1813
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001814void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1816 instruction,
1817 instruction->NeedsTypeCheck() ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
1818 locations->SetInAt(0, Location::RequiresRegister());
1819 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1820 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1821 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001822 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001823 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001824 }
1825}
1826
1827void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
1828 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01001829 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001830 bool may_need_runtime_call = locations->CanCall();
1831 bool needs_write_barrier =
1832 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01001833
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001834 Register array = InputRegisterAt(instruction, 0);
1835 CPURegister value = InputCPURegisterAt(instruction, 2);
1836 CPURegister source = value;
1837 Location index = locations->InAt(1);
1838 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
1839 MemOperand destination = HeapOperand(array);
1840 MacroAssembler* masm = GetVIXLAssembler();
1841 BlockPoolsScope block_pools(masm);
1842
1843 if (!needs_write_barrier) {
1844 DCHECK(!may_need_runtime_call);
1845 if (index.IsConstant()) {
1846 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
1847 destination = HeapOperand(array, offset);
1848 } else {
1849 UseScratchRegisterScope temps(masm);
1850 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001851 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
1852 // We do not need to compute the intermediate address from the array: the
1853 // input instruction has done it already. See the comment in
1854 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
1855 if (kIsDebugBuild) {
1856 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
1857 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
1858 }
1859 temp = array;
1860 } else {
1861 __ Add(temp, array, offset);
1862 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001863 destination = HeapOperand(temp,
1864 XRegisterFrom(index),
1865 LSL,
1866 Primitive::ComponentSizeShift(value_type));
1867 }
1868 codegen_->Store(value_type, value, destination);
1869 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001870 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001871 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001872 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001873 vixl::Label done;
1874 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01001875 {
1876 // We use a block to end the scratch scope before the write barrier, thus
1877 // freeing the temporary registers so they can be used in `MarkGCCard`.
1878 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001879 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01001880 if (index.IsConstant()) {
1881 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001882 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01001883 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01001884 destination = HeapOperand(temp,
1885 XRegisterFrom(index),
1886 LSL,
1887 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01001888 }
1889
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001890 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1891 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1892 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1893
1894 if (may_need_runtime_call) {
1895 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
1896 codegen_->AddSlowPath(slow_path);
1897 if (instruction->GetValueCanBeNull()) {
1898 vixl::Label non_zero;
1899 __ Cbnz(Register(value), &non_zero);
1900 if (!index.IsConstant()) {
1901 __ Add(temp, array, offset);
1902 }
1903 __ Str(wzr, destination);
1904 codegen_->MaybeRecordImplicitNullCheck(instruction);
1905 __ B(&done);
1906 __ Bind(&non_zero);
1907 }
1908
1909 Register temp2 = temps.AcquireSameSizeAs(array);
1910 __ Ldr(temp, HeapOperand(array, class_offset));
1911 codegen_->MaybeRecordImplicitNullCheck(instruction);
1912 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1913 __ Ldr(temp, HeapOperand(temp, component_offset));
1914 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
1915 // No need to poison/unpoison, we're comparing two poisoned references.
1916 __ Cmp(temp, temp2);
1917 if (instruction->StaticTypeOfArrayIsObjectArray()) {
1918 vixl::Label do_put;
1919 __ B(eq, &do_put);
1920 GetAssembler()->MaybeUnpoisonHeapReference(temp);
1921 __ Ldr(temp, HeapOperand(temp, super_offset));
1922 // No need to unpoison, we're comparing against null.
1923 __ Cbnz(temp, slow_path->GetEntryLabel());
1924 __ Bind(&do_put);
1925 } else {
1926 __ B(ne, slow_path->GetEntryLabel());
1927 }
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001928 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001929 }
1930
1931 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001932 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001933 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01001934 __ Mov(temp2, value.W());
1935 GetAssembler()->PoisonHeapReference(temp2);
1936 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001937 }
1938
1939 if (!index.IsConstant()) {
1940 __ Add(temp, array, offset);
1941 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01001942 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001943
1944 if (!may_need_runtime_call) {
1945 codegen_->MaybeRecordImplicitNullCheck(instruction);
1946 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001947 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01001948
1949 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
1950
1951 if (done.IsLinked()) {
1952 __ Bind(&done);
1953 }
1954
1955 if (slow_path != nullptr) {
1956 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01001957 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001958 }
1959}
1960
Alexandre Rames67555f72014-11-18 10:55:16 +00001961void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001962 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1963 ? LocationSummary::kCallOnSlowPath
1964 : LocationSummary::kNoCall;
1965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00001966 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00001967 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00001968 if (instruction->HasUses()) {
1969 locations->SetOut(Location::SameAsFirstInput());
1970 }
1971}
1972
1973void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001974 BoundsCheckSlowPathARM64* slow_path =
1975 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00001976 codegen_->AddSlowPath(slow_path);
1977
1978 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
1979 __ B(slow_path->GetEntryLabel(), hs);
1980}
1981
Alexandre Rames67555f72014-11-18 10:55:16 +00001982void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
1983 LocationSummary* locations =
1984 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1985 locations->SetInAt(0, Location::RequiresRegister());
1986 if (check->HasUses()) {
1987 locations->SetOut(Location::SameAsFirstInput());
1988 }
1989}
1990
1991void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
1992 // We assume the class is not null.
1993 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
1994 check->GetLoadClass(), check, check->GetDexPc(), true);
1995 codegen_->AddSlowPath(slow_path);
1996 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
1997}
1998
Roland Levillain7f63c522015-07-13 15:54:55 +00001999static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2000 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2001 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2002}
2003
Serban Constantinescu02164b32014-11-13 14:05:07 +00002004void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002005 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002006 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2007 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002008 switch (in_type) {
2009 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002010 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002011 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2013 break;
2014 }
2015 case Primitive::kPrimFloat:
2016 case Primitive::kPrimDouble: {
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002018 locations->SetInAt(1,
2019 IsFloatingPointZeroConstant(compare->InputAt(1))
2020 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2021 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002022 locations->SetOut(Location::RequiresRegister());
2023 break;
2024 }
2025 default:
2026 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2027 }
2028}
2029
2030void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2031 Primitive::Type in_type = compare->InputAt(0)->GetType();
2032
2033 // 0 if: left == right
2034 // 1 if: left > right
2035 // -1 if: left < right
2036 switch (in_type) {
2037 case Primitive::kPrimLong: {
2038 Register result = OutputRegister(compare);
2039 Register left = InputRegisterAt(compare, 0);
2040 Operand right = InputOperandAt(compare, 1);
2041
2042 __ Cmp(left, right);
2043 __ Cset(result, ne);
2044 __ Cneg(result, result, lt);
2045 break;
2046 }
2047 case Primitive::kPrimFloat:
2048 case Primitive::kPrimDouble: {
2049 Register result = OutputRegister(compare);
2050 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002051 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002052 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2053 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002054 __ Fcmp(left, 0.0);
2055 } else {
2056 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2057 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002058 if (compare->IsGtBias()) {
2059 __ Cset(result, ne);
2060 } else {
2061 __ Csetm(result, ne);
2062 }
2063 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01002064 break;
2065 }
2066 default:
2067 LOG(FATAL) << "Unimplemented compare type " << in_type;
2068 }
2069}
2070
2071void LocationsBuilderARM64::VisitCondition(HCondition* instruction) {
2072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002073
2074 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2075 locations->SetInAt(0, Location::RequiresFpuRegister());
2076 locations->SetInAt(1,
2077 IsFloatingPointZeroConstant(instruction->InputAt(1))
2078 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2079 : Location::RequiresFpuRegister());
2080 } else {
2081 // Integer cases.
2082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2084 }
2085
Alexandre Rames5319def2014-10-23 10:03:10 +01002086 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002087 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002088 }
2089}
2090
2091void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) {
2092 if (!instruction->NeedsMaterialization()) {
2093 return;
2094 }
2095
2096 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002097 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002098 IfCondition if_cond = instruction->GetCondition();
2099 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002100
Roland Levillain7f63c522015-07-13 15:54:55 +00002101 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2102 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2103 if (locations->InAt(1).IsConstant()) {
2104 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2105 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2106 __ Fcmp(lhs, 0.0);
2107 } else {
2108 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2109 }
2110 __ Cset(res, arm64_cond);
2111 if (instruction->IsFPConditionTrueIfNaN()) {
2112 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
2113 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
2114 } else if (instruction->IsFPConditionFalseIfNaN()) {
2115 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
2116 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2117 }
2118 } else {
2119 // Integer cases.
2120 Register lhs = InputRegisterAt(instruction, 0);
2121 Operand rhs = InputOperandAt(instruction, 1);
2122 __ Cmp(lhs, rhs);
2123 __ Cset(res, arm64_cond);
2124 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002125}
2126
2127#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2128 M(Equal) \
2129 M(NotEqual) \
2130 M(LessThan) \
2131 M(LessThanOrEqual) \
2132 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002133 M(GreaterThanOrEqual) \
2134 M(Below) \
2135 M(BelowOrEqual) \
2136 M(Above) \
2137 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002138#define DEFINE_CONDITION_VISITORS(Name) \
2139void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \
2140void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); }
2141FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002142#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002143#undef FOR_EACH_CONDITION_INSTRUCTION
2144
Zheng Xuc6667102015-05-15 16:08:45 +08002145void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2146 DCHECK(instruction->IsDiv() || instruction->IsRem());
2147
2148 LocationSummary* locations = instruction->GetLocations();
2149 Location second = locations->InAt(1);
2150 DCHECK(second.IsConstant());
2151
2152 Register out = OutputRegister(instruction);
2153 Register dividend = InputRegisterAt(instruction, 0);
2154 int64_t imm = Int64FromConstant(second.GetConstant());
2155 DCHECK(imm == 1 || imm == -1);
2156
2157 if (instruction->IsRem()) {
2158 __ Mov(out, 0);
2159 } else {
2160 if (imm == 1) {
2161 __ Mov(out, dividend);
2162 } else {
2163 __ Neg(out, dividend);
2164 }
2165 }
2166}
2167
2168void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2169 DCHECK(instruction->IsDiv() || instruction->IsRem());
2170
2171 LocationSummary* locations = instruction->GetLocations();
2172 Location second = locations->InAt(1);
2173 DCHECK(second.IsConstant());
2174
2175 Register out = OutputRegister(instruction);
2176 Register dividend = InputRegisterAt(instruction, 0);
2177 int64_t imm = Int64FromConstant(second.GetConstant());
Vladimir Marko80afd022015-05-19 18:08:00 +01002178 uint64_t abs_imm = static_cast<uint64_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002179 DCHECK(IsPowerOfTwo(abs_imm));
2180 int ctz_imm = CTZ(abs_imm);
2181
2182 UseScratchRegisterScope temps(GetVIXLAssembler());
2183 Register temp = temps.AcquireSameSizeAs(out);
2184
2185 if (instruction->IsDiv()) {
2186 __ Add(temp, dividend, abs_imm - 1);
2187 __ Cmp(dividend, 0);
2188 __ Csel(out, temp, dividend, lt);
2189 if (imm > 0) {
2190 __ Asr(out, out, ctz_imm);
2191 } else {
2192 __ Neg(out, Operand(out, ASR, ctz_imm));
2193 }
2194 } else {
2195 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2196 __ Asr(temp, dividend, bits - 1);
2197 __ Lsr(temp, temp, bits - ctz_imm);
2198 __ Add(out, dividend, temp);
2199 __ And(out, out, abs_imm - 1);
2200 __ Sub(out, out, temp);
2201 }
2202}
2203
2204void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2205 DCHECK(instruction->IsDiv() || instruction->IsRem());
2206
2207 LocationSummary* locations = instruction->GetLocations();
2208 Location second = locations->InAt(1);
2209 DCHECK(second.IsConstant());
2210
2211 Register out = OutputRegister(instruction);
2212 Register dividend = InputRegisterAt(instruction, 0);
2213 int64_t imm = Int64FromConstant(second.GetConstant());
2214
2215 Primitive::Type type = instruction->GetResultType();
2216 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2217
2218 int64_t magic;
2219 int shift;
2220 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2221
2222 UseScratchRegisterScope temps(GetVIXLAssembler());
2223 Register temp = temps.AcquireSameSizeAs(out);
2224
2225 // temp = get_high(dividend * magic)
2226 __ Mov(temp, magic);
2227 if (type == Primitive::kPrimLong) {
2228 __ Smulh(temp, dividend, temp);
2229 } else {
2230 __ Smull(temp.X(), dividend, temp);
2231 __ Lsr(temp.X(), temp.X(), 32);
2232 }
2233
2234 if (imm > 0 && magic < 0) {
2235 __ Add(temp, temp, dividend);
2236 } else if (imm < 0 && magic > 0) {
2237 __ Sub(temp, temp, dividend);
2238 }
2239
2240 if (shift != 0) {
2241 __ Asr(temp, temp, shift);
2242 }
2243
2244 if (instruction->IsDiv()) {
2245 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2246 } else {
2247 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2248 // TODO: Strength reduction for msub.
2249 Register temp_imm = temps.AcquireSameSizeAs(out);
2250 __ Mov(temp_imm, imm);
2251 __ Msub(out, temp, temp_imm, dividend);
2252 }
2253}
2254
2255void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2256 DCHECK(instruction->IsDiv() || instruction->IsRem());
2257 Primitive::Type type = instruction->GetResultType();
2258 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2259
2260 LocationSummary* locations = instruction->GetLocations();
2261 Register out = OutputRegister(instruction);
2262 Location second = locations->InAt(1);
2263
2264 if (second.IsConstant()) {
2265 int64_t imm = Int64FromConstant(second.GetConstant());
2266
2267 if (imm == 0) {
2268 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2269 } else if (imm == 1 || imm == -1) {
2270 DivRemOneOrMinusOne(instruction);
2271 } else if (IsPowerOfTwo(std::abs(imm))) {
2272 DivRemByPowerOfTwo(instruction);
2273 } else {
2274 DCHECK(imm <= -2 || imm >= 2);
2275 GenerateDivRemWithAnyConstant(instruction);
2276 }
2277 } else {
2278 Register dividend = InputRegisterAt(instruction, 0);
2279 Register divisor = InputRegisterAt(instruction, 1);
2280 if (instruction->IsDiv()) {
2281 __ Sdiv(out, dividend, divisor);
2282 } else {
2283 UseScratchRegisterScope temps(GetVIXLAssembler());
2284 Register temp = temps.AcquireSameSizeAs(out);
2285 __ Sdiv(temp, dividend, divisor);
2286 __ Msub(out, temp, divisor, dividend);
2287 }
2288 }
2289}
2290
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002291void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2292 LocationSummary* locations =
2293 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2294 switch (div->GetResultType()) {
2295 case Primitive::kPrimInt:
2296 case Primitive::kPrimLong:
2297 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002298 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2300 break;
2301
2302 case Primitive::kPrimFloat:
2303 case Primitive::kPrimDouble:
2304 locations->SetInAt(0, Location::RequiresFpuRegister());
2305 locations->SetInAt(1, Location::RequiresFpuRegister());
2306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2307 break;
2308
2309 default:
2310 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2311 }
2312}
2313
2314void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2315 Primitive::Type type = div->GetResultType();
2316 switch (type) {
2317 case Primitive::kPrimInt:
2318 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002319 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002320 break;
2321
2322 case Primitive::kPrimFloat:
2323 case Primitive::kPrimDouble:
2324 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2325 break;
2326
2327 default:
2328 LOG(FATAL) << "Unexpected div type " << type;
2329 }
2330}
2331
Alexandre Rames67555f72014-11-18 10:55:16 +00002332void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002333 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2334 ? LocationSummary::kCallOnSlowPath
2335 : LocationSummary::kNoCall;
2336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002337 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2338 if (instruction->HasUses()) {
2339 locations->SetOut(Location::SameAsFirstInput());
2340 }
2341}
2342
2343void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2344 SlowPathCodeARM64* slow_path =
2345 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2346 codegen_->AddSlowPath(slow_path);
2347 Location value = instruction->GetLocations()->InAt(0);
2348
Alexandre Rames3e69f162014-12-10 10:36:50 +00002349 Primitive::Type type = instruction->GetType();
2350
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002351 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2352 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002353 return;
2354 }
2355
Alexandre Rames67555f72014-11-18 10:55:16 +00002356 if (value.IsConstant()) {
2357 int64_t divisor = Int64ConstantFrom(value);
2358 if (divisor == 0) {
2359 __ B(slow_path->GetEntryLabel());
2360 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002361 // A division by a non-null constant is valid. We don't need to perform
2362 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002363 }
2364 } else {
2365 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2366 }
2367}
2368
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002369void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2372 locations->SetOut(Location::ConstantLocation(constant));
2373}
2374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002375void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2376 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002377 // Will be generated at use site.
2378}
2379
Alexandre Rames5319def2014-10-23 10:03:10 +01002380void LocationsBuilderARM64::VisitExit(HExit* exit) {
2381 exit->SetLocations(nullptr);
2382}
2383
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002384void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002385}
2386
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002387void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2388 LocationSummary* locations =
2389 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2390 locations->SetOut(Location::ConstantLocation(constant));
2391}
2392
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002393void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002394 // Will be generated at use site.
2395}
2396
David Brazdilfc6a86a2015-06-26 10:33:45 +00002397void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002398 DCHECK(!successor->IsExitBlock());
2399 HBasicBlock* block = got->GetBlock();
2400 HInstruction* previous = got->GetPrevious();
2401 HLoopInformation* info = block->GetLoopInformation();
2402
David Brazdil46e2a392015-03-16 17:31:52 +00002403 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002404 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2405 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2406 return;
2407 }
2408 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2409 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2410 }
2411 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002412 __ B(codegen_->GetLabelOf(successor));
2413 }
2414}
2415
David Brazdilfc6a86a2015-06-26 10:33:45 +00002416void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2417 got->SetLocations(nullptr);
2418}
2419
2420void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2421 HandleGoto(got, got->GetSuccessor());
2422}
2423
2424void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2425 try_boundary->SetLocations(nullptr);
2426}
2427
2428void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2429 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2430 if (!successor->IsExitBlock()) {
2431 HandleGoto(try_boundary, successor);
2432 }
2433}
2434
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002435void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002436 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002437 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002438 vixl::Label* false_target) {
2439 // FP branching requires both targets to be explicit. If either of the targets
2440 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2441 vixl::Label fallthrough_target;
2442 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002443
David Brazdil0debae72015-11-12 18:37:00 +00002444 if (true_target == nullptr && false_target == nullptr) {
2445 // Nothing to do. The code always falls through.
2446 return;
2447 } else if (cond->IsIntConstant()) {
2448 // Constant condition, statically compared against 1.
2449 if (cond->AsIntConstant()->IsOne()) {
2450 if (true_target != nullptr) {
2451 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002452 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002453 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002454 DCHECK(cond->AsIntConstant()->IsZero());
2455 if (false_target != nullptr) {
2456 __ B(false_target);
2457 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458 }
David Brazdil0debae72015-11-12 18:37:00 +00002459 return;
2460 }
2461
2462 // The following code generates these patterns:
2463 // (1) true_target == nullptr && false_target != nullptr
2464 // - opposite condition true => branch to false_target
2465 // (2) true_target != nullptr && false_target == nullptr
2466 // - condition true => branch to true_target
2467 // (3) true_target != nullptr && false_target != nullptr
2468 // - condition true => branch to true_target
2469 // - branch to false_target
2470 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002471 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002472 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002473 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002474 if (true_target == nullptr) {
2475 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2476 } else {
2477 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2478 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002479 } else {
2480 // The condition instruction has not been materialized, use its inputs as
2481 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002482 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002483
David Brazdil0debae72015-11-12 18:37:00 +00002484 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002485 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002486 FPRegister lhs = InputFPRegisterAt(condition, 0);
2487 if (condition->GetLocations()->InAt(1).IsConstant()) {
2488 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2489 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2490 __ Fcmp(lhs, 0.0);
2491 } else {
2492 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2493 }
2494 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002495 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002496 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002497 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002498 }
David Brazdil0debae72015-11-12 18:37:00 +00002499 if (true_target == nullptr) {
2500 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2501 } else {
2502 __ B(ARM64Condition(condition->GetCondition()), true_target);
2503 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002504 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002505 // Integer cases.
2506 Register lhs = InputRegisterAt(condition, 0);
2507 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002508
2509 Condition arm64_cond;
2510 vixl::Label* non_fallthrough_target;
2511 if (true_target == nullptr) {
2512 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2513 non_fallthrough_target = false_target;
2514 } else {
2515 arm64_cond = ARM64Condition(condition->GetCondition());
2516 non_fallthrough_target = true_target;
2517 }
2518
Roland Levillain7f63c522015-07-13 15:54:55 +00002519 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2520 switch (arm64_cond) {
2521 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002522 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002523 break;
2524 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002525 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002526 break;
2527 case lt:
2528 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002529 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002530 break;
2531 case ge:
2532 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002533 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002534 break;
2535 default:
2536 // Without the `static_cast` the compiler throws an error for
2537 // `-Werror=sign-promo`.
2538 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2539 }
2540 } else {
2541 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002542 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002543 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002544 }
2545 }
David Brazdil0debae72015-11-12 18:37:00 +00002546
2547 // If neither branch falls through (case 3), the conditional branch to `true_target`
2548 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2549 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002550 __ B(false_target);
2551 }
David Brazdil0debae72015-11-12 18:37:00 +00002552
2553 if (fallthrough_target.IsLinked()) {
2554 __ Bind(&fallthrough_target);
2555 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002556}
2557
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002558void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002560 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002561 locations->SetInAt(0, Location::RequiresRegister());
2562 }
2563}
2564
2565void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002566 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2567 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2568 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2569 nullptr : codegen_->GetLabelOf(true_successor);
2570 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2571 nullptr : codegen_->GetLabelOf(false_successor);
2572 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002573}
2574
2575void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2576 LocationSummary* locations = new (GetGraph()->GetArena())
2577 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002578 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002579 locations->SetInAt(0, Location::RequiresRegister());
2580 }
2581}
2582
2583void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2584 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
2585 DeoptimizationSlowPathARM64(deoptimize);
2586 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00002587 GenerateTestAndBranch(deoptimize,
2588 /* condition_input_index */ 0,
2589 slow_path->GetEntryLabel(),
2590 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002591}
2592
Alexandre Rames5319def2014-10-23 10:03:10 +01002593void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002594 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002595}
2596
2597void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002598 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002599}
2600
2601void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002602 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002603}
2604
2605void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002606 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002607}
2608
Alexandre Rames67555f72014-11-18 10:55:16 +00002609void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002610 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2611 switch (instruction->GetTypeCheckKind()) {
2612 case TypeCheckKind::kExactCheck:
2613 case TypeCheckKind::kAbstractClassCheck:
2614 case TypeCheckKind::kClassHierarchyCheck:
2615 case TypeCheckKind::kArrayObjectCheck:
2616 call_kind = LocationSummary::kNoCall;
2617 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002618 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002619 case TypeCheckKind::kInterfaceCheck:
2620 call_kind = LocationSummary::kCall;
2621 break;
2622 case TypeCheckKind::kArrayCheck:
2623 call_kind = LocationSummary::kCallOnSlowPath;
2624 break;
2625 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002627 if (call_kind != LocationSummary::kCall) {
2628 locations->SetInAt(0, Location::RequiresRegister());
2629 locations->SetInAt(1, Location::RequiresRegister());
2630 // The out register is used as a temporary, so it overlaps with the inputs.
2631 // Note that TypeCheckSlowPathARM64 uses this register too.
2632 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2633 } else {
2634 InvokeRuntimeCallingConvention calling_convention;
2635 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2636 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2637 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
2638 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002639}
2640
2641void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
2642 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002643 Register obj = InputRegisterAt(instruction, 0);
2644 Register cls = InputRegisterAt(instruction, 1);
Alexandre Rames67555f72014-11-18 10:55:16 +00002645 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002646 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2647 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2648 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2649 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002650
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002651 vixl::Label done, zero;
2652 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00002653
2654 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002655 // Avoid null check if we know `obj` is not null.
2656 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002657 __ Cbz(obj, &zero);
2658 }
2659
Calin Juravle98893e12015-10-02 21:05:03 +01002660 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002661 // This is safe, as the register is caller-save, and the object must be in another
2662 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01002663 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
2664 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002665 ? obj
2666 : out;
2667 __ Ldr(target, HeapOperand(obj.W(), class_offset));
2668 GetAssembler()->MaybeUnpoisonHeapReference(target);
2669
2670 switch (instruction->GetTypeCheckKind()) {
2671 case TypeCheckKind::kExactCheck: {
2672 __ Cmp(out, cls);
2673 __ Cset(out, eq);
2674 if (zero.IsLinked()) {
2675 __ B(&done);
2676 }
2677 break;
2678 }
2679 case TypeCheckKind::kAbstractClassCheck: {
2680 // If the class is abstract, we eagerly fetch the super class of the
2681 // object to avoid doing a comparison we know will fail.
2682 vixl::Label loop, success;
2683 __ Bind(&loop);
2684 __ Ldr(out, HeapOperand(out, super_offset));
2685 GetAssembler()->MaybeUnpoisonHeapReference(out);
2686 // If `out` is null, we use it for the result, and jump to `done`.
2687 __ Cbz(out, &done);
2688 __ Cmp(out, cls);
2689 __ B(ne, &loop);
2690 __ Mov(out, 1);
2691 if (zero.IsLinked()) {
2692 __ B(&done);
2693 }
2694 break;
2695 }
2696 case TypeCheckKind::kClassHierarchyCheck: {
2697 // Walk over the class hierarchy to find a match.
2698 vixl::Label loop, success;
2699 __ Bind(&loop);
2700 __ Cmp(out, cls);
2701 __ B(eq, &success);
2702 __ Ldr(out, HeapOperand(out, super_offset));
2703 GetAssembler()->MaybeUnpoisonHeapReference(out);
2704 __ Cbnz(out, &loop);
2705 // If `out` is null, we use it for the result, and jump to `done`.
2706 __ B(&done);
2707 __ Bind(&success);
2708 __ Mov(out, 1);
2709 if (zero.IsLinked()) {
2710 __ B(&done);
2711 }
2712 break;
2713 }
2714 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002715 // Do an exact check.
2716 vixl::Label exact_check;
2717 __ Cmp(out, cls);
2718 __ B(eq, &exact_check);
2719 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002720 __ Ldr(out, HeapOperand(out, component_offset));
2721 GetAssembler()->MaybeUnpoisonHeapReference(out);
2722 // If `out` is null, we use it for the result, and jump to `done`.
2723 __ Cbz(out, &done);
2724 __ Ldrh(out, HeapOperand(out, primitive_offset));
2725 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2726 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002727 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002728 __ Mov(out, 1);
2729 __ B(&done);
2730 break;
2731 }
2732 case TypeCheckKind::kArrayCheck: {
2733 __ Cmp(out, cls);
2734 DCHECK(locations->OnlyCallsOnSlowPath());
2735 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2736 instruction, /* is_fatal */ false);
2737 codegen_->AddSlowPath(slow_path);
2738 __ B(ne, slow_path->GetEntryLabel());
2739 __ Mov(out, 1);
2740 if (zero.IsLinked()) {
2741 __ B(&done);
2742 }
2743 break;
2744 }
Calin Juravle98893e12015-10-02 21:05:03 +01002745 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002746 case TypeCheckKind::kInterfaceCheck:
2747 default: {
2748 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
2749 instruction,
2750 instruction->GetDexPc(),
2751 nullptr);
2752 if (zero.IsLinked()) {
2753 __ B(&done);
2754 }
2755 break;
2756 }
2757 }
2758
2759 if (zero.IsLinked()) {
2760 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002761 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002762 }
2763
2764 if (done.IsLinked()) {
2765 __ Bind(&done);
2766 }
2767
2768 if (slow_path != nullptr) {
2769 __ Bind(slow_path->GetExitLabel());
2770 }
2771}
2772
2773void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
2774 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2775 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2776
2777 switch (instruction->GetTypeCheckKind()) {
2778 case TypeCheckKind::kExactCheck:
2779 case TypeCheckKind::kAbstractClassCheck:
2780 case TypeCheckKind::kClassHierarchyCheck:
2781 case TypeCheckKind::kArrayObjectCheck:
2782 call_kind = throws_into_catch
2783 ? LocationSummary::kCallOnSlowPath
2784 : LocationSummary::kNoCall;
2785 break;
Calin Juravle98893e12015-10-02 21:05:03 +01002786 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002787 case TypeCheckKind::kInterfaceCheck:
2788 call_kind = LocationSummary::kCall;
2789 break;
2790 case TypeCheckKind::kArrayCheck:
2791 call_kind = LocationSummary::kCallOnSlowPath;
2792 break;
2793 }
2794
2795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2796 instruction, call_kind);
2797 if (call_kind != LocationSummary::kCall) {
2798 locations->SetInAt(0, Location::RequiresRegister());
2799 locations->SetInAt(1, Location::RequiresRegister());
2800 // Note that TypeCheckSlowPathARM64 uses this register too.
2801 locations->AddTemp(Location::RequiresRegister());
2802 } else {
2803 InvokeRuntimeCallingConvention calling_convention;
2804 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(0)));
2805 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
2806 }
2807}
2808
2809void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
2810 LocationSummary* locations = instruction->GetLocations();
2811 Register obj = InputRegisterAt(instruction, 0);
2812 Register cls = InputRegisterAt(instruction, 1);
2813 Register temp;
2814 if (!locations->WillCall()) {
2815 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
2816 }
2817
2818 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2819 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2820 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2821 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2822 SlowPathCodeARM64* slow_path = nullptr;
2823
2824 if (!locations->WillCall()) {
2825 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(
2826 instruction, !locations->CanCall());
2827 codegen_->AddSlowPath(slow_path);
2828 }
2829
2830 vixl::Label done;
2831 // Avoid null check if we know obj is not null.
2832 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01002833 __ Cbz(obj, &done);
2834 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002835
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002836 if (locations->WillCall()) {
2837 __ Ldr(obj, HeapOperand(obj, class_offset));
2838 GetAssembler()->MaybeUnpoisonHeapReference(obj);
Alexandre Rames67555f72014-11-18 10:55:16 +00002839 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002840 __ Ldr(temp, HeapOperand(obj, class_offset));
2841 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01002842 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002844 switch (instruction->GetTypeCheckKind()) {
2845 case TypeCheckKind::kExactCheck:
2846 case TypeCheckKind::kArrayCheck: {
2847 __ Cmp(temp, cls);
2848 // Jump to slow path for throwing the exception or doing a
2849 // more involved array check.
2850 __ B(ne, slow_path->GetEntryLabel());
2851 break;
2852 }
2853 case TypeCheckKind::kAbstractClassCheck: {
2854 // If the class is abstract, we eagerly fetch the super class of the
2855 // object to avoid doing a comparison we know will fail.
2856 vixl::Label loop;
2857 __ Bind(&loop);
2858 __ Ldr(temp, HeapOperand(temp, super_offset));
2859 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2860 // Jump to the slow path to throw the exception.
2861 __ Cbz(temp, slow_path->GetEntryLabel());
2862 __ Cmp(temp, cls);
2863 __ B(ne, &loop);
2864 break;
2865 }
2866 case TypeCheckKind::kClassHierarchyCheck: {
2867 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002868 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002869 __ Bind(&loop);
2870 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002871 __ B(eq, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002872 __ Ldr(temp, HeapOperand(temp, super_offset));
2873 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2874 __ Cbnz(temp, &loop);
2875 // Jump to the slow path to throw the exception.
2876 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002877 break;
2878 }
2879 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002880 // Do an exact check.
2881 __ Cmp(temp, cls);
2882 __ B(eq, &done);
2883 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002884 __ Ldr(temp, HeapOperand(temp, component_offset));
2885 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2886 __ Cbz(temp, slow_path->GetEntryLabel());
2887 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
2888 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2889 __ Cbnz(temp, slow_path->GetEntryLabel());
2890 break;
2891 }
Calin Juravle98893e12015-10-02 21:05:03 +01002892 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002893 case TypeCheckKind::kInterfaceCheck:
2894 default:
2895 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
2896 instruction,
2897 instruction->GetDexPc(),
2898 nullptr);
2899 break;
2900 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00002901 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002902
2903 if (slow_path != nullptr) {
2904 __ Bind(slow_path->GetExitLabel());
2905 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002906}
2907
Alexandre Rames5319def2014-10-23 10:03:10 +01002908void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
2909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2910 locations->SetOut(Location::ConstantLocation(constant));
2911}
2912
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002913void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002914 // Will be generated at use site.
2915}
2916
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002917void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
2918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2919 locations->SetOut(Location::ConstantLocation(constant));
2920}
2921
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002922void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002923 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002924}
2925
Calin Juravle175dc732015-08-25 15:42:32 +01002926void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2927 // The trampoline uses the same calling convention as dex calling conventions,
2928 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2929 // the method_idx.
2930 HandleInvoke(invoke);
2931}
2932
2933void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2934 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2935}
2936
Alexandre Rames5319def2014-10-23 10:03:10 +01002937void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002938 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002939 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01002940}
2941
Alexandre Rames67555f72014-11-18 10:55:16 +00002942void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2943 HandleInvoke(invoke);
2944}
2945
2946void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
2947 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002948 Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0));
2949 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2950 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00002951 Location receiver = invoke->GetLocations()->InAt(0);
2952 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002953 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00002954
2955 // The register ip1 is required to be used for the hidden argument in
2956 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002957 MacroAssembler* masm = GetVIXLAssembler();
2958 UseScratchRegisterScope scratch_scope(masm);
2959 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00002960 scratch_scope.Exclude(ip1);
2961 __ Mov(ip1, invoke->GetDexMethodIndex());
2962
2963 // temp = object->GetClass();
2964 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002965 __ Ldr(temp.W(), StackOperandFrom(receiver));
2966 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002967 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002968 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002969 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002970 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01002971 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00002972 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002973 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00002974 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002975 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00002976 // lr();
2977 __ Blr(lr);
2978 DCHECK(!codegen_->IsLeafMethod());
2979 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2980}
2981
2982void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08002983 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2984 if (intrinsic.TryDispatch(invoke)) {
2985 return;
2986 }
2987
Alexandre Rames67555f72014-11-18 10:55:16 +00002988 HandleInvoke(invoke);
2989}
2990
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002991void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002992 // When we do not run baseline, explicit clinit checks triggered by static
2993 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2994 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002995
Andreas Gampe878d58c2015-01-15 23:24:00 -08002996 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
2997 if (intrinsic.TryDispatch(invoke)) {
2998 return;
2999 }
3000
Alexandre Rames67555f72014-11-18 10:55:16 +00003001 HandleInvoke(invoke);
3002}
3003
Andreas Gampe878d58c2015-01-15 23:24:00 -08003004static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3005 if (invoke->GetLocations()->Intrinsified()) {
3006 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3007 intrinsic.Dispatch(invoke);
3008 return true;
3009 }
3010 return false;
3011}
3012
Vladimir Markodc151b22015-10-15 18:02:30 +01003013HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3014 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3015 MethodReference target_method ATTRIBUTE_UNUSED) {
3016 // On arm64 we support all dispatch types.
3017 return desired_dispatch_info;
3018}
3019
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003020void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003021 // For better instruction scheduling we load the direct code pointer before the method pointer.
3022 bool direct_code_loaded = false;
3023 switch (invoke->GetCodePtrLocation()) {
3024 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3025 // LR = code address from literal pool with link-time patch.
3026 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3027 direct_code_loaded = true;
3028 break;
3029 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3030 // LR = invoke->GetDirectCodePtr();
3031 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3032 direct_code_loaded = true;
3033 break;
3034 default:
3035 break;
3036 }
3037
Andreas Gampe878d58c2015-01-15 23:24:00 -08003038 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003039 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3040 switch (invoke->GetMethodLoadKind()) {
3041 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3042 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003043 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003044 break;
3045 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003046 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003047 break;
3048 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3049 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003050 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003051 break;
3052 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3053 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003054 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003055 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3056 break;
3057 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3058 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003059 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3060 invoke->GetDexCacheArrayOffset());
3061 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003062 {
3063 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003064 __ Bind(pc_insn_label);
3065 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003066 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003067 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003068 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003069 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3070 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003071 {
3072 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3073 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3074 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3075 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3076 }
Vladimir Marko58155012015-08-19 12:49:41 +00003077 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003078 }
Vladimir Marko58155012015-08-19 12:49:41 +00003079 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003080 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003081 Register reg = XRegisterFrom(temp);
3082 Register method_reg;
3083 if (current_method.IsRegister()) {
3084 method_reg = XRegisterFrom(current_method);
3085 } else {
3086 DCHECK(invoke->GetLocations()->Intrinsified());
3087 DCHECK(!current_method.IsValid());
3088 method_reg = reg;
3089 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3090 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003091
Vladimir Marko58155012015-08-19 12:49:41 +00003092 // temp = current_method->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003093 __ Ldr(reg.X(),
3094 MemOperand(method_reg.X(),
3095 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003096 // temp = temp[index_in_cache];
3097 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3098 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3099 break;
3100 }
3101 }
3102
3103 switch (invoke->GetCodePtrLocation()) {
3104 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3105 __ Bl(&frame_entry_label_);
3106 break;
3107 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3108 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3109 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003110 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3111 __ Bind(label);
3112 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003113 break;
3114 }
3115 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3116 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3117 // LR prepared above for better instruction scheduling.
3118 DCHECK(direct_code_loaded);
3119 // lr()
3120 __ Blr(lr);
3121 break;
3122 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3123 // LR = callee_method->entry_point_from_quick_compiled_code_;
3124 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003125 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003126 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3127 // lr()
3128 __ Blr(lr);
3129 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003130 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003131
Andreas Gampe878d58c2015-01-15 23:24:00 -08003132 DCHECK(!IsLeafMethod());
3133}
3134
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003135void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3136 LocationSummary* locations = invoke->GetLocations();
3137 Location receiver = locations->InAt(0);
3138 Register temp = XRegisterFrom(temp_in);
3139 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3140 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3141 Offset class_offset = mirror::Object::ClassOffset();
3142 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3143
3144 BlockPoolsScope block_pools(GetVIXLAssembler());
3145
3146 DCHECK(receiver.IsRegister());
3147 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
3148 MaybeRecordImplicitNullCheck(invoke);
3149 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3150 // temp = temp->GetMethodAt(method_offset);
3151 __ Ldr(temp, MemOperand(temp, method_offset));
3152 // lr = temp->GetEntryPoint();
3153 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3154 // lr();
3155 __ Blr(lr);
3156}
3157
Vladimir Marko58155012015-08-19 12:49:41 +00003158void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3159 DCHECK(linker_patches->empty());
3160 size_t size =
3161 method_patches_.size() +
3162 call_patches_.size() +
3163 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003164 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003165 linker_patches->reserve(size);
3166 for (const auto& entry : method_patches_) {
3167 const MethodReference& target_method = entry.first;
3168 vixl::Literal<uint64_t>* literal = entry.second;
3169 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3170 target_method.dex_file,
3171 target_method.dex_method_index));
3172 }
3173 for (const auto& entry : call_patches_) {
3174 const MethodReference& target_method = entry.first;
3175 vixl::Literal<uint64_t>* literal = entry.second;
3176 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3177 target_method.dex_file,
3178 target_method.dex_method_index));
3179 }
3180 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003181 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003182 info.target_method.dex_file,
3183 info.target_method.dex_method_index));
3184 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003185 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003186 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003187 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003188 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003189 info.element_offset));
3190 }
3191}
3192
3193vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3194 // Look up the literal for value.
3195 auto lb = uint64_literals_.lower_bound(value);
3196 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3197 return lb->second;
3198 }
3199 // We don't have a literal for this value, insert a new one.
3200 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3201 uint64_literals_.PutBefore(lb, value, literal);
3202 return literal;
3203}
3204
3205vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3206 MethodReference target_method,
3207 MethodToLiteralMap* map) {
3208 // Look up the literal for target_method.
3209 auto lb = map->lower_bound(target_method);
3210 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3211 return lb->second;
3212 }
3213 // We don't have a literal for this method yet, insert a new one.
3214 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3215 map->PutBefore(lb, target_method, literal);
3216 return literal;
3217}
3218
3219vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3220 MethodReference target_method) {
3221 return DeduplicateMethodLiteral(target_method, &method_patches_);
3222}
3223
3224vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3225 MethodReference target_method) {
3226 return DeduplicateMethodLiteral(target_method, &call_patches_);
3227}
3228
3229
Andreas Gampe878d58c2015-01-15 23:24:00 -08003230void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003231 // When we do not run baseline, explicit clinit checks triggered by static
3232 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3233 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003234
Andreas Gampe878d58c2015-01-15 23:24:00 -08003235 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3236 return;
3237 }
3238
Alexandre Ramesd921d642015-04-16 15:07:16 +01003239 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003240 LocationSummary* locations = invoke->GetLocations();
3241 codegen_->GenerateStaticOrDirectCall(
3242 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003243 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003244}
3245
3246void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003247 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3248 return;
3249 }
3250
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003251 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003252 DCHECK(!codegen_->IsLeafMethod());
3253 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3254}
3255
Alexandre Rames67555f72014-11-18 10:55:16 +00003256void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003257 InvokeRuntimeCallingConvention calling_convention;
3258 CodeGenerator::CreateLoadClassLocationSummary(
3259 cls,
3260 LocationFrom(calling_convention.GetRegisterAt(0)),
3261 LocationFrom(vixl::x0));
Alexandre Rames67555f72014-11-18 10:55:16 +00003262}
3263
3264void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003265 if (cls->NeedsAccessCheck()) {
3266 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3267 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3268 cls,
3269 cls->GetDexPc(),
3270 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01003271 return;
3272 }
3273
3274 Register out = OutputRegister(cls);
3275 Register current_method = InputRegisterAt(cls, 0);
3276 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003277 DCHECK(!cls->CanCallRuntime());
3278 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07003279 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003280 } else {
3281 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01003282 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
3283 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
3284 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
3285 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003286
3287 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3288 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3289 codegen_->AddSlowPath(slow_path);
3290 __ Cbz(out, slow_path->GetEntryLabel());
3291 if (cls->MustGenerateClinitCheck()) {
3292 GenerateClassInitializationCheck(slow_path, out);
3293 } else {
3294 __ Bind(slow_path->GetExitLabel());
3295 }
3296 }
3297}
3298
David Brazdilcb1c0552015-08-04 16:22:25 +01003299static MemOperand GetExceptionTlsAddress() {
3300 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3301}
3302
Alexandre Rames67555f72014-11-18 10:55:16 +00003303void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3304 LocationSummary* locations =
3305 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3306 locations->SetOut(Location::RequiresRegister());
3307}
3308
3309void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003310 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3311}
3312
3313void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3314 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3315}
3316
3317void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3318 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003319}
3320
Alexandre Rames5319def2014-10-23 10:03:10 +01003321void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3322 load->SetLocations(nullptr);
3323}
3324
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003325void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003326 // Nothing to do, this is driven by the code generator.
3327}
3328
Alexandre Rames67555f72014-11-18 10:55:16 +00003329void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
3330 LocationSummary* locations =
3331 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003332 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003333 locations->SetOut(Location::RequiresRegister());
3334}
3335
3336void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
3337 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3338 codegen_->AddSlowPath(slow_path);
3339
3340 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003341 Register current_method = InputRegisterAt(load, 0);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003342 __ Ldr(out, MemOperand(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003343 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset()));
3344 __ Ldr(out, MemOperand(out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3345 // TODO: We will need a read barrier here.
Alexandre Rames67555f72014-11-18 10:55:16 +00003346 __ Cbz(out, slow_path->GetEntryLabel());
3347 __ Bind(slow_path->GetExitLabel());
3348}
3349
Alexandre Rames5319def2014-10-23 10:03:10 +01003350void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3351 local->SetLocations(nullptr);
3352}
3353
3354void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3355 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3356}
3357
3358void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3360 locations->SetOut(Location::ConstantLocation(constant));
3361}
3362
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003363void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003364 // Will be generated at use site.
3365}
3366
Alexandre Rames67555f72014-11-18 10:55:16 +00003367void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3368 LocationSummary* locations =
3369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3370 InvokeRuntimeCallingConvention calling_convention;
3371 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3372}
3373
3374void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3375 codegen_->InvokeRuntime(instruction->IsEnter()
3376 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3377 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003378 instruction->GetDexPc(),
3379 nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003380 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003381}
3382
Alexandre Rames42d641b2014-10-27 14:00:51 +00003383void LocationsBuilderARM64::VisitMul(HMul* mul) {
3384 LocationSummary* locations =
3385 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3386 switch (mul->GetResultType()) {
3387 case Primitive::kPrimInt:
3388 case Primitive::kPrimLong:
3389 locations->SetInAt(0, Location::RequiresRegister());
3390 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003392 break;
3393
3394 case Primitive::kPrimFloat:
3395 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003396 locations->SetInAt(0, Location::RequiresFpuRegister());
3397 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003398 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003399 break;
3400
3401 default:
3402 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3403 }
3404}
3405
3406void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3407 switch (mul->GetResultType()) {
3408 case Primitive::kPrimInt:
3409 case Primitive::kPrimLong:
3410 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3411 break;
3412
3413 case Primitive::kPrimFloat:
3414 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003415 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003416 break;
3417
3418 default:
3419 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3420 }
3421}
3422
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003423void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3424 LocationSummary* locations =
3425 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3426 switch (neg->GetResultType()) {
3427 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003428 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003429 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003431 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003432
3433 case Primitive::kPrimFloat:
3434 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003435 locations->SetInAt(0, Location::RequiresFpuRegister());
3436 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003437 break;
3438
3439 default:
3440 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3441 }
3442}
3443
3444void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
3445 switch (neg->GetResultType()) {
3446 case Primitive::kPrimInt:
3447 case Primitive::kPrimLong:
3448 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
3449 break;
3450
3451 case Primitive::kPrimFloat:
3452 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003453 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003454 break;
3455
3456 default:
3457 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3458 }
3459}
3460
3461void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
3462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3464 InvokeRuntimeCallingConvention calling_convention;
3465 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003466 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003467 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003468 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003469 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
Mathieu Chartiere401d142015-04-22 13:56:20 -07003470 void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003471}
3472
3473void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
3474 LocationSummary* locations = instruction->GetLocations();
3475 InvokeRuntimeCallingConvention calling_convention;
3476 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
3477 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003478 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003479 // Note: if heap poisoning is enabled, the entry point takes cares
3480 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003481 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3482 instruction,
3483 instruction->GetDexPc(),
3484 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003485 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003486}
3487
Alexandre Rames5319def2014-10-23 10:03:10 +01003488void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
3489 LocationSummary* locations =
3490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3491 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003492 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3493 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Rames5319def2014-10-23 10:03:10 +01003494 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003495 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003496}
3497
3498void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003499 // Note: if heap poisoning is enabled, the entry point takes cares
3500 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003501 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3502 instruction,
3503 instruction->GetDexPc(),
3504 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003505 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Alexandre Rames5319def2014-10-23 10:03:10 +01003506}
3507
3508void LocationsBuilderARM64::VisitNot(HNot* instruction) {
3509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00003510 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003512}
3513
3514void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003515 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003516 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003517 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01003518 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003519 break;
3520
3521 default:
3522 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3523 }
3524}
3525
David Brazdil66d126e2015-04-03 16:02:44 +01003526void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
3527 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3528 locations->SetInAt(0, Location::RequiresRegister());
3529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3530}
3531
3532void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01003533 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
3534}
3535
Alexandre Rames5319def2014-10-23 10:03:10 +01003536void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003537 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3538 ? LocationSummary::kCallOnSlowPath
3539 : LocationSummary::kNoCall;
3540 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01003541 locations->SetInAt(0, Location::RequiresRegister());
3542 if (instruction->HasUses()) {
3543 locations->SetOut(Location::SameAsFirstInput());
3544 }
3545}
3546
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003547void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003548 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3549 return;
3550 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003551
Alexandre Ramesd921d642015-04-16 15:07:16 +01003552 BlockPoolsScope block_pools(GetVIXLAssembler());
3553 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003554 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
3555 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3556}
3557
3558void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003559 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
3560 codegen_->AddSlowPath(slow_path);
3561
3562 LocationSummary* locations = instruction->GetLocations();
3563 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003564
3565 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01003566}
3567
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003568void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003569 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003570 GenerateImplicitNullCheck(instruction);
3571 } else {
3572 GenerateExplicitNullCheck(instruction);
3573 }
3574}
3575
Alexandre Rames67555f72014-11-18 10:55:16 +00003576void LocationsBuilderARM64::VisitOr(HOr* instruction) {
3577 HandleBinaryOp(instruction);
3578}
3579
3580void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
3581 HandleBinaryOp(instruction);
3582}
3583
Alexandre Rames3e69f162014-12-10 10:36:50 +00003584void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3585 LOG(FATAL) << "Unreachable";
3586}
3587
3588void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
3589 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3590}
3591
Alexandre Rames5319def2014-10-23 10:03:10 +01003592void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
3593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3594 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3595 if (location.IsStackSlot()) {
3596 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3597 } else if (location.IsDoubleStackSlot()) {
3598 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3599 }
3600 locations->SetOut(location);
3601}
3602
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003603void InstructionCodeGeneratorARM64::VisitParameterValue(
3604 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003605 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003606}
3607
3608void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
3609 LocationSummary* locations =
3610 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003611 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003612}
3613
3614void InstructionCodeGeneratorARM64::VisitCurrentMethod(
3615 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3616 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01003617}
3618
3619void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
3620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3621 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3622 locations->SetInAt(i, Location::Any());
3623 }
3624 locations->SetOut(Location::Any());
3625}
3626
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003627void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003628 LOG(FATAL) << "Unreachable";
3629}
3630
Serban Constantinescu02164b32014-11-13 14:05:07 +00003631void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003632 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00003633 LocationSummary::CallKind call_kind =
3634 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003635 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3636
3637 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003638 case Primitive::kPrimInt:
3639 case Primitive::kPrimLong:
3640 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003641 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003642 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3643 break;
3644
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003645 case Primitive::kPrimFloat:
3646 case Primitive::kPrimDouble: {
3647 InvokeRuntimeCallingConvention calling_convention;
3648 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
3649 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
3650 locations->SetOut(calling_convention.GetReturnLocation(type));
3651
3652 break;
3653 }
3654
Serban Constantinescu02164b32014-11-13 14:05:07 +00003655 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003656 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00003657 }
3658}
3659
3660void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
3661 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003662
Serban Constantinescu02164b32014-11-13 14:05:07 +00003663 switch (type) {
3664 case Primitive::kPrimInt:
3665 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08003666 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003667 break;
3668 }
3669
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003670 case Primitive::kPrimFloat:
3671 case Primitive::kPrimDouble: {
3672 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3673 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003674 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00003675 break;
3676 }
3677
Serban Constantinescu02164b32014-11-13 14:05:07 +00003678 default:
3679 LOG(FATAL) << "Unexpected rem type " << type;
3680 }
3681}
3682
Calin Juravle27df7582015-04-17 19:12:31 +01003683void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3684 memory_barrier->SetLocations(nullptr);
3685}
3686
3687void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3688 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3689}
3690
Alexandre Rames5319def2014-10-23 10:03:10 +01003691void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
3692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3693 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003694 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01003695}
3696
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003697void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003698 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003699}
3700
3701void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
3702 instruction->SetLocations(nullptr);
3703}
3704
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003705void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003706 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01003707}
3708
Serban Constantinescu02164b32014-11-13 14:05:07 +00003709void LocationsBuilderARM64::VisitShl(HShl* shl) {
3710 HandleShift(shl);
3711}
3712
3713void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
3714 HandleShift(shl);
3715}
3716
3717void LocationsBuilderARM64::VisitShr(HShr* shr) {
3718 HandleShift(shr);
3719}
3720
3721void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
3722 HandleShift(shr);
3723}
3724
Alexandre Rames5319def2014-10-23 10:03:10 +01003725void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
3726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3727 Primitive::Type field_type = store->InputAt(1)->GetType();
3728 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003729 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01003730 case Primitive::kPrimBoolean:
3731 case Primitive::kPrimByte:
3732 case Primitive::kPrimChar:
3733 case Primitive::kPrimShort:
3734 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003735 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01003736 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3737 break;
3738
3739 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003740 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01003741 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3742 break;
3743
3744 default:
3745 LOG(FATAL) << "Unimplemented local type " << field_type;
3746 }
3747}
3748
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003749void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003750}
3751
3752void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003753 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003754}
3755
3756void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003757 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003758}
3759
Alexandre Rames67555f72014-11-18 10:55:16 +00003760void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003761 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003762}
3763
3764void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003765 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00003766}
3767
3768void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003769 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003770}
3771
Alexandre Rames67555f72014-11-18 10:55:16 +00003772void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003773 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003774}
3775
Calin Juravlee460d1d2015-09-29 04:52:17 +01003776void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
3777 HUnresolvedInstanceFieldGet* instruction) {
3778 FieldAccessCallingConventionARM64 calling_convention;
3779 codegen_->CreateUnresolvedFieldLocationSummary(
3780 instruction, instruction->GetFieldType(), calling_convention);
3781}
3782
3783void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
3784 HUnresolvedInstanceFieldGet* instruction) {
3785 FieldAccessCallingConventionARM64 calling_convention;
3786 codegen_->GenerateUnresolvedFieldAccess(instruction,
3787 instruction->GetFieldType(),
3788 instruction->GetFieldIndex(),
3789 instruction->GetDexPc(),
3790 calling_convention);
3791}
3792
3793void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
3794 HUnresolvedInstanceFieldSet* instruction) {
3795 FieldAccessCallingConventionARM64 calling_convention;
3796 codegen_->CreateUnresolvedFieldLocationSummary(
3797 instruction, instruction->GetFieldType(), calling_convention);
3798}
3799
3800void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
3801 HUnresolvedInstanceFieldSet* instruction) {
3802 FieldAccessCallingConventionARM64 calling_convention;
3803 codegen_->GenerateUnresolvedFieldAccess(instruction,
3804 instruction->GetFieldType(),
3805 instruction->GetFieldIndex(),
3806 instruction->GetDexPc(),
3807 calling_convention);
3808}
3809
3810void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
3811 HUnresolvedStaticFieldGet* instruction) {
3812 FieldAccessCallingConventionARM64 calling_convention;
3813 codegen_->CreateUnresolvedFieldLocationSummary(
3814 instruction, instruction->GetFieldType(), calling_convention);
3815}
3816
3817void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
3818 HUnresolvedStaticFieldGet* instruction) {
3819 FieldAccessCallingConventionARM64 calling_convention;
3820 codegen_->GenerateUnresolvedFieldAccess(instruction,
3821 instruction->GetFieldType(),
3822 instruction->GetFieldIndex(),
3823 instruction->GetDexPc(),
3824 calling_convention);
3825}
3826
3827void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
3828 HUnresolvedStaticFieldSet* instruction) {
3829 FieldAccessCallingConventionARM64 calling_convention;
3830 codegen_->CreateUnresolvedFieldLocationSummary(
3831 instruction, instruction->GetFieldType(), calling_convention);
3832}
3833
3834void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
3835 HUnresolvedStaticFieldSet* instruction) {
3836 FieldAccessCallingConventionARM64 calling_convention;
3837 codegen_->GenerateUnresolvedFieldAccess(instruction,
3838 instruction->GetFieldType(),
3839 instruction->GetFieldIndex(),
3840 instruction->GetDexPc(),
3841 calling_convention);
3842}
3843
Alexandre Rames5319def2014-10-23 10:03:10 +01003844void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
3845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3846}
3847
3848void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003849 HBasicBlock* block = instruction->GetBlock();
3850 if (block->GetLoopInformation() != nullptr) {
3851 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3852 // The back edge will generate the suspend check.
3853 return;
3854 }
3855 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3856 // The goto will generate the suspend check.
3857 return;
3858 }
3859 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01003860}
3861
3862void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
3863 temp->SetLocations(nullptr);
3864}
3865
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003866void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003867 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01003868}
3869
Alexandre Rames67555f72014-11-18 10:55:16 +00003870void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
3871 LocationSummary* locations =
3872 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3873 InvokeRuntimeCallingConvention calling_convention;
3874 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3875}
3876
3877void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
3878 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003879 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003880 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00003881}
3882
3883void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
3884 LocationSummary* locations =
3885 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
3886 Primitive::Type input_type = conversion->GetInputType();
3887 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003888 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00003889 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3890 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3891 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3892 }
3893
Alexandre Rames542361f2015-01-29 16:57:31 +00003894 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003895 locations->SetInAt(0, Location::RequiresFpuRegister());
3896 } else {
3897 locations->SetInAt(0, Location::RequiresRegister());
3898 }
3899
Alexandre Rames542361f2015-01-29 16:57:31 +00003900 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003901 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3902 } else {
3903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3904 }
3905}
3906
3907void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
3908 Primitive::Type result_type = conversion->GetResultType();
3909 Primitive::Type input_type = conversion->GetInputType();
3910
3911 DCHECK_NE(input_type, result_type);
3912
Alexandre Rames542361f2015-01-29 16:57:31 +00003913 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003914 int result_size = Primitive::ComponentSize(result_type);
3915 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00003916 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003917 Register output = OutputRegister(conversion);
3918 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00003919 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01003920 // 'int' values are used directly as W registers, discarding the top
3921 // bits, so we don't need to sign-extend and can just perform a move.
3922 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
3923 // top 32 bits of the target register. We theoretically could leave those
3924 // bits unchanged, but we would have to make sure that no code uses a
3925 // 32bit input value as a 64bit value assuming that the top 32 bits are
3926 // zero.
3927 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00003928 } else if (result_type == Primitive::kPrimChar ||
3929 (input_type == Primitive::kPrimChar && input_size < result_size)) {
3930 __ Ubfx(output,
3931 output.IsX() ? source.X() : source.W(),
3932 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003933 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003934 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00003935 }
Alexandre Rames542361f2015-01-29 16:57:31 +00003936 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003937 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003938 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003939 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3940 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00003941 } else if (Primitive::IsFloatingPointType(result_type) &&
3942 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003943 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
3944 } else {
3945 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3946 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00003947 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003948}
Alexandre Rames67555f72014-11-18 10:55:16 +00003949
Serban Constantinescu02164b32014-11-13 14:05:07 +00003950void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
3951 HandleShift(ushr);
3952}
3953
3954void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
3955 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00003956}
3957
3958void LocationsBuilderARM64::VisitXor(HXor* instruction) {
3959 HandleBinaryOp(instruction);
3960}
3961
3962void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
3963 HandleBinaryOp(instruction);
3964}
3965
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003966void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00003967 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00003968 LOG(FATAL) << "Unreachable";
3969}
3970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003971void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00003972 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00003973 LOG(FATAL) << "Unreachable";
3974}
3975
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003976void LocationsBuilderARM64::VisitFakeString(HFakeString* instruction) {
3977 DCHECK(codegen_->IsBaseline());
3978 LocationSummary* locations =
3979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3980 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3981}
3982
3983void InstructionCodeGeneratorARM64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3984 DCHECK(codegen_->IsBaseline());
3985 // Will be generated at use site.
3986}
3987
Mark Mendellfe57faa2015-09-18 09:26:15 -04003988// Simple implementation of packed switch - generate cascaded compare/jumps.
3989void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3990 LocationSummary* locations =
3991 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3992 locations->SetInAt(0, Location::RequiresRegister());
3993}
3994
3995void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3996 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08003997 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04003998 Register value_reg = InputRegisterAt(switch_instr, 0);
3999 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4000
Zheng Xu3927c8b2015-11-18 17:46:25 +08004001 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4002 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4003 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4004 // make sure we don't emit it if the target may run out of range.
4005 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4006 // ranges and emit the tables only as required.
4007 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004008
Zheng Xu3927c8b2015-11-18 17:46:25 +08004009 if (num_entries < kPackedSwitchJumpTableThreshold ||
4010 // Current instruction id is an upper bound of the number of HIRs in the graph.
4011 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4012 // Create a series of compare/jumps.
4013 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4014 for (uint32_t i = 0; i < num_entries; i++) {
4015 int32_t case_value = lower_bound + i;
4016 vixl::Label* succ = codegen_->GetLabelOf(successors[i]);
4017 if (case_value == 0) {
4018 __ Cbz(value_reg, succ);
4019 } else {
4020 __ Cmp(value_reg, Operand(case_value));
4021 __ B(eq, succ);
4022 }
4023 }
4024
4025 // And the default for any other value.
4026 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4027 __ B(codegen_->GetLabelOf(default_block));
4028 }
4029 } else {
4030 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4031 codegen_->AddJumpTable(jump_table);
4032
4033 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4034
4035 // Below instructions should use at most one blocked register. Since there are two blocked
4036 // registers, we are free to block one.
4037 Register temp_w = temps.AcquireW();
4038 Register index;
4039 // Remove the bias.
4040 if (lower_bound != 0) {
4041 index = temp_w;
4042 __ Sub(index, value_reg, Operand(lower_bound));
4043 } else {
4044 index = value_reg;
4045 }
4046
4047 // Jump to default block if index is out of the range.
4048 __ Cmp(index, Operand(num_entries));
4049 __ B(hs, codegen_->GetLabelOf(default_block));
4050
4051 // In current VIXL implementation, it won't require any blocked registers to encode the
4052 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4053 // register pressure.
4054 Register table_base = temps.AcquireX();
4055 // Load jump offset from the table.
4056 __ Adr(table_base, jump_table->GetTableStartLabel());
4057 Register jump_offset = temp_w;
4058 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4059
4060 // Jump to target block by branching to table_base(pc related) + offset.
4061 Register target_address = table_base;
4062 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4063 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004064 }
4065}
4066
Alexandre Rames67555f72014-11-18 10:55:16 +00004067#undef __
4068#undef QUICK_ENTRY_POINT
4069
Alexandre Rames5319def2014-10-23 10:03:10 +01004070} // namespace arm64
4071} // namespace art