blob: 5e905fc9aaaff89c79e0bade694bae14a28bc58c [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
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Alexandre Ramesa89086e2014-11-07 17:13:25 +000096Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +000097 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
98 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
99 // but we use the exact registers for clarity.
100 if (return_type == Primitive::kPrimFloat) {
101 return LocationFrom(s0);
102 } else if (return_type == Primitive::kPrimDouble) {
103 return LocationFrom(d0);
104 } else if (return_type == Primitive::kPrimLong) {
105 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100106 } else if (return_type == Primitive::kPrimVoid) {
107 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000108 } else {
109 return LocationFrom(w0);
110 }
111}
112
Alexandre Rames5319def2014-10-23 10:03:10 +0100113Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100115}
116
Alexandre Rames67555f72014-11-18 10:55:16 +0000117#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
118#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100119
Zheng Xuda403092015-04-24 17:35:39 +0800120// Calculate memory accessing operand for save/restore live registers.
121static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
122 RegisterSet* register_set,
123 int64_t spill_offset,
124 bool is_save) {
125 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
126 codegen->GetNumberOfCoreRegisters(),
127 register_set->GetFloatingPointRegisters(),
128 codegen->GetNumberOfFloatingPointRegisters()));
129
130 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
131 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000132 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
133 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800134
135 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
136 UseScratchRegisterScope temps(masm);
137
138 Register base = masm->StackPointer();
139 int64_t core_spill_size = core_list.TotalSizeInBytes();
140 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
141 int64_t reg_size = kXRegSizeInBytes;
142 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
143 uint32_t ls_access_size = WhichPowerOf2(reg_size);
144 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
145 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
146 // If the offset does not fit in the instruction's immediate field, use an alternate register
147 // to compute the base address(float point registers spill base address).
148 Register new_base = temps.AcquireSameSizeAs(base);
149 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
150 base = new_base;
151 spill_offset = -core_spill_size;
152 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
153 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
154 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
155 }
156
157 if (is_save) {
158 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
159 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
160 } else {
161 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
162 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
163 }
164}
165
166void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
167 RegisterSet* register_set = locations->GetLiveRegisters();
168 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
169 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
170 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
171 // If the register holds an object, update the stack mask.
172 if (locations->RegisterContainsObject(i)) {
173 locations->SetStackBit(stack_offset / kVRegSize);
174 }
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_core_stack_offsets_[i] = stack_offset;
178 stack_offset += kXRegSizeInBytes;
179 }
180 }
181
182 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
183 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
184 register_set->ContainsFloatingPointRegister(i)) {
185 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
186 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
187 saved_fpu_stack_offsets_[i] = stack_offset;
188 stack_offset += kDRegSizeInBytes;
189 }
190 }
191
192 SaveRestoreLiveRegistersHelper(codegen, register_set,
193 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
194}
195
196void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
197 RegisterSet* register_set = locations->GetLiveRegisters();
198 SaveRestoreLiveRegistersHelper(codegen, register_set,
199 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
200}
201
Alexandre Rames5319def2014-10-23 10:03:10 +0100202class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
203 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100204 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100205
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100207 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000208 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100209
Alexandre Rames5319def2014-10-23 10:03:10 +0100210 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000211 if (instruction_->CanThrowIntoCatchBlock()) {
212 // Live registers will be restored in the catch block if caught.
213 SaveLiveRegisters(codegen, instruction_->GetLocations());
214 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000215 // We're moving two locations to locations that could overlap, so we need a parallel
216 // move resolver.
217 InvokeRuntimeCallingConvention calling_convention;
218 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100219 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
220 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000221 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000222 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100224 }
225
Alexandre Rames8158f282015-08-07 10:26:17 +0100226 bool IsFatal() const OVERRIDE { return true; }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000231 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232
Alexandre Rames5319def2014-10-23 10:03:10 +0100233 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
234};
235
Alexandre Rames67555f72014-11-18 10:55:16 +0000236class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
237 public:
238 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
239
240 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
241 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
242 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000243 if (instruction_->CanThrowIntoCatchBlock()) {
244 // Live registers will be restored in the catch block if caught.
245 SaveLiveRegisters(codegen, instruction_->GetLocations());
246 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000247 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000248 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800249 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 }
251
Alexandre Rames8158f282015-08-07 10:26:17 +0100252 bool IsFatal() const OVERRIDE { return true; }
253
Alexandre Rames9931f312015-06-19 14:47:01 +0100254 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
255
Alexandre Rames67555f72014-11-18 10:55:16 +0000256 private:
257 HDivZeroCheck* const instruction_;
258 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
259};
260
261class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
262 public:
263 LoadClassSlowPathARM64(HLoadClass* cls,
264 HInstruction* at,
265 uint32_t dex_pc,
266 bool do_clinit)
267 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
268 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
269 }
270
271 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
272 LocationSummary* locations = at_->GetLocations();
273 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
274
275 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000277
278 InvokeRuntimeCallingConvention calling_convention;
279 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
281 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000282 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800283 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100284 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800285 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100286 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800287 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000288
289 // Move the class to the desired location.
290 Location out = locations->Out();
291 if (out.IsValid()) {
292 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
293 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000294 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 }
296
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 __ B(GetExitLabel());
299 }
300
Alexandre Rames9931f312015-06-19 14:47:01 +0100301 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
302
Alexandre Rames67555f72014-11-18 10:55:16 +0000303 private:
304 // The class this slow path will load.
305 HLoadClass* const cls_;
306
307 // The instruction where this slow path is happening.
308 // (Might be the load class or an initialization check).
309 HInstruction* const at_;
310
311 // The dex PC of `at_`.
312 const uint32_t dex_pc_;
313
314 // Whether to initialize the class.
315 const bool do_clinit_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
318};
319
320class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
321 public:
322 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
323
324 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
325 LocationSummary* locations = instruction_->GetLocations();
326 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
327 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
328
329 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000330 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000331
332 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800333 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000335 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100336 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000338 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000340 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 __ B(GetExitLabel());
342 }
343
Alexandre Rames9931f312015-06-19 14:47:01 +0100344 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
345
Alexandre Rames67555f72014-11-18 10:55:16 +0000346 private:
347 HLoadString* const instruction_;
348
349 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
350};
351
Alexandre Rames5319def2014-10-23 10:03:10 +0100352class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
353 public:
354 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
355
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100358 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000359 if (instruction_->CanThrowIntoCatchBlock()) {
360 // Live registers will be restored in the catch block if caught.
361 SaveLiveRegisters(codegen, instruction_->GetLocations());
362 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000364 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800365 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100366 }
367
Alexandre Rames8158f282015-08-07 10:26:17 +0100368 bool IsFatal() const OVERRIDE { return true; }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
371
Alexandre Rames5319def2014-10-23 10:03:10 +0100372 private:
373 HNullCheck* const instruction_;
374
375 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
376};
377
378class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
379 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100380 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 : instruction_(instruction), successor_(successor) {}
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
384 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000386 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000388 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800389 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000390 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000391 if (successor_ == nullptr) {
392 __ B(GetReturnLabel());
393 } else {
394 __ B(arm64_codegen->GetLabelOf(successor_));
395 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100396 }
397
398 vixl::Label* GetReturnLabel() {
399 DCHECK(successor_ == nullptr);
400 return &return_label_;
401 }
402
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100403 HBasicBlock* GetSuccessor() const {
404 return successor_;
405 }
406
Alexandre Rames9931f312015-06-19 14:47:01 +0100407 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
408
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 private:
410 HSuspendCheck* const instruction_;
411 // If not null, the block to branch to after the suspend check.
412 HBasicBlock* const successor_;
413
414 // If `successor_` is null, the label to branch to after the suspend check.
415 vixl::Label return_label_;
416
417 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
418};
419
Alexandre Rames67555f72014-11-18 10:55:16 +0000420class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
421 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000422 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
423 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000424
425 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000426 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100427 Location class_to_check = locations->InAt(1);
428 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
429 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000430 DCHECK(instruction_->IsCheckCast()
431 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
432 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100433 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000434
Alexandre Rames67555f72014-11-18 10:55:16 +0000435 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000437 if (!is_fatal_) {
438 SaveLiveRegisters(codegen, locations);
439 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440
441 // We're moving two locations to locations that could overlap, so we need a parallel
442 // move resolver.
443 InvokeRuntimeCallingConvention calling_convention;
444 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
446 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447
448 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000449 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100450 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000451 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
452 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453 Primitive::Type ret_type = instruction_->GetType();
454 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
455 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
456 } else {
457 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800459 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460 }
461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000462 if (!is_fatal_) {
463 RestoreLiveRegisters(codegen, locations);
464 __ B(GetExitLabel());
465 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 }
467
Alexandre Rames9931f312015-06-19 14:47:01 +0100468 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000469 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100470
Alexandre Rames67555f72014-11-18 10:55:16 +0000471 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000473 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474
Alexandre Rames67555f72014-11-18 10:55:16 +0000475 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
476};
477
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700478class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
479 public:
Aart Bik42249c32016-01-07 15:33:50 -0800480 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100481 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700482
483 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800484 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700485 __ Bind(GetEntryLabel());
486 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800487 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
488 instruction_,
489 instruction_->GetDexPc(),
490 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000491 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700492 }
493
Alexandre Rames9931f312015-06-19 14:47:01 +0100494 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 private:
Aart Bik42249c32016-01-07 15:33:50 -0800497 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
499};
500
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100501class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
502 public:
503 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
504
505 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506 LocationSummary* locations = instruction_->GetLocations();
507 __ Bind(GetEntryLabel());
508 SaveLiveRegisters(codegen, locations);
509
510 InvokeRuntimeCallingConvention calling_convention;
511 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
512 parallel_move.AddMove(
513 locations->InAt(0),
514 LocationFrom(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 nullptr);
517 parallel_move.AddMove(
518 locations->InAt(1),
519 LocationFrom(calling_convention.GetRegisterAt(1)),
520 Primitive::kPrimInt,
521 nullptr);
522 parallel_move.AddMove(
523 locations->InAt(2),
524 LocationFrom(calling_convention.GetRegisterAt(2)),
525 Primitive::kPrimNot,
526 nullptr);
527 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
528
529 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
530 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
531 instruction_,
532 instruction_->GetDexPc(),
533 this);
534 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
535 RestoreLiveRegisters(codegen, locations);
536 __ B(GetExitLabel());
537 }
538
539 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
540
541 private:
542 HInstruction* const instruction_;
543
544 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
545};
546
Zheng Xu3927c8b2015-11-18 17:46:25 +0800547void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
548 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000549 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800550
551 // We are about to use the assembler to place literals directly. Make sure we have enough
552 // underlying code buffer and we have generated the jump table with right size.
553 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
554 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
555
556 __ Bind(&table_start_);
557 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
558 for (uint32_t i = 0; i < num_entries; i++) {
559 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
560 DCHECK(target_label->IsBound());
561 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
562 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
563 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
564 Literal<int32_t> literal(jump_offset);
565 __ place(&literal);
566 }
567}
568
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000569// Slow path generating a read barrier for a heap reference.
570class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
571 public:
572 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
573 Location out,
574 Location ref,
575 Location obj,
576 uint32_t offset,
577 Location index)
578 : instruction_(instruction),
579 out_(out),
580 ref_(ref),
581 obj_(obj),
582 offset_(offset),
583 index_(index) {
584 DCHECK(kEmitCompilerReadBarrier);
585 // If `obj` is equal to `out` or `ref`, it means the initial object
586 // has been overwritten by (or after) the heap object reference load
587 // to be instrumented, e.g.:
588 //
589 // __ Ldr(out, HeapOperand(out, class_offset);
590 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
591 //
592 // In that case, we have lost the information about the original
593 // object, and the emitted read barrier cannot work properly.
594 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
595 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
596 }
597
598 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
599 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
600 LocationSummary* locations = instruction_->GetLocations();
601 Primitive::Type type = Primitive::kPrimNot;
602 DCHECK(locations->CanCall());
603 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
604 DCHECK(!instruction_->IsInvoke() ||
605 (instruction_->IsInvokeStaticOrDirect() &&
606 instruction_->GetLocations()->Intrinsified()));
607
608 __ Bind(GetEntryLabel());
609
610 // Note: In the case of a HArrayGet instruction, when the base
611 // address is a HArm64IntermediateAddress instruction, it does not
612 // point to the array object itself, but to an offset within this
613 // object. However, the read barrier entry point needs the array
614 // object address to be passed as first argument. So we
615 // temporarily set back `obj_` to that address, and restore its
616 // initial value later.
617 if (instruction_->IsArrayGet() &&
618 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()) {
619 if (kIsDebugBuild) {
620 HArm64IntermediateAddress* intermediate_address =
621 instruction_->AsArrayGet()->GetArray()->AsArm64IntermediateAddress();
622 uint32_t intermediate_address_offset =
623 intermediate_address->GetOffset()->AsIntConstant()->GetValueAsUint64();
624 DCHECK_EQ(intermediate_address_offset, offset_);
625 DCHECK_EQ(mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value(), offset_);
626 }
627 Register obj_reg = RegisterFrom(obj_, Primitive::kPrimInt);
628 __ Sub(obj_reg, obj_reg, offset_);
629 }
630
631 SaveLiveRegisters(codegen, locations);
632
633 // We may have to change the index's value, but as `index_` is a
634 // constant member (like other "inputs" of this slow path),
635 // introduce a copy of it, `index`.
636 Location index = index_;
637 if (index_.IsValid()) {
638 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
639 if (instruction_->IsArrayGet()) {
640 // Compute the actual memory offset and store it in `index`.
641 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
642 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
643 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
644 // We are about to change the value of `index_reg` (see the
645 // calls to vixl::MacroAssembler::Lsl and
646 // vixl::MacroAssembler::Mov below), but it has
647 // not been saved by the previous call to
648 // art::SlowPathCode::SaveLiveRegisters, as it is a
649 // callee-save register --
650 // art::SlowPathCode::SaveLiveRegisters does not consider
651 // callee-save registers, as it has been designed with the
652 // assumption that callee-save registers are supposed to be
653 // handled by the called function. So, as a callee-save
654 // register, `index_reg` _would_ eventually be saved onto
655 // the stack, but it would be too late: we would have
656 // changed its value earlier. Therefore, we manually save
657 // it here into another freely available register,
658 // `free_reg`, chosen of course among the caller-save
659 // registers (as a callee-save `free_reg` register would
660 // exhibit the same problem).
661 //
662 // Note we could have requested a temporary register from
663 // the register allocator instead; but we prefer not to, as
664 // this is a slow path, and we know we can find a
665 // caller-save register that is available.
666 Register free_reg = FindAvailableCallerSaveRegister(codegen);
667 __ Mov(free_reg.W(), index_reg);
668 index_reg = free_reg;
669 index = LocationFrom(index_reg);
670 } else {
671 // The initial register stored in `index_` has already been
672 // saved in the call to art::SlowPathCode::SaveLiveRegisters
673 // (as it is not a callee-save register), so we can freely
674 // use it.
675 }
676 // Shifting the index value contained in `index_reg` by the scale
677 // factor (2) cannot overflow in practice, as the runtime is
678 // unable to allocate object arrays with a size larger than
679 // 2^26 - 1 (that is, 2^28 - 4 bytes).
680 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
681 static_assert(
682 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
683 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
684 __ Add(index_reg, index_reg, Operand(offset_));
685 } else {
686 DCHECK(instruction_->IsInvoke());
687 DCHECK(instruction_->GetLocations()->Intrinsified());
688 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
689 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
690 << instruction_->AsInvoke()->GetIntrinsic();
691 DCHECK_EQ(offset_, 0U);
692 DCHECK(index_.IsRegisterPair());
693 // UnsafeGet's offset location is a register pair, the low
694 // part contains the correct offset.
695 index = index_.ToLow();
696 }
697 }
698
699 // We're moving two or three locations to locations that could
700 // overlap, so we need a parallel move resolver.
701 InvokeRuntimeCallingConvention calling_convention;
702 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
703 parallel_move.AddMove(ref_,
704 LocationFrom(calling_convention.GetRegisterAt(0)),
705 type,
706 nullptr);
707 parallel_move.AddMove(obj_,
708 LocationFrom(calling_convention.GetRegisterAt(1)),
709 type,
710 nullptr);
711 if (index.IsValid()) {
712 parallel_move.AddMove(index,
713 LocationFrom(calling_convention.GetRegisterAt(2)),
714 Primitive::kPrimInt,
715 nullptr);
716 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
717 } else {
718 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
719 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
720 }
721 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
722 instruction_,
723 instruction_->GetDexPc(),
724 this);
725 CheckEntrypointTypes<
726 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
727 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
728
729 RestoreLiveRegisters(codegen, locations);
730
731 // Restore the value of `obj_` when it corresponds to a
732 // HArm64IntermediateAddress instruction.
733 if (instruction_->IsArrayGet() &&
734 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()) {
735 if (kIsDebugBuild) {
736 HArm64IntermediateAddress* intermediate_address =
737 instruction_->AsArrayGet()->GetArray()->AsArm64IntermediateAddress();
738 uint32_t intermediate_address_offset =
739 intermediate_address->GetOffset()->AsIntConstant()->GetValueAsUint64();
740 DCHECK_EQ(intermediate_address_offset, offset_);
741 DCHECK_EQ(mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value(), offset_);
742 }
743 Register obj_reg = RegisterFrom(obj_, Primitive::kPrimInt);
744 __ Add(obj_reg, obj_reg, offset_);
745 }
746
747 __ B(GetExitLabel());
748 }
749
750 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
751
752 private:
753 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
754 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
755 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
756 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
757 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
758 return Register(VIXLRegCodeFromART(i), kXRegSize);
759 }
760 }
761 // We shall never fail to find a free caller-save register, as
762 // there are more than two core caller-save registers on ARM64
763 // (meaning it is possible to find one which is different from
764 // `ref` and `obj`).
765 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
766 LOG(FATAL) << "Could not find a free register";
767 UNREACHABLE();
768 }
769
770 HInstruction* const instruction_;
771 const Location out_;
772 const Location ref_;
773 const Location obj_;
774 const uint32_t offset_;
775 // An additional location containing an index to an array.
776 // Only used for HArrayGet and the UnsafeGetObject &
777 // UnsafeGetObjectVolatile intrinsics.
778 const Location index_;
779
780 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
781};
782
783// Slow path generating a read barrier for a GC root.
784class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
785 public:
786 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
787 : instruction_(instruction), out_(out), root_(root) {}
788
789 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
790 LocationSummary* locations = instruction_->GetLocations();
791 Primitive::Type type = Primitive::kPrimNot;
792 DCHECK(locations->CanCall());
793 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
794 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
795
796 __ Bind(GetEntryLabel());
797 SaveLiveRegisters(codegen, locations);
798
799 InvokeRuntimeCallingConvention calling_convention;
800 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
801 // The argument of the ReadBarrierForRootSlow is not a managed
802 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
803 // thus we need a 64-bit move here, and we cannot use
804 //
805 // arm64_codegen->MoveLocation(
806 // LocationFrom(calling_convention.GetRegisterAt(0)),
807 // root_,
808 // type);
809 //
810 // which would emit a 32-bit move, as `type` is a (32-bit wide)
811 // reference type (`Primitive::kPrimNot`).
812 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
813 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
814 instruction_,
815 instruction_->GetDexPc(),
816 this);
817 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
818 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
819
820 RestoreLiveRegisters(codegen, locations);
821 __ B(GetExitLabel());
822 }
823
824 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
825
826 private:
827 HInstruction* const instruction_;
828 const Location out_;
829 const Location root_;
830
831 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
832};
833
Alexandre Rames5319def2014-10-23 10:03:10 +0100834#undef __
835
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100836Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100837 Location next_location;
838 if (type == Primitive::kPrimVoid) {
839 LOG(FATAL) << "Unreachable type " << type;
840 }
841
Alexandre Rames542361f2015-01-29 16:57:31 +0000842 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100843 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
844 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000845 } else if (!Primitive::IsFloatingPointType(type) &&
846 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000847 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
848 } else {
849 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000850 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
851 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 }
853
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000854 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000855 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100856 return next_location;
857}
858
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100859Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100860 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100861}
862
Serban Constantinescu579885a2015-02-22 20:51:33 +0000863CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
864 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100865 const CompilerOptions& compiler_options,
866 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100867 : CodeGenerator(graph,
868 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000869 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000870 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000871 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000872 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100873 compiler_options,
874 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100875 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800876 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100877 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000878 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000879 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000880 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100881 uint64_literals_(std::less<uint64_t>(),
882 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
883 method_patches_(MethodReferenceComparator(),
884 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
885 call_patches_(MethodReferenceComparator(),
886 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
887 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000888 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000889 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000890 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000891}
Alexandre Rames5319def2014-10-23 10:03:10 +0100892
Alexandre Rames67555f72014-11-18 10:55:16 +0000893#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100894
Zheng Xu3927c8b2015-11-18 17:46:25 +0800895void CodeGeneratorARM64::EmitJumpTables() {
896 for (auto jump_table : jump_tables_) {
897 jump_table->EmitTable(this);
898 }
899}
900
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000901void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800902 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000903 // Ensure we emit the literal pool.
904 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000905
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000906 CodeGenerator::Finalize(allocator);
907}
908
Zheng Xuad4450e2015-04-17 18:48:56 +0800909void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
910 // Note: There are 6 kinds of moves:
911 // 1. constant -> GPR/FPR (non-cycle)
912 // 2. constant -> stack (non-cycle)
913 // 3. GPR/FPR -> GPR/FPR
914 // 4. GPR/FPR -> stack
915 // 5. stack -> GPR/FPR
916 // 6. stack -> stack (non-cycle)
917 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
918 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
919 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
920 // dependency.
921 vixl_temps_.Open(GetVIXLAssembler());
922}
923
924void ParallelMoveResolverARM64::FinishEmitNativeCode() {
925 vixl_temps_.Close();
926}
927
928Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
929 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
930 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
931 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
932 Location scratch = GetScratchLocation(kind);
933 if (!scratch.Equals(Location::NoLocation())) {
934 return scratch;
935 }
936 // Allocate from VIXL temp registers.
937 if (kind == Location::kRegister) {
938 scratch = LocationFrom(vixl_temps_.AcquireX());
939 } else {
940 DCHECK(kind == Location::kFpuRegister);
941 scratch = LocationFrom(vixl_temps_.AcquireD());
942 }
943 AddScratchLocation(scratch);
944 return scratch;
945}
946
947void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
948 if (loc.IsRegister()) {
949 vixl_temps_.Release(XRegisterFrom(loc));
950 } else {
951 DCHECK(loc.IsFpuRegister());
952 vixl_temps_.Release(DRegisterFrom(loc));
953 }
954 RemoveScratchLocation(loc);
955}
956
Alexandre Rames3e69f162014-12-10 10:36:50 +0000957void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100958 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100959 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000960}
961
Alexandre Rames5319def2014-10-23 10:03:10 +0100962void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100963 MacroAssembler* masm = GetVIXLAssembler();
964 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000965 __ Bind(&frame_entry_label_);
966
Serban Constantinescu02164b32014-11-13 14:05:07 +0000967 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
968 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100969 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000970 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000971 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000972 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000973 __ Ldr(wzr, MemOperand(temp, 0));
974 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +0000975 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100976
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000977 if (!HasEmptyFrame()) {
978 int frame_size = GetFrameSize();
979 // Stack layout:
980 // sp[frame_size - 8] : lr.
981 // ... : other preserved core registers.
982 // ... : other preserved fp registers.
983 // ... : reserved frame space.
984 // sp[0] : current method.
985 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100986 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +0800987 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
988 frame_size - GetCoreSpillSize());
989 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
990 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000991 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100992}
993
994void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100995 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +0100996 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000997 if (!HasEmptyFrame()) {
998 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +0800999 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1000 frame_size - FrameEntrySpillSize());
1001 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1002 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001003 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001004 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001005 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001006 __ Ret();
1007 GetAssembler()->cfi().RestoreState();
1008 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001009}
1010
Zheng Xuda403092015-04-24 17:35:39 +08001011vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1012 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1013 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1014 core_spill_mask_);
1015}
1016
1017vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1018 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1019 GetNumberOfFloatingPointRegisters()));
1020 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1021 fpu_spill_mask_);
1022}
1023
Alexandre Rames5319def2014-10-23 10:03:10 +01001024void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1025 __ Bind(GetLabelOf(block));
1026}
1027
Alexandre Rames5319def2014-10-23 10:03:10 +01001028void CodeGeneratorARM64::Move(HInstruction* instruction,
1029 Location location,
1030 HInstruction* move_for) {
1031 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001032 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001033 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001034
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001035 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001036 MoveLocation(location,
1037 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1038 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001039 } else if (locations != nullptr && locations->Out().Equals(location)) {
1040 return;
1041 } else if (instruction->IsIntConstant()
1042 || instruction->IsLongConstant()
1043 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001044 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001045 if (location.IsRegister()) {
1046 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001047 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001048 (instruction->IsLongConstant() && dst.Is64Bits()));
1049 __ Mov(dst, value);
1050 } else {
1051 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001052 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001053 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1054 ? temps.AcquireW()
1055 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001056 __ Mov(temp, value);
1057 __ Str(temp, StackOperandFrom(location));
1058 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001059 } else if (instruction->IsTemporary()) {
1060 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001061 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001062 } else if (instruction->IsLoadLocal()) {
1063 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001064 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001065 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001066 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001067 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001068 }
1069
1070 } else {
1071 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001072 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001073 }
1074}
1075
Calin Juravle175dc732015-08-25 15:42:32 +01001076void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1077 DCHECK(location.IsRegister());
1078 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1079}
1080
Calin Juravlee460d1d2015-09-29 04:52:17 +01001081void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1082 if (location.IsRegister()) {
1083 locations->AddTemp(location);
1084 } else {
1085 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1086 }
1087}
1088
Alexandre Rames5319def2014-10-23 10:03:10 +01001089Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1090 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001091
Alexandre Rames5319def2014-10-23 10:03:10 +01001092 switch (type) {
1093 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001094 case Primitive::kPrimInt:
1095 case Primitive::kPrimFloat:
1096 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1097
1098 case Primitive::kPrimLong:
1099 case Primitive::kPrimDouble:
1100 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1101
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 case Primitive::kPrimBoolean:
1103 case Primitive::kPrimByte:
1104 case Primitive::kPrimChar:
1105 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001106 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001107 LOG(FATAL) << "Unexpected type " << type;
1108 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109
Alexandre Rames5319def2014-10-23 10:03:10 +01001110 LOG(FATAL) << "Unreachable";
1111 return Location::NoLocation();
1112}
1113
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001114void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001115 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001116 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001117 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001118 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001119 if (value_can_be_null) {
1120 __ Cbz(value, &done);
1121 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1123 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001124 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001125 if (value_can_be_null) {
1126 __ Bind(&done);
1127 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001128}
1129
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001130void CodeGeneratorARM64::SetupBlockedRegisters(bool is_baseline) const {
1131 // Blocked core registers:
1132 // lr : Runtime reserved.
1133 // tr : Runtime reserved.
1134 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1135 // ip1 : VIXL core temp.
1136 // ip0 : VIXL core temp.
1137 //
1138 // Blocked fp registers:
1139 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001140 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1141 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001142 while (!reserved_core_registers.IsEmpty()) {
1143 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1144 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001145
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001146 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001147 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001148 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1149 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001150
1151 if (is_baseline) {
1152 CPURegList reserved_core_baseline_registers = callee_saved_core_registers;
1153 while (!reserved_core_baseline_registers.IsEmpty()) {
1154 blocked_core_registers_[reserved_core_baseline_registers.PopLowestIndex().code()] = true;
1155 }
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001156 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001157
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001158 if (is_baseline || GetGraph()->IsDebuggable()) {
1159 // Stubs do not save callee-save floating point registers. If the graph
1160 // is debuggable, we need to deal with these registers differently. For
1161 // now, just block them.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001162 CPURegList reserved_fp_baseline_registers = callee_saved_fp_registers;
1163 while (!reserved_fp_baseline_registers.IsEmpty()) {
1164 blocked_fpu_registers_[reserved_fp_baseline_registers.PopLowestIndex().code()] = true;
1165 }
1166 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001167}
1168
1169Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const {
1170 if (type == Primitive::kPrimVoid) {
1171 LOG(FATAL) << "Unreachable type " << type;
1172 }
1173
Alexandre Rames542361f2015-01-29 16:57:31 +00001174 if (Primitive::IsFloatingPointType(type)) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001175 ssize_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfAllocatableFPRegisters);
1176 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001177 return Location::FpuRegisterLocation(reg);
1178 } else {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001179 ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfAllocatableRegisters);
1180 DCHECK_NE(reg, -1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001181 return Location::RegisterLocation(reg);
1182 }
1183}
1184
Alexandre Rames3e69f162014-12-10 10:36:50 +00001185size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1186 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1187 __ Str(reg, MemOperand(sp, stack_index));
1188 return kArm64WordSize;
1189}
1190
1191size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1192 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1193 __ Ldr(reg, MemOperand(sp, stack_index));
1194 return kArm64WordSize;
1195}
1196
1197size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1198 FPRegister reg = FPRegister(reg_id, kDRegSize);
1199 __ Str(reg, MemOperand(sp, stack_index));
1200 return kArm64WordSize;
1201}
1202
1203size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 FPRegister reg = FPRegister(reg_id, kDRegSize);
1205 __ Ldr(reg, MemOperand(sp, stack_index));
1206 return kArm64WordSize;
1207}
1208
Alexandre Rames5319def2014-10-23 10:03:10 +01001209void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001210 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001211}
1212
1213void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001214 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001215}
1216
Alexandre Rames67555f72014-11-18 10:55:16 +00001217void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001218 if (constant->IsIntConstant()) {
1219 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1220 } else if (constant->IsLongConstant()) {
1221 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1222 } else if (constant->IsNullConstant()) {
1223 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001224 } else if (constant->IsFloatConstant()) {
1225 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1226 } else {
1227 DCHECK(constant->IsDoubleConstant());
1228 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1229 }
1230}
1231
Alexandre Rames3e69f162014-12-10 10:36:50 +00001232
1233static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1234 DCHECK(constant.IsConstant());
1235 HConstant* cst = constant.GetConstant();
1236 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001237 // Null is mapped to a core W register, which we associate with kPrimInt.
1238 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001239 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1240 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1241 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1242}
1243
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244void CodeGeneratorARM64::MoveLocation(Location destination,
1245 Location source,
1246 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001247 if (source.Equals(destination)) {
1248 return;
1249 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001250
1251 // A valid move can always be inferred from the destination and source
1252 // locations. When moving from and to a register, the argument type can be
1253 // used to generate 32bit instead of 64bit moves. In debug mode we also
1254 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001256
1257 if (destination.IsRegister() || destination.IsFpuRegister()) {
1258 if (unspecified_type) {
1259 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1260 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001261 (src_cst != nullptr && (src_cst->IsIntConstant()
1262 || src_cst->IsFloatConstant()
1263 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001264 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001265 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001266 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 // If the source is a double stack slot or a 64bit constant, a 64bit
1268 // type is appropriate. Else the source is a register, and since the
1269 // type has not been specified, we chose a 64bit type to force a 64bit
1270 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001272 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001273 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1275 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1276 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1278 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1279 __ Ldr(dst, StackOperandFrom(source));
1280 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001282 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001285 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001286 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001287 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1289 ? Primitive::kPrimLong
1290 : Primitive::kPrimInt;
1291 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1292 }
1293 } else {
1294 DCHECK(source.IsFpuRegister());
1295 if (destination.IsRegister()) {
1296 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1297 ? Primitive::kPrimDouble
1298 : Primitive::kPrimFloat;
1299 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1300 } else {
1301 DCHECK(destination.IsFpuRegister());
1302 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001303 }
1304 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001305 } else { // The destination is not a register. It must be a stack slot.
1306 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1307 if (source.IsRegister() || source.IsFpuRegister()) {
1308 if (unspecified_type) {
1309 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001310 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001311 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001313 }
1314 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001315 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1316 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1317 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001318 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001319 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1320 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001321 UseScratchRegisterScope temps(GetVIXLAssembler());
1322 HConstant* src_cst = source.GetConstant();
1323 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001324 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001325 temp = temps.AcquireW();
1326 } else if (src_cst->IsLongConstant()) {
1327 temp = temps.AcquireX();
1328 } else if (src_cst->IsFloatConstant()) {
1329 temp = temps.AcquireS();
1330 } else {
1331 DCHECK(src_cst->IsDoubleConstant());
1332 temp = temps.AcquireD();
1333 }
1334 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001335 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001336 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001338 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001339 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001340 // There is generally less pressure on FP registers.
1341 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001342 __ Ldr(temp, StackOperandFrom(source));
1343 __ Str(temp, StackOperandFrom(destination));
1344 }
1345 }
1346}
1347
1348void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001349 CPURegister dst,
1350 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001351 switch (type) {
1352 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001353 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001354 break;
1355 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001356 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001357 break;
1358 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001359 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001360 break;
1361 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001362 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001363 break;
1364 case Primitive::kPrimInt:
1365 case Primitive::kPrimNot:
1366 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001367 case Primitive::kPrimFloat:
1368 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001369 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001370 __ Ldr(dst, src);
1371 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001372 case Primitive::kPrimVoid:
1373 LOG(FATAL) << "Unreachable type " << type;
1374 }
1375}
1376
Calin Juravle77520bc2015-01-12 18:45:46 +00001377void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001378 CPURegister dst,
1379 const MemOperand& src) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001380 MacroAssembler* masm = GetVIXLAssembler();
1381 BlockPoolsScope block_pools(masm);
1382 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001383 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001384 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001385
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001386 DCHECK(!src.IsPreIndex());
1387 DCHECK(!src.IsPostIndex());
1388
1389 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001390 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 MemOperand base = MemOperand(temp_base);
1392 switch (type) {
1393 case Primitive::kPrimBoolean:
1394 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001395 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396 break;
1397 case Primitive::kPrimByte:
1398 __ Ldarb(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001399 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001400 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1401 break;
1402 case Primitive::kPrimChar:
1403 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001404 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001405 break;
1406 case Primitive::kPrimShort:
1407 __ Ldarh(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001408 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001409 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1410 break;
1411 case Primitive::kPrimInt:
1412 case Primitive::kPrimNot:
1413 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001414 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001415 __ Ldar(Register(dst), base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001416 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001417 break;
1418 case Primitive::kPrimFloat:
1419 case Primitive::kPrimDouble: {
1420 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001421 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001422
1423 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1424 __ Ldar(temp, base);
Calin Juravle77520bc2015-01-12 18:45:46 +00001425 MaybeRecordImplicitNullCheck(instruction);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001426 __ Fmov(FPRegister(dst), temp);
1427 break;
1428 }
1429 case Primitive::kPrimVoid:
1430 LOG(FATAL) << "Unreachable type " << type;
1431 }
1432}
1433
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001435 CPURegister src,
1436 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001437 switch (type) {
1438 case Primitive::kPrimBoolean:
1439 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001440 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001441 break;
1442 case Primitive::kPrimChar:
1443 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001445 break;
1446 case Primitive::kPrimInt:
1447 case Primitive::kPrimNot:
1448 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001449 case Primitive::kPrimFloat:
1450 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001451 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001452 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001453 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001454 case Primitive::kPrimVoid:
1455 LOG(FATAL) << "Unreachable type " << type;
1456 }
1457}
1458
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001459void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1460 CPURegister src,
1461 const MemOperand& dst) {
1462 UseScratchRegisterScope temps(GetVIXLAssembler());
1463 Register temp_base = temps.AcquireX();
1464
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001465 DCHECK(!dst.IsPreIndex());
1466 DCHECK(!dst.IsPostIndex());
1467
1468 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001469 Operand op = OperandFromMemOperand(dst);
1470 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001471 MemOperand base = MemOperand(temp_base);
1472 switch (type) {
1473 case Primitive::kPrimBoolean:
1474 case Primitive::kPrimByte:
1475 __ Stlrb(Register(src), base);
1476 break;
1477 case Primitive::kPrimChar:
1478 case Primitive::kPrimShort:
1479 __ Stlrh(Register(src), base);
1480 break;
1481 case Primitive::kPrimInt:
1482 case Primitive::kPrimNot:
1483 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001484 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001485 __ Stlr(Register(src), base);
1486 break;
1487 case Primitive::kPrimFloat:
1488 case Primitive::kPrimDouble: {
1489 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001490 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001491
1492 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1493 __ Fmov(temp, FPRegister(src));
1494 __ Stlr(temp, base);
1495 break;
1496 }
1497 case Primitive::kPrimVoid:
1498 LOG(FATAL) << "Unreachable type " << type;
1499 }
1500}
1501
Calin Juravle175dc732015-08-25 15:42:32 +01001502void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1503 HInstruction* instruction,
1504 uint32_t dex_pc,
1505 SlowPathCode* slow_path) {
1506 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1507 instruction,
1508 dex_pc,
1509 slow_path);
1510}
1511
Alexandre Rames67555f72014-11-18 10:55:16 +00001512void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1513 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001514 uint32_t dex_pc,
1515 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001516 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001517 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001518 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1519 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001520 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001521}
1522
1523void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1524 vixl::Register class_reg) {
1525 UseScratchRegisterScope temps(GetVIXLAssembler());
1526 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001527 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
Serban Constantinescu579885a2015-02-22 20:51:33 +00001528 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001529
Serban Constantinescu02164b32014-11-13 14:05:07 +00001530 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu579885a2015-02-22 20:51:33 +00001531 if (use_acquire_release) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001532 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1533 __ Add(temp, class_reg, status_offset);
1534 __ Ldar(temp, HeapOperand(temp));
1535 __ Cmp(temp, mirror::Class::kStatusInitialized);
1536 __ B(lt, slow_path->GetEntryLabel());
1537 } else {
1538 __ Ldr(temp, HeapOperand(class_reg, status_offset));
1539 __ Cmp(temp, mirror::Class::kStatusInitialized);
1540 __ B(lt, slow_path->GetEntryLabel());
1541 __ Dmb(InnerShareable, BarrierReads);
1542 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001543 __ Bind(slow_path->GetExitLabel());
1544}
Alexandre Rames5319def2014-10-23 10:03:10 +01001545
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001546void InstructionCodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
1547 BarrierType type = BarrierAll;
1548
1549 switch (kind) {
1550 case MemBarrierKind::kAnyAny:
1551 case MemBarrierKind::kAnyStore: {
1552 type = BarrierAll;
1553 break;
1554 }
1555 case MemBarrierKind::kLoadAny: {
1556 type = BarrierReads;
1557 break;
1558 }
1559 case MemBarrierKind::kStoreStore: {
1560 type = BarrierWrites;
1561 break;
1562 }
1563 default:
1564 LOG(FATAL) << "Unexpected memory barrier " << kind;
1565 }
1566 __ Dmb(InnerShareable, type);
1567}
1568
Serban Constantinescu02164b32014-11-13 14:05:07 +00001569void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1570 HBasicBlock* successor) {
1571 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001572 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1573 if (slow_path == nullptr) {
1574 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1575 instruction->SetSlowPath(slow_path);
1576 codegen_->AddSlowPath(slow_path);
1577 if (successor != nullptr) {
1578 DCHECK(successor->IsLoopHeader());
1579 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1580 }
1581 } else {
1582 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1583 }
1584
Serban Constantinescu02164b32014-11-13 14:05:07 +00001585 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1586 Register temp = temps.AcquireW();
1587
1588 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1589 if (successor == nullptr) {
1590 __ Cbnz(temp, slow_path->GetEntryLabel());
1591 __ Bind(slow_path->GetReturnLabel());
1592 } else {
1593 __ Cbz(temp, codegen_->GetLabelOf(successor));
1594 __ B(slow_path->GetEntryLabel());
1595 // slow_path will return to GetLabelOf(successor).
1596 }
1597}
1598
Alexandre Rames5319def2014-10-23 10:03:10 +01001599InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1600 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001601 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001602 assembler_(codegen->GetAssembler()),
1603 codegen_(codegen) {}
1604
1605#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001606 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001607
1608#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1609
1610enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001611 // Using a base helps identify when we hit such breakpoints.
1612 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001613#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1614 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1615#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1616};
1617
1618#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001619 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001620 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1621 } \
1622 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1623 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1624 locations->SetOut(Location::Any()); \
1625 }
1626 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1627#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1628
1629#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001630#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001631
Alexandre Rames67555f72014-11-18 10:55:16 +00001632void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001633 DCHECK_EQ(instr->InputCount(), 2U);
1634 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1635 Primitive::Type type = instr->GetResultType();
1636 switch (type) {
1637 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001638 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001639 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001640 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001642 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001643
1644 case Primitive::kPrimFloat:
1645 case Primitive::kPrimDouble:
1646 locations->SetInAt(0, Location::RequiresFpuRegister());
1647 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001648 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001649 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001650
Alexandre Rames5319def2014-10-23 10:03:10 +01001651 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001652 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001653 }
1654}
1655
Alexandre Rames09a99962015-04-15 11:47:56 +01001656void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001657 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1658
1659 bool object_field_get_with_read_barrier =
1660 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001661 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001662 new (GetGraph()->GetArena()) LocationSummary(instruction,
1663 object_field_get_with_read_barrier ?
1664 LocationSummary::kCallOnSlowPath :
1665 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001666 locations->SetInAt(0, Location::RequiresRegister());
1667 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1668 locations->SetOut(Location::RequiresFpuRegister());
1669 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001670 // The output overlaps for an object field get when read barriers
1671 // are enabled: we do not want the load to overwrite the object's
1672 // location, as we need it to emit the read barrier.
1673 locations->SetOut(
1674 Location::RequiresRegister(),
1675 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001676 }
1677}
1678
1679void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1680 const FieldInfo& field_info) {
1681 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain4d027112015-07-01 15:41:14 +01001682 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001683 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001684
1685 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
1686 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1687
1688 if (field_info.IsVolatile()) {
1689 if (use_acquire_release) {
1690 // NB: LoadAcquire will record the pc info if needed.
1691 codegen_->LoadAcquire(instruction, OutputCPURegister(instruction), field);
1692 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001693 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001694 codegen_->MaybeRecordImplicitNullCheck(instruction);
1695 // For IRIW sequential consistency kLoadAny is not sufficient.
1696 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1697 }
1698 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001699 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001700 codegen_->MaybeRecordImplicitNullCheck(instruction);
1701 }
Roland Levillain4d027112015-07-01 15:41:14 +01001702
1703 if (field_type == Primitive::kPrimNot) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001704 LocationSummary* locations = instruction->GetLocations();
1705 Location base = locations->InAt(0);
1706 Location out = locations->Out();
1707 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
1708 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01001709 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001710}
1711
1712void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1713 LocationSummary* locations =
1714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1715 locations->SetInAt(0, Location::RequiresRegister());
1716 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1717 locations->SetInAt(1, Location::RequiresFpuRegister());
1718 } else {
1719 locations->SetInAt(1, Location::RequiresRegister());
1720 }
1721}
1722
1723void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001724 const FieldInfo& field_info,
1725 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001726 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001727 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001728
1729 Register obj = InputRegisterAt(instruction, 0);
1730 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001731 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001732 Offset offset = field_info.GetFieldOffset();
1733 Primitive::Type field_type = field_info.GetFieldType();
1734 bool use_acquire_release = codegen_->GetInstructionSetFeatures().PreferAcquireRelease();
1735
Roland Levillain4d027112015-07-01 15:41:14 +01001736 {
1737 // We use a block to end the scratch scope before the write barrier, thus
1738 // freeing the temporary registers so they can be used in `MarkGCCard`.
1739 UseScratchRegisterScope temps(GetVIXLAssembler());
1740
1741 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1742 DCHECK(value.IsW());
1743 Register temp = temps.AcquireW();
1744 __ Mov(temp, value.W());
1745 GetAssembler()->PoisonHeapReference(temp.W());
1746 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001747 }
Roland Levillain4d027112015-07-01 15:41:14 +01001748
1749 if (field_info.IsVolatile()) {
1750 if (use_acquire_release) {
1751 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1752 codegen_->MaybeRecordImplicitNullCheck(instruction);
1753 } else {
1754 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
1755 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1756 codegen_->MaybeRecordImplicitNullCheck(instruction);
1757 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
1758 }
1759 } else {
1760 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1761 codegen_->MaybeRecordImplicitNullCheck(instruction);
1762 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001763 }
1764
1765 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001766 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001767 }
1768}
1769
Alexandre Rames67555f72014-11-18 10:55:16 +00001770void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001771 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001772
1773 switch (type) {
1774 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001775 case Primitive::kPrimLong: {
1776 Register dst = OutputRegister(instr);
1777 Register lhs = InputRegisterAt(instr, 0);
1778 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001779 if (instr->IsAdd()) {
1780 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001781 } else if (instr->IsAnd()) {
1782 __ And(dst, lhs, rhs);
1783 } else if (instr->IsOr()) {
1784 __ Orr(dst, lhs, rhs);
1785 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001786 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001787 } else if (instr->IsRor()) {
1788 if (rhs.IsImmediate()) {
1789 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1790 __ Ror(dst, lhs, shift);
1791 } else {
1792 // Ensure shift distance is in the same size register as the result. If
1793 // we are rotating a long and the shift comes in a w register originally,
1794 // we don't need to sxtw for use as an x since the shift distances are
1795 // all & reg_bits - 1.
1796 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1797 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else {
1799 DCHECK(instr->IsXor());
1800 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001801 }
1802 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001803 }
1804 case Primitive::kPrimFloat:
1805 case Primitive::kPrimDouble: {
1806 FPRegister dst = OutputFPRegister(instr);
1807 FPRegister lhs = InputFPRegisterAt(instr, 0);
1808 FPRegister rhs = InputFPRegisterAt(instr, 1);
1809 if (instr->IsAdd()) {
1810 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001811 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001812 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001813 } else {
1814 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001815 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001816 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001817 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001818 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001819 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001820 }
1821}
1822
Serban Constantinescu02164b32014-11-13 14:05:07 +00001823void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1824 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1825
1826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1827 Primitive::Type type = instr->GetResultType();
1828 switch (type) {
1829 case Primitive::kPrimInt:
1830 case Primitive::kPrimLong: {
1831 locations->SetInAt(0, Location::RequiresRegister());
1832 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1833 locations->SetOut(Location::RequiresRegister());
1834 break;
1835 }
1836 default:
1837 LOG(FATAL) << "Unexpected shift type " << type;
1838 }
1839}
1840
1841void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1842 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1843
1844 Primitive::Type type = instr->GetType();
1845 switch (type) {
1846 case Primitive::kPrimInt:
1847 case Primitive::kPrimLong: {
1848 Register dst = OutputRegister(instr);
1849 Register lhs = InputRegisterAt(instr, 0);
1850 Operand rhs = InputOperandAt(instr, 1);
1851 if (rhs.IsImmediate()) {
1852 uint32_t shift_value = (type == Primitive::kPrimInt)
1853 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1854 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1855 if (instr->IsShl()) {
1856 __ Lsl(dst, lhs, shift_value);
1857 } else if (instr->IsShr()) {
1858 __ Asr(dst, lhs, shift_value);
1859 } else {
1860 __ Lsr(dst, lhs, shift_value);
1861 }
1862 } else {
1863 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1864
1865 if (instr->IsShl()) {
1866 __ Lsl(dst, lhs, rhs_reg);
1867 } else if (instr->IsShr()) {
1868 __ Asr(dst, lhs, rhs_reg);
1869 } else {
1870 __ Lsr(dst, lhs, rhs_reg);
1871 }
1872 }
1873 break;
1874 }
1875 default:
1876 LOG(FATAL) << "Unexpected shift operation type " << type;
1877 }
1878}
1879
Alexandre Rames5319def2014-10-23 10:03:10 +01001880void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001881 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001882}
1883
1884void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001885 HandleBinaryOp(instruction);
1886}
1887
1888void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1889 HandleBinaryOp(instruction);
1890}
1891
1892void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1893 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001894}
1895
Alexandre Rames8626b742015-11-25 16:28:08 +00001896void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1897 HArm64DataProcWithShifterOp* instruction) {
1898 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1899 instruction->GetType() == Primitive::kPrimLong);
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1902 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1903 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1904 } else {
1905 locations->SetInAt(0, Location::RequiresRegister());
1906 }
1907 locations->SetInAt(1, Location::RequiresRegister());
1908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1909}
1910
1911void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1912 HArm64DataProcWithShifterOp* instruction) {
1913 Primitive::Type type = instruction->GetType();
1914 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1915 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1916 Register out = OutputRegister(instruction);
1917 Register left;
1918 if (kind != HInstruction::kNeg) {
1919 left = InputRegisterAt(instruction, 0);
1920 }
1921 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1922 // shifter operand operation, the IR generating `right_reg` (input to the type
1923 // conversion) can have a different type from the current instruction's type,
1924 // so we manually indicate the type.
1925 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1926 int64_t shift_amount = (type == Primitive::kPrimInt)
1927 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1928 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1929
1930 Operand right_operand(0);
1931
1932 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1933 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1934 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1935 } else {
1936 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1937 }
1938
1939 // Logical binary operations do not support extension operations in the
1940 // operand. Note that VIXL would still manage if it was passed by generating
1941 // the extension as a separate instruction.
1942 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1943 DCHECK(!right_operand.IsExtendedRegister() ||
1944 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1945 kind != HInstruction::kNeg));
1946 switch (kind) {
1947 case HInstruction::kAdd:
1948 __ Add(out, left, right_operand);
1949 break;
1950 case HInstruction::kAnd:
1951 __ And(out, left, right_operand);
1952 break;
1953 case HInstruction::kNeg:
1954 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1955 __ Neg(out, right_operand);
1956 break;
1957 case HInstruction::kOr:
1958 __ Orr(out, left, right_operand);
1959 break;
1960 case HInstruction::kSub:
1961 __ Sub(out, left, right_operand);
1962 break;
1963 case HInstruction::kXor:
1964 __ Eor(out, left, right_operand);
1965 break;
1966 default:
1967 LOG(FATAL) << "Unexpected operation kind: " << kind;
1968 UNREACHABLE();
1969 }
1970}
1971
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001972void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
1973 LocationSummary* locations =
1974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1975 locations->SetInAt(0, Location::RequiresRegister());
1976 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1977 locations->SetOut(Location::RequiresRegister());
1978}
1979
1980void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1981 HArm64IntermediateAddress* instruction) {
1982 __ Add(OutputRegister(instruction),
1983 InputRegisterAt(instruction, 0),
1984 Operand(InputOperandAt(instruction, 1)));
1985}
1986
Alexandre Rames418318f2015-11-20 15:55:47 +00001987void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
1990 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
1991 Location::RequiresRegister());
1992 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1993 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
1994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1995}
1996
1997void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
1998 Register res = OutputRegister(instr);
1999 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
2000 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
2001 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
2002
2003 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2004 // This fixup should be carried out for all multiply-accumulate instructions:
2005 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2006 if (instr->GetType() == Primitive::kPrimLong &&
2007 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2008 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2009 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2010 if (prev->IsLoadOrStore()) {
2011 // Make sure we emit only exactly one nop.
2012 vixl::CodeBufferCheckScope scope(masm,
2013 vixl::kInstructionSize,
2014 vixl::CodeBufferCheckScope::kCheck,
2015 vixl::CodeBufferCheckScope::kExactSize);
2016 __ nop();
2017 }
2018 }
2019
2020 if (instr->GetOpKind() == HInstruction::kAdd) {
2021 __ Madd(res, mul_left, mul_right, accumulator);
2022 } else {
2023 DCHECK(instr->GetOpKind() == HInstruction::kSub);
2024 __ Msub(res, mul_left, mul_right, accumulator);
2025 }
2026}
2027
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002028void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002029 bool object_array_get_with_read_barrier =
2030 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002031 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002032 new (GetGraph()->GetArena()) LocationSummary(instruction,
2033 object_array_get_with_read_barrier ?
2034 LocationSummary::kCallOnSlowPath :
2035 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002036 locations->SetInAt(0, Location::RequiresRegister());
2037 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002038 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2039 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2040 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002041 // The output overlaps in the case of an object array get with
2042 // read barriers enabled: we do not want the move to overwrite the
2043 // array's location, as we need it to emit the read barrier.
2044 locations->SetOut(
2045 Location::RequiresRegister(),
2046 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002047 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002048}
2049
2050void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002051 Primitive::Type type = instruction->GetType();
2052 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002053 LocationSummary* locations = instruction->GetLocations();
2054 Location index = locations->InAt(1);
2055 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002056 MemOperand source = HeapOperand(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002057 CPURegister dest = OutputCPURegister(instruction);
2058
Alexandre Ramesd921d642015-04-16 15:07:16 +01002059 MacroAssembler* masm = GetVIXLAssembler();
2060 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002061 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002062 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002063
2064 if (index.IsConstant()) {
2065 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002066 source = HeapOperand(obj, offset);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002067 } else {
2068 Register temp = temps.AcquireSameSizeAs(obj);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002069 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2070 // We do not need to compute the intermediate address from the array: the
2071 // input instruction has done it already. See the comment in
2072 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2073 if (kIsDebugBuild) {
2074 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2075 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2076 }
2077 temp = obj;
2078 } else {
2079 __ Add(temp, obj, offset);
2080 }
Alexandre Rames82000b02015-07-07 11:34:16 +01002081 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082 }
2083
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002084 codegen_->Load(type, dest, source);
Calin Juravle77520bc2015-01-12 18:45:46 +00002085 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002086
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002087 if (type == Primitive::kPrimNot) {
2088 static_assert(
2089 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2090 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2091 Location obj_loc = locations->InAt(0);
2092 Location out = locations->Out();
2093 if (index.IsConstant()) {
2094 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
2095 } else {
2096 // Note: when `obj_loc` is a HArm64IntermediateAddress, it does
2097 // not contain the base address of the array object, which is
2098 // needed by the read barrier entry point. So the read barrier
2099 // slow path will temporarily set back `obj_loc` to the right
2100 // address (see ReadBarrierForHeapReferenceSlowPathARM64::EmitNativeCode).
2101 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset, index);
2102 }
Roland Levillain4d027112015-07-01 15:41:14 +01002103 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002104}
2105
Alexandre Rames5319def2014-10-23 10:03:10 +01002106void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2108 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002109 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002110}
2111
2112void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002113 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002114 __ Ldr(OutputRegister(instruction),
2115 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002116 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002117}
2118
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002119void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002120 Primitive::Type value_type = instruction->GetComponentType();
2121
2122 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2123 bool object_array_set_with_read_barrier =
2124 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2126 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002127 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2128 LocationSummary::kCallOnSlowPath :
2129 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002130 locations->SetInAt(0, Location::RequiresRegister());
2131 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002132 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002133 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002134 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002135 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002136 }
2137}
2138
2139void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2140 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002141 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002142 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002143 bool needs_write_barrier =
2144 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002145
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002146 Register array = InputRegisterAt(instruction, 0);
2147 CPURegister value = InputCPURegisterAt(instruction, 2);
2148 CPURegister source = value;
2149 Location index = locations->InAt(1);
2150 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2151 MemOperand destination = HeapOperand(array);
2152 MacroAssembler* masm = GetVIXLAssembler();
2153 BlockPoolsScope block_pools(masm);
2154
2155 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002156 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002157 if (index.IsConstant()) {
2158 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2159 destination = HeapOperand(array, offset);
2160 } else {
2161 UseScratchRegisterScope temps(masm);
2162 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002163 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2164 // We do not need to compute the intermediate address from the array: the
2165 // input instruction has done it already. See the comment in
2166 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2167 if (kIsDebugBuild) {
2168 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2169 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2170 }
2171 temp = array;
2172 } else {
2173 __ Add(temp, array, offset);
2174 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002175 destination = HeapOperand(temp,
2176 XRegisterFrom(index),
2177 LSL,
2178 Primitive::ComponentSizeShift(value_type));
2179 }
2180 codegen_->Store(value_type, value, destination);
2181 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002182 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002183 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002184 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002185 vixl::Label done;
2186 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002187 {
2188 // We use a block to end the scratch scope before the write barrier, thus
2189 // freeing the temporary registers so they can be used in `MarkGCCard`.
2190 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002191 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002192 if (index.IsConstant()) {
2193 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002194 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002195 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002196 destination = HeapOperand(temp,
2197 XRegisterFrom(index),
2198 LSL,
2199 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002200 }
2201
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002202 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2203 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2204 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2205
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002206 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002207 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2208 codegen_->AddSlowPath(slow_path);
2209 if (instruction->GetValueCanBeNull()) {
2210 vixl::Label non_zero;
2211 __ Cbnz(Register(value), &non_zero);
2212 if (!index.IsConstant()) {
2213 __ Add(temp, array, offset);
2214 }
2215 __ Str(wzr, destination);
2216 codegen_->MaybeRecordImplicitNullCheck(instruction);
2217 __ B(&done);
2218 __ Bind(&non_zero);
2219 }
2220
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002221 if (kEmitCompilerReadBarrier) {
2222 // When read barriers are enabled, the type checking
2223 // instrumentation requires two read barriers:
2224 //
2225 // __ Mov(temp2, temp);
2226 // // /* HeapReference<Class> */ temp = temp->component_type_
2227 // __ Ldr(temp, HeapOperand(temp, component_offset));
2228 // codegen_->GenerateReadBarrier(
2229 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2230 //
2231 // // /* HeapReference<Class> */ temp2 = value->klass_
2232 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2233 // codegen_->GenerateReadBarrier(
2234 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2235 //
2236 // __ Cmp(temp, temp2);
2237 //
2238 // However, the second read barrier may trash `temp`, as it
2239 // is a temporary register, and as such would not be saved
2240 // along with live registers before calling the runtime (nor
2241 // restored afterwards). So in this case, we bail out and
2242 // delegate the work to the array set slow path.
2243 //
2244 // TODO: Extend the register allocator to support a new
2245 // "(locally) live temp" location so as to avoid always
2246 // going into the slow path when read barriers are enabled.
2247 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002248 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002249 Register temp2 = temps.AcquireSameSizeAs(array);
2250 // /* HeapReference<Class> */ temp = array->klass_
2251 __ Ldr(temp, HeapOperand(array, class_offset));
2252 codegen_->MaybeRecordImplicitNullCheck(instruction);
2253 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2254
2255 // /* HeapReference<Class> */ temp = temp->component_type_
2256 __ Ldr(temp, HeapOperand(temp, component_offset));
2257 // /* HeapReference<Class> */ temp2 = value->klass_
2258 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2259 // If heap poisoning is enabled, no need to unpoison `temp`
2260 // nor `temp2`, as we are comparing two poisoned references.
2261 __ Cmp(temp, temp2);
2262
2263 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2264 vixl::Label do_put;
2265 __ B(eq, &do_put);
2266 // If heap poisoning is enabled, the `temp` reference has
2267 // not been unpoisoned yet; unpoison it now.
2268 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2269
2270 // /* HeapReference<Class> */ temp = temp->super_class_
2271 __ Ldr(temp, HeapOperand(temp, super_offset));
2272 // If heap poisoning is enabled, no need to unpoison
2273 // `temp`, as we are comparing against null below.
2274 __ Cbnz(temp, slow_path->GetEntryLabel());
2275 __ Bind(&do_put);
2276 } else {
2277 __ B(ne, slow_path->GetEntryLabel());
2278 }
2279 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002280 }
2281 }
2282
2283 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002284 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002285 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002286 __ Mov(temp2, value.W());
2287 GetAssembler()->PoisonHeapReference(temp2);
2288 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002289 }
2290
2291 if (!index.IsConstant()) {
2292 __ Add(temp, array, offset);
2293 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002294 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002295
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002296 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002297 codegen_->MaybeRecordImplicitNullCheck(instruction);
2298 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002299 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002300
2301 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2302
2303 if (done.IsLinked()) {
2304 __ Bind(&done);
2305 }
2306
2307 if (slow_path != nullptr) {
2308 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002309 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002310 }
2311}
2312
Alexandre Rames67555f72014-11-18 10:55:16 +00002313void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002314 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2315 ? LocationSummary::kCallOnSlowPath
2316 : LocationSummary::kNoCall;
2317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002318 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002319 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002320 if (instruction->HasUses()) {
2321 locations->SetOut(Location::SameAsFirstInput());
2322 }
2323}
2324
2325void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002326 BoundsCheckSlowPathARM64* slow_path =
2327 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002328 codegen_->AddSlowPath(slow_path);
2329
2330 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2331 __ B(slow_path->GetEntryLabel(), hs);
2332}
2333
Alexandre Rames67555f72014-11-18 10:55:16 +00002334void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2335 LocationSummary* locations =
2336 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2337 locations->SetInAt(0, Location::RequiresRegister());
2338 if (check->HasUses()) {
2339 locations->SetOut(Location::SameAsFirstInput());
2340 }
2341}
2342
2343void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2344 // We assume the class is not null.
2345 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2346 check->GetLoadClass(), check, check->GetDexPc(), true);
2347 codegen_->AddSlowPath(slow_path);
2348 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2349}
2350
Roland Levillain7f63c522015-07-13 15:54:55 +00002351static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2352 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2353 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2354}
2355
Serban Constantinescu02164b32014-11-13 14:05:07 +00002356void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002357 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002358 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2359 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002360 switch (in_type) {
2361 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002362 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002363 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2365 break;
2366 }
2367 case Primitive::kPrimFloat:
2368 case Primitive::kPrimDouble: {
2369 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002370 locations->SetInAt(1,
2371 IsFloatingPointZeroConstant(compare->InputAt(1))
2372 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2373 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002374 locations->SetOut(Location::RequiresRegister());
2375 break;
2376 }
2377 default:
2378 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2379 }
2380}
2381
2382void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2383 Primitive::Type in_type = compare->InputAt(0)->GetType();
2384
2385 // 0 if: left == right
2386 // 1 if: left > right
2387 // -1 if: left < right
2388 switch (in_type) {
2389 case Primitive::kPrimLong: {
2390 Register result = OutputRegister(compare);
2391 Register left = InputRegisterAt(compare, 0);
2392 Operand right = InputOperandAt(compare, 1);
2393
2394 __ Cmp(left, right);
2395 __ Cset(result, ne);
2396 __ Cneg(result, result, lt);
2397 break;
2398 }
2399 case Primitive::kPrimFloat:
2400 case Primitive::kPrimDouble: {
2401 Register result = OutputRegister(compare);
2402 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002403 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002404 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2405 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002406 __ Fcmp(left, 0.0);
2407 } else {
2408 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2409 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002410 if (compare->IsGtBias()) {
2411 __ Cset(result, ne);
2412 } else {
2413 __ Csetm(result, ne);
2414 }
2415 __ Cneg(result, result, compare->IsGtBias() ? mi : gt);
Alexandre Rames5319def2014-10-23 10:03:10 +01002416 break;
2417 }
2418 default:
2419 LOG(FATAL) << "Unimplemented compare type " << in_type;
2420 }
2421}
2422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002423void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002425
2426 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2427 locations->SetInAt(0, Location::RequiresFpuRegister());
2428 locations->SetInAt(1,
2429 IsFloatingPointZeroConstant(instruction->InputAt(1))
2430 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2431 : Location::RequiresFpuRegister());
2432 } else {
2433 // Integer cases.
2434 locations->SetInAt(0, Location::RequiresRegister());
2435 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2436 }
2437
Alexandre Rames5319def2014-10-23 10:03:10 +01002438 if (instruction->NeedsMaterialization()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002440 }
2441}
2442
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002443void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002444 if (!instruction->NeedsMaterialization()) {
2445 return;
2446 }
2447
2448 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002449 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002450 IfCondition if_cond = instruction->GetCondition();
2451 Condition arm64_cond = ARM64Condition(if_cond);
Alexandre Rames5319def2014-10-23 10:03:10 +01002452
Roland Levillain7f63c522015-07-13 15:54:55 +00002453 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2454 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2455 if (locations->InAt(1).IsConstant()) {
2456 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2457 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2458 __ Fcmp(lhs, 0.0);
2459 } else {
2460 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2461 }
2462 __ Cset(res, arm64_cond);
2463 if (instruction->IsFPConditionTrueIfNaN()) {
2464 // res = IsUnordered(arm64_cond) ? 1 : res <=> res = IsNotUnordered(arm64_cond) ? res : 1
2465 __ Csel(res, res, Operand(1), vc); // VC for "not unordered".
2466 } else if (instruction->IsFPConditionFalseIfNaN()) {
2467 // res = IsUnordered(arm64_cond) ? 0 : res <=> res = IsNotUnordered(arm64_cond) ? res : 0
2468 __ Csel(res, res, Operand(0), vc); // VC for "not unordered".
2469 }
2470 } else {
2471 // Integer cases.
2472 Register lhs = InputRegisterAt(instruction, 0);
2473 Operand rhs = InputOperandAt(instruction, 1);
2474 __ Cmp(lhs, rhs);
2475 __ Cset(res, arm64_cond);
2476 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002477}
2478
2479#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2480 M(Equal) \
2481 M(NotEqual) \
2482 M(LessThan) \
2483 M(LessThanOrEqual) \
2484 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002485 M(GreaterThanOrEqual) \
2486 M(Below) \
2487 M(BelowOrEqual) \
2488 M(Above) \
2489 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002490#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002491void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2492void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002493FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002494#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002495#undef FOR_EACH_CONDITION_INSTRUCTION
2496
Zheng Xuc6667102015-05-15 16:08:45 +08002497void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2498 DCHECK(instruction->IsDiv() || instruction->IsRem());
2499
2500 LocationSummary* locations = instruction->GetLocations();
2501 Location second = locations->InAt(1);
2502 DCHECK(second.IsConstant());
2503
2504 Register out = OutputRegister(instruction);
2505 Register dividend = InputRegisterAt(instruction, 0);
2506 int64_t imm = Int64FromConstant(second.GetConstant());
2507 DCHECK(imm == 1 || imm == -1);
2508
2509 if (instruction->IsRem()) {
2510 __ Mov(out, 0);
2511 } else {
2512 if (imm == 1) {
2513 __ Mov(out, dividend);
2514 } else {
2515 __ Neg(out, dividend);
2516 }
2517 }
2518}
2519
2520void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2521 DCHECK(instruction->IsDiv() || instruction->IsRem());
2522
2523 LocationSummary* locations = instruction->GetLocations();
2524 Location second = locations->InAt(1);
2525 DCHECK(second.IsConstant());
2526
2527 Register out = OutputRegister(instruction);
2528 Register dividend = InputRegisterAt(instruction, 0);
2529 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002530 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002531 int ctz_imm = CTZ(abs_imm);
2532
2533 UseScratchRegisterScope temps(GetVIXLAssembler());
2534 Register temp = temps.AcquireSameSizeAs(out);
2535
2536 if (instruction->IsDiv()) {
2537 __ Add(temp, dividend, abs_imm - 1);
2538 __ Cmp(dividend, 0);
2539 __ Csel(out, temp, dividend, lt);
2540 if (imm > 0) {
2541 __ Asr(out, out, ctz_imm);
2542 } else {
2543 __ Neg(out, Operand(out, ASR, ctz_imm));
2544 }
2545 } else {
2546 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2547 __ Asr(temp, dividend, bits - 1);
2548 __ Lsr(temp, temp, bits - ctz_imm);
2549 __ Add(out, dividend, temp);
2550 __ And(out, out, abs_imm - 1);
2551 __ Sub(out, out, temp);
2552 }
2553}
2554
2555void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2556 DCHECK(instruction->IsDiv() || instruction->IsRem());
2557
2558 LocationSummary* locations = instruction->GetLocations();
2559 Location second = locations->InAt(1);
2560 DCHECK(second.IsConstant());
2561
2562 Register out = OutputRegister(instruction);
2563 Register dividend = InputRegisterAt(instruction, 0);
2564 int64_t imm = Int64FromConstant(second.GetConstant());
2565
2566 Primitive::Type type = instruction->GetResultType();
2567 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2568
2569 int64_t magic;
2570 int shift;
2571 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2572
2573 UseScratchRegisterScope temps(GetVIXLAssembler());
2574 Register temp = temps.AcquireSameSizeAs(out);
2575
2576 // temp = get_high(dividend * magic)
2577 __ Mov(temp, magic);
2578 if (type == Primitive::kPrimLong) {
2579 __ Smulh(temp, dividend, temp);
2580 } else {
2581 __ Smull(temp.X(), dividend, temp);
2582 __ Lsr(temp.X(), temp.X(), 32);
2583 }
2584
2585 if (imm > 0 && magic < 0) {
2586 __ Add(temp, temp, dividend);
2587 } else if (imm < 0 && magic > 0) {
2588 __ Sub(temp, temp, dividend);
2589 }
2590
2591 if (shift != 0) {
2592 __ Asr(temp, temp, shift);
2593 }
2594
2595 if (instruction->IsDiv()) {
2596 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2597 } else {
2598 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2599 // TODO: Strength reduction for msub.
2600 Register temp_imm = temps.AcquireSameSizeAs(out);
2601 __ Mov(temp_imm, imm);
2602 __ Msub(out, temp, temp_imm, dividend);
2603 }
2604}
2605
2606void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2607 DCHECK(instruction->IsDiv() || instruction->IsRem());
2608 Primitive::Type type = instruction->GetResultType();
2609 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2610
2611 LocationSummary* locations = instruction->GetLocations();
2612 Register out = OutputRegister(instruction);
2613 Location second = locations->InAt(1);
2614
2615 if (second.IsConstant()) {
2616 int64_t imm = Int64FromConstant(second.GetConstant());
2617
2618 if (imm == 0) {
2619 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2620 } else if (imm == 1 || imm == -1) {
2621 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002622 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002623 DivRemByPowerOfTwo(instruction);
2624 } else {
2625 DCHECK(imm <= -2 || imm >= 2);
2626 GenerateDivRemWithAnyConstant(instruction);
2627 }
2628 } else {
2629 Register dividend = InputRegisterAt(instruction, 0);
2630 Register divisor = InputRegisterAt(instruction, 1);
2631 if (instruction->IsDiv()) {
2632 __ Sdiv(out, dividend, divisor);
2633 } else {
2634 UseScratchRegisterScope temps(GetVIXLAssembler());
2635 Register temp = temps.AcquireSameSizeAs(out);
2636 __ Sdiv(temp, dividend, divisor);
2637 __ Msub(out, temp, divisor, dividend);
2638 }
2639 }
2640}
2641
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002642void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2643 LocationSummary* locations =
2644 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2645 switch (div->GetResultType()) {
2646 case Primitive::kPrimInt:
2647 case Primitive::kPrimLong:
2648 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002649 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2651 break;
2652
2653 case Primitive::kPrimFloat:
2654 case Primitive::kPrimDouble:
2655 locations->SetInAt(0, Location::RequiresFpuRegister());
2656 locations->SetInAt(1, Location::RequiresFpuRegister());
2657 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2658 break;
2659
2660 default:
2661 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2662 }
2663}
2664
2665void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2666 Primitive::Type type = div->GetResultType();
2667 switch (type) {
2668 case Primitive::kPrimInt:
2669 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002670 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 break;
2672
2673 case Primitive::kPrimFloat:
2674 case Primitive::kPrimDouble:
2675 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected div type " << type;
2680 }
2681}
2682
Alexandre Rames67555f72014-11-18 10:55:16 +00002683void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002684 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2685 ? LocationSummary::kCallOnSlowPath
2686 : LocationSummary::kNoCall;
2687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002688 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2689 if (instruction->HasUses()) {
2690 locations->SetOut(Location::SameAsFirstInput());
2691 }
2692}
2693
2694void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2695 SlowPathCodeARM64* slow_path =
2696 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2697 codegen_->AddSlowPath(slow_path);
2698 Location value = instruction->GetLocations()->InAt(0);
2699
Alexandre Rames3e69f162014-12-10 10:36:50 +00002700 Primitive::Type type = instruction->GetType();
2701
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002702 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2703 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002704 return;
2705 }
2706
Alexandre Rames67555f72014-11-18 10:55:16 +00002707 if (value.IsConstant()) {
2708 int64_t divisor = Int64ConstantFrom(value);
2709 if (divisor == 0) {
2710 __ B(slow_path->GetEntryLabel());
2711 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002712 // A division by a non-null constant is valid. We don't need to perform
2713 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002714 }
2715 } else {
2716 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2717 }
2718}
2719
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002720void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2723 locations->SetOut(Location::ConstantLocation(constant));
2724}
2725
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002726void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2727 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002728 // Will be generated at use site.
2729}
2730
Alexandre Rames5319def2014-10-23 10:03:10 +01002731void LocationsBuilderARM64::VisitExit(HExit* exit) {
2732 exit->SetLocations(nullptr);
2733}
2734
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002735void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002736}
2737
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002738void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2739 LocationSummary* locations =
2740 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2741 locations->SetOut(Location::ConstantLocation(constant));
2742}
2743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002744void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002745 // Will be generated at use site.
2746}
2747
David Brazdilfc6a86a2015-06-26 10:33:45 +00002748void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002749 DCHECK(!successor->IsExitBlock());
2750 HBasicBlock* block = got->GetBlock();
2751 HInstruction* previous = got->GetPrevious();
2752 HLoopInformation* info = block->GetLoopInformation();
2753
David Brazdil46e2a392015-03-16 17:31:52 +00002754 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002755 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2756 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2757 return;
2758 }
2759 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2760 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2761 }
2762 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002763 __ B(codegen_->GetLabelOf(successor));
2764 }
2765}
2766
David Brazdilfc6a86a2015-06-26 10:33:45 +00002767void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2768 got->SetLocations(nullptr);
2769}
2770
2771void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2772 HandleGoto(got, got->GetSuccessor());
2773}
2774
2775void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2776 try_boundary->SetLocations(nullptr);
2777}
2778
2779void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2780 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2781 if (!successor->IsExitBlock()) {
2782 HandleGoto(try_boundary, successor);
2783 }
2784}
2785
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002786void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002787 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002788 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002789 vixl::Label* false_target) {
2790 // FP branching requires both targets to be explicit. If either of the targets
2791 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2792 vixl::Label fallthrough_target;
2793 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002794
David Brazdil0debae72015-11-12 18:37:00 +00002795 if (true_target == nullptr && false_target == nullptr) {
2796 // Nothing to do. The code always falls through.
2797 return;
2798 } else if (cond->IsIntConstant()) {
2799 // Constant condition, statically compared against 1.
2800 if (cond->AsIntConstant()->IsOne()) {
2801 if (true_target != nullptr) {
2802 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002803 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002804 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002805 DCHECK(cond->AsIntConstant()->IsZero());
2806 if (false_target != nullptr) {
2807 __ B(false_target);
2808 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002809 }
David Brazdil0debae72015-11-12 18:37:00 +00002810 return;
2811 }
2812
2813 // The following code generates these patterns:
2814 // (1) true_target == nullptr && false_target != nullptr
2815 // - opposite condition true => branch to false_target
2816 // (2) true_target != nullptr && false_target == nullptr
2817 // - condition true => branch to true_target
2818 // (3) true_target != nullptr && false_target != nullptr
2819 // - condition true => branch to true_target
2820 // - branch to false_target
2821 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002822 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002823 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002824 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002825 if (true_target == nullptr) {
2826 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2827 } else {
2828 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2829 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002830 } else {
2831 // The condition instruction has not been materialized, use its inputs as
2832 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002833 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002834
David Brazdil0debae72015-11-12 18:37:00 +00002835 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002836 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002837 FPRegister lhs = InputFPRegisterAt(condition, 0);
2838 if (condition->GetLocations()->InAt(1).IsConstant()) {
2839 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2840 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2841 __ Fcmp(lhs, 0.0);
2842 } else {
2843 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2844 }
2845 if (condition->IsFPConditionTrueIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002846 __ B(vs, true_target == nullptr ? &fallthrough_target : true_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002847 } else if (condition->IsFPConditionFalseIfNaN()) {
David Brazdil0debae72015-11-12 18:37:00 +00002848 __ B(vs, false_target == nullptr ? &fallthrough_target : false_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002849 }
David Brazdil0debae72015-11-12 18:37:00 +00002850 if (true_target == nullptr) {
2851 __ B(ARM64Condition(condition->GetOppositeCondition()), false_target);
2852 } else {
2853 __ B(ARM64Condition(condition->GetCondition()), true_target);
2854 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002855 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002856 // Integer cases.
2857 Register lhs = InputRegisterAt(condition, 0);
2858 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002859
2860 Condition arm64_cond;
2861 vixl::Label* non_fallthrough_target;
2862 if (true_target == nullptr) {
2863 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2864 non_fallthrough_target = false_target;
2865 } else {
2866 arm64_cond = ARM64Condition(condition->GetCondition());
2867 non_fallthrough_target = true_target;
2868 }
2869
Roland Levillain7f63c522015-07-13 15:54:55 +00002870 if ((arm64_cond != gt && arm64_cond != le) && rhs.IsImmediate() && (rhs.immediate() == 0)) {
2871 switch (arm64_cond) {
2872 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002873 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002874 break;
2875 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002876 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002877 break;
2878 case lt:
2879 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002880 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002881 break;
2882 case ge:
2883 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002884 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002885 break;
2886 default:
2887 // Without the `static_cast` the compiler throws an error for
2888 // `-Werror=sign-promo`.
2889 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2890 }
2891 } else {
2892 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002893 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002894 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002895 }
2896 }
David Brazdil0debae72015-11-12 18:37:00 +00002897
2898 // If neither branch falls through (case 3), the conditional branch to `true_target`
2899 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2900 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002901 __ B(false_target);
2902 }
David Brazdil0debae72015-11-12 18:37:00 +00002903
2904 if (fallthrough_target.IsLinked()) {
2905 __ Bind(&fallthrough_target);
2906 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002907}
2908
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002909void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002911 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002912 locations->SetInAt(0, Location::RequiresRegister());
2913 }
2914}
2915
2916void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002917 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2918 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2919 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2920 nullptr : codegen_->GetLabelOf(true_successor);
2921 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2922 nullptr : codegen_->GetLabelOf(false_successor);
2923 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002924}
2925
2926void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2927 LocationSummary* locations = new (GetGraph()->GetArena())
2928 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002929 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002930 locations->SetInAt(0, Location::RequiresRegister());
2931 }
2932}
2933
2934void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002935 SlowPathCodeARM64* slow_path =
2936 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002937 GenerateTestAndBranch(deoptimize,
2938 /* condition_input_index */ 0,
2939 slow_path->GetEntryLabel(),
2940 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002941}
2942
David Srbecky0cf44932015-12-09 14:09:59 +00002943void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2944 new (GetGraph()->GetArena()) LocationSummary(info);
2945}
2946
2947void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00002948 if (codegen_->HasStackMapAtCurrentPc()) {
2949 // Ensure that we do not collide with the stack map of the previous instruction.
2950 __ Nop();
2951 }
David Srbecky0cf44932015-12-09 14:09:59 +00002952 codegen_->RecordPcInfo(info, info->GetDexPc());
2953}
2954
Alexandre Rames5319def2014-10-23 10:03:10 +01002955void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002956 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002957}
2958
2959void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002960 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01002961}
2962
2963void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002964 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002965}
2966
2967void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002968 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01002969}
2970
Alexandre Rames67555f72014-11-18 10:55:16 +00002971void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002972 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002973 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2974 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002975 case TypeCheckKind::kExactCheck:
2976 case TypeCheckKind::kAbstractClassCheck:
2977 case TypeCheckKind::kClassHierarchyCheck:
2978 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002979 call_kind =
2980 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002981 break;
2982 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002983 case TypeCheckKind::kUnresolvedCheck:
2984 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002985 call_kind = LocationSummary::kCallOnSlowPath;
2986 break;
2987 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002988
Alexandre Rames67555f72014-11-18 10:55:16 +00002989 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002990 locations->SetInAt(0, Location::RequiresRegister());
2991 locations->SetInAt(1, Location::RequiresRegister());
2992 // The "out" register is used as a temporary, so it overlaps with the inputs.
2993 // Note that TypeCheckSlowPathARM64 uses this register too.
2994 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2995 // When read barriers are enabled, we need a temporary register for
2996 // some cases.
2997 if (kEmitCompilerReadBarrier &&
2998 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2999 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3000 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3001 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003002 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003003}
3004
3005void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
3006 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003007 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003008 Register obj = InputRegisterAt(instruction, 0);
3009 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003010 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003011 Register out = OutputRegister(instruction);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003012 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3013 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3014 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3015 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003016
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003017 vixl::Label done, zero;
3018 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003019
3020 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003021 // Avoid null check if we know `obj` is not null.
3022 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003023 __ Cbz(obj, &zero);
3024 }
3025
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003026 // /* HeapReference<Class> */ out = obj->klass_
3027 __ Ldr(out, HeapOperand(obj.W(), class_offset));
3028 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003029
3030 switch (instruction->GetTypeCheckKind()) {
3031 case TypeCheckKind::kExactCheck: {
3032 __ Cmp(out, cls);
3033 __ Cset(out, eq);
3034 if (zero.IsLinked()) {
3035 __ B(&done);
3036 }
3037 break;
3038 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003039
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003040 case TypeCheckKind::kAbstractClassCheck: {
3041 // If the class is abstract, we eagerly fetch the super class of the
3042 // object to avoid doing a comparison we know will fail.
3043 vixl::Label loop, success;
3044 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003045 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3046 if (kEmitCompilerReadBarrier) {
3047 // Save the value of `out` into `temp` before overwriting it
3048 // in the following move operation, as we will need it for the
3049 // read barrier below.
3050 Register temp = WRegisterFrom(temp_loc);
3051 __ Mov(temp, out);
3052 }
3053 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003054 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003055 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003056 // If `out` is null, we use it for the result, and jump to `done`.
3057 __ Cbz(out, &done);
3058 __ Cmp(out, cls);
3059 __ B(ne, &loop);
3060 __ Mov(out, 1);
3061 if (zero.IsLinked()) {
3062 __ B(&done);
3063 }
3064 break;
3065 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003066
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003067 case TypeCheckKind::kClassHierarchyCheck: {
3068 // Walk over the class hierarchy to find a match.
3069 vixl::Label loop, success;
3070 __ Bind(&loop);
3071 __ Cmp(out, cls);
3072 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003073 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3074 if (kEmitCompilerReadBarrier) {
3075 // Save the value of `out` into `temp` before overwriting it
3076 // in the following move operation, as we will need it for the
3077 // read barrier below.
3078 Register temp = WRegisterFrom(temp_loc);
3079 __ Mov(temp, out);
3080 }
3081 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003082 __ Ldr(out, HeapOperand(out, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003083 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003084 __ Cbnz(out, &loop);
3085 // If `out` is null, we use it for the result, and jump to `done`.
3086 __ B(&done);
3087 __ Bind(&success);
3088 __ Mov(out, 1);
3089 if (zero.IsLinked()) {
3090 __ B(&done);
3091 }
3092 break;
3093 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003094
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003095 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003096 // Do an exact check.
3097 vixl::Label exact_check;
3098 __ Cmp(out, cls);
3099 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003100 // Otherwise, we need to check that the object's class is a non-primitive array.
3101 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
3102 if (kEmitCompilerReadBarrier) {
3103 // Save the value of `out` into `temp` before overwriting it
3104 // in the following move operation, as we will need it for the
3105 // read barrier below.
3106 Register temp = WRegisterFrom(temp_loc);
3107 __ Mov(temp, out);
3108 }
3109 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003110 __ Ldr(out, HeapOperand(out, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003111 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003112 // If `out` is null, we use it for the result, and jump to `done`.
3113 __ Cbz(out, &done);
3114 __ Ldrh(out, HeapOperand(out, primitive_offset));
3115 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3116 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003117 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003118 __ Mov(out, 1);
3119 __ B(&done);
3120 break;
3121 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003122
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 case TypeCheckKind::kArrayCheck: {
3124 __ Cmp(out, cls);
3125 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3127 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003128 codegen_->AddSlowPath(slow_path);
3129 __ B(ne, slow_path->GetEntryLabel());
3130 __ Mov(out, 1);
3131 if (zero.IsLinked()) {
3132 __ B(&done);
3133 }
3134 break;
3135 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003136
Calin Juravle98893e12015-10-02 21:05:03 +01003137 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003138 case TypeCheckKind::kInterfaceCheck: {
3139 // Note that we indeed only call on slow path, but we always go
3140 // into the slow path for the unresolved and interface check
3141 // cases.
3142 //
3143 // We cannot directly call the InstanceofNonTrivial runtime
3144 // entry point without resorting to a type checking slow path
3145 // here (i.e. by calling InvokeRuntime directly), as it would
3146 // require to assign fixed registers for the inputs of this
3147 // HInstanceOf instruction (following the runtime calling
3148 // convention), which might be cluttered by the potential first
3149 // read barrier emission at the beginning of this method.
3150 DCHECK(locations->OnlyCallsOnSlowPath());
3151 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3152 /* is_fatal */ false);
3153 codegen_->AddSlowPath(slow_path);
3154 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003155 if (zero.IsLinked()) {
3156 __ B(&done);
3157 }
3158 break;
3159 }
3160 }
3161
3162 if (zero.IsLinked()) {
3163 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003164 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165 }
3166
3167 if (done.IsLinked()) {
3168 __ Bind(&done);
3169 }
3170
3171 if (slow_path != nullptr) {
3172 __ Bind(slow_path->GetExitLabel());
3173 }
3174}
3175
3176void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3177 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3178 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3179
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003180 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3181 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003182 case TypeCheckKind::kExactCheck:
3183 case TypeCheckKind::kAbstractClassCheck:
3184 case TypeCheckKind::kClassHierarchyCheck:
3185 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003186 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3187 LocationSummary::kCallOnSlowPath :
3188 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003189 break;
3190 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003191 case TypeCheckKind::kUnresolvedCheck:
3192 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003193 call_kind = LocationSummary::kCallOnSlowPath;
3194 break;
3195 }
3196
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3198 locations->SetInAt(0, Location::RequiresRegister());
3199 locations->SetInAt(1, Location::RequiresRegister());
3200 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3201 locations->AddTemp(Location::RequiresRegister());
3202 locations->AddTemp(Location::RequiresRegister());
3203 // When read barriers are enabled, we need an additional temporary
3204 // register for some cases.
3205 if (kEmitCompilerReadBarrier &&
3206 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3207 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3208 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3209 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003210 }
3211}
3212
3213void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
3214 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003215 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003216 Register obj = InputRegisterAt(instruction, 0);
3217 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003218 Location temp_loc = locations->GetTemp(0);
3219 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003220 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3221 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3222 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3223 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003224
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003225 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3226 bool is_type_check_slow_path_fatal =
3227 (type_check_kind == TypeCheckKind::kExactCheck ||
3228 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3229 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3230 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3231 !instruction->CanThrowIntoCatchBlock();
3232 SlowPathCodeARM64* type_check_slow_path =
3233 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3234 is_type_check_slow_path_fatal);
3235 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003236
3237 vixl::Label done;
3238 // Avoid null check if we know obj is not null.
3239 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003240 __ Cbz(obj, &done);
3241 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003242
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003243 // /* HeapReference<Class> */ temp = obj->klass_
3244 __ Ldr(temp, HeapOperand(obj, class_offset));
3245 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003246
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003248 case TypeCheckKind::kExactCheck:
3249 case TypeCheckKind::kArrayCheck: {
3250 __ Cmp(temp, cls);
3251 // Jump to slow path for throwing the exception or doing a
3252 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003253 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003254 break;
3255 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003256
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003257 case TypeCheckKind::kAbstractClassCheck: {
3258 // If the class is abstract, we eagerly fetch the super class of the
3259 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003260 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003262 Location temp2_loc =
3263 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3264 if (kEmitCompilerReadBarrier) {
3265 // Save the value of `temp` into `temp2` before overwriting it
3266 // in the following move operation, as we will need it for the
3267 // read barrier below.
3268 Register temp2 = WRegisterFrom(temp2_loc);
3269 __ Mov(temp2, temp);
3270 }
3271 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003273 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3274
3275 // If the class reference currently in `temp` is not null, jump
3276 // to the `compare_classes` label to compare it with the checked
3277 // class.
3278 __ Cbnz(temp, &compare_classes);
3279 // Otherwise, jump to the slow path to throw the exception.
3280 //
3281 // But before, move back the object's class into `temp` before
3282 // going into the slow path, as it has been overwritten in the
3283 // meantime.
3284 // /* HeapReference<Class> */ temp = obj->klass_
3285 __ Ldr(temp, HeapOperand(obj, class_offset));
3286 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3287 __ B(type_check_slow_path->GetEntryLabel());
3288
3289 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003290 __ Cmp(temp, cls);
3291 __ B(ne, &loop);
3292 break;
3293 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003294
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003295 case TypeCheckKind::kClassHierarchyCheck: {
3296 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003297 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003298 __ Bind(&loop);
3299 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003300 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003301
3302 Location temp2_loc =
3303 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3304 if (kEmitCompilerReadBarrier) {
3305 // Save the value of `temp` into `temp2` before overwriting it
3306 // in the following move operation, as we will need it for the
3307 // read barrier below.
3308 Register temp2 = WRegisterFrom(temp2_loc);
3309 __ Mov(temp2, temp);
3310 }
3311 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003312 __ Ldr(temp, HeapOperand(temp, super_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003313 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
3314
3315 // If the class reference currently in `temp` is not null, jump
3316 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003317 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003318 // Otherwise, jump to the slow path to throw the exception.
3319 //
3320 // But before, move back the object's class into `temp` before
3321 // going into the slow path, as it has been overwritten in the
3322 // meantime.
3323 // /* HeapReference<Class> */ temp = obj->klass_
3324 __ Ldr(temp, HeapOperand(obj, class_offset));
3325 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3326 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003327 break;
3328 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003330 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003331 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003332 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003333 __ Cmp(temp, cls);
3334 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003335
3336 // Otherwise, we need to check that the object's class is a non-primitive array.
3337 Location temp2_loc =
3338 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
3339 if (kEmitCompilerReadBarrier) {
3340 // Save the value of `temp` into `temp2` before overwriting it
3341 // in the following move operation, as we will need it for the
3342 // read barrier below.
3343 Register temp2 = WRegisterFrom(temp2_loc);
3344 __ Mov(temp2, temp);
3345 }
3346 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003347 __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003348 codegen_->MaybeGenerateReadBarrier(
3349 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
3350
3351 // If the component type is not null (i.e. the object is indeed
3352 // an array), jump to label `check_non_primitive_component_type`
3353 // to further check that this component type is not a primitive
3354 // type.
3355 __ Cbnz(temp, &check_non_primitive_component_type);
3356 // Otherwise, jump to the slow path to throw the exception.
3357 //
3358 // But before, move back the object's class into `temp` before
3359 // going into the slow path, as it has been overwritten in the
3360 // meantime.
3361 // /* HeapReference<Class> */ temp = obj->klass_
3362 __ Ldr(temp, HeapOperand(obj, class_offset));
3363 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3364 __ B(type_check_slow_path->GetEntryLabel());
3365
3366 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003367 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3368 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 __ Cbz(temp, &done);
3370 // Same comment as above regarding `temp` and the slow path.
3371 // /* HeapReference<Class> */ temp = obj->klass_
3372 __ Ldr(temp, HeapOperand(obj, class_offset));
3373 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
3374 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003375 break;
3376 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003377
Calin Juravle98893e12015-10-02 21:05:03 +01003378 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003379 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003380 // We always go into the type check slow path for the unresolved
3381 // and interface check cases.
3382 //
3383 // We cannot directly call the CheckCast runtime entry point
3384 // without resorting to a type checking slow path here (i.e. by
3385 // calling InvokeRuntime directly), as it would require to
3386 // assign fixed registers for the inputs of this HInstanceOf
3387 // instruction (following the runtime calling convention), which
3388 // might be cluttered by the potential first read barrier
3389 // emission at the beginning of this method.
3390 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003391 break;
3392 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003393 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003394
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003396}
3397
Alexandre Rames5319def2014-10-23 10:03:10 +01003398void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3400 locations->SetOut(Location::ConstantLocation(constant));
3401}
3402
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003403void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003404 // Will be generated at use site.
3405}
3406
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003407void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3408 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3409 locations->SetOut(Location::ConstantLocation(constant));
3410}
3411
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003412void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003413 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003414}
3415
Calin Juravle175dc732015-08-25 15:42:32 +01003416void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3417 // The trampoline uses the same calling convention as dex calling conventions,
3418 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3419 // the method_idx.
3420 HandleInvoke(invoke);
3421}
3422
3423void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3424 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3425}
3426
Alexandre Rames5319def2014-10-23 10:03:10 +01003427void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003428 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003429 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003430}
3431
Alexandre Rames67555f72014-11-18 10:55:16 +00003432void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3433 HandleInvoke(invoke);
3434}
3435
3436void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3437 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438 LocationSummary* locations = invoke->GetLocations();
3439 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003440 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3441 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003442 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003443 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003444 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003445
3446 // The register ip1 is required to be used for the hidden argument in
3447 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003448 MacroAssembler* masm = GetVIXLAssembler();
3449 UseScratchRegisterScope scratch_scope(masm);
3450 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003451 scratch_scope.Exclude(ip1);
3452 __ Mov(ip1, invoke->GetDexMethodIndex());
3453
Alexandre Rames67555f72014-11-18 10:55:16 +00003454 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003455 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003456 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003457 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003458 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003459 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003460 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003461 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003462 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003463 // Instead of simply (possibly) unpoisoning `temp` here, we should
3464 // emit a read barrier for the previous class reference load.
3465 // However this is not required in practice, as this is an
3466 // intermediate/temporary reference and because the current
3467 // concurrent copying collector keeps the from-space memory
3468 // intact/accessible until the end of the marking phase (the
3469 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003470 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003471 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003472 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003473 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003474 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003475 // lr();
3476 __ Blr(lr);
3477 DCHECK(!codegen_->IsLeafMethod());
3478 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3479}
3480
3481void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003482 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3483 if (intrinsic.TryDispatch(invoke)) {
3484 return;
3485 }
3486
Alexandre Rames67555f72014-11-18 10:55:16 +00003487 HandleInvoke(invoke);
3488}
3489
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003490void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003491 // When we do not run baseline, explicit clinit checks triggered by static
3492 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3493 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003494
Andreas Gampe878d58c2015-01-15 23:24:00 -08003495 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3496 if (intrinsic.TryDispatch(invoke)) {
3497 return;
3498 }
3499
Alexandre Rames67555f72014-11-18 10:55:16 +00003500 HandleInvoke(invoke);
3501}
3502
Andreas Gampe878d58c2015-01-15 23:24:00 -08003503static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3504 if (invoke->GetLocations()->Intrinsified()) {
3505 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3506 intrinsic.Dispatch(invoke);
3507 return true;
3508 }
3509 return false;
3510}
3511
Vladimir Markodc151b22015-10-15 18:02:30 +01003512HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3513 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3514 MethodReference target_method ATTRIBUTE_UNUSED) {
3515 // On arm64 we support all dispatch types.
3516 return desired_dispatch_info;
3517}
3518
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003519void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003520 // For better instruction scheduling we load the direct code pointer before the method pointer.
3521 bool direct_code_loaded = false;
3522 switch (invoke->GetCodePtrLocation()) {
3523 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3524 // LR = code address from literal pool with link-time patch.
3525 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3526 direct_code_loaded = true;
3527 break;
3528 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3529 // LR = invoke->GetDirectCodePtr();
3530 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3531 direct_code_loaded = true;
3532 break;
3533 default:
3534 break;
3535 }
3536
Andreas Gampe878d58c2015-01-15 23:24:00 -08003537 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003538 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3539 switch (invoke->GetMethodLoadKind()) {
3540 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3541 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003542 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003543 break;
3544 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003545 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003546 break;
3547 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3548 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003549 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003550 break;
3551 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3552 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003553 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003554 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3555 break;
3556 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3557 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003558 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3559 invoke->GetDexCacheArrayOffset());
3560 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003561 {
3562 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003563 __ Bind(pc_insn_label);
3564 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003565 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003566 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003567 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003568 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3569 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003570 {
3571 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3572 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3573 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3574 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3575 }
Vladimir Marko58155012015-08-19 12:49:41 +00003576 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003577 }
Vladimir Marko58155012015-08-19 12:49:41 +00003578 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003579 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003580 Register reg = XRegisterFrom(temp);
3581 Register method_reg;
3582 if (current_method.IsRegister()) {
3583 method_reg = XRegisterFrom(current_method);
3584 } else {
3585 DCHECK(invoke->GetLocations()->Intrinsified());
3586 DCHECK(!current_method.IsValid());
3587 method_reg = reg;
3588 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3589 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003590
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003591 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003592 __ Ldr(reg.X(),
3593 MemOperand(method_reg.X(),
3594 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003595 // temp = temp[index_in_cache];
3596 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3597 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3598 break;
3599 }
3600 }
3601
3602 switch (invoke->GetCodePtrLocation()) {
3603 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3604 __ Bl(&frame_entry_label_);
3605 break;
3606 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3607 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3608 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003609 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3610 __ Bind(label);
3611 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003612 break;
3613 }
3614 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3615 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3616 // LR prepared above for better instruction scheduling.
3617 DCHECK(direct_code_loaded);
3618 // lr()
3619 __ Blr(lr);
3620 break;
3621 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3622 // LR = callee_method->entry_point_from_quick_compiled_code_;
3623 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003624 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003625 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3626 // lr()
3627 __ Blr(lr);
3628 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003629 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003630
Andreas Gampe878d58c2015-01-15 23:24:00 -08003631 DCHECK(!IsLeafMethod());
3632}
3633
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003634void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003635 // Use the calling convention instead of the location of the receiver, as
3636 // intrinsics may have put the receiver in a different register. In the intrinsics
3637 // slow path, the arguments have been moved to the right place, so here we are
3638 // guaranteed that the receiver is the first register of the calling convention.
3639 InvokeDexCallingConvention calling_convention;
3640 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003641 Register temp = XRegisterFrom(temp_in);
3642 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3643 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3644 Offset class_offset = mirror::Object::ClassOffset();
3645 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3646
3647 BlockPoolsScope block_pools(GetVIXLAssembler());
3648
3649 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003650 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003651 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003652 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003653 // Instead of simply (possibly) unpoisoning `temp` here, we should
3654 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003655 // intermediate/temporary reference and because the current
3656 // concurrent copying collector keeps the from-space memory
3657 // intact/accessible until the end of the marking phase (the
3658 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003659 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3660 // temp = temp->GetMethodAt(method_offset);
3661 __ Ldr(temp, MemOperand(temp, method_offset));
3662 // lr = temp->GetEntryPoint();
3663 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3664 // lr();
3665 __ Blr(lr);
3666}
3667
Vladimir Marko58155012015-08-19 12:49:41 +00003668void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3669 DCHECK(linker_patches->empty());
3670 size_t size =
3671 method_patches_.size() +
3672 call_patches_.size() +
3673 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003674 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003675 linker_patches->reserve(size);
3676 for (const auto& entry : method_patches_) {
3677 const MethodReference& target_method = entry.first;
3678 vixl::Literal<uint64_t>* literal = entry.second;
3679 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3680 target_method.dex_file,
3681 target_method.dex_method_index));
3682 }
3683 for (const auto& entry : call_patches_) {
3684 const MethodReference& target_method = entry.first;
3685 vixl::Literal<uint64_t>* literal = entry.second;
3686 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3687 target_method.dex_file,
3688 target_method.dex_method_index));
3689 }
3690 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003691 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003692 info.target_method.dex_file,
3693 info.target_method.dex_method_index));
3694 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003695 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003696 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003697 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003698 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003699 info.element_offset));
3700 }
3701}
3702
3703vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3704 // Look up the literal for value.
3705 auto lb = uint64_literals_.lower_bound(value);
3706 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3707 return lb->second;
3708 }
3709 // We don't have a literal for this value, insert a new one.
3710 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3711 uint64_literals_.PutBefore(lb, value, literal);
3712 return literal;
3713}
3714
3715vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3716 MethodReference target_method,
3717 MethodToLiteralMap* map) {
3718 // Look up the literal for target_method.
3719 auto lb = map->lower_bound(target_method);
3720 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3721 return lb->second;
3722 }
3723 // We don't have a literal for this method yet, insert a new one.
3724 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3725 map->PutBefore(lb, target_method, literal);
3726 return literal;
3727}
3728
3729vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3730 MethodReference target_method) {
3731 return DeduplicateMethodLiteral(target_method, &method_patches_);
3732}
3733
3734vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3735 MethodReference target_method) {
3736 return DeduplicateMethodLiteral(target_method, &call_patches_);
3737}
3738
3739
Andreas Gampe878d58c2015-01-15 23:24:00 -08003740void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01003741 // When we do not run baseline, explicit clinit checks triggered by static
3742 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3743 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003744
Andreas Gampe878d58c2015-01-15 23:24:00 -08003745 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3746 return;
3747 }
3748
Alexandre Ramesd921d642015-04-16 15:07:16 +01003749 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003750 LocationSummary* locations = invoke->GetLocations();
3751 codegen_->GenerateStaticOrDirectCall(
3752 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003753 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003754}
3755
3756void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003757 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3758 return;
3759 }
3760
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003761 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003762 DCHECK(!codegen_->IsLeafMethod());
3763 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3764}
3765
Alexandre Rames67555f72014-11-18 10:55:16 +00003766void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003767 InvokeRuntimeCallingConvention calling_convention;
3768 CodeGenerator::CreateLoadClassLocationSummary(
3769 cls,
3770 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003771 LocationFrom(vixl::x0),
3772 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003773}
3774
3775void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003776 if (cls->NeedsAccessCheck()) {
3777 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3778 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3779 cls,
3780 cls->GetDexPc(),
3781 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003782 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003783 return;
3784 }
3785
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003786 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003787 Register out = OutputRegister(cls);
3788 Register current_method = InputRegisterAt(cls, 0);
3789 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003790 DCHECK(!cls->CanCallRuntime());
3791 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003792 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3793 if (kEmitCompilerReadBarrier) {
3794 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3795 __ Add(out.X(), current_method.X(), declaring_class_offset);
3796 // /* mirror::Class* */ out = out->Read()
3797 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3798 } else {
3799 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3800 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3801 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003802 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003803 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003804 // /* GcRoot<mirror::Class>[] */ out =
3805 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003806 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003807
3808 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
3809 if (kEmitCompilerReadBarrier) {
3810 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
3811 __ Add(out.X(), out.X(), cache_offset);
3812 // /* mirror::Class* */ out = out->Read()
3813 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
3814 } else {
3815 // /* GcRoot<mirror::Class> */ out = out[type_index]
3816 __ Ldr(out, MemOperand(out.X(), cache_offset));
3817 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003818
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003819 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3820 DCHECK(cls->CanCallRuntime());
3821 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3822 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3823 codegen_->AddSlowPath(slow_path);
3824 if (!cls->IsInDexCache()) {
3825 __ Cbz(out, slow_path->GetEntryLabel());
3826 }
3827 if (cls->MustGenerateClinitCheck()) {
3828 GenerateClassInitializationCheck(slow_path, out);
3829 } else {
3830 __ Bind(slow_path->GetExitLabel());
3831 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003832 }
3833 }
3834}
3835
David Brazdilcb1c0552015-08-04 16:22:25 +01003836static MemOperand GetExceptionTlsAddress() {
3837 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3838}
3839
Alexandre Rames67555f72014-11-18 10:55:16 +00003840void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3841 LocationSummary* locations =
3842 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3843 locations->SetOut(Location::RequiresRegister());
3844}
3845
3846void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003847 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3848}
3849
3850void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3851 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3852}
3853
3854void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3855 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003856}
3857
Alexandre Rames5319def2014-10-23 10:03:10 +01003858void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3859 load->SetLocations(nullptr);
3860}
3861
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003862void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003863 // Nothing to do, this is driven by the code generator.
3864}
3865
Alexandre Rames67555f72014-11-18 10:55:16 +00003866void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003867 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3868 ? LocationSummary::kCallOnSlowPath
3869 : LocationSummary::kNoCall;
3870 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003871 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003872 locations->SetOut(Location::RequiresRegister());
3873}
3874
3875void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003876 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003877 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003878 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003879
3880 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
3881 if (kEmitCompilerReadBarrier) {
3882 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
3883 __ Add(out.X(), current_method.X(), declaring_class_offset);
3884 // /* mirror::Class* */ out = out->Read()
3885 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3886 } else {
3887 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3888 __ Ldr(out, MemOperand(current_method, declaring_class_offset));
3889 }
3890
3891 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3892 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
3893
3894 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
3895 if (kEmitCompilerReadBarrier) {
3896 // /* GcRoot<mirror::String>* */ out = &out[string_index]
3897 __ Add(out.X(), out.X(), cache_offset);
3898 // /* mirror::String* */ out = out->Read()
3899 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
3900 } else {
3901 // /* GcRoot<mirror::String> */ out = out[string_index]
3902 __ Ldr(out, MemOperand(out.X(), cache_offset));
3903 }
3904
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003905 if (!load->IsInDexCache()) {
3906 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3907 codegen_->AddSlowPath(slow_path);
3908 __ Cbz(out, slow_path->GetEntryLabel());
3909 __ Bind(slow_path->GetExitLabel());
3910 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003911}
3912
Alexandre Rames5319def2014-10-23 10:03:10 +01003913void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3914 local->SetLocations(nullptr);
3915}
3916
3917void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3918 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3919}
3920
3921void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3922 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3923 locations->SetOut(Location::ConstantLocation(constant));
3924}
3925
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003926void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003927 // Will be generated at use site.
3928}
3929
Alexandre Rames67555f72014-11-18 10:55:16 +00003930void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3931 LocationSummary* locations =
3932 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3933 InvokeRuntimeCallingConvention calling_convention;
3934 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3935}
3936
3937void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3938 codegen_->InvokeRuntime(instruction->IsEnter()
3939 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3940 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003941 instruction->GetDexPc(),
3942 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003943 if (instruction->IsEnter()) {
3944 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3945 } else {
3946 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3947 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003948}
3949
Alexandre Rames42d641b2014-10-27 14:00:51 +00003950void LocationsBuilderARM64::VisitMul(HMul* mul) {
3951 LocationSummary* locations =
3952 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3953 switch (mul->GetResultType()) {
3954 case Primitive::kPrimInt:
3955 case Primitive::kPrimLong:
3956 locations->SetInAt(0, Location::RequiresRegister());
3957 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003959 break;
3960
3961 case Primitive::kPrimFloat:
3962 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003963 locations->SetInAt(0, Location::RequiresFpuRegister());
3964 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003965 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003966 break;
3967
3968 default:
3969 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3970 }
3971}
3972
3973void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3974 switch (mul->GetResultType()) {
3975 case Primitive::kPrimInt:
3976 case Primitive::kPrimLong:
3977 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3978 break;
3979
3980 case Primitive::kPrimFloat:
3981 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003982 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003983 break;
3984
3985 default:
3986 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3987 }
3988}
3989
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003990void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3991 LocationSummary* locations =
3992 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3993 switch (neg->GetResultType()) {
3994 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003995 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003996 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003997 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003998 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003999
4000 case Primitive::kPrimFloat:
4001 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004002 locations->SetInAt(0, Location::RequiresFpuRegister());
4003 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004004 break;
4005
4006 default:
4007 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4008 }
4009}
4010
4011void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4012 switch (neg->GetResultType()) {
4013 case Primitive::kPrimInt:
4014 case Primitive::kPrimLong:
4015 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4016 break;
4017
4018 case Primitive::kPrimFloat:
4019 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004020 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004021 break;
4022
4023 default:
4024 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4025 }
4026}
4027
4028void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4031 InvokeRuntimeCallingConvention calling_convention;
4032 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004033 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004034 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004035 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004036}
4037
4038void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4039 LocationSummary* locations = instruction->GetLocations();
4040 InvokeRuntimeCallingConvention calling_convention;
4041 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4042 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004043 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004044 // Note: if heap poisoning is enabled, the entry point takes cares
4045 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004046 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4047 instruction,
4048 instruction->GetDexPc(),
4049 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004050 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004051}
4052
Alexandre Rames5319def2014-10-23 10:03:10 +01004053void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4054 LocationSummary* locations =
4055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4056 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004057 if (instruction->IsStringAlloc()) {
4058 locations->AddTemp(LocationFrom(kArtMethodRegister));
4059 } else {
4060 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4061 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4062 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004063 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4064}
4065
4066void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004067 // Note: if heap poisoning is enabled, the entry point takes cares
4068 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004069 if (instruction->IsStringAlloc()) {
4070 // String is allocated through StringFactory. Call NewEmptyString entry point.
4071 Location temp = instruction->GetLocations()->GetTemp(0);
4072 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4073 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4074 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4075 __ Blr(lr);
4076 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4077 } else {
4078 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4079 instruction,
4080 instruction->GetDexPc(),
4081 nullptr);
4082 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4083 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004084}
4085
4086void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004088 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004090}
4091
4092void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004093 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004094 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004095 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004096 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004097 break;
4098
4099 default:
4100 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4101 }
4102}
4103
David Brazdil66d126e2015-04-03 16:02:44 +01004104void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4106 locations->SetInAt(0, Location::RequiresRegister());
4107 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4108}
4109
4110void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004111 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4112}
4113
Alexandre Rames5319def2014-10-23 10:03:10 +01004114void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004115 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4116 ? LocationSummary::kCallOnSlowPath
4117 : LocationSummary::kNoCall;
4118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004119 locations->SetInAt(0, Location::RequiresRegister());
4120 if (instruction->HasUses()) {
4121 locations->SetOut(Location::SameAsFirstInput());
4122 }
4123}
4124
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004125void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4127 return;
4128 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004129
Alexandre Ramesd921d642015-04-16 15:07:16 +01004130 BlockPoolsScope block_pools(GetVIXLAssembler());
4131 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004132 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4133 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4134}
4135
4136void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004137 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4138 codegen_->AddSlowPath(slow_path);
4139
4140 LocationSummary* locations = instruction->GetLocations();
4141 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004142
4143 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004144}
4145
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004146void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004147 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004148 GenerateImplicitNullCheck(instruction);
4149 } else {
4150 GenerateExplicitNullCheck(instruction);
4151 }
4152}
4153
Alexandre Rames67555f72014-11-18 10:55:16 +00004154void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4155 HandleBinaryOp(instruction);
4156}
4157
4158void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4159 HandleBinaryOp(instruction);
4160}
4161
Alexandre Rames3e69f162014-12-10 10:36:50 +00004162void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4163 LOG(FATAL) << "Unreachable";
4164}
4165
4166void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4167 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4168}
4169
Alexandre Rames5319def2014-10-23 10:03:10 +01004170void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4172 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4173 if (location.IsStackSlot()) {
4174 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4175 } else if (location.IsDoubleStackSlot()) {
4176 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4177 }
4178 locations->SetOut(location);
4179}
4180
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004181void InstructionCodeGeneratorARM64::VisitParameterValue(
4182 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004183 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004184}
4185
4186void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4187 LocationSummary* locations =
4188 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004189 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004190}
4191
4192void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4193 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4194 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004195}
4196
4197void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4199 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4200 locations->SetInAt(i, Location::Any());
4201 }
4202 locations->SetOut(Location::Any());
4203}
4204
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004205void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004206 LOG(FATAL) << "Unreachable";
4207}
4208
Serban Constantinescu02164b32014-11-13 14:05:07 +00004209void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004210 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004211 LocationSummary::CallKind call_kind =
4212 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4214
4215 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004216 case Primitive::kPrimInt:
4217 case Primitive::kPrimLong:
4218 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004219 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4221 break;
4222
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004223 case Primitive::kPrimFloat:
4224 case Primitive::kPrimDouble: {
4225 InvokeRuntimeCallingConvention calling_convention;
4226 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4227 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4228 locations->SetOut(calling_convention.GetReturnLocation(type));
4229
4230 break;
4231 }
4232
Serban Constantinescu02164b32014-11-13 14:05:07 +00004233 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004234 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004235 }
4236}
4237
4238void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4239 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004240
Serban Constantinescu02164b32014-11-13 14:05:07 +00004241 switch (type) {
4242 case Primitive::kPrimInt:
4243 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004244 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004245 break;
4246 }
4247
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004248 case Primitive::kPrimFloat:
4249 case Primitive::kPrimDouble: {
4250 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4251 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004252 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004253 if (type == Primitive::kPrimFloat) {
4254 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4255 } else {
4256 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4257 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004258 break;
4259 }
4260
Serban Constantinescu02164b32014-11-13 14:05:07 +00004261 default:
4262 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004263 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004264 }
4265}
4266
Calin Juravle27df7582015-04-17 19:12:31 +01004267void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4268 memory_barrier->SetLocations(nullptr);
4269}
4270
4271void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4272 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4273}
4274
Alexandre Rames5319def2014-10-23 10:03:10 +01004275void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4277 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004278 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004279}
4280
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004281void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004282 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004283}
4284
4285void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4286 instruction->SetLocations(nullptr);
4287}
4288
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004289void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004290 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004291}
4292
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004293void LocationsBuilderARM64::VisitRor(HRor* ror) {
4294 HandleBinaryOp(ror);
4295}
4296
4297void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4298 HandleBinaryOp(ror);
4299}
4300
Serban Constantinescu02164b32014-11-13 14:05:07 +00004301void LocationsBuilderARM64::VisitShl(HShl* shl) {
4302 HandleShift(shl);
4303}
4304
4305void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4306 HandleShift(shl);
4307}
4308
4309void LocationsBuilderARM64::VisitShr(HShr* shr) {
4310 HandleShift(shr);
4311}
4312
4313void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4314 HandleShift(shr);
4315}
4316
Alexandre Rames5319def2014-10-23 10:03:10 +01004317void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4319 Primitive::Type field_type = store->InputAt(1)->GetType();
4320 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004321 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004322 case Primitive::kPrimBoolean:
4323 case Primitive::kPrimByte:
4324 case Primitive::kPrimChar:
4325 case Primitive::kPrimShort:
4326 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004327 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004328 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4329 break;
4330
4331 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004332 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004333 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4334 break;
4335
4336 default:
4337 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004338 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004339 }
4340}
4341
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004342void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004343}
4344
4345void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004346 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004347}
4348
4349void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004350 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004351}
4352
Alexandre Rames67555f72014-11-18 10:55:16 +00004353void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004354 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004355}
4356
4357void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004358 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004359}
4360
4361void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004362 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004363}
4364
Alexandre Rames67555f72014-11-18 10:55:16 +00004365void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004366 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004367}
4368
Calin Juravlee460d1d2015-09-29 04:52:17 +01004369void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4370 HUnresolvedInstanceFieldGet* instruction) {
4371 FieldAccessCallingConventionARM64 calling_convention;
4372 codegen_->CreateUnresolvedFieldLocationSummary(
4373 instruction, instruction->GetFieldType(), calling_convention);
4374}
4375
4376void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4377 HUnresolvedInstanceFieldGet* instruction) {
4378 FieldAccessCallingConventionARM64 calling_convention;
4379 codegen_->GenerateUnresolvedFieldAccess(instruction,
4380 instruction->GetFieldType(),
4381 instruction->GetFieldIndex(),
4382 instruction->GetDexPc(),
4383 calling_convention);
4384}
4385
4386void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4387 HUnresolvedInstanceFieldSet* instruction) {
4388 FieldAccessCallingConventionARM64 calling_convention;
4389 codegen_->CreateUnresolvedFieldLocationSummary(
4390 instruction, instruction->GetFieldType(), calling_convention);
4391}
4392
4393void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4394 HUnresolvedInstanceFieldSet* instruction) {
4395 FieldAccessCallingConventionARM64 calling_convention;
4396 codegen_->GenerateUnresolvedFieldAccess(instruction,
4397 instruction->GetFieldType(),
4398 instruction->GetFieldIndex(),
4399 instruction->GetDexPc(),
4400 calling_convention);
4401}
4402
4403void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4404 HUnresolvedStaticFieldGet* instruction) {
4405 FieldAccessCallingConventionARM64 calling_convention;
4406 codegen_->CreateUnresolvedFieldLocationSummary(
4407 instruction, instruction->GetFieldType(), calling_convention);
4408}
4409
4410void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4411 HUnresolvedStaticFieldGet* instruction) {
4412 FieldAccessCallingConventionARM64 calling_convention;
4413 codegen_->GenerateUnresolvedFieldAccess(instruction,
4414 instruction->GetFieldType(),
4415 instruction->GetFieldIndex(),
4416 instruction->GetDexPc(),
4417 calling_convention);
4418}
4419
4420void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4421 HUnresolvedStaticFieldSet* instruction) {
4422 FieldAccessCallingConventionARM64 calling_convention;
4423 codegen_->CreateUnresolvedFieldLocationSummary(
4424 instruction, instruction->GetFieldType(), calling_convention);
4425}
4426
4427void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4428 HUnresolvedStaticFieldSet* instruction) {
4429 FieldAccessCallingConventionARM64 calling_convention;
4430 codegen_->GenerateUnresolvedFieldAccess(instruction,
4431 instruction->GetFieldType(),
4432 instruction->GetFieldIndex(),
4433 instruction->GetDexPc(),
4434 calling_convention);
4435}
4436
Alexandre Rames5319def2014-10-23 10:03:10 +01004437void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4438 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4439}
4440
4441void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004442 HBasicBlock* block = instruction->GetBlock();
4443 if (block->GetLoopInformation() != nullptr) {
4444 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4445 // The back edge will generate the suspend check.
4446 return;
4447 }
4448 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4449 // The goto will generate the suspend check.
4450 return;
4451 }
4452 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004453}
4454
4455void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4456 temp->SetLocations(nullptr);
4457}
4458
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004459void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004460 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004461}
4462
Alexandre Rames67555f72014-11-18 10:55:16 +00004463void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4464 LocationSummary* locations =
4465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4466 InvokeRuntimeCallingConvention calling_convention;
4467 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4468}
4469
4470void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4471 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004472 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004473 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004474}
4475
4476void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4477 LocationSummary* locations =
4478 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4479 Primitive::Type input_type = conversion->GetInputType();
4480 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004481 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004482 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4483 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4484 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4485 }
4486
Alexandre Rames542361f2015-01-29 16:57:31 +00004487 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004488 locations->SetInAt(0, Location::RequiresFpuRegister());
4489 } else {
4490 locations->SetInAt(0, Location::RequiresRegister());
4491 }
4492
Alexandre Rames542361f2015-01-29 16:57:31 +00004493 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4495 } else {
4496 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4497 }
4498}
4499
4500void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4501 Primitive::Type result_type = conversion->GetResultType();
4502 Primitive::Type input_type = conversion->GetInputType();
4503
4504 DCHECK_NE(input_type, result_type);
4505
Alexandre Rames542361f2015-01-29 16:57:31 +00004506 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004507 int result_size = Primitive::ComponentSize(result_type);
4508 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004509 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004510 Register output = OutputRegister(conversion);
4511 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004512 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004513 // 'int' values are used directly as W registers, discarding the top
4514 // bits, so we don't need to sign-extend and can just perform a move.
4515 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4516 // top 32 bits of the target register. We theoretically could leave those
4517 // bits unchanged, but we would have to make sure that no code uses a
4518 // 32bit input value as a 64bit value assuming that the top 32 bits are
4519 // zero.
4520 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004521 } else if (result_type == Primitive::kPrimChar ||
4522 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4523 __ Ubfx(output,
4524 output.IsX() ? source.X() : source.W(),
4525 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004526 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004527 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004528 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004529 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004530 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004531 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004532 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4533 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004534 } else if (Primitive::IsFloatingPointType(result_type) &&
4535 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004536 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4537 } else {
4538 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4539 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004540 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004541}
Alexandre Rames67555f72014-11-18 10:55:16 +00004542
Serban Constantinescu02164b32014-11-13 14:05:07 +00004543void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4544 HandleShift(ushr);
4545}
4546
4547void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4548 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004549}
4550
4551void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4552 HandleBinaryOp(instruction);
4553}
4554
4555void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4556 HandleBinaryOp(instruction);
4557}
4558
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004559void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004560 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004561 LOG(FATAL) << "Unreachable";
4562}
4563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004564void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004565 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004566 LOG(FATAL) << "Unreachable";
4567}
4568
Mark Mendellfe57faa2015-09-18 09:26:15 -04004569// Simple implementation of packed switch - generate cascaded compare/jumps.
4570void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4571 LocationSummary* locations =
4572 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4573 locations->SetInAt(0, Location::RequiresRegister());
4574}
4575
4576void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4577 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004578 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004579 Register value_reg = InputRegisterAt(switch_instr, 0);
4580 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4581
Zheng Xu3927c8b2015-11-18 17:46:25 +08004582 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4583 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4584 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4585 // make sure we don't emit it if the target may run out of range.
4586 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4587 // ranges and emit the tables only as required.
4588 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004589
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004590 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004591 // Current instruction id is an upper bound of the number of HIRs in the graph.
4592 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4593 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004594 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4595 Register temp = temps.AcquireW();
4596 __ Subs(temp, value_reg, Operand(lower_bound));
4597
Zheng Xu3927c8b2015-11-18 17:46:25 +08004598 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004599 // Jump to successors[0] if value == lower_bound.
4600 __ B(eq, codegen_->GetLabelOf(successors[0]));
4601 int32_t last_index = 0;
4602 for (; num_entries - last_index > 2; last_index += 2) {
4603 __ Subs(temp, temp, Operand(2));
4604 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4605 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4606 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4607 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4608 }
4609 if (num_entries - last_index == 2) {
4610 // The last missing case_value.
4611 __ Cmp(temp, Operand(1));
4612 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004613 }
4614
4615 // And the default for any other value.
4616 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4617 __ B(codegen_->GetLabelOf(default_block));
4618 }
4619 } else {
4620 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4621 codegen_->AddJumpTable(jump_table);
4622
4623 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4624
4625 // Below instructions should use at most one blocked register. Since there are two blocked
4626 // registers, we are free to block one.
4627 Register temp_w = temps.AcquireW();
4628 Register index;
4629 // Remove the bias.
4630 if (lower_bound != 0) {
4631 index = temp_w;
4632 __ Sub(index, value_reg, Operand(lower_bound));
4633 } else {
4634 index = value_reg;
4635 }
4636
4637 // Jump to default block if index is out of the range.
4638 __ Cmp(index, Operand(num_entries));
4639 __ B(hs, codegen_->GetLabelOf(default_block));
4640
4641 // In current VIXL implementation, it won't require any blocked registers to encode the
4642 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4643 // register pressure.
4644 Register table_base = temps.AcquireX();
4645 // Load jump offset from the table.
4646 __ Adr(table_base, jump_table->GetTableStartLabel());
4647 Register jump_offset = temp_w;
4648 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4649
4650 // Jump to target block by branching to table_base(pc related) + offset.
4651 Register target_address = table_base;
4652 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4653 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004654 }
4655}
4656
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004657void CodeGeneratorARM64::GenerateReadBarrier(HInstruction* instruction,
4658 Location out,
4659 Location ref,
4660 Location obj,
4661 uint32_t offset,
4662 Location index) {
4663 DCHECK(kEmitCompilerReadBarrier);
4664
4665 // If heap poisoning is enabled, the unpoisoning of the loaded
4666 // reference will be carried out by the runtime within the slow
4667 // path.
4668 //
4669 // Note that `ref` currently does not get unpoisoned (when heap
4670 // poisoning is enabled), which is alright as the `ref` argument is
4671 // not used by the artReadBarrierSlow entry point.
4672 //
4673 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4674 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4675 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4676 AddSlowPath(slow_path);
4677
4678 // TODO: When read barrier has a fast path, add it here.
4679 /* Currently the read barrier call is inserted after the original load.
4680 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
4681 * original load. This load-load ordering is required by the read barrier.
4682 * The fast path/slow path (for Baker's algorithm) should look like:
4683 *
4684 * bool isGray = obj.LockWord & kReadBarrierMask;
4685 * lfence; // load fence or artificial data dependence to prevent load-load reordering
4686 * ref = obj.field; // this is the original load
4687 * if (isGray) {
4688 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
4689 * }
4690 */
4691
4692 __ B(slow_path->GetEntryLabel());
4693 __ Bind(slow_path->GetExitLabel());
4694}
4695
4696void CodeGeneratorARM64::MaybeGenerateReadBarrier(HInstruction* instruction,
4697 Location out,
4698 Location ref,
4699 Location obj,
4700 uint32_t offset,
4701 Location index) {
4702 if (kEmitCompilerReadBarrier) {
4703 // If heap poisoning is enabled, unpoisoning will be taken care of
4704 // by the runtime within the slow path.
4705 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
4706 } else if (kPoisonHeapReferences) {
4707 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4708 }
4709}
4710
4711void CodeGeneratorARM64::GenerateReadBarrierForRoot(HInstruction* instruction,
4712 Location out,
4713 Location root) {
4714 DCHECK(kEmitCompilerReadBarrier);
4715
4716 // Note that GC roots are not affected by heap poisoning, so we do
4717 // not need to do anything special for this here.
4718 SlowPathCodeARM64* slow_path =
4719 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4720 AddSlowPath(slow_path);
4721
4722 // TODO: Implement a fast path for ReadBarrierForRoot, performing
4723 // the following operation (for Baker's algorithm):
4724 //
4725 // if (thread.tls32_.is_gc_marking) {
4726 // root = Mark(root);
4727 // }
4728
4729 __ B(slow_path->GetEntryLabel());
4730 __ Bind(slow_path->GetExitLabel());
4731}
4732
Alexandre Rames67555f72014-11-18 10:55:16 +00004733#undef __
4734#undef QUICK_ENTRY_POINT
4735
Alexandre Rames5319def2014-10-23 10:03:10 +01004736} // namespace arm64
4737} // namespace art