blob: e20e04400f67a4de4ace98317ef89188d6a18e72 [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
Vladimir Markod6e069b2016-01-18 11:11:01 +000096inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
97 // The ARM64 condition codes can express all the necessary branches, see the
98 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
99 // There is no dex instruction or HIR that would need the missing conditions
100 // "equal or unordered" or "not equal".
101 switch (cond) {
102 case kCondEQ: return eq;
103 case kCondNE: return ne /* unordered */;
104 case kCondLT: return gt_bias ? cc : lt /* unordered */;
105 case kCondLE: return gt_bias ? ls : le /* unordered */;
106 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
107 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
108 default:
109 LOG(FATAL) << "UNREACHABLE";
110 UNREACHABLE();
111 }
112}
113
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
116 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
117 // but we use the exact registers for clarity.
118 if (return_type == Primitive::kPrimFloat) {
119 return LocationFrom(s0);
120 } else if (return_type == Primitive::kPrimDouble) {
121 return LocationFrom(d0);
122 } else if (return_type == Primitive::kPrimLong) {
123 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100124 } else if (return_type == Primitive::kPrimVoid) {
125 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000126 } else {
127 return LocationFrom(w0);
128 }
129}
130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133}
134
Alexandre Rames67555f72014-11-18 10:55:16 +0000135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()->
136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Zheng Xuda403092015-04-24 17:35:39 +0800138// Calculate memory accessing operand for save/restore live registers.
139static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
140 RegisterSet* register_set,
141 int64_t spill_offset,
142 bool is_save) {
143 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
144 codegen->GetNumberOfCoreRegisters(),
145 register_set->GetFloatingPointRegisters(),
146 codegen->GetNumberOfFloatingPointRegisters()));
147
148 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800152
153 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
154 UseScratchRegisterScope temps(masm);
155
156 Register base = masm->StackPointer();
157 int64_t core_spill_size = core_list.TotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
159 int64_t reg_size = kXRegSizeInBytes;
160 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
161 uint32_t ls_access_size = WhichPowerOf2(reg_size);
162 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
163 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
164 // If the offset does not fit in the instruction's immediate field, use an alternate register
165 // to compute the base address(float point registers spill base address).
166 Register new_base = temps.AcquireSameSizeAs(base);
167 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
168 base = new_base;
169 spill_offset = -core_spill_size;
170 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
171 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
172 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
173 }
174
175 if (is_save) {
176 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
177 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
178 } else {
179 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
180 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
181 }
182}
183
184void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
185 RegisterSet* register_set = locations->GetLiveRegisters();
186 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
187 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
188 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
189 // If the register holds an object, update the stack mask.
190 if (locations->RegisterContainsObject(i)) {
191 locations->SetStackBit(stack_offset / kVRegSize);
192 }
193 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
194 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
195 saved_core_stack_offsets_[i] = stack_offset;
196 stack_offset += kXRegSizeInBytes;
197 }
198 }
199
200 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
201 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
202 register_set->ContainsFloatingPointRegister(i)) {
203 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
204 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
205 saved_fpu_stack_offsets_[i] = stack_offset;
206 stack_offset += kDRegSizeInBytes;
207 }
208 }
209
210 SaveRestoreLiveRegistersHelper(codegen, register_set,
211 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
212}
213
214void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
215 RegisterSet* register_set = locations->GetLiveRegisters();
216 SaveRestoreLiveRegistersHelper(codegen, register_set,
217 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
218}
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
221 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000226 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000229 if (instruction_->CanThrowIntoCatchBlock()) {
230 // Live registers will be restored in the catch block if caught.
231 SaveLiveRegisters(codegen, instruction_->GetLocations());
232 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 // We're moving two locations to locations that could overlap, so we need a parallel
234 // move resolver.
235 InvokeRuntimeCallingConvention calling_convention;
236 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100237 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
238 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000239 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000240 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800241 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100242 }
243
Alexandre Rames8158f282015-08-07 10:26:17 +0100244 bool IsFatal() const OVERRIDE { return true; }
245
Alexandre Rames9931f312015-06-19 14:47:01 +0100246 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
247
Alexandre Rames5319def2014-10-23 10:03:10 +0100248 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000249 HBoundsCheck* const instruction_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
252};
253
Alexandre Rames67555f72014-11-18 10:55:16 +0000254class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
255 public:
256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : instruction_(instruction) {}
257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000261 if (instruction_->CanThrowIntoCatchBlock()) {
262 // Live registers will be restored in the catch block if caught.
263 SaveLiveRegisters(codegen, instruction_->GetLocations());
264 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000265 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000266 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 }
269
Alexandre Rames8158f282015-08-07 10:26:17 +0100270 bool IsFatal() const OVERRIDE { return true; }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
273
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 private:
275 HDivZeroCheck* const instruction_;
276 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
277};
278
279class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
280 public:
281 LoadClassSlowPathARM64(HLoadClass* cls,
282 HInstruction* at,
283 uint32_t dex_pc,
284 bool do_clinit)
285 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
286 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
287 }
288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 LocationSummary* locations = at_->GetLocations();
291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292
293 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295
296 InvokeRuntimeCallingConvention calling_convention;
297 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
299 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000300 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100304 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800305 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000306
307 // Move the class to the desired location.
308 Location out = locations->Out();
309 if (out.IsValid()) {
310 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
311 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000312 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 __ B(GetExitLabel());
317 }
318
Alexandre Rames9931f312015-06-19 14:47:01 +0100319 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
320
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 private:
322 // The class this slow path will load.
323 HLoadClass* const cls_;
324
325 // The instruction where this slow path is happening.
326 // (Might be the load class or an initialization check).
327 HInstruction* const at_;
328
329 // The dex PC of `at_`.
330 const uint32_t dex_pc_;
331
332 // Whether to initialize the class.
333 const bool do_clinit_;
334
335 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
336};
337
338class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
339 public:
340 explicit LoadStringSlowPathARM64(HLoadString* instruction) : instruction_(instruction) {}
341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
344 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
346
347 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349
350 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800351 __ Mov(calling_convention.GetRegisterAt(0).W(), instruction_->GetStringIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000353 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100354 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000356 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000357
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000358 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 __ B(GetExitLabel());
360 }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
363
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 private:
365 HLoadString* const instruction_;
366
367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
368};
369
Alexandre Rames5319def2014-10-23 10:03:10 +0100370class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
371 public:
372 explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {}
373
Alexandre Rames67555f72014-11-18 10:55:16 +0000374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100376 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000382 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800383 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100384 }
385
Alexandre Rames8158f282015-08-07 10:26:17 +0100386 bool IsFatal() const OVERRIDE { return true; }
387
Alexandre Rames9931f312015-06-19 14:47:01 +0100388 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
389
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 private:
391 HNullCheck* const instruction_;
392
393 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
394};
395
396class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
397 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100398 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexandre Rames5319def2014-10-23 10:03:10 +0100399 : instruction_(instruction), successor_(successor) {}
400
Alexandre Rames67555f72014-11-18 10:55:16 +0000401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000404 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000406 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800407 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000408 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000409 if (successor_ == nullptr) {
410 __ B(GetReturnLabel());
411 } else {
412 __ B(arm64_codegen->GetLabelOf(successor_));
413 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 }
415
416 vixl::Label* GetReturnLabel() {
417 DCHECK(successor_ == nullptr);
418 return &return_label_;
419 }
420
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100421 HBasicBlock* GetSuccessor() const {
422 return successor_;
423 }
424
Alexandre Rames9931f312015-06-19 14:47:01 +0100425 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
426
Alexandre Rames5319def2014-10-23 10:03:10 +0100427 private:
428 HSuspendCheck* const instruction_;
429 // If not null, the block to branch to after the suspend check.
430 HBasicBlock* const successor_;
431
432 // If `successor_` is null, the label to branch to after the suspend check.
433 vixl::Label return_label_;
434
435 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
436};
437
Alexandre Rames67555f72014-11-18 10:55:16 +0000438class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
439 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000440 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
441 : instruction_(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000444 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 Location class_to_check = locations->InAt(1);
446 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
447 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448 DCHECK(instruction_->IsCheckCast()
449 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
450 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100451 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452
Alexandre Rames67555f72014-11-18 10:55:16 +0000453 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000455 if (!is_fatal_) {
456 SaveLiveRegisters(codegen, locations);
457 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000458
459 // We're moving two locations to locations that could overlap, so we need a parallel
460 // move resolver.
461 InvokeRuntimeCallingConvention calling_convention;
462 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100463 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
464 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465
466 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000467 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100468 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000469 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
470 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000471 Primitive::Type ret_type = instruction_->GetType();
472 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
473 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
474 } else {
475 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100476 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800477 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000478 }
479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000480 if (!is_fatal_) {
481 RestoreLiveRegisters(codegen, locations);
482 __ B(GetExitLabel());
483 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000484 }
485
Alexandre Rames9931f312015-06-19 14:47:01 +0100486 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100488
Alexandre Rames67555f72014-11-18 10:55:16 +0000489 private:
Alexandre Rames3e69f162014-12-10 10:36:50 +0000490 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000491 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000492
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
494};
495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
497 public:
Aart Bik42249c32016-01-07 15:33:50 -0800498 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100499 : instruction_(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700500
501 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800502 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 __ Bind(GetEntryLabel());
504 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800505 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
506 instruction_,
507 instruction_->GetDexPc(),
508 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000509 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 }
511
Alexandre Rames9931f312015-06-19 14:47:01 +0100512 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
513
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700514 private:
Aart Bik42249c32016-01-07 15:33:50 -0800515 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700516 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
517};
518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100519class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
520 public:
521 explicit ArraySetSlowPathARM64(HInstruction* instruction) : instruction_(instruction) {}
522
523 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
524 LocationSummary* locations = instruction_->GetLocations();
525 __ Bind(GetEntryLabel());
526 SaveLiveRegisters(codegen, locations);
527
528 InvokeRuntimeCallingConvention calling_convention;
529 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
530 parallel_move.AddMove(
531 locations->InAt(0),
532 LocationFrom(calling_convention.GetRegisterAt(0)),
533 Primitive::kPrimNot,
534 nullptr);
535 parallel_move.AddMove(
536 locations->InAt(1),
537 LocationFrom(calling_convention.GetRegisterAt(1)),
538 Primitive::kPrimInt,
539 nullptr);
540 parallel_move.AddMove(
541 locations->InAt(2),
542 LocationFrom(calling_convention.GetRegisterAt(2)),
543 Primitive::kPrimNot,
544 nullptr);
545 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
546
547 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
548 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
549 instruction_,
550 instruction_->GetDexPc(),
551 this);
552 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
553 RestoreLiveRegisters(codegen, locations);
554 __ B(GetExitLabel());
555 }
556
557 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
558
559 private:
560 HInstruction* const instruction_;
561
562 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
563};
564
Zheng Xu3927c8b2015-11-18 17:46:25 +0800565void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
566 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000567 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800568
569 // We are about to use the assembler to place literals directly. Make sure we have enough
570 // underlying code buffer and we have generated the jump table with right size.
571 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
572 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
573
574 __ Bind(&table_start_);
575 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
576 for (uint32_t i = 0; i < num_entries; i++) {
577 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
578 DCHECK(target_label->IsBound());
579 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
580 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
581 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
582 Literal<int32_t> literal(jump_offset);
583 __ place(&literal);
584 }
585}
586
Roland Levillain44015862016-01-22 11:47:17 +0000587// Slow path marking an object during a read barrier.
588class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
589 public:
590 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location out, Location obj)
591 : instruction_(instruction), out_(out), obj_(obj) {
592 DCHECK(kEmitCompilerReadBarrier);
593 }
594
595 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
596
597 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
598 LocationSummary* locations = instruction_->GetLocations();
599 Primitive::Type type = Primitive::kPrimNot;
600 DCHECK(locations->CanCall());
601 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
602 DCHECK(instruction_->IsInstanceFieldGet() ||
603 instruction_->IsStaticFieldGet() ||
604 instruction_->IsArrayGet() ||
605 instruction_->IsLoadClass() ||
606 instruction_->IsLoadString() ||
607 instruction_->IsInstanceOf() ||
608 instruction_->IsCheckCast())
609 << "Unexpected instruction in read barrier marking slow path: "
610 << instruction_->DebugName();
611
612 __ Bind(GetEntryLabel());
613 SaveLiveRegisters(codegen, locations);
614
615 InvokeRuntimeCallingConvention calling_convention;
616 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
617 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)), obj_, type);
618 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
619 instruction_,
620 instruction_->GetDexPc(),
621 this);
622 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
623 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
624
625 RestoreLiveRegisters(codegen, locations);
626 __ B(GetExitLabel());
627 }
628
629 private:
630 HInstruction* const instruction_;
631 const Location out_;
632 const Location obj_;
633
634 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
635};
636
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000637// Slow path generating a read barrier for a heap reference.
638class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
639 public:
640 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
641 Location out,
642 Location ref,
643 Location obj,
644 uint32_t offset,
645 Location index)
646 : instruction_(instruction),
647 out_(out),
648 ref_(ref),
649 obj_(obj),
650 offset_(offset),
651 index_(index) {
652 DCHECK(kEmitCompilerReadBarrier);
653 // If `obj` is equal to `out` or `ref`, it means the initial object
654 // has been overwritten by (or after) the heap object reference load
655 // to be instrumented, e.g.:
656 //
657 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000658 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000659 //
660 // In that case, we have lost the information about the original
661 // object, and the emitted read barrier cannot work properly.
662 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
663 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
664 }
665
666 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
667 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
668 LocationSummary* locations = instruction_->GetLocations();
669 Primitive::Type type = Primitive::kPrimNot;
670 DCHECK(locations->CanCall());
671 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
672 DCHECK(!instruction_->IsInvoke() ||
673 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain44015862016-01-22 11:47:17 +0000674 instruction_->GetLocations()->Intrinsified()))
675 << "Unexpected instruction in read barrier for heap reference slow path: "
676 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000677 // The read barrier instrumentation does not support the
678 // HArm64IntermediateAddress instruction yet.
679 DCHECK(!(instruction_->IsArrayGet() &&
680 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000681
682 __ Bind(GetEntryLabel());
683
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000684 SaveLiveRegisters(codegen, locations);
685
686 // We may have to change the index's value, but as `index_` is a
687 // constant member (like other "inputs" of this slow path),
688 // introduce a copy of it, `index`.
689 Location index = index_;
690 if (index_.IsValid()) {
691 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
692 if (instruction_->IsArrayGet()) {
693 // Compute the actual memory offset and store it in `index`.
694 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
695 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
696 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
697 // We are about to change the value of `index_reg` (see the
698 // calls to vixl::MacroAssembler::Lsl and
699 // vixl::MacroAssembler::Mov below), but it has
700 // not been saved by the previous call to
701 // art::SlowPathCode::SaveLiveRegisters, as it is a
702 // callee-save register --
703 // art::SlowPathCode::SaveLiveRegisters does not consider
704 // callee-save registers, as it has been designed with the
705 // assumption that callee-save registers are supposed to be
706 // handled by the called function. So, as a callee-save
707 // register, `index_reg` _would_ eventually be saved onto
708 // the stack, but it would be too late: we would have
709 // changed its value earlier. Therefore, we manually save
710 // it here into another freely available register,
711 // `free_reg`, chosen of course among the caller-save
712 // registers (as a callee-save `free_reg` register would
713 // exhibit the same problem).
714 //
715 // Note we could have requested a temporary register from
716 // the register allocator instead; but we prefer not to, as
717 // this is a slow path, and we know we can find a
718 // caller-save register that is available.
719 Register free_reg = FindAvailableCallerSaveRegister(codegen);
720 __ Mov(free_reg.W(), index_reg);
721 index_reg = free_reg;
722 index = LocationFrom(index_reg);
723 } else {
724 // The initial register stored in `index_` has already been
725 // saved in the call to art::SlowPathCode::SaveLiveRegisters
726 // (as it is not a callee-save register), so we can freely
727 // use it.
728 }
729 // Shifting the index value contained in `index_reg` by the scale
730 // factor (2) cannot overflow in practice, as the runtime is
731 // unable to allocate object arrays with a size larger than
732 // 2^26 - 1 (that is, 2^28 - 4 bytes).
733 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
734 static_assert(
735 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
736 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
737 __ Add(index_reg, index_reg, Operand(offset_));
738 } else {
739 DCHECK(instruction_->IsInvoke());
740 DCHECK(instruction_->GetLocations()->Intrinsified());
741 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
742 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
743 << instruction_->AsInvoke()->GetIntrinsic();
744 DCHECK_EQ(offset_, 0U);
745 DCHECK(index_.IsRegisterPair());
746 // UnsafeGet's offset location is a register pair, the low
747 // part contains the correct offset.
748 index = index_.ToLow();
749 }
750 }
751
752 // We're moving two or three locations to locations that could
753 // overlap, so we need a parallel move resolver.
754 InvokeRuntimeCallingConvention calling_convention;
755 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
756 parallel_move.AddMove(ref_,
757 LocationFrom(calling_convention.GetRegisterAt(0)),
758 type,
759 nullptr);
760 parallel_move.AddMove(obj_,
761 LocationFrom(calling_convention.GetRegisterAt(1)),
762 type,
763 nullptr);
764 if (index.IsValid()) {
765 parallel_move.AddMove(index,
766 LocationFrom(calling_convention.GetRegisterAt(2)),
767 Primitive::kPrimInt,
768 nullptr);
769 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
770 } else {
771 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
772 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
773 }
774 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
775 instruction_,
776 instruction_->GetDexPc(),
777 this);
778 CheckEntrypointTypes<
779 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
780 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
781
782 RestoreLiveRegisters(codegen, locations);
783
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000784 __ B(GetExitLabel());
785 }
786
787 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
788
789 private:
790 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
791 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
792 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
793 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
794 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
795 return Register(VIXLRegCodeFromART(i), kXRegSize);
796 }
797 }
798 // We shall never fail to find a free caller-save register, as
799 // there are more than two core caller-save registers on ARM64
800 // (meaning it is possible to find one which is different from
801 // `ref` and `obj`).
802 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
803 LOG(FATAL) << "Could not find a free register";
804 UNREACHABLE();
805 }
806
807 HInstruction* const instruction_;
808 const Location out_;
809 const Location ref_;
810 const Location obj_;
811 const uint32_t offset_;
812 // An additional location containing an index to an array.
813 // Only used for HArrayGet and the UnsafeGetObject &
814 // UnsafeGetObjectVolatile intrinsics.
815 const Location index_;
816
817 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
818};
819
820// Slow path generating a read barrier for a GC root.
821class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
822 public:
823 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
Roland Levillain44015862016-01-22 11:47:17 +0000824 : instruction_(instruction), out_(out), root_(root) {
825 DCHECK(kEmitCompilerReadBarrier);
826 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000827
828 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
829 LocationSummary* locations = instruction_->GetLocations();
830 Primitive::Type type = Primitive::kPrimNot;
831 DCHECK(locations->CanCall());
832 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000833 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
834 << "Unexpected instruction in read barrier for GC root slow path: "
835 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000836
837 __ Bind(GetEntryLabel());
838 SaveLiveRegisters(codegen, locations);
839
840 InvokeRuntimeCallingConvention calling_convention;
841 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
842 // The argument of the ReadBarrierForRootSlow is not a managed
843 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
844 // thus we need a 64-bit move here, and we cannot use
845 //
846 // arm64_codegen->MoveLocation(
847 // LocationFrom(calling_convention.GetRegisterAt(0)),
848 // root_,
849 // type);
850 //
851 // which would emit a 32-bit move, as `type` is a (32-bit wide)
852 // reference type (`Primitive::kPrimNot`).
853 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
854 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
855 instruction_,
856 instruction_->GetDexPc(),
857 this);
858 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
859 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
860
861 RestoreLiveRegisters(codegen, locations);
862 __ B(GetExitLabel());
863 }
864
865 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
866
867 private:
868 HInstruction* const instruction_;
869 const Location out_;
870 const Location root_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
873};
874
Alexandre Rames5319def2014-10-23 10:03:10 +0100875#undef __
876
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100877Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100878 Location next_location;
879 if (type == Primitive::kPrimVoid) {
880 LOG(FATAL) << "Unreachable type " << type;
881 }
882
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
885 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000886 } else if (!Primitive::IsFloatingPointType(type) &&
887 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000888 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
889 } else {
890 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000891 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
892 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100893 }
894
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000896 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100897 return next_location;
898}
899
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100900Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100901 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100902}
903
Serban Constantinescu579885a2015-02-22 20:51:33 +0000904CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
905 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100906 const CompilerOptions& compiler_options,
907 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100908 : CodeGenerator(graph,
909 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000910 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000911 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000912 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000913 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100914 compiler_options,
915 stats),
Alexandre Rames5319def2014-10-23 10:03:10 +0100916 block_labels_(nullptr),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100918 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000919 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000920 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000921 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100922 uint64_literals_(std::less<uint64_t>(),
923 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
924 method_patches_(MethodReferenceComparator(),
925 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
926 call_patches_(MethodReferenceComparator(),
927 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
928 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000929 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000930 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000931 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000932}
Alexandre Rames5319def2014-10-23 10:03:10 +0100933
Alexandre Rames67555f72014-11-18 10:55:16 +0000934#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100935
Zheng Xu3927c8b2015-11-18 17:46:25 +0800936void CodeGeneratorARM64::EmitJumpTables() {
937 for (auto jump_table : jump_tables_) {
938 jump_table->EmitTable(this);
939 }
940}
941
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000942void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800943 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000944 // Ensure we emit the literal pool.
945 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000946
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000947 CodeGenerator::Finalize(allocator);
948}
949
Zheng Xuad4450e2015-04-17 18:48:56 +0800950void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
951 // Note: There are 6 kinds of moves:
952 // 1. constant -> GPR/FPR (non-cycle)
953 // 2. constant -> stack (non-cycle)
954 // 3. GPR/FPR -> GPR/FPR
955 // 4. GPR/FPR -> stack
956 // 5. stack -> GPR/FPR
957 // 6. stack -> stack (non-cycle)
958 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
959 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
960 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
961 // dependency.
962 vixl_temps_.Open(GetVIXLAssembler());
963}
964
965void ParallelMoveResolverARM64::FinishEmitNativeCode() {
966 vixl_temps_.Close();
967}
968
969Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
970 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
971 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
972 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
973 Location scratch = GetScratchLocation(kind);
974 if (!scratch.Equals(Location::NoLocation())) {
975 return scratch;
976 }
977 // Allocate from VIXL temp registers.
978 if (kind == Location::kRegister) {
979 scratch = LocationFrom(vixl_temps_.AcquireX());
980 } else {
981 DCHECK(kind == Location::kFpuRegister);
982 scratch = LocationFrom(vixl_temps_.AcquireD());
983 }
984 AddScratchLocation(scratch);
985 return scratch;
986}
987
988void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
989 if (loc.IsRegister()) {
990 vixl_temps_.Release(XRegisterFrom(loc));
991 } else {
992 DCHECK(loc.IsFpuRegister());
993 vixl_temps_.Release(DRegisterFrom(loc));
994 }
995 RemoveScratchLocation(loc);
996}
997
Alexandre Rames3e69f162014-12-10 10:36:50 +0000998void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100999 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001000 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001001}
1002
Alexandre Rames5319def2014-10-23 10:03:10 +01001003void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001004 MacroAssembler* masm = GetVIXLAssembler();
1005 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001006 __ Bind(&frame_entry_label_);
1007
Serban Constantinescu02164b32014-11-13 14:05:07 +00001008 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1009 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001010 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001011 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001012 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001013 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001014 __ Ldr(wzr, MemOperand(temp, 0));
1015 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001016 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001017
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001018 if (!HasEmptyFrame()) {
1019 int frame_size = GetFrameSize();
1020 // Stack layout:
1021 // sp[frame_size - 8] : lr.
1022 // ... : other preserved core registers.
1023 // ... : other preserved fp registers.
1024 // ... : reserved frame space.
1025 // sp[0] : current method.
1026 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001027 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001028 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1029 frame_size - GetCoreSpillSize());
1030 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1031 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001032 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001033}
1034
1035void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001036 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001037 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001038 if (!HasEmptyFrame()) {
1039 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001040 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1041 frame_size - FrameEntrySpillSize());
1042 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1043 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001044 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001045 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001046 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001047 __ Ret();
1048 GetAssembler()->cfi().RestoreState();
1049 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001050}
1051
Zheng Xuda403092015-04-24 17:35:39 +08001052vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1053 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1054 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1055 core_spill_mask_);
1056}
1057
1058vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1059 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1060 GetNumberOfFloatingPointRegisters()));
1061 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1062 fpu_spill_mask_);
1063}
1064
Alexandre Rames5319def2014-10-23 10:03:10 +01001065void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1066 __ Bind(GetLabelOf(block));
1067}
1068
Alexandre Rames5319def2014-10-23 10:03:10 +01001069void CodeGeneratorARM64::Move(HInstruction* instruction,
1070 Location location,
1071 HInstruction* move_for) {
1072 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01001073 Primitive::Type type = instruction->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001074 DCHECK_NE(type, Primitive::kPrimVoid);
Alexandre Rames5319def2014-10-23 10:03:10 +01001075
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001076 if (instruction->IsCurrentMethod()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001077 MoveLocation(location,
1078 Location::DoubleStackSlot(kCurrentMethodStackOffset),
1079 Primitive::kPrimVoid);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001080 } else if (locations != nullptr && locations->Out().Equals(location)) {
1081 return;
1082 } else if (instruction->IsIntConstant()
1083 || instruction->IsLongConstant()
1084 || instruction->IsNullConstant()) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001085 int64_t value = GetInt64ValueOf(instruction->AsConstant());
Alexandre Rames5319def2014-10-23 10:03:10 +01001086 if (location.IsRegister()) {
1087 Register dst = RegisterFrom(location, type);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001088 DCHECK(((instruction->IsIntConstant() || instruction->IsNullConstant()) && dst.Is32Bits()) ||
Alexandre Rames5319def2014-10-23 10:03:10 +01001089 (instruction->IsLongConstant() && dst.Is64Bits()));
1090 __ Mov(dst, value);
1091 } else {
1092 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001093 UseScratchRegisterScope temps(GetVIXLAssembler());
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001094 Register temp = (instruction->IsIntConstant() || instruction->IsNullConstant())
1095 ? temps.AcquireW()
1096 : temps.AcquireX();
Alexandre Rames5319def2014-10-23 10:03:10 +01001097 __ Mov(temp, value);
1098 __ Str(temp, StackOperandFrom(location));
1099 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001100 } else if (instruction->IsTemporary()) {
1101 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001102 MoveLocation(location, temp_location, type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001103 } else if (instruction->IsLoadLocal()) {
1104 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Alexandre Rames542361f2015-01-29 16:57:31 +00001105 if (Primitive::Is64BitType(type)) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001106 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001107 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001108 MoveLocation(location, Location::StackSlot(stack_slot), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 }
1110
1111 } else {
1112 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001113 MoveLocation(location, locations->Out(), type);
Alexandre Rames5319def2014-10-23 10:03:10 +01001114 }
1115}
1116
Calin Juravle175dc732015-08-25 15:42:32 +01001117void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1118 DCHECK(location.IsRegister());
1119 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1120}
1121
Calin Juravlee460d1d2015-09-29 04:52:17 +01001122void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1123 if (location.IsRegister()) {
1124 locations->AddTemp(location);
1125 } else {
1126 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1127 }
1128}
1129
Alexandre Rames5319def2014-10-23 10:03:10 +01001130Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const {
1131 Primitive::Type type = load->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001132
Alexandre Rames5319def2014-10-23 10:03:10 +01001133 switch (type) {
1134 case Primitive::kPrimNot:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001135 case Primitive::kPrimInt:
1136 case Primitive::kPrimFloat:
1137 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1138
1139 case Primitive::kPrimLong:
1140 case Primitive::kPrimDouble:
1141 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1142
Alexandre Rames5319def2014-10-23 10:03:10 +01001143 case Primitive::kPrimBoolean:
1144 case Primitive::kPrimByte:
1145 case Primitive::kPrimChar:
1146 case Primitive::kPrimShort:
Alexandre Rames5319def2014-10-23 10:03:10 +01001147 case Primitive::kPrimVoid:
Alexandre Rames5319def2014-10-23 10:03:10 +01001148 LOG(FATAL) << "Unexpected type " << type;
1149 }
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001150
Alexandre Rames5319def2014-10-23 10:03:10 +01001151 LOG(FATAL) << "Unreachable";
1152 return Location::NoLocation();
1153}
1154
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001155void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001156 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001157 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001158 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001159 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001160 if (value_can_be_null) {
1161 __ Cbz(value, &done);
1162 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001163 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1164 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001165 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001166 if (value_can_be_null) {
1167 __ Bind(&done);
1168 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001169}
1170
David Brazdil58282f42016-01-14 12:45:10 +00001171void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001172 // Blocked core registers:
1173 // lr : Runtime reserved.
1174 // tr : Runtime reserved.
1175 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1176 // ip1 : VIXL core temp.
1177 // ip0 : VIXL core temp.
1178 //
1179 // Blocked fp registers:
1180 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001181 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1182 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001183 while (!reserved_core_registers.IsEmpty()) {
1184 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1185 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001186
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001187 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001188 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001189 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1190 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001191
David Brazdil58282f42016-01-14 12:45:10 +00001192 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001193 // Stubs do not save callee-save floating point registers. If the graph
1194 // is debuggable, we need to deal with these registers differently. For
1195 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001196 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1197 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1198 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001199 }
1200 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001201}
1202
Alexandre Rames3e69f162014-12-10 10:36:50 +00001203size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1204 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1205 __ Str(reg, MemOperand(sp, stack_index));
1206 return kArm64WordSize;
1207}
1208
1209size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1210 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1211 __ Ldr(reg, MemOperand(sp, stack_index));
1212 return kArm64WordSize;
1213}
1214
1215size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1216 FPRegister reg = FPRegister(reg_id, kDRegSize);
1217 __ Str(reg, MemOperand(sp, stack_index));
1218 return kArm64WordSize;
1219}
1220
1221size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1222 FPRegister reg = FPRegister(reg_id, kDRegSize);
1223 __ Ldr(reg, MemOperand(sp, stack_index));
1224 return kArm64WordSize;
1225}
1226
Alexandre Rames5319def2014-10-23 10:03:10 +01001227void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001228 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001229}
1230
1231void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001232 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001233}
1234
Alexandre Rames67555f72014-11-18 10:55:16 +00001235void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001236 if (constant->IsIntConstant()) {
1237 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1238 } else if (constant->IsLongConstant()) {
1239 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1240 } else if (constant->IsNullConstant()) {
1241 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001242 } else if (constant->IsFloatConstant()) {
1243 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1244 } else {
1245 DCHECK(constant->IsDoubleConstant());
1246 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1247 }
1248}
1249
Alexandre Rames3e69f162014-12-10 10:36:50 +00001250
1251static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1252 DCHECK(constant.IsConstant());
1253 HConstant* cst = constant.GetConstant();
1254 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001255 // Null is mapped to a core W register, which we associate with kPrimInt.
1256 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001257 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1258 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1259 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1260}
1261
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262void CodeGeneratorARM64::MoveLocation(Location destination,
1263 Location source,
1264 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001265 if (source.Equals(destination)) {
1266 return;
1267 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268
1269 // A valid move can always be inferred from the destination and source
1270 // locations. When moving from and to a register, the argument type can be
1271 // used to generate 32bit instead of 64bit moves. In debug mode we also
1272 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274
1275 if (destination.IsRegister() || destination.IsFpuRegister()) {
1276 if (unspecified_type) {
1277 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1278 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001279 (src_cst != nullptr && (src_cst->IsIntConstant()
1280 || src_cst->IsFloatConstant()
1281 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001282 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001284 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001285 // If the source is a double stack slot or a 64bit constant, a 64bit
1286 // type is appropriate. Else the source is a register, and since the
1287 // type has not been specified, we chose a 64bit type to force a 64bit
1288 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001290 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001291 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001292 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1293 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1294 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001295 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1296 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1297 __ Ldr(dst, StackOperandFrom(source));
1298 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001299 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001301 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001302 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001303 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001304 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001305 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001306 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1307 ? Primitive::kPrimLong
1308 : Primitive::kPrimInt;
1309 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1310 }
1311 } else {
1312 DCHECK(source.IsFpuRegister());
1313 if (destination.IsRegister()) {
1314 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1315 ? Primitive::kPrimDouble
1316 : Primitive::kPrimFloat;
1317 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1318 } else {
1319 DCHECK(destination.IsFpuRegister());
1320 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001321 }
1322 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001323 } else { // The destination is not a register. It must be a stack slot.
1324 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1325 if (source.IsRegister() || source.IsFpuRegister()) {
1326 if (unspecified_type) {
1327 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001328 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001329 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001330 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001331 }
1332 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001333 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1334 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1335 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001336 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1338 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001339 UseScratchRegisterScope temps(GetVIXLAssembler());
1340 HConstant* src_cst = source.GetConstant();
1341 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001342 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001343 temp = temps.AcquireW();
1344 } else if (src_cst->IsLongConstant()) {
1345 temp = temps.AcquireX();
1346 } else if (src_cst->IsFloatConstant()) {
1347 temp = temps.AcquireS();
1348 } else {
1349 DCHECK(src_cst->IsDoubleConstant());
1350 temp = temps.AcquireD();
1351 }
1352 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001353 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001354 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001355 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001356 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001357 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001358 // There is generally less pressure on FP registers.
1359 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001360 __ Ldr(temp, StackOperandFrom(source));
1361 __ Str(temp, StackOperandFrom(destination));
1362 }
1363 }
1364}
1365
1366void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001367 CPURegister dst,
1368 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001369 switch (type) {
1370 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001371 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001372 break;
1373 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001374 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001375 break;
1376 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001377 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001378 break;
1379 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001380 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001381 break;
1382 case Primitive::kPrimInt:
1383 case Primitive::kPrimNot:
1384 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001387 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001388 __ Ldr(dst, src);
1389 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001390 case Primitive::kPrimVoid:
1391 LOG(FATAL) << "Unreachable type " << type;
1392 }
1393}
1394
Calin Juravle77520bc2015-01-12 18:45:46 +00001395void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001397 const MemOperand& src,
1398 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001399 MacroAssembler* masm = GetVIXLAssembler();
1400 BlockPoolsScope block_pools(masm);
1401 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001403 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001404
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001405 DCHECK(!src.IsPreIndex());
1406 DCHECK(!src.IsPostIndex());
1407
1408 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001409 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001410 MemOperand base = MemOperand(temp_base);
1411 switch (type) {
1412 case Primitive::kPrimBoolean:
1413 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001414 if (needs_null_check) {
1415 MaybeRecordImplicitNullCheck(instruction);
1416 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001417 break;
1418 case Primitive::kPrimByte:
1419 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001420 if (needs_null_check) {
1421 MaybeRecordImplicitNullCheck(instruction);
1422 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001423 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1424 break;
1425 case Primitive::kPrimChar:
1426 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001427 if (needs_null_check) {
1428 MaybeRecordImplicitNullCheck(instruction);
1429 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001430 break;
1431 case Primitive::kPrimShort:
1432 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001433 if (needs_null_check) {
1434 MaybeRecordImplicitNullCheck(instruction);
1435 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001436 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1437 break;
1438 case Primitive::kPrimInt:
1439 case Primitive::kPrimNot:
1440 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001441 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001442 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001443 if (needs_null_check) {
1444 MaybeRecordImplicitNullCheck(instruction);
1445 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001446 break;
1447 case Primitive::kPrimFloat:
1448 case Primitive::kPrimDouble: {
1449 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001450 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001451
1452 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1453 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001454 if (needs_null_check) {
1455 MaybeRecordImplicitNullCheck(instruction);
1456 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001457 __ Fmov(FPRegister(dst), temp);
1458 break;
1459 }
1460 case Primitive::kPrimVoid:
1461 LOG(FATAL) << "Unreachable type " << type;
1462 }
1463}
1464
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001465void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001466 CPURegister src,
1467 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001468 switch (type) {
1469 case Primitive::kPrimBoolean:
1470 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001471 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001472 break;
1473 case Primitive::kPrimChar:
1474 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001475 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001476 break;
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimNot:
1479 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001480 case Primitive::kPrimFloat:
1481 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001482 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001483 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001484 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001485 case Primitive::kPrimVoid:
1486 LOG(FATAL) << "Unreachable type " << type;
1487 }
1488}
1489
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001490void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1491 CPURegister src,
1492 const MemOperand& dst) {
1493 UseScratchRegisterScope temps(GetVIXLAssembler());
1494 Register temp_base = temps.AcquireX();
1495
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001496 DCHECK(!dst.IsPreIndex());
1497 DCHECK(!dst.IsPostIndex());
1498
1499 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001500 Operand op = OperandFromMemOperand(dst);
1501 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001502 MemOperand base = MemOperand(temp_base);
1503 switch (type) {
1504 case Primitive::kPrimBoolean:
1505 case Primitive::kPrimByte:
1506 __ Stlrb(Register(src), base);
1507 break;
1508 case Primitive::kPrimChar:
1509 case Primitive::kPrimShort:
1510 __ Stlrh(Register(src), base);
1511 break;
1512 case Primitive::kPrimInt:
1513 case Primitive::kPrimNot:
1514 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001515 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001516 __ Stlr(Register(src), base);
1517 break;
1518 case Primitive::kPrimFloat:
1519 case Primitive::kPrimDouble: {
1520 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001521 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001522
1523 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1524 __ Fmov(temp, FPRegister(src));
1525 __ Stlr(temp, base);
1526 break;
1527 }
1528 case Primitive::kPrimVoid:
1529 LOG(FATAL) << "Unreachable type " << type;
1530 }
1531}
1532
Calin Juravle175dc732015-08-25 15:42:32 +01001533void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1534 HInstruction* instruction,
1535 uint32_t dex_pc,
1536 SlowPathCode* slow_path) {
1537 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1538 instruction,
1539 dex_pc,
1540 slow_path);
1541}
1542
Alexandre Rames67555f72014-11-18 10:55:16 +00001543void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1544 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001545 uint32_t dex_pc,
1546 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001548 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001549 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1550 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001551 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001552}
1553
1554void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1555 vixl::Register class_reg) {
1556 UseScratchRegisterScope temps(GetVIXLAssembler());
1557 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001558 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1559
Serban Constantinescu02164b32014-11-13 14:05:07 +00001560 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001561 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1562 __ Add(temp, class_reg, status_offset);
1563 __ Ldar(temp, HeapOperand(temp));
1564 __ Cmp(temp, mirror::Class::kStatusInitialized);
1565 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001566 __ Bind(slow_path->GetExitLabel());
1567}
Alexandre Rames5319def2014-10-23 10:03:10 +01001568
Roland Levillain44015862016-01-22 11:47:17 +00001569void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001570 BarrierType type = BarrierAll;
1571
1572 switch (kind) {
1573 case MemBarrierKind::kAnyAny:
1574 case MemBarrierKind::kAnyStore: {
1575 type = BarrierAll;
1576 break;
1577 }
1578 case MemBarrierKind::kLoadAny: {
1579 type = BarrierReads;
1580 break;
1581 }
1582 case MemBarrierKind::kStoreStore: {
1583 type = BarrierWrites;
1584 break;
1585 }
1586 default:
1587 LOG(FATAL) << "Unexpected memory barrier " << kind;
1588 }
1589 __ Dmb(InnerShareable, type);
1590}
1591
Serban Constantinescu02164b32014-11-13 14:05:07 +00001592void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1593 HBasicBlock* successor) {
1594 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001595 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1596 if (slow_path == nullptr) {
1597 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1598 instruction->SetSlowPath(slow_path);
1599 codegen_->AddSlowPath(slow_path);
1600 if (successor != nullptr) {
1601 DCHECK(successor->IsLoopHeader());
1602 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1603 }
1604 } else {
1605 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1606 }
1607
Serban Constantinescu02164b32014-11-13 14:05:07 +00001608 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1609 Register temp = temps.AcquireW();
1610
1611 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1612 if (successor == nullptr) {
1613 __ Cbnz(temp, slow_path->GetEntryLabel());
1614 __ Bind(slow_path->GetReturnLabel());
1615 } else {
1616 __ Cbz(temp, codegen_->GetLabelOf(successor));
1617 __ B(slow_path->GetEntryLabel());
1618 // slow_path will return to GetLabelOf(successor).
1619 }
1620}
1621
Alexandre Rames5319def2014-10-23 10:03:10 +01001622InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1623 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001624 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001625 assembler_(codegen->GetAssembler()),
1626 codegen_(codegen) {}
1627
1628#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001629 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001630
1631#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1632
1633enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001634 // Using a base helps identify when we hit such breakpoints.
1635 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001636#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1637 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1638#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1639};
1640
1641#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001642 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1644 } \
1645 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1647 locations->SetOut(Location::Any()); \
1648 }
1649 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1650#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1651
1652#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001653#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001654
Alexandre Rames67555f72014-11-18 10:55:16 +00001655void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001656 DCHECK_EQ(instr->InputCount(), 2U);
1657 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1658 Primitive::Type type = instr->GetResultType();
1659 switch (type) {
1660 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001661 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001663 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001665 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001666
1667 case Primitive::kPrimFloat:
1668 case Primitive::kPrimDouble:
1669 locations->SetInAt(0, Location::RequiresFpuRegister());
1670 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001671 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001672 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001673
Alexandre Rames5319def2014-10-23 10:03:10 +01001674 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001675 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001676 }
1677}
1678
Alexandre Rames09a99962015-04-15 11:47:56 +01001679void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001680 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1681
1682 bool object_field_get_with_read_barrier =
1683 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001684 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001685 new (GetGraph()->GetArena()) LocationSummary(instruction,
1686 object_field_get_with_read_barrier ?
1687 LocationSummary::kCallOnSlowPath :
1688 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001689 locations->SetInAt(0, Location::RequiresRegister());
1690 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1691 locations->SetOut(Location::RequiresFpuRegister());
1692 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001693 // The output overlaps for an object field get when read barriers
1694 // are enabled: we do not want the load to overwrite the object's
1695 // location, as we need it to emit the read barrier.
1696 locations->SetOut(
1697 Location::RequiresRegister(),
1698 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001699 }
1700}
1701
1702void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1703 const FieldInfo& field_info) {
1704 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001705 LocationSummary* locations = instruction->GetLocations();
1706 Location base_loc = locations->InAt(0);
1707 Location out = locations->Out();
1708 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001709 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001710 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001711 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001712
Roland Levillain44015862016-01-22 11:47:17 +00001713 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1714 // Object FieldGet with Baker's read barrier case.
1715 MacroAssembler* masm = GetVIXLAssembler();
1716 UseScratchRegisterScope temps(masm);
1717 // /* HeapReference<Object> */ out = *(base + offset)
1718 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1719 Register temp = temps.AcquireW();
1720 // Note that potential implicit null checks are handled in this
1721 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1722 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1723 instruction,
1724 out,
1725 base,
1726 offset,
1727 temp,
1728 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001729 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001730 } else {
1731 // General case.
1732 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001733 // Note that a potential implicit null check is handled in this
1734 // CodeGeneratorARM64::LoadAcquire call.
1735 // NB: LoadAcquire will record the pc info if needed.
1736 codegen_->LoadAcquire(
1737 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001738 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001739 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001741 }
Roland Levillain44015862016-01-22 11:47:17 +00001742 if (field_type == Primitive::kPrimNot) {
1743 // If read barriers are enabled, emit read barriers other than
1744 // Baker's using a slow path (and also unpoison the loaded
1745 // reference, if heap poisoning is enabled).
1746 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1747 }
Roland Levillain4d027112015-07-01 15:41:14 +01001748 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001749}
1750
1751void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1752 LocationSummary* locations =
1753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1754 locations->SetInAt(0, Location::RequiresRegister());
1755 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1756 locations->SetInAt(1, Location::RequiresFpuRegister());
1757 } else {
1758 locations->SetInAt(1, Location::RequiresRegister());
1759 }
1760}
1761
1762void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001763 const FieldInfo& field_info,
1764 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001765 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001766 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001767
1768 Register obj = InputRegisterAt(instruction, 0);
1769 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001770 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001771 Offset offset = field_info.GetFieldOffset();
1772 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001773
Roland Levillain4d027112015-07-01 15:41:14 +01001774 {
1775 // We use a block to end the scratch scope before the write barrier, thus
1776 // freeing the temporary registers so they can be used in `MarkGCCard`.
1777 UseScratchRegisterScope temps(GetVIXLAssembler());
1778
1779 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1780 DCHECK(value.IsW());
1781 Register temp = temps.AcquireW();
1782 __ Mov(temp, value.W());
1783 GetAssembler()->PoisonHeapReference(temp.W());
1784 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001785 }
Roland Levillain4d027112015-07-01 15:41:14 +01001786
1787 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001788 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1789 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001790 } else {
1791 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1792 codegen_->MaybeRecordImplicitNullCheck(instruction);
1793 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001794 }
1795
1796 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001797 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001798 }
1799}
1800
Alexandre Rames67555f72014-11-18 10:55:16 +00001801void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001802 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001803
1804 switch (type) {
1805 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001806 case Primitive::kPrimLong: {
1807 Register dst = OutputRegister(instr);
1808 Register lhs = InputRegisterAt(instr, 0);
1809 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001810 if (instr->IsAdd()) {
1811 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001812 } else if (instr->IsAnd()) {
1813 __ And(dst, lhs, rhs);
1814 } else if (instr->IsOr()) {
1815 __ Orr(dst, lhs, rhs);
1816 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001817 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001818 } else if (instr->IsRor()) {
1819 if (rhs.IsImmediate()) {
1820 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1821 __ Ror(dst, lhs, shift);
1822 } else {
1823 // Ensure shift distance is in the same size register as the result. If
1824 // we are rotating a long and the shift comes in a w register originally,
1825 // we don't need to sxtw for use as an x since the shift distances are
1826 // all & reg_bits - 1.
1827 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1828 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001829 } else {
1830 DCHECK(instr->IsXor());
1831 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001832 }
1833 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001834 }
1835 case Primitive::kPrimFloat:
1836 case Primitive::kPrimDouble: {
1837 FPRegister dst = OutputFPRegister(instr);
1838 FPRegister lhs = InputFPRegisterAt(instr, 0);
1839 FPRegister rhs = InputFPRegisterAt(instr, 1);
1840 if (instr->IsAdd()) {
1841 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001842 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001843 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001844 } else {
1845 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001846 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001847 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001848 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001849 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001850 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001851 }
1852}
1853
Serban Constantinescu02164b32014-11-13 14:05:07 +00001854void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1855 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1856
1857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1858 Primitive::Type type = instr->GetResultType();
1859 switch (type) {
1860 case Primitive::kPrimInt:
1861 case Primitive::kPrimLong: {
1862 locations->SetInAt(0, Location::RequiresRegister());
1863 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1864 locations->SetOut(Location::RequiresRegister());
1865 break;
1866 }
1867 default:
1868 LOG(FATAL) << "Unexpected shift type " << type;
1869 }
1870}
1871
1872void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1873 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1874
1875 Primitive::Type type = instr->GetType();
1876 switch (type) {
1877 case Primitive::kPrimInt:
1878 case Primitive::kPrimLong: {
1879 Register dst = OutputRegister(instr);
1880 Register lhs = InputRegisterAt(instr, 0);
1881 Operand rhs = InputOperandAt(instr, 1);
1882 if (rhs.IsImmediate()) {
1883 uint32_t shift_value = (type == Primitive::kPrimInt)
1884 ? static_cast<uint32_t>(rhs.immediate() & kMaxIntShiftValue)
1885 : static_cast<uint32_t>(rhs.immediate() & kMaxLongShiftValue);
1886 if (instr->IsShl()) {
1887 __ Lsl(dst, lhs, shift_value);
1888 } else if (instr->IsShr()) {
1889 __ Asr(dst, lhs, shift_value);
1890 } else {
1891 __ Lsr(dst, lhs, shift_value);
1892 }
1893 } else {
1894 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1895
1896 if (instr->IsShl()) {
1897 __ Lsl(dst, lhs, rhs_reg);
1898 } else if (instr->IsShr()) {
1899 __ Asr(dst, lhs, rhs_reg);
1900 } else {
1901 __ Lsr(dst, lhs, rhs_reg);
1902 }
1903 }
1904 break;
1905 }
1906 default:
1907 LOG(FATAL) << "Unexpected shift operation type " << type;
1908 }
1909}
1910
Alexandre Rames5319def2014-10-23 10:03:10 +01001911void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001912 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001913}
1914
1915void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001916 HandleBinaryOp(instruction);
1917}
1918
1919void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1920 HandleBinaryOp(instruction);
1921}
1922
1923void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1924 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001925}
1926
Alexandre Rames8626b742015-11-25 16:28:08 +00001927void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1928 HArm64DataProcWithShifterOp* instruction) {
1929 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1930 instruction->GetType() == Primitive::kPrimLong);
1931 LocationSummary* locations =
1932 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1933 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1934 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1935 } else {
1936 locations->SetInAt(0, Location::RequiresRegister());
1937 }
1938 locations->SetInAt(1, Location::RequiresRegister());
1939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1940}
1941
1942void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1943 HArm64DataProcWithShifterOp* instruction) {
1944 Primitive::Type type = instruction->GetType();
1945 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1946 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1947 Register out = OutputRegister(instruction);
1948 Register left;
1949 if (kind != HInstruction::kNeg) {
1950 left = InputRegisterAt(instruction, 0);
1951 }
1952 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1953 // shifter operand operation, the IR generating `right_reg` (input to the type
1954 // conversion) can have a different type from the current instruction's type,
1955 // so we manually indicate the type.
1956 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
1957 int64_t shift_amount = (type == Primitive::kPrimInt)
1958 ? static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxIntShiftValue)
1959 : static_cast<uint32_t>(instruction->GetShiftAmount() & kMaxLongShiftValue);
1960
1961 Operand right_operand(0);
1962
1963 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1964 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1965 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1966 } else {
1967 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1968 }
1969
1970 // Logical binary operations do not support extension operations in the
1971 // operand. Note that VIXL would still manage if it was passed by generating
1972 // the extension as a separate instruction.
1973 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1974 DCHECK(!right_operand.IsExtendedRegister() ||
1975 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1976 kind != HInstruction::kNeg));
1977 switch (kind) {
1978 case HInstruction::kAdd:
1979 __ Add(out, left, right_operand);
1980 break;
1981 case HInstruction::kAnd:
1982 __ And(out, left, right_operand);
1983 break;
1984 case HInstruction::kNeg:
1985 DCHECK(instruction->InputAt(0)->AsConstant()->IsZero());
1986 __ Neg(out, right_operand);
1987 break;
1988 case HInstruction::kOr:
1989 __ Orr(out, left, right_operand);
1990 break;
1991 case HInstruction::kSub:
1992 __ Sub(out, left, right_operand);
1993 break;
1994 case HInstruction::kXor:
1995 __ Eor(out, left, right_operand);
1996 break;
1997 default:
1998 LOG(FATAL) << "Unexpected operation kind: " << kind;
1999 UNREACHABLE();
2000 }
2001}
2002
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002003void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002004 // The read barrier instrumentation does not support the
2005 // HArm64IntermediateAddress instruction yet.
2006 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002007 LocationSummary* locations =
2008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2009 locations->SetInAt(0, Location::RequiresRegister());
2010 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2011 locations->SetOut(Location::RequiresRegister());
2012}
2013
2014void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
2015 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002016 // The read barrier instrumentation does not support the
2017 // HArm64IntermediateAddress instruction yet.
2018 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002019 __ Add(OutputRegister(instruction),
2020 InputRegisterAt(instruction, 0),
2021 Operand(InputOperandAt(instruction, 1)));
2022}
2023
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00002024void LocationsBuilderARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002025 LocationSummary* locations =
2026 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00002027 locations->SetInAt(HArm64MultiplyAccumulate::kInputAccumulatorIndex,
2028 Location::RequiresRegister());
2029 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2030 locations->SetInAt(HArm64MultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2032}
2033
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00002034void InstructionCodeGeneratorARM64::VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002035 Register res = OutputRegister(instr);
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00002036 Register accumulator = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputAccumulatorIndex);
2037 Register mul_left = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulLeftIndex);
2038 Register mul_right = InputRegisterAt(instr, HArm64MultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002039
2040 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2041 // This fixup should be carried out for all multiply-accumulate instructions:
2042 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2043 if (instr->GetType() == Primitive::kPrimLong &&
2044 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2045 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2046 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2047 if (prev->IsLoadOrStore()) {
2048 // Make sure we emit only exactly one nop.
2049 vixl::CodeBufferCheckScope scope(masm,
2050 vixl::kInstructionSize,
2051 vixl::CodeBufferCheckScope::kCheck,
2052 vixl::CodeBufferCheckScope::kExactSize);
2053 __ nop();
2054 }
2055 }
2056
2057 if (instr->GetOpKind() == HInstruction::kAdd) {
2058 __ Madd(res, mul_left, mul_right, accumulator);
2059 } else {
2060 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00002061 __ Msub(res, mul_left, mul_right, accumulator);
Alexandre Rames418318f2015-11-20 15:55:47 +00002062 }
2063}
2064
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002065void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002066 bool object_array_get_with_read_barrier =
2067 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002068 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002069 new (GetGraph()->GetArena()) LocationSummary(instruction,
2070 object_array_get_with_read_barrier ?
2071 LocationSummary::kCallOnSlowPath :
2072 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002073 locations->SetInAt(0, Location::RequiresRegister());
2074 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002075 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2076 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2077 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002078 // The output overlaps in the case of an object array get with
2079 // read barriers enabled: we do not want the move to overwrite the
2080 // array's location, as we need it to emit the read barrier.
2081 locations->SetOut(
2082 Location::RequiresRegister(),
2083 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002084 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002085}
2086
2087void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002088 Primitive::Type type = instruction->GetType();
2089 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002090 LocationSummary* locations = instruction->GetLocations();
2091 Location index = locations->InAt(1);
2092 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Roland Levillain44015862016-01-22 11:47:17 +00002093 Location out = locations->Out();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002094
Alexandre Ramesd921d642015-04-16 15:07:16 +01002095 MacroAssembler* masm = GetVIXLAssembler();
2096 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002097 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002098 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002099
Roland Levillain44015862016-01-22 11:47:17 +00002100 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2101 // Object ArrayGet with Baker's read barrier case.
2102 Register temp = temps.AcquireW();
2103 // The read barrier instrumentation does not support the
2104 // HArm64IntermediateAddress instruction yet.
2105 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2106 // Note that a potential implicit null check is handled in the
2107 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2108 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2109 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002110 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002111 // General case.
2112 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002113 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002114 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2115 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002116 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002117 Register temp = temps.AcquireSameSizeAs(obj);
2118 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2119 // The read barrier instrumentation does not support the
2120 // HArm64IntermediateAddress instruction yet.
2121 DCHECK(!kEmitCompilerReadBarrier);
2122 // We do not need to compute the intermediate address from the array: the
2123 // input instruction has done it already. See the comment in
2124 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2125 if (kIsDebugBuild) {
2126 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2127 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2128 }
2129 temp = obj;
2130 } else {
2131 __ Add(temp, obj, offset);
2132 }
2133 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2134 }
2135
2136 codegen_->Load(type, OutputCPURegister(instruction), source);
2137 codegen_->MaybeRecordImplicitNullCheck(instruction);
2138
2139 if (type == Primitive::kPrimNot) {
2140 static_assert(
2141 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2142 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2143 Location obj_loc = locations->InAt(0);
2144 if (index.IsConstant()) {
2145 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2146 } else {
2147 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2148 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002149 }
Roland Levillain4d027112015-07-01 15:41:14 +01002150 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002151}
2152
Alexandre Rames5319def2014-10-23 10:03:10 +01002153void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2155 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002157}
2158
2159void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01002160 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01002161 __ Ldr(OutputRegister(instruction),
2162 HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset()));
Calin Juravle77520bc2015-01-12 18:45:46 +00002163 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002164}
2165
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002166void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002167 Primitive::Type value_type = instruction->GetComponentType();
2168
2169 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2170 bool object_array_set_with_read_barrier =
2171 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2173 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002174 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2175 LocationSummary::kCallOnSlowPath :
2176 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 locations->SetInAt(0, Location::RequiresRegister());
2178 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002179 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002180 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002181 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002182 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002183 }
2184}
2185
2186void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2187 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002188 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002189 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002190 bool needs_write_barrier =
2191 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002192
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002193 Register array = InputRegisterAt(instruction, 0);
2194 CPURegister value = InputCPURegisterAt(instruction, 2);
2195 CPURegister source = value;
2196 Location index = locations->InAt(1);
2197 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2198 MemOperand destination = HeapOperand(array);
2199 MacroAssembler* masm = GetVIXLAssembler();
2200 BlockPoolsScope block_pools(masm);
2201
2202 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002203 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002204 if (index.IsConstant()) {
2205 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2206 destination = HeapOperand(array, offset);
2207 } else {
2208 UseScratchRegisterScope temps(masm);
2209 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002210 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002211 // The read barrier instrumentation does not support the
2212 // HArm64IntermediateAddress instruction yet.
2213 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002214 // We do not need to compute the intermediate address from the array: the
2215 // input instruction has done it already. See the comment in
2216 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2217 if (kIsDebugBuild) {
2218 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2219 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2220 }
2221 temp = array;
2222 } else {
2223 __ Add(temp, array, offset);
2224 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002225 destination = HeapOperand(temp,
2226 XRegisterFrom(index),
2227 LSL,
2228 Primitive::ComponentSizeShift(value_type));
2229 }
2230 codegen_->Store(value_type, value, destination);
2231 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002232 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002233 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002234 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002235 vixl::Label done;
2236 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002237 {
2238 // We use a block to end the scratch scope before the write barrier, thus
2239 // freeing the temporary registers so they can be used in `MarkGCCard`.
2240 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002241 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002242 if (index.IsConstant()) {
2243 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002244 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002245 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002246 destination = HeapOperand(temp,
2247 XRegisterFrom(index),
2248 LSL,
2249 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002250 }
2251
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002252 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2253 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2254 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2255
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002256 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002257 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2258 codegen_->AddSlowPath(slow_path);
2259 if (instruction->GetValueCanBeNull()) {
2260 vixl::Label non_zero;
2261 __ Cbnz(Register(value), &non_zero);
2262 if (!index.IsConstant()) {
2263 __ Add(temp, array, offset);
2264 }
2265 __ Str(wzr, destination);
2266 codegen_->MaybeRecordImplicitNullCheck(instruction);
2267 __ B(&done);
2268 __ Bind(&non_zero);
2269 }
2270
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002271 if (kEmitCompilerReadBarrier) {
2272 // When read barriers are enabled, the type checking
2273 // instrumentation requires two read barriers:
2274 //
2275 // __ Mov(temp2, temp);
2276 // // /* HeapReference<Class> */ temp = temp->component_type_
2277 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002278 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002279 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2280 //
2281 // // /* HeapReference<Class> */ temp2 = value->klass_
2282 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002283 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002284 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2285 //
2286 // __ Cmp(temp, temp2);
2287 //
2288 // However, the second read barrier may trash `temp`, as it
2289 // is a temporary register, and as such would not be saved
2290 // along with live registers before calling the runtime (nor
2291 // restored afterwards). So in this case, we bail out and
2292 // delegate the work to the array set slow path.
2293 //
2294 // TODO: Extend the register allocator to support a new
2295 // "(locally) live temp" location so as to avoid always
2296 // going into the slow path when read barriers are enabled.
2297 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002298 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002299 Register temp2 = temps.AcquireSameSizeAs(array);
2300 // /* HeapReference<Class> */ temp = array->klass_
2301 __ Ldr(temp, HeapOperand(array, class_offset));
2302 codegen_->MaybeRecordImplicitNullCheck(instruction);
2303 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2304
2305 // /* HeapReference<Class> */ temp = temp->component_type_
2306 __ Ldr(temp, HeapOperand(temp, component_offset));
2307 // /* HeapReference<Class> */ temp2 = value->klass_
2308 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2309 // If heap poisoning is enabled, no need to unpoison `temp`
2310 // nor `temp2`, as we are comparing two poisoned references.
2311 __ Cmp(temp, temp2);
2312
2313 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2314 vixl::Label do_put;
2315 __ B(eq, &do_put);
2316 // If heap poisoning is enabled, the `temp` reference has
2317 // not been unpoisoned yet; unpoison it now.
2318 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2319
2320 // /* HeapReference<Class> */ temp = temp->super_class_
2321 __ Ldr(temp, HeapOperand(temp, super_offset));
2322 // If heap poisoning is enabled, no need to unpoison
2323 // `temp`, as we are comparing against null below.
2324 __ Cbnz(temp, slow_path->GetEntryLabel());
2325 __ Bind(&do_put);
2326 } else {
2327 __ B(ne, slow_path->GetEntryLabel());
2328 }
2329 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002330 }
2331 }
2332
2333 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002334 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002335 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002336 __ Mov(temp2, value.W());
2337 GetAssembler()->PoisonHeapReference(temp2);
2338 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002339 }
2340
2341 if (!index.IsConstant()) {
2342 __ Add(temp, array, offset);
2343 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002344 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002345
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002346 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002347 codegen_->MaybeRecordImplicitNullCheck(instruction);
2348 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002349 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002350
2351 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2352
2353 if (done.IsLinked()) {
2354 __ Bind(&done);
2355 }
2356
2357 if (slow_path != nullptr) {
2358 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002359 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002360 }
2361}
2362
Alexandre Rames67555f72014-11-18 10:55:16 +00002363void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002364 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2365 ? LocationSummary::kCallOnSlowPath
2366 : LocationSummary::kNoCall;
2367 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002368 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002369 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002370 if (instruction->HasUses()) {
2371 locations->SetOut(Location::SameAsFirstInput());
2372 }
2373}
2374
2375void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002376 BoundsCheckSlowPathARM64* slow_path =
2377 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002378 codegen_->AddSlowPath(slow_path);
2379
2380 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2381 __ B(slow_path->GetEntryLabel(), hs);
2382}
2383
Alexandre Rames67555f72014-11-18 10:55:16 +00002384void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2385 LocationSummary* locations =
2386 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2387 locations->SetInAt(0, Location::RequiresRegister());
2388 if (check->HasUses()) {
2389 locations->SetOut(Location::SameAsFirstInput());
2390 }
2391}
2392
2393void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2394 // We assume the class is not null.
2395 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2396 check->GetLoadClass(), check, check->GetDexPc(), true);
2397 codegen_->AddSlowPath(slow_path);
2398 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2399}
2400
Roland Levillain7f63c522015-07-13 15:54:55 +00002401static bool IsFloatingPointZeroConstant(HInstruction* instruction) {
2402 return (instruction->IsFloatConstant() && (instruction->AsFloatConstant()->GetValue() == 0.0f))
2403 || (instruction->IsDoubleConstant() && (instruction->AsDoubleConstant()->GetValue() == 0.0));
2404}
2405
Serban Constantinescu02164b32014-11-13 14:05:07 +00002406void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002407 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002408 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2409 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002410 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08002411 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002412 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002413 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002414 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2416 break;
2417 }
2418 case Primitive::kPrimFloat:
2419 case Primitive::kPrimDouble: {
2420 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002421 locations->SetInAt(1,
2422 IsFloatingPointZeroConstant(compare->InputAt(1))
2423 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2424 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002425 locations->SetOut(Location::RequiresRegister());
2426 break;
2427 }
2428 default:
2429 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2430 }
2431}
2432
2433void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2434 Primitive::Type in_type = compare->InputAt(0)->GetType();
2435
2436 // 0 if: left == right
2437 // 1 if: left > right
2438 // -1 if: left < right
2439 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08002440 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002441 case Primitive::kPrimLong: {
2442 Register result = OutputRegister(compare);
2443 Register left = InputRegisterAt(compare, 0);
2444 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002445 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002446 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2447 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002448 break;
2449 }
2450 case Primitive::kPrimFloat:
2451 case Primitive::kPrimDouble: {
2452 Register result = OutputRegister(compare);
2453 FPRegister left = InputFPRegisterAt(compare, 0);
Alexandre Rames93415462015-02-17 15:08:20 +00002454 if (compare->GetLocations()->InAt(1).IsConstant()) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002455 DCHECK(IsFloatingPointZeroConstant(compare->GetLocations()->InAt(1).GetConstant()));
2456 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
Alexandre Rames93415462015-02-17 15:08:20 +00002457 __ Fcmp(left, 0.0);
2458 } else {
2459 __ Fcmp(left, InputFPRegisterAt(compare, 1));
2460 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002461 __ Cset(result, ne);
2462 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002463 break;
2464 }
2465 default:
2466 LOG(FATAL) << "Unimplemented compare type " << in_type;
2467 }
2468}
2469
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002470void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002472
2473 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2474 locations->SetInAt(0, Location::RequiresFpuRegister());
2475 locations->SetInAt(1,
2476 IsFloatingPointZeroConstant(instruction->InputAt(1))
2477 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2478 : Location::RequiresFpuRegister());
2479 } else {
2480 // Integer cases.
2481 locations->SetInAt(0, Location::RequiresRegister());
2482 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2483 }
2484
David Brazdilb3e773e2016-01-26 11:28:37 +00002485 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002487 }
2488}
2489
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002490void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002491 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002492 return;
2493 }
2494
2495 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002496 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002497 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002498
Roland Levillain7f63c522015-07-13 15:54:55 +00002499 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2500 FPRegister lhs = InputFPRegisterAt(instruction, 0);
2501 if (locations->InAt(1).IsConstant()) {
2502 DCHECK(IsFloatingPointZeroConstant(locations->InAt(1).GetConstant()));
2503 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2504 __ Fcmp(lhs, 0.0);
2505 } else {
2506 __ Fcmp(lhs, InputFPRegisterAt(instruction, 1));
2507 }
Vladimir Markod6e069b2016-01-18 11:11:01 +00002508 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002509 } else {
2510 // Integer cases.
2511 Register lhs = InputRegisterAt(instruction, 0);
2512 Operand rhs = InputOperandAt(instruction, 1);
2513 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002514 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002515 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002516}
2517
2518#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2519 M(Equal) \
2520 M(NotEqual) \
2521 M(LessThan) \
2522 M(LessThanOrEqual) \
2523 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002524 M(GreaterThanOrEqual) \
2525 M(Below) \
2526 M(BelowOrEqual) \
2527 M(Above) \
2528 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002529#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002530void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2531void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002532FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002533#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002534#undef FOR_EACH_CONDITION_INSTRUCTION
2535
Zheng Xuc6667102015-05-15 16:08:45 +08002536void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2537 DCHECK(instruction->IsDiv() || instruction->IsRem());
2538
2539 LocationSummary* locations = instruction->GetLocations();
2540 Location second = locations->InAt(1);
2541 DCHECK(second.IsConstant());
2542
2543 Register out = OutputRegister(instruction);
2544 Register dividend = InputRegisterAt(instruction, 0);
2545 int64_t imm = Int64FromConstant(second.GetConstant());
2546 DCHECK(imm == 1 || imm == -1);
2547
2548 if (instruction->IsRem()) {
2549 __ Mov(out, 0);
2550 } else {
2551 if (imm == 1) {
2552 __ Mov(out, dividend);
2553 } else {
2554 __ Neg(out, dividend);
2555 }
2556 }
2557}
2558
2559void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2560 DCHECK(instruction->IsDiv() || instruction->IsRem());
2561
2562 LocationSummary* locations = instruction->GetLocations();
2563 Location second = locations->InAt(1);
2564 DCHECK(second.IsConstant());
2565
2566 Register out = OutputRegister(instruction);
2567 Register dividend = InputRegisterAt(instruction, 0);
2568 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002569 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002570 int ctz_imm = CTZ(abs_imm);
2571
2572 UseScratchRegisterScope temps(GetVIXLAssembler());
2573 Register temp = temps.AcquireSameSizeAs(out);
2574
2575 if (instruction->IsDiv()) {
2576 __ Add(temp, dividend, abs_imm - 1);
2577 __ Cmp(dividend, 0);
2578 __ Csel(out, temp, dividend, lt);
2579 if (imm > 0) {
2580 __ Asr(out, out, ctz_imm);
2581 } else {
2582 __ Neg(out, Operand(out, ASR, ctz_imm));
2583 }
2584 } else {
2585 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2586 __ Asr(temp, dividend, bits - 1);
2587 __ Lsr(temp, temp, bits - ctz_imm);
2588 __ Add(out, dividend, temp);
2589 __ And(out, out, abs_imm - 1);
2590 __ Sub(out, out, temp);
2591 }
2592}
2593
2594void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2595 DCHECK(instruction->IsDiv() || instruction->IsRem());
2596
2597 LocationSummary* locations = instruction->GetLocations();
2598 Location second = locations->InAt(1);
2599 DCHECK(second.IsConstant());
2600
2601 Register out = OutputRegister(instruction);
2602 Register dividend = InputRegisterAt(instruction, 0);
2603 int64_t imm = Int64FromConstant(second.GetConstant());
2604
2605 Primitive::Type type = instruction->GetResultType();
2606 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2607
2608 int64_t magic;
2609 int shift;
2610 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2611
2612 UseScratchRegisterScope temps(GetVIXLAssembler());
2613 Register temp = temps.AcquireSameSizeAs(out);
2614
2615 // temp = get_high(dividend * magic)
2616 __ Mov(temp, magic);
2617 if (type == Primitive::kPrimLong) {
2618 __ Smulh(temp, dividend, temp);
2619 } else {
2620 __ Smull(temp.X(), dividend, temp);
2621 __ Lsr(temp.X(), temp.X(), 32);
2622 }
2623
2624 if (imm > 0 && magic < 0) {
2625 __ Add(temp, temp, dividend);
2626 } else if (imm < 0 && magic > 0) {
2627 __ Sub(temp, temp, dividend);
2628 }
2629
2630 if (shift != 0) {
2631 __ Asr(temp, temp, shift);
2632 }
2633
2634 if (instruction->IsDiv()) {
2635 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2636 } else {
2637 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2638 // TODO: Strength reduction for msub.
2639 Register temp_imm = temps.AcquireSameSizeAs(out);
2640 __ Mov(temp_imm, imm);
2641 __ Msub(out, temp, temp_imm, dividend);
2642 }
2643}
2644
2645void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2646 DCHECK(instruction->IsDiv() || instruction->IsRem());
2647 Primitive::Type type = instruction->GetResultType();
2648 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2649
2650 LocationSummary* locations = instruction->GetLocations();
2651 Register out = OutputRegister(instruction);
2652 Location second = locations->InAt(1);
2653
2654 if (second.IsConstant()) {
2655 int64_t imm = Int64FromConstant(second.GetConstant());
2656
2657 if (imm == 0) {
2658 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2659 } else if (imm == 1 || imm == -1) {
2660 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002661 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002662 DivRemByPowerOfTwo(instruction);
2663 } else {
2664 DCHECK(imm <= -2 || imm >= 2);
2665 GenerateDivRemWithAnyConstant(instruction);
2666 }
2667 } else {
2668 Register dividend = InputRegisterAt(instruction, 0);
2669 Register divisor = InputRegisterAt(instruction, 1);
2670 if (instruction->IsDiv()) {
2671 __ Sdiv(out, dividend, divisor);
2672 } else {
2673 UseScratchRegisterScope temps(GetVIXLAssembler());
2674 Register temp = temps.AcquireSameSizeAs(out);
2675 __ Sdiv(temp, dividend, divisor);
2676 __ Msub(out, temp, divisor, dividend);
2677 }
2678 }
2679}
2680
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002681void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2682 LocationSummary* locations =
2683 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2684 switch (div->GetResultType()) {
2685 case Primitive::kPrimInt:
2686 case Primitive::kPrimLong:
2687 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002688 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002689 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2690 break;
2691
2692 case Primitive::kPrimFloat:
2693 case Primitive::kPrimDouble:
2694 locations->SetInAt(0, Location::RequiresFpuRegister());
2695 locations->SetInAt(1, Location::RequiresFpuRegister());
2696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2701 }
2702}
2703
2704void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2705 Primitive::Type type = div->GetResultType();
2706 switch (type) {
2707 case Primitive::kPrimInt:
2708 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002709 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002710 break;
2711
2712 case Primitive::kPrimFloat:
2713 case Primitive::kPrimDouble:
2714 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2715 break;
2716
2717 default:
2718 LOG(FATAL) << "Unexpected div type " << type;
2719 }
2720}
2721
Alexandre Rames67555f72014-11-18 10:55:16 +00002722void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002723 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2724 ? LocationSummary::kCallOnSlowPath
2725 : LocationSummary::kNoCall;
2726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002727 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2728 if (instruction->HasUses()) {
2729 locations->SetOut(Location::SameAsFirstInput());
2730 }
2731}
2732
2733void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2734 SlowPathCodeARM64* slow_path =
2735 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2736 codegen_->AddSlowPath(slow_path);
2737 Location value = instruction->GetLocations()->InAt(0);
2738
Alexandre Rames3e69f162014-12-10 10:36:50 +00002739 Primitive::Type type = instruction->GetType();
2740
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002741 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
2742 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002743 return;
2744 }
2745
Alexandre Rames67555f72014-11-18 10:55:16 +00002746 if (value.IsConstant()) {
2747 int64_t divisor = Int64ConstantFrom(value);
2748 if (divisor == 0) {
2749 __ B(slow_path->GetEntryLabel());
2750 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002751 // A division by a non-null constant is valid. We don't need to perform
2752 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002753 }
2754 } else {
2755 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2756 }
2757}
2758
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002759void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2762 locations->SetOut(Location::ConstantLocation(constant));
2763}
2764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002765void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2766 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002767 // Will be generated at use site.
2768}
2769
Alexandre Rames5319def2014-10-23 10:03:10 +01002770void LocationsBuilderARM64::VisitExit(HExit* exit) {
2771 exit->SetLocations(nullptr);
2772}
2773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002774void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002775}
2776
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002777void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2778 LocationSummary* locations =
2779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2780 locations->SetOut(Location::ConstantLocation(constant));
2781}
2782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002783void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002784 // Will be generated at use site.
2785}
2786
David Brazdilfc6a86a2015-06-26 10:33:45 +00002787void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002788 DCHECK(!successor->IsExitBlock());
2789 HBasicBlock* block = got->GetBlock();
2790 HInstruction* previous = got->GetPrevious();
2791 HLoopInformation* info = block->GetLoopInformation();
2792
David Brazdil46e2a392015-03-16 17:31:52 +00002793 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002794 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2795 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2796 return;
2797 }
2798 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2799 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2800 }
2801 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002802 __ B(codegen_->GetLabelOf(successor));
2803 }
2804}
2805
David Brazdilfc6a86a2015-06-26 10:33:45 +00002806void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2807 got->SetLocations(nullptr);
2808}
2809
2810void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2811 HandleGoto(got, got->GetSuccessor());
2812}
2813
2814void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2815 try_boundary->SetLocations(nullptr);
2816}
2817
2818void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2819 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2820 if (!successor->IsExitBlock()) {
2821 HandleGoto(try_boundary, successor);
2822 }
2823}
2824
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002825void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002826 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002827 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002828 vixl::Label* false_target) {
2829 // FP branching requires both targets to be explicit. If either of the targets
2830 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2831 vixl::Label fallthrough_target;
2832 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002833
David Brazdil0debae72015-11-12 18:37:00 +00002834 if (true_target == nullptr && false_target == nullptr) {
2835 // Nothing to do. The code always falls through.
2836 return;
2837 } else if (cond->IsIntConstant()) {
2838 // Constant condition, statically compared against 1.
2839 if (cond->AsIntConstant()->IsOne()) {
2840 if (true_target != nullptr) {
2841 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002842 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002843 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002844 DCHECK(cond->AsIntConstant()->IsZero());
2845 if (false_target != nullptr) {
2846 __ B(false_target);
2847 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002848 }
David Brazdil0debae72015-11-12 18:37:00 +00002849 return;
2850 }
2851
2852 // The following code generates these patterns:
2853 // (1) true_target == nullptr && false_target != nullptr
2854 // - opposite condition true => branch to false_target
2855 // (2) true_target != nullptr && false_target == nullptr
2856 // - condition true => branch to true_target
2857 // (3) true_target != nullptr && false_target != nullptr
2858 // - condition true => branch to true_target
2859 // - branch to false_target
2860 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002861 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002862 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002863 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002864 if (true_target == nullptr) {
2865 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2866 } else {
2867 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2868 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002869 } else {
2870 // The condition instruction has not been materialized, use its inputs as
2871 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002872 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002873
David Brazdil0debae72015-11-12 18:37:00 +00002874 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002875 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002876 FPRegister lhs = InputFPRegisterAt(condition, 0);
2877 if (condition->GetLocations()->InAt(1).IsConstant()) {
2878 DCHECK(IsFloatingPointZeroConstant(condition->GetLocations()->InAt(1).GetConstant()));
2879 // 0.0 is the only immediate that can be encoded directly in an FCMP instruction.
2880 __ Fcmp(lhs, 0.0);
2881 } else {
2882 __ Fcmp(lhs, InputFPRegisterAt(condition, 1));
2883 }
David Brazdil0debae72015-11-12 18:37:00 +00002884 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002885 IfCondition opposite_condition = condition->GetOppositeCondition();
2886 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002887 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002888 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002889 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002890 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002891 // Integer cases.
2892 Register lhs = InputRegisterAt(condition, 0);
2893 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002894
2895 Condition arm64_cond;
2896 vixl::Label* non_fallthrough_target;
2897 if (true_target == nullptr) {
2898 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2899 non_fallthrough_target = false_target;
2900 } else {
2901 arm64_cond = ARM64Condition(condition->GetCondition());
2902 non_fallthrough_target = true_target;
2903 }
2904
Aart Bik086d27e2016-01-20 17:02:00 -08002905 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2906 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002907 switch (arm64_cond) {
2908 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002909 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002910 break;
2911 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002912 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002913 break;
2914 case lt:
2915 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002916 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002917 break;
2918 case ge:
2919 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002920 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002921 break;
2922 default:
2923 // Without the `static_cast` the compiler throws an error for
2924 // `-Werror=sign-promo`.
2925 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2926 }
2927 } else {
2928 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002929 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002930 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002931 }
2932 }
David Brazdil0debae72015-11-12 18:37:00 +00002933
2934 // If neither branch falls through (case 3), the conditional branch to `true_target`
2935 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2936 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002937 __ B(false_target);
2938 }
David Brazdil0debae72015-11-12 18:37:00 +00002939
2940 if (fallthrough_target.IsLinked()) {
2941 __ Bind(&fallthrough_target);
2942 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002943}
2944
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002945void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002947 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002948 locations->SetInAt(0, Location::RequiresRegister());
2949 }
2950}
2951
2952void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002953 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2954 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2955 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2956 nullptr : codegen_->GetLabelOf(true_successor);
2957 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2958 nullptr : codegen_->GetLabelOf(false_successor);
2959 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002960}
2961
2962void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2963 LocationSummary* locations = new (GetGraph()->GetArena())
2964 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002965 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002966 locations->SetInAt(0, Location::RequiresRegister());
2967 }
2968}
2969
2970void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002971 SlowPathCodeARM64* slow_path =
2972 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002973 GenerateTestAndBranch(deoptimize,
2974 /* condition_input_index */ 0,
2975 slow_path->GetEntryLabel(),
2976 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002977}
2978
David Brazdil74eb1b22015-12-14 11:44:01 +00002979void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2981 if (Primitive::IsFloatingPointType(select->GetType())) {
2982 locations->SetInAt(0, Location::RequiresFpuRegister());
2983 locations->SetInAt(1, Location::RequiresFpuRegister());
2984 } else {
2985 locations->SetInAt(0, Location::RequiresRegister());
2986 locations->SetInAt(1, Location::RequiresRegister());
2987 }
2988 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2989 locations->SetInAt(2, Location::RequiresRegister());
2990 }
2991 locations->SetOut(Location::SameAsFirstInput());
2992}
2993
2994void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
2995 LocationSummary* locations = select->GetLocations();
2996 vixl::Label false_target;
2997 GenerateTestAndBranch(select,
2998 /* condition_input_index */ 2,
2999 /* true_target */ nullptr,
3000 &false_target);
3001 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3002 __ Bind(&false_target);
3003}
3004
David Srbecky0cf44932015-12-09 14:09:59 +00003005void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3006 new (GetGraph()->GetArena()) LocationSummary(info);
3007}
3008
3009void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00003010 if (codegen_->HasStackMapAtCurrentPc()) {
3011 // Ensure that we do not collide with the stack map of the previous instruction.
3012 __ Nop();
3013 }
David Srbecky0cf44932015-12-09 14:09:59 +00003014 codegen_->RecordPcInfo(info, info->GetDexPc());
3015}
3016
Alexandre Rames5319def2014-10-23 10:03:10 +01003017void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003018 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003019}
3020
3021void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003022 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003023}
3024
3025void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003026 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003027}
3028
3029void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003030 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003031}
3032
Roland Levillain44015862016-01-22 11:47:17 +00003033static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3034 return kEmitCompilerReadBarrier &&
3035 (kUseBakerReadBarrier ||
3036 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3037 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3038 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3039}
3040
Alexandre Rames67555f72014-11-18 10:55:16 +00003041void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003042 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003043 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3044 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003045 case TypeCheckKind::kExactCheck:
3046 case TypeCheckKind::kAbstractClassCheck:
3047 case TypeCheckKind::kClassHierarchyCheck:
3048 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003049 call_kind =
3050 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003051 break;
3052 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003053 case TypeCheckKind::kUnresolvedCheck:
3054 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003055 call_kind = LocationSummary::kCallOnSlowPath;
3056 break;
3057 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003058
Alexandre Rames67555f72014-11-18 10:55:16 +00003059 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003060 locations->SetInAt(0, Location::RequiresRegister());
3061 locations->SetInAt(1, Location::RequiresRegister());
3062 // The "out" register is used as a temporary, so it overlaps with the inputs.
3063 // Note that TypeCheckSlowPathARM64 uses this register too.
3064 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3065 // When read barriers are enabled, we need a temporary register for
3066 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003067 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003068 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003069 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003070}
3071
3072void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003073 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003074 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003075 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003076 Register obj = InputRegisterAt(instruction, 0);
3077 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003078 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003079 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003080 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3081 locations->GetTemp(0) :
3082 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003083 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3084 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3085 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3086 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003087
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003088 vixl::Label done, zero;
3089 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003090
3091 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003092 // Avoid null check if we know `obj` is not null.
3093 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003094 __ Cbz(obj, &zero);
3095 }
3096
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003097 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003098 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003099
Roland Levillain44015862016-01-22 11:47:17 +00003100 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003101 case TypeCheckKind::kExactCheck: {
3102 __ Cmp(out, cls);
3103 __ Cset(out, eq);
3104 if (zero.IsLinked()) {
3105 __ B(&done);
3106 }
3107 break;
3108 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003109
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003110 case TypeCheckKind::kAbstractClassCheck: {
3111 // If the class is abstract, we eagerly fetch the super class of the
3112 // object to avoid doing a comparison we know will fail.
3113 vixl::Label loop, success;
3114 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003115 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003116 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003117 // If `out` is null, we use it for the result, and jump to `done`.
3118 __ Cbz(out, &done);
3119 __ Cmp(out, cls);
3120 __ B(ne, &loop);
3121 __ Mov(out, 1);
3122 if (zero.IsLinked()) {
3123 __ B(&done);
3124 }
3125 break;
3126 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003127
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003128 case TypeCheckKind::kClassHierarchyCheck: {
3129 // Walk over the class hierarchy to find a match.
3130 vixl::Label loop, success;
3131 __ Bind(&loop);
3132 __ Cmp(out, cls);
3133 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003134 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003135 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003136 __ Cbnz(out, &loop);
3137 // If `out` is null, we use it for the result, and jump to `done`.
3138 __ B(&done);
3139 __ Bind(&success);
3140 __ Mov(out, 1);
3141 if (zero.IsLinked()) {
3142 __ B(&done);
3143 }
3144 break;
3145 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003146
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003147 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003148 // Do an exact check.
3149 vixl::Label exact_check;
3150 __ Cmp(out, cls);
3151 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003152 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003153 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003154 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003155 // If `out` is null, we use it for the result, and jump to `done`.
3156 __ Cbz(out, &done);
3157 __ Ldrh(out, HeapOperand(out, primitive_offset));
3158 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3159 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003160 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003161 __ Mov(out, 1);
3162 __ B(&done);
3163 break;
3164 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003166 case TypeCheckKind::kArrayCheck: {
3167 __ Cmp(out, cls);
3168 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003169 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3170 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003171 codegen_->AddSlowPath(slow_path);
3172 __ B(ne, slow_path->GetEntryLabel());
3173 __ Mov(out, 1);
3174 if (zero.IsLinked()) {
3175 __ B(&done);
3176 }
3177 break;
3178 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003179
Calin Juravle98893e12015-10-02 21:05:03 +01003180 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003181 case TypeCheckKind::kInterfaceCheck: {
3182 // Note that we indeed only call on slow path, but we always go
3183 // into the slow path for the unresolved and interface check
3184 // cases.
3185 //
3186 // We cannot directly call the InstanceofNonTrivial runtime
3187 // entry point without resorting to a type checking slow path
3188 // here (i.e. by calling InvokeRuntime directly), as it would
3189 // require to assign fixed registers for the inputs of this
3190 // HInstanceOf instruction (following the runtime calling
3191 // convention), which might be cluttered by the potential first
3192 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003193 //
3194 // TODO: Introduce a new runtime entry point taking the object
3195 // to test (instead of its class) as argument, and let it deal
3196 // with the read barrier issues. This will let us refactor this
3197 // case of the `switch` code as it was previously (with a direct
3198 // call to the runtime not using a type checking slow path).
3199 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003200 DCHECK(locations->OnlyCallsOnSlowPath());
3201 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3202 /* is_fatal */ false);
3203 codegen_->AddSlowPath(slow_path);
3204 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003205 if (zero.IsLinked()) {
3206 __ B(&done);
3207 }
3208 break;
3209 }
3210 }
3211
3212 if (zero.IsLinked()) {
3213 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003214 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003215 }
3216
3217 if (done.IsLinked()) {
3218 __ Bind(&done);
3219 }
3220
3221 if (slow_path != nullptr) {
3222 __ Bind(slow_path->GetExitLabel());
3223 }
3224}
3225
3226void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3227 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3228 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3229
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003230 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3231 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003232 case TypeCheckKind::kExactCheck:
3233 case TypeCheckKind::kAbstractClassCheck:
3234 case TypeCheckKind::kClassHierarchyCheck:
3235 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003236 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3237 LocationSummary::kCallOnSlowPath :
3238 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003239 break;
3240 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003241 case TypeCheckKind::kUnresolvedCheck:
3242 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003243 call_kind = LocationSummary::kCallOnSlowPath;
3244 break;
3245 }
3246
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3248 locations->SetInAt(0, Location::RequiresRegister());
3249 locations->SetInAt(1, Location::RequiresRegister());
3250 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3251 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003252 // When read barriers are enabled, we need an additional temporary
3253 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003254 if (TypeCheckNeedsATemporary(type_check_kind)) {
3255 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003256 }
3257}
3258
3259void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003260 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003262 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003263 Register obj = InputRegisterAt(instruction, 0);
3264 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003265 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003266 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3267 locations->GetTemp(1) :
3268 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003269 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003270 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3271 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3272 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3273 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003274
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003275 bool is_type_check_slow_path_fatal =
3276 (type_check_kind == TypeCheckKind::kExactCheck ||
3277 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3278 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3279 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3280 !instruction->CanThrowIntoCatchBlock();
3281 SlowPathCodeARM64* type_check_slow_path =
3282 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3283 is_type_check_slow_path_fatal);
3284 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285
3286 vixl::Label done;
3287 // Avoid null check if we know obj is not null.
3288 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003289 __ Cbz(obj, &done);
3290 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003291
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003292 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003293 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003294
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003295 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003296 case TypeCheckKind::kExactCheck:
3297 case TypeCheckKind::kArrayCheck: {
3298 __ Cmp(temp, cls);
3299 // Jump to slow path for throwing the exception or doing a
3300 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003301 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003302 break;
3303 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003304
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003305 case TypeCheckKind::kAbstractClassCheck: {
3306 // If the class is abstract, we eagerly fetch the super class of the
3307 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003308 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003309 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003310 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003311 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003312
3313 // If the class reference currently in `temp` is not null, jump
3314 // to the `compare_classes` label to compare it with the checked
3315 // class.
3316 __ Cbnz(temp, &compare_classes);
3317 // Otherwise, jump to the slow path to throw the exception.
3318 //
3319 // But before, move back the object's class into `temp` before
3320 // going into the slow path, as it has been overwritten in the
3321 // meantime.
3322 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003323 GenerateReferenceLoadTwoRegisters(
3324 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003325 __ B(type_check_slow_path->GetEntryLabel());
3326
3327 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003328 __ Cmp(temp, cls);
3329 __ B(ne, &loop);
3330 break;
3331 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003333 case TypeCheckKind::kClassHierarchyCheck: {
3334 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003335 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003336 __ Bind(&loop);
3337 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003338 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003340 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003341 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003342
3343 // If the class reference currently in `temp` is not null, jump
3344 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003345 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003346 // Otherwise, jump to the slow path to throw the exception.
3347 //
3348 // But before, move back the object's class into `temp` before
3349 // going into the slow path, as it has been overwritten in the
3350 // meantime.
3351 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003352 GenerateReferenceLoadTwoRegisters(
3353 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 break;
3356 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003357
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003358 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003359 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003360 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003361 __ Cmp(temp, cls);
3362 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003363
3364 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003365 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003366 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003367
3368 // If the component type is not null (i.e. the object is indeed
3369 // an array), jump to label `check_non_primitive_component_type`
3370 // to further check that this component type is not a primitive
3371 // type.
3372 __ Cbnz(temp, &check_non_primitive_component_type);
3373 // Otherwise, jump to the slow path to throw the exception.
3374 //
3375 // But before, move back the object's class into `temp` before
3376 // going into the slow path, as it has been overwritten in the
3377 // meantime.
3378 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003379 GenerateReferenceLoadTwoRegisters(
3380 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003381 __ B(type_check_slow_path->GetEntryLabel());
3382
3383 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3385 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386 __ Cbz(temp, &done);
3387 // Same comment as above regarding `temp` and the slow path.
3388 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003389 GenerateReferenceLoadTwoRegisters(
3390 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003392 break;
3393 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394
Calin Juravle98893e12015-10-02 21:05:03 +01003395 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003396 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003397 // We always go into the type check slow path for the unresolved
3398 // and interface check cases.
3399 //
3400 // We cannot directly call the CheckCast runtime entry point
3401 // without resorting to a type checking slow path here (i.e. by
3402 // calling InvokeRuntime directly), as it would require to
3403 // assign fixed registers for the inputs of this HInstanceOf
3404 // instruction (following the runtime calling convention), which
3405 // might be cluttered by the potential first read barrier
3406 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003407 //
3408 // TODO: Introduce a new runtime entry point taking the object
3409 // to test (instead of its class) as argument, and let it deal
3410 // with the read barrier issues. This will let us refactor this
3411 // case of the `switch` code as it was previously (with a direct
3412 // call to the runtime not using a type checking slow path).
3413 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003414 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003415 break;
3416 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003417 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003419 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003420}
3421
Alexandre Rames5319def2014-10-23 10:03:10 +01003422void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3423 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3424 locations->SetOut(Location::ConstantLocation(constant));
3425}
3426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003427void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003428 // Will be generated at use site.
3429}
3430
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003431void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3433 locations->SetOut(Location::ConstantLocation(constant));
3434}
3435
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003436void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003437 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003438}
3439
Calin Juravle175dc732015-08-25 15:42:32 +01003440void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3441 // The trampoline uses the same calling convention as dex calling conventions,
3442 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3443 // the method_idx.
3444 HandleInvoke(invoke);
3445}
3446
3447void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3448 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3449}
3450
Alexandre Rames5319def2014-10-23 10:03:10 +01003451void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003452 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003453 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003454}
3455
Alexandre Rames67555f72014-11-18 10:55:16 +00003456void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3457 HandleInvoke(invoke);
3458}
3459
3460void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3461 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003462 LocationSummary* locations = invoke->GetLocations();
3463 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003464 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3465 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003466 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003467 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003468 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003469
3470 // The register ip1 is required to be used for the hidden argument in
3471 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003472 MacroAssembler* masm = GetVIXLAssembler();
3473 UseScratchRegisterScope scratch_scope(masm);
3474 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003475 scratch_scope.Exclude(ip1);
3476 __ Mov(ip1, invoke->GetDexMethodIndex());
3477
Alexandre Rames67555f72014-11-18 10:55:16 +00003478 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003479 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003480 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003481 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003482 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003483 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003484 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003485 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003486 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003487 // Instead of simply (possibly) unpoisoning `temp` here, we should
3488 // emit a read barrier for the previous class reference load.
3489 // However this is not required in practice, as this is an
3490 // intermediate/temporary reference and because the current
3491 // concurrent copying collector keeps the from-space memory
3492 // intact/accessible until the end of the marking phase (the
3493 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003494 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003495 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003496 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003497 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003498 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003499 // lr();
3500 __ Blr(lr);
3501 DCHECK(!codegen_->IsLeafMethod());
3502 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3503}
3504
3505void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003506 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3507 if (intrinsic.TryDispatch(invoke)) {
3508 return;
3509 }
3510
Alexandre Rames67555f72014-11-18 10:55:16 +00003511 HandleInvoke(invoke);
3512}
3513
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003514void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003515 // Explicit clinit checks triggered by static invokes must have been pruned by
3516 // art::PrepareForRegisterAllocation.
3517 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003518
Andreas Gampe878d58c2015-01-15 23:24:00 -08003519 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3520 if (intrinsic.TryDispatch(invoke)) {
3521 return;
3522 }
3523
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 HandleInvoke(invoke);
3525}
3526
Andreas Gampe878d58c2015-01-15 23:24:00 -08003527static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3528 if (invoke->GetLocations()->Intrinsified()) {
3529 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3530 intrinsic.Dispatch(invoke);
3531 return true;
3532 }
3533 return false;
3534}
3535
Vladimir Markodc151b22015-10-15 18:02:30 +01003536HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3537 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3538 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003539 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003540 return desired_dispatch_info;
3541}
3542
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003543void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003544 // For better instruction scheduling we load the direct code pointer before the method pointer.
3545 bool direct_code_loaded = false;
3546 switch (invoke->GetCodePtrLocation()) {
3547 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3548 // LR = code address from literal pool with link-time patch.
3549 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3550 direct_code_loaded = true;
3551 break;
3552 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3553 // LR = invoke->GetDirectCodePtr();
3554 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3555 direct_code_loaded = true;
3556 break;
3557 default:
3558 break;
3559 }
3560
Andreas Gampe878d58c2015-01-15 23:24:00 -08003561 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003562 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3563 switch (invoke->GetMethodLoadKind()) {
3564 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3565 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003566 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003567 break;
3568 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003569 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003570 break;
3571 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3572 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003573 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003574 break;
3575 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3576 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003577 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003578 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3579 break;
3580 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3581 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003582 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3583 invoke->GetDexCacheArrayOffset());
3584 vixl::Label* pc_insn_label = &pc_relative_dex_cache_patches_.back().label;
Vladimir Marko58155012015-08-19 12:49:41 +00003585 {
3586 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003587 __ Bind(pc_insn_label);
3588 __ adrp(XRegisterFrom(temp), 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003589 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003590 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
Vladimir Marko58155012015-08-19 12:49:41 +00003591 // Add LDR with its PC-relative DexCache access patch.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003592 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3593 invoke->GetDexCacheArrayOffset());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003594 {
3595 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3596 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3597 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), 0));
3598 pc_relative_dex_cache_patches_.back().pc_insn_label = pc_insn_label;
3599 }
Vladimir Marko58155012015-08-19 12:49:41 +00003600 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003601 }
Vladimir Marko58155012015-08-19 12:49:41 +00003602 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003603 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003604 Register reg = XRegisterFrom(temp);
3605 Register method_reg;
3606 if (current_method.IsRegister()) {
3607 method_reg = XRegisterFrom(current_method);
3608 } else {
3609 DCHECK(invoke->GetLocations()->Intrinsified());
3610 DCHECK(!current_method.IsValid());
3611 method_reg = reg;
3612 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3613 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003614
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003615 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003616 __ Ldr(reg.X(),
3617 MemOperand(method_reg.X(),
3618 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003619 // temp = temp[index_in_cache];
3620 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3621 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3622 break;
3623 }
3624 }
3625
3626 switch (invoke->GetCodePtrLocation()) {
3627 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3628 __ Bl(&frame_entry_label_);
3629 break;
3630 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3631 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3632 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003633 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3634 __ Bind(label);
3635 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003636 break;
3637 }
3638 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3639 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3640 // LR prepared above for better instruction scheduling.
3641 DCHECK(direct_code_loaded);
3642 // lr()
3643 __ Blr(lr);
3644 break;
3645 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3646 // LR = callee_method->entry_point_from_quick_compiled_code_;
3647 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003648 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003649 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3650 // lr()
3651 __ Blr(lr);
3652 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003653 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003654
Andreas Gampe878d58c2015-01-15 23:24:00 -08003655 DCHECK(!IsLeafMethod());
3656}
3657
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003658void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003659 // Use the calling convention instead of the location of the receiver, as
3660 // intrinsics may have put the receiver in a different register. In the intrinsics
3661 // slow path, the arguments have been moved to the right place, so here we are
3662 // guaranteed that the receiver is the first register of the calling convention.
3663 InvokeDexCallingConvention calling_convention;
3664 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003665 Register temp = XRegisterFrom(temp_in);
3666 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3667 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3668 Offset class_offset = mirror::Object::ClassOffset();
3669 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3670
3671 BlockPoolsScope block_pools(GetVIXLAssembler());
3672
3673 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003674 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003675 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003676 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003677 // Instead of simply (possibly) unpoisoning `temp` here, we should
3678 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003679 // intermediate/temporary reference and because the current
3680 // concurrent copying collector keeps the from-space memory
3681 // intact/accessible until the end of the marking phase (the
3682 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003683 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3684 // temp = temp->GetMethodAt(method_offset);
3685 __ Ldr(temp, MemOperand(temp, method_offset));
3686 // lr = temp->GetEntryPoint();
3687 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3688 // lr();
3689 __ Blr(lr);
3690}
3691
Vladimir Marko58155012015-08-19 12:49:41 +00003692void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3693 DCHECK(linker_patches->empty());
3694 size_t size =
3695 method_patches_.size() +
3696 call_patches_.size() +
3697 relative_call_patches_.size() +
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003698 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003699 linker_patches->reserve(size);
3700 for (const auto& entry : method_patches_) {
3701 const MethodReference& target_method = entry.first;
3702 vixl::Literal<uint64_t>* literal = entry.second;
3703 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3704 target_method.dex_file,
3705 target_method.dex_method_index));
3706 }
3707 for (const auto& entry : call_patches_) {
3708 const MethodReference& target_method = entry.first;
3709 vixl::Literal<uint64_t>* literal = entry.second;
3710 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3711 target_method.dex_file,
3712 target_method.dex_method_index));
3713 }
3714 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003715 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003716 info.target_method.dex_file,
3717 info.target_method.dex_method_index));
3718 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003719 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003720 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003721 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003722 info.pc_insn_label->location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003723 info.element_offset));
3724 }
3725}
3726
3727vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
3728 // Look up the literal for value.
3729 auto lb = uint64_literals_.lower_bound(value);
3730 if (lb != uint64_literals_.end() && !uint64_literals_.key_comp()(value, lb->first)) {
3731 return lb->second;
3732 }
3733 // We don't have a literal for this value, insert a new one.
3734 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(value);
3735 uint64_literals_.PutBefore(lb, value, literal);
3736 return literal;
3737}
3738
3739vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3740 MethodReference target_method,
3741 MethodToLiteralMap* map) {
3742 // Look up the literal for target_method.
3743 auto lb = map->lower_bound(target_method);
3744 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
3745 return lb->second;
3746 }
3747 // We don't have a literal for this method yet, insert a new one.
3748 vixl::Literal<uint64_t>* literal = __ CreateLiteralDestroyedWithPool<uint64_t>(0u);
3749 map->PutBefore(lb, target_method, literal);
3750 return literal;
3751}
3752
3753vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3754 MethodReference target_method) {
3755 return DeduplicateMethodLiteral(target_method, &method_patches_);
3756}
3757
3758vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3759 MethodReference target_method) {
3760 return DeduplicateMethodLiteral(target_method, &call_patches_);
3761}
3762
3763
Andreas Gampe878d58c2015-01-15 23:24:00 -08003764void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003765 // Explicit clinit checks triggered by static invokes must have been pruned by
3766 // art::PrepareForRegisterAllocation.
3767 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003768
Andreas Gampe878d58c2015-01-15 23:24:00 -08003769 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3770 return;
3771 }
3772
Alexandre Ramesd921d642015-04-16 15:07:16 +01003773 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003774 LocationSummary* locations = invoke->GetLocations();
3775 codegen_->GenerateStaticOrDirectCall(
3776 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003777 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003778}
3779
3780void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003781 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3782 return;
3783 }
3784
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003785 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003786 DCHECK(!codegen_->IsLeafMethod());
3787 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3788}
3789
Alexandre Rames67555f72014-11-18 10:55:16 +00003790void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003791 InvokeRuntimeCallingConvention calling_convention;
3792 CodeGenerator::CreateLoadClassLocationSummary(
3793 cls,
3794 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003795 LocationFrom(vixl::x0),
3796 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003797}
3798
3799void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003800 if (cls->NeedsAccessCheck()) {
3801 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3802 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3803 cls,
3804 cls->GetDexPc(),
3805 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003806 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003807 return;
3808 }
3809
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003810 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003811 Register out = OutputRegister(cls);
3812 Register current_method = InputRegisterAt(cls, 0);
3813 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003814 DCHECK(!cls->CanCallRuntime());
3815 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain44015862016-01-22 11:47:17 +00003816 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3817 GenerateGcRootFieldLoad(
3818 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Alexandre Rames67555f72014-11-18 10:55:16 +00003819 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003820 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003821 // /* GcRoot<mirror::Class>[] */ out =
3822 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003823 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003824 // /* GcRoot<mirror::Class> */ out = out[type_index]
3825 GenerateGcRootFieldLoad(
3826 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003827
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003828 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3829 DCHECK(cls->CanCallRuntime());
3830 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3831 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3832 codegen_->AddSlowPath(slow_path);
3833 if (!cls->IsInDexCache()) {
3834 __ Cbz(out, slow_path->GetEntryLabel());
3835 }
3836 if (cls->MustGenerateClinitCheck()) {
3837 GenerateClassInitializationCheck(slow_path, out);
3838 } else {
3839 __ Bind(slow_path->GetExitLabel());
3840 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003841 }
3842 }
3843}
3844
David Brazdilcb1c0552015-08-04 16:22:25 +01003845static MemOperand GetExceptionTlsAddress() {
3846 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3847}
3848
Alexandre Rames67555f72014-11-18 10:55:16 +00003849void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3850 LocationSummary* locations =
3851 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3852 locations->SetOut(Location::RequiresRegister());
3853}
3854
3855void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003856 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3857}
3858
3859void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3860 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3861}
3862
3863void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3864 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003865}
3866
Alexandre Rames5319def2014-10-23 10:03:10 +01003867void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) {
3868 load->SetLocations(nullptr);
3869}
3870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003871void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003872 // Nothing to do, this is driven by the code generator.
3873}
3874
Alexandre Rames67555f72014-11-18 10:55:16 +00003875void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003876 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
3877 ? LocationSummary::kCallOnSlowPath
3878 : LocationSummary::kNoCall;
3879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003880 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003881 locations->SetOut(Location::RequiresRegister());
3882}
3883
3884void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003886 Register out = OutputRegister(load);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01003887 Register current_method = InputRegisterAt(load, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888
Roland Levillain44015862016-01-22 11:47:17 +00003889 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3890 GenerateGcRootFieldLoad(
3891 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003892 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
3893 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003894 // /* GcRoot<mirror::String> */ out = out[string_index]
3895 GenerateGcRootFieldLoad(
3896 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003897
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003898 if (!load->IsInDexCache()) {
3899 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
3900 codegen_->AddSlowPath(slow_path);
3901 __ Cbz(out, slow_path->GetEntryLabel());
3902 __ Bind(slow_path->GetExitLabel());
3903 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003904}
3905
Alexandre Rames5319def2014-10-23 10:03:10 +01003906void LocationsBuilderARM64::VisitLocal(HLocal* local) {
3907 local->SetLocations(nullptr);
3908}
3909
3910void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) {
3911 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3912}
3913
3914void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
3915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3916 locations->SetOut(Location::ConstantLocation(constant));
3917}
3918
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003919void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003920 // Will be generated at use site.
3921}
3922
Alexandre Rames67555f72014-11-18 10:55:16 +00003923void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3924 LocationSummary* locations =
3925 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3926 InvokeRuntimeCallingConvention calling_convention;
3927 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
3928}
3929
3930void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
3931 codegen_->InvokeRuntime(instruction->IsEnter()
3932 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
3933 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003934 instruction->GetDexPc(),
3935 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003936 if (instruction->IsEnter()) {
3937 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3938 } else {
3939 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3940 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003941}
3942
Alexandre Rames42d641b2014-10-27 14:00:51 +00003943void LocationsBuilderARM64::VisitMul(HMul* mul) {
3944 LocationSummary* locations =
3945 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3946 switch (mul->GetResultType()) {
3947 case Primitive::kPrimInt:
3948 case Primitive::kPrimLong:
3949 locations->SetInAt(0, Location::RequiresRegister());
3950 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003952 break;
3953
3954 case Primitive::kPrimFloat:
3955 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003956 locations->SetInAt(0, Location::RequiresFpuRegister());
3957 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00003958 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00003959 break;
3960
3961 default:
3962 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3963 }
3964}
3965
3966void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
3967 switch (mul->GetResultType()) {
3968 case Primitive::kPrimInt:
3969 case Primitive::kPrimLong:
3970 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
3971 break;
3972
3973 case Primitive::kPrimFloat:
3974 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003975 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00003976 break;
3977
3978 default:
3979 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3980 }
3981}
3982
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003983void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
3984 LocationSummary* locations =
3985 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3986 switch (neg->GetResultType()) {
3987 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00003988 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003989 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00003990 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003991 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003992
3993 case Primitive::kPrimFloat:
3994 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00003995 locations->SetInAt(0, Location::RequiresFpuRegister());
3996 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003997 break;
3998
3999 default:
4000 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4001 }
4002}
4003
4004void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4005 switch (neg->GetResultType()) {
4006 case Primitive::kPrimInt:
4007 case Primitive::kPrimLong:
4008 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4009 break;
4010
4011 case Primitive::kPrimFloat:
4012 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004013 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004014 break;
4015
4016 default:
4017 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4018 }
4019}
4020
4021void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4022 LocationSummary* locations =
4023 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4024 InvokeRuntimeCallingConvention calling_convention;
4025 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004026 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004027 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004028 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004029}
4030
4031void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4032 LocationSummary* locations = instruction->GetLocations();
4033 InvokeRuntimeCallingConvention calling_convention;
4034 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4035 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004036 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004037 // Note: if heap poisoning is enabled, the entry point takes cares
4038 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004039 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4040 instruction,
4041 instruction->GetDexPc(),
4042 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004043 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004044}
4045
Alexandre Rames5319def2014-10-23 10:03:10 +01004046void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4047 LocationSummary* locations =
4048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4049 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004050 if (instruction->IsStringAlloc()) {
4051 locations->AddTemp(LocationFrom(kArtMethodRegister));
4052 } else {
4053 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4054 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4055 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004056 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4057}
4058
4059void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004060 // Note: if heap poisoning is enabled, the entry point takes cares
4061 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004062 if (instruction->IsStringAlloc()) {
4063 // String is allocated through StringFactory. Call NewEmptyString entry point.
4064 Location temp = instruction->GetLocations()->GetTemp(0);
4065 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4066 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4067 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4068 __ Blr(lr);
4069 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4070 } else {
4071 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4072 instruction,
4073 instruction->GetDexPc(),
4074 nullptr);
4075 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4076 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004077}
4078
4079void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004081 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004082 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004083}
4084
4085void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004086 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004087 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004088 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004089 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004090 break;
4091
4092 default:
4093 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4094 }
4095}
4096
David Brazdil66d126e2015-04-03 16:02:44 +01004097void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4099 locations->SetInAt(0, Location::RequiresRegister());
4100 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4101}
4102
4103void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004104 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4105}
4106
Alexandre Rames5319def2014-10-23 10:03:10 +01004107void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004108 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4109 ? LocationSummary::kCallOnSlowPath
4110 : LocationSummary::kNoCall;
4111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004112 locations->SetInAt(0, Location::RequiresRegister());
4113 if (instruction->HasUses()) {
4114 locations->SetOut(Location::SameAsFirstInput());
4115 }
4116}
4117
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004118void InstructionCodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004119 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4120 return;
4121 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004122
Alexandre Ramesd921d642015-04-16 15:07:16 +01004123 BlockPoolsScope block_pools(GetVIXLAssembler());
4124 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004125 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
4126 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4127}
4128
4129void InstructionCodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004130 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
4131 codegen_->AddSlowPath(slow_path);
4132
4133 LocationSummary* locations = instruction->GetLocations();
4134 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004135
4136 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004137}
4138
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004139void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004140 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004141 GenerateImplicitNullCheck(instruction);
4142 } else {
4143 GenerateExplicitNullCheck(instruction);
4144 }
4145}
4146
Alexandre Rames67555f72014-11-18 10:55:16 +00004147void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4148 HandleBinaryOp(instruction);
4149}
4150
4151void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4152 HandleBinaryOp(instruction);
4153}
4154
Alexandre Rames3e69f162014-12-10 10:36:50 +00004155void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4156 LOG(FATAL) << "Unreachable";
4157}
4158
4159void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4160 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4161}
4162
Alexandre Rames5319def2014-10-23 10:03:10 +01004163void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4165 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4166 if (location.IsStackSlot()) {
4167 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4168 } else if (location.IsDoubleStackSlot()) {
4169 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4170 }
4171 locations->SetOut(location);
4172}
4173
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004174void InstructionCodeGeneratorARM64::VisitParameterValue(
4175 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004176 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004177}
4178
4179void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4180 LocationSummary* locations =
4181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004182 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004183}
4184
4185void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4186 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4187 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004188}
4189
4190void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4192 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4193 locations->SetInAt(i, Location::Any());
4194 }
4195 locations->SetOut(Location::Any());
4196}
4197
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004198void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004199 LOG(FATAL) << "Unreachable";
4200}
4201
Serban Constantinescu02164b32014-11-13 14:05:07 +00004202void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004203 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004204 LocationSummary::CallKind call_kind =
4205 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4207
4208 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004209 case Primitive::kPrimInt:
4210 case Primitive::kPrimLong:
4211 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004212 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4214 break;
4215
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004216 case Primitive::kPrimFloat:
4217 case Primitive::kPrimDouble: {
4218 InvokeRuntimeCallingConvention calling_convention;
4219 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4220 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4221 locations->SetOut(calling_convention.GetReturnLocation(type));
4222
4223 break;
4224 }
4225
Serban Constantinescu02164b32014-11-13 14:05:07 +00004226 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004227 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004228 }
4229}
4230
4231void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4232 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004233
Serban Constantinescu02164b32014-11-13 14:05:07 +00004234 switch (type) {
4235 case Primitive::kPrimInt:
4236 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004237 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004238 break;
4239 }
4240
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004241 case Primitive::kPrimFloat:
4242 case Primitive::kPrimDouble: {
4243 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4244 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004245 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004246 if (type == Primitive::kPrimFloat) {
4247 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4248 } else {
4249 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4250 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004251 break;
4252 }
4253
Serban Constantinescu02164b32014-11-13 14:05:07 +00004254 default:
4255 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004256 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004257 }
4258}
4259
Calin Juravle27df7582015-04-17 19:12:31 +01004260void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4261 memory_barrier->SetLocations(nullptr);
4262}
4263
4264void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004265 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004266}
4267
Alexandre Rames5319def2014-10-23 10:03:10 +01004268void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4270 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004271 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004272}
4273
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004274void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004275 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004276}
4277
4278void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4279 instruction->SetLocations(nullptr);
4280}
4281
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004282void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004283 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004284}
4285
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004286void LocationsBuilderARM64::VisitRor(HRor* ror) {
4287 HandleBinaryOp(ror);
4288}
4289
4290void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4291 HandleBinaryOp(ror);
4292}
4293
Serban Constantinescu02164b32014-11-13 14:05:07 +00004294void LocationsBuilderARM64::VisitShl(HShl* shl) {
4295 HandleShift(shl);
4296}
4297
4298void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4299 HandleShift(shl);
4300}
4301
4302void LocationsBuilderARM64::VisitShr(HShr* shr) {
4303 HandleShift(shr);
4304}
4305
4306void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4307 HandleShift(shr);
4308}
4309
Alexandre Rames5319def2014-10-23 10:03:10 +01004310void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) {
4311 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4312 Primitive::Type field_type = store->InputAt(1)->GetType();
4313 switch (field_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004314 case Primitive::kPrimNot:
Alexandre Rames5319def2014-10-23 10:03:10 +01004315 case Primitive::kPrimBoolean:
4316 case Primitive::kPrimByte:
4317 case Primitive::kPrimChar:
4318 case Primitive::kPrimShort:
4319 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004320 case Primitive::kPrimFloat:
Alexandre Rames5319def2014-10-23 10:03:10 +01004321 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4322 break;
4323
4324 case Primitive::kPrimLong:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004325 case Primitive::kPrimDouble:
Alexandre Rames5319def2014-10-23 10:03:10 +01004326 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4327 break;
4328
4329 default:
4330 LOG(FATAL) << "Unimplemented local type " << field_type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004331 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +01004332 }
4333}
4334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004335void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004336}
4337
4338void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004339 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004340}
4341
4342void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004343 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004344}
4345
Alexandre Rames67555f72014-11-18 10:55:16 +00004346void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004347 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004348}
4349
4350void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004351 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004352}
4353
4354void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004355 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004356}
4357
Alexandre Rames67555f72014-11-18 10:55:16 +00004358void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004359 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004360}
4361
Calin Juravlee460d1d2015-09-29 04:52:17 +01004362void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4363 HUnresolvedInstanceFieldGet* instruction) {
4364 FieldAccessCallingConventionARM64 calling_convention;
4365 codegen_->CreateUnresolvedFieldLocationSummary(
4366 instruction, instruction->GetFieldType(), calling_convention);
4367}
4368
4369void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4370 HUnresolvedInstanceFieldGet* instruction) {
4371 FieldAccessCallingConventionARM64 calling_convention;
4372 codegen_->GenerateUnresolvedFieldAccess(instruction,
4373 instruction->GetFieldType(),
4374 instruction->GetFieldIndex(),
4375 instruction->GetDexPc(),
4376 calling_convention);
4377}
4378
4379void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4380 HUnresolvedInstanceFieldSet* instruction) {
4381 FieldAccessCallingConventionARM64 calling_convention;
4382 codegen_->CreateUnresolvedFieldLocationSummary(
4383 instruction, instruction->GetFieldType(), calling_convention);
4384}
4385
4386void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4387 HUnresolvedInstanceFieldSet* instruction) {
4388 FieldAccessCallingConventionARM64 calling_convention;
4389 codegen_->GenerateUnresolvedFieldAccess(instruction,
4390 instruction->GetFieldType(),
4391 instruction->GetFieldIndex(),
4392 instruction->GetDexPc(),
4393 calling_convention);
4394}
4395
4396void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4397 HUnresolvedStaticFieldGet* instruction) {
4398 FieldAccessCallingConventionARM64 calling_convention;
4399 codegen_->CreateUnresolvedFieldLocationSummary(
4400 instruction, instruction->GetFieldType(), calling_convention);
4401}
4402
4403void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4404 HUnresolvedStaticFieldGet* instruction) {
4405 FieldAccessCallingConventionARM64 calling_convention;
4406 codegen_->GenerateUnresolvedFieldAccess(instruction,
4407 instruction->GetFieldType(),
4408 instruction->GetFieldIndex(),
4409 instruction->GetDexPc(),
4410 calling_convention);
4411}
4412
4413void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4414 HUnresolvedStaticFieldSet* instruction) {
4415 FieldAccessCallingConventionARM64 calling_convention;
4416 codegen_->CreateUnresolvedFieldLocationSummary(
4417 instruction, instruction->GetFieldType(), calling_convention);
4418}
4419
4420void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4421 HUnresolvedStaticFieldSet* instruction) {
4422 FieldAccessCallingConventionARM64 calling_convention;
4423 codegen_->GenerateUnresolvedFieldAccess(instruction,
4424 instruction->GetFieldType(),
4425 instruction->GetFieldIndex(),
4426 instruction->GetDexPc(),
4427 calling_convention);
4428}
4429
Alexandre Rames5319def2014-10-23 10:03:10 +01004430void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4432}
4433
4434void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004435 HBasicBlock* block = instruction->GetBlock();
4436 if (block->GetLoopInformation() != nullptr) {
4437 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4438 // The back edge will generate the suspend check.
4439 return;
4440 }
4441 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4442 // The goto will generate the suspend check.
4443 return;
4444 }
4445 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004446}
4447
4448void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) {
4449 temp->SetLocations(nullptr);
4450}
4451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004452void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004453 // Nothing to do, this is driven by the code generator.
Alexandre Rames5319def2014-10-23 10:03:10 +01004454}
4455
Alexandre Rames67555f72014-11-18 10:55:16 +00004456void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4457 LocationSummary* locations =
4458 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4459 InvokeRuntimeCallingConvention calling_convention;
4460 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4461}
4462
4463void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4464 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004465 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004466 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004467}
4468
4469void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4470 LocationSummary* locations =
4471 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4472 Primitive::Type input_type = conversion->GetInputType();
4473 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004474 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004475 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4476 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4477 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4478 }
4479
Alexandre Rames542361f2015-01-29 16:57:31 +00004480 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004481 locations->SetInAt(0, Location::RequiresFpuRegister());
4482 } else {
4483 locations->SetInAt(0, Location::RequiresRegister());
4484 }
4485
Alexandre Rames542361f2015-01-29 16:57:31 +00004486 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004487 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4488 } else {
4489 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4490 }
4491}
4492
4493void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4494 Primitive::Type result_type = conversion->GetResultType();
4495 Primitive::Type input_type = conversion->GetInputType();
4496
4497 DCHECK_NE(input_type, result_type);
4498
Alexandre Rames542361f2015-01-29 16:57:31 +00004499 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004500 int result_size = Primitive::ComponentSize(result_type);
4501 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004502 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004503 Register output = OutputRegister(conversion);
4504 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004505 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004506 // 'int' values are used directly as W registers, discarding the top
4507 // bits, so we don't need to sign-extend and can just perform a move.
4508 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4509 // top 32 bits of the target register. We theoretically could leave those
4510 // bits unchanged, but we would have to make sure that no code uses a
4511 // 32bit input value as a 64bit value assuming that the top 32 bits are
4512 // zero.
4513 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004514 } else if (result_type == Primitive::kPrimChar ||
4515 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4516 __ Ubfx(output,
4517 output.IsX() ? source.X() : source.W(),
4518 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004519 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004520 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004521 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004522 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004523 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004524 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004525 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4526 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004527 } else if (Primitive::IsFloatingPointType(result_type) &&
4528 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004529 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4530 } else {
4531 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4532 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004533 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004534}
Alexandre Rames67555f72014-11-18 10:55:16 +00004535
Serban Constantinescu02164b32014-11-13 14:05:07 +00004536void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4537 HandleShift(ushr);
4538}
4539
4540void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4541 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004542}
4543
4544void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4545 HandleBinaryOp(instruction);
4546}
4547
4548void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4549 HandleBinaryOp(instruction);
4550}
4551
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004552void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004553 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004554 LOG(FATAL) << "Unreachable";
4555}
4556
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004557void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004558 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004559 LOG(FATAL) << "Unreachable";
4560}
4561
Mark Mendellfe57faa2015-09-18 09:26:15 -04004562// Simple implementation of packed switch - generate cascaded compare/jumps.
4563void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4564 LocationSummary* locations =
4565 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4566 locations->SetInAt(0, Location::RequiresRegister());
4567}
4568
4569void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4570 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004571 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004572 Register value_reg = InputRegisterAt(switch_instr, 0);
4573 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4574
Zheng Xu3927c8b2015-11-18 17:46:25 +08004575 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4576 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4577 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4578 // make sure we don't emit it if the target may run out of range.
4579 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4580 // ranges and emit the tables only as required.
4581 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004582
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004583 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004584 // Current instruction id is an upper bound of the number of HIRs in the graph.
4585 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4586 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004587 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4588 Register temp = temps.AcquireW();
4589 __ Subs(temp, value_reg, Operand(lower_bound));
4590
Zheng Xu3927c8b2015-11-18 17:46:25 +08004591 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004592 // Jump to successors[0] if value == lower_bound.
4593 __ B(eq, codegen_->GetLabelOf(successors[0]));
4594 int32_t last_index = 0;
4595 for (; num_entries - last_index > 2; last_index += 2) {
4596 __ Subs(temp, temp, Operand(2));
4597 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4598 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4599 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4600 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4601 }
4602 if (num_entries - last_index == 2) {
4603 // The last missing case_value.
4604 __ Cmp(temp, Operand(1));
4605 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004606 }
4607
4608 // And the default for any other value.
4609 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4610 __ B(codegen_->GetLabelOf(default_block));
4611 }
4612 } else {
4613 JumpTableARM64* jump_table = new (GetGraph()->GetArena()) JumpTableARM64(switch_instr);
4614 codegen_->AddJumpTable(jump_table);
4615
4616 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4617
4618 // Below instructions should use at most one blocked register. Since there are two blocked
4619 // registers, we are free to block one.
4620 Register temp_w = temps.AcquireW();
4621 Register index;
4622 // Remove the bias.
4623 if (lower_bound != 0) {
4624 index = temp_w;
4625 __ Sub(index, value_reg, Operand(lower_bound));
4626 } else {
4627 index = value_reg;
4628 }
4629
4630 // Jump to default block if index is out of the range.
4631 __ Cmp(index, Operand(num_entries));
4632 __ B(hs, codegen_->GetLabelOf(default_block));
4633
4634 // In current VIXL implementation, it won't require any blocked registers to encode the
4635 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4636 // register pressure.
4637 Register table_base = temps.AcquireX();
4638 // Load jump offset from the table.
4639 __ Adr(table_base, jump_table->GetTableStartLabel());
4640 Register jump_offset = temp_w;
4641 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4642
4643 // Jump to target block by branching to table_base(pc related) + offset.
4644 Register target_address = table_base;
4645 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4646 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004647 }
4648}
4649
Roland Levillain44015862016-01-22 11:47:17 +00004650void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4651 Location out,
4652 uint32_t offset,
4653 Location maybe_temp) {
4654 Primitive::Type type = Primitive::kPrimNot;
4655 Register out_reg = RegisterFrom(out, type);
4656 if (kEmitCompilerReadBarrier) {
4657 Register temp_reg = RegisterFrom(maybe_temp, type);
4658 if (kUseBakerReadBarrier) {
4659 // Load with fast path based Baker's read barrier.
4660 // /* HeapReference<Object> */ out = *(out + offset)
4661 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4662 out,
4663 out_reg,
4664 offset,
4665 temp_reg,
4666 /* needs_null_check */ false,
4667 /* use_load_acquire */ false);
4668 } else {
4669 // Load with slow path based read barrier.
4670 // Save the value of `out` into `maybe_temp` before overwriting it
4671 // in the following move operation, as we will need it for the
4672 // read barrier below.
4673 __ Mov(temp_reg, out_reg);
4674 // /* HeapReference<Object> */ out = *(out + offset)
4675 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4676 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4677 }
4678 } else {
4679 // Plain load with no read barrier.
4680 // /* HeapReference<Object> */ out = *(out + offset)
4681 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4682 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4683 }
4684}
4685
4686void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4687 Location out,
4688 Location obj,
4689 uint32_t offset,
4690 Location maybe_temp) {
4691 Primitive::Type type = Primitive::kPrimNot;
4692 Register out_reg = RegisterFrom(out, type);
4693 Register obj_reg = RegisterFrom(obj, type);
4694 if (kEmitCompilerReadBarrier) {
4695 if (kUseBakerReadBarrier) {
4696 // Load with fast path based Baker's read barrier.
4697 Register temp_reg = RegisterFrom(maybe_temp, type);
4698 // /* HeapReference<Object> */ out = *(obj + offset)
4699 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4700 out,
4701 obj_reg,
4702 offset,
4703 temp_reg,
4704 /* needs_null_check */ false,
4705 /* use_load_acquire */ false);
4706 } else {
4707 // Load with slow path based read barrier.
4708 // /* HeapReference<Object> */ out = *(obj + offset)
4709 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4710 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4711 }
4712 } else {
4713 // Plain load with no read barrier.
4714 // /* HeapReference<Object> */ out = *(obj + offset)
4715 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4716 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4717 }
4718}
4719
4720void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4721 Location root,
4722 vixl::Register obj,
4723 uint32_t offset) {
4724 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4725 if (kEmitCompilerReadBarrier) {
4726 if (kUseBakerReadBarrier) {
4727 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4728 // Baker's read barrier are used:
4729 //
4730 // root = obj.field;
4731 // if (Thread::Current()->GetIsGcMarking()) {
4732 // root = ReadBarrier::Mark(root)
4733 // }
4734
4735 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4736 __ Ldr(root_reg, MemOperand(obj, offset));
4737 static_assert(
4738 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4739 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4740 "have different sizes.");
4741 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4742 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4743 "have different sizes.");
4744
4745 // Slow path used to mark the GC root `root`.
4746 SlowPathCodeARM64* slow_path =
4747 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
4748 codegen_->AddSlowPath(slow_path);
4749
4750 MacroAssembler* masm = GetVIXLAssembler();
4751 UseScratchRegisterScope temps(masm);
4752 Register temp = temps.AcquireW();
4753 // temp = Thread::Current()->GetIsGcMarking()
4754 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
4755 __ Cbnz(temp, slow_path->GetEntryLabel());
4756 __ Bind(slow_path->GetExitLabel());
4757 } else {
4758 // GC root loaded through a slow path for read barriers other
4759 // than Baker's.
4760 // /* GcRoot<mirror::Object>* */ root = obj + offset
4761 __ Add(root_reg.X(), obj.X(), offset);
4762 // /* mirror::Object* */ root = root->Read()
4763 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4764 }
4765 } else {
4766 // Plain GC root load with no read barrier.
4767 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4768 __ Ldr(root_reg, MemOperand(obj, offset));
4769 // Note that GC roots are not affected by heap poisoning, thus we
4770 // do not have to unpoison `root_reg` here.
4771 }
4772}
4773
4774void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4775 Location ref,
4776 vixl::Register obj,
4777 uint32_t offset,
4778 Register temp,
4779 bool needs_null_check,
4780 bool use_load_acquire) {
4781 DCHECK(kEmitCompilerReadBarrier);
4782 DCHECK(kUseBakerReadBarrier);
4783
4784 // /* HeapReference<Object> */ ref = *(obj + offset)
4785 Location no_index = Location::NoLocation();
4786 GenerateReferenceLoadWithBakerReadBarrier(
4787 instruction, ref, obj, offset, no_index, temp, needs_null_check, use_load_acquire);
4788}
4789
4790void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4791 Location ref,
4792 vixl::Register obj,
4793 uint32_t data_offset,
4794 Location index,
4795 Register temp,
4796 bool needs_null_check) {
4797 DCHECK(kEmitCompilerReadBarrier);
4798 DCHECK(kUseBakerReadBarrier);
4799
4800 // Array cells are never volatile variables, therefore array loads
4801 // never use Load-Acquire instructions on ARM64.
4802 const bool use_load_acquire = false;
4803
4804 // /* HeapReference<Object> */ ref =
4805 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4806 GenerateReferenceLoadWithBakerReadBarrier(
4807 instruction, ref, obj, data_offset, index, temp, needs_null_check, use_load_acquire);
4808}
4809
4810void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4811 Location ref,
4812 vixl::Register obj,
4813 uint32_t offset,
4814 Location index,
4815 Register temp,
4816 bool needs_null_check,
4817 bool use_load_acquire) {
4818 DCHECK(kEmitCompilerReadBarrier);
4819 DCHECK(kUseBakerReadBarrier);
4820 // If `index` is a valid location, then we are emitting an array
4821 // load, so we shouldn't be using a Load Acquire instruction.
4822 // In other words: `index.IsValid()` => `!use_load_acquire`.
4823 DCHECK(!index.IsValid() || !use_load_acquire);
4824
4825 MacroAssembler* masm = GetVIXLAssembler();
4826 UseScratchRegisterScope temps(masm);
4827
4828 // In slow path based read barriers, the read barrier call is
4829 // inserted after the original load. However, in fast path based
4830 // Baker's read barriers, we need to perform the load of
4831 // mirror::Object::monitor_ *before* the original reference load.
4832 // This load-load ordering is required by the read barrier.
4833 // The fast path/slow path (for Baker's algorithm) should look like:
4834 //
4835 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4836 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4837 // HeapReference<Object> ref = *src; // Original reference load.
4838 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
4839 // if (is_gray) {
4840 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4841 // }
4842 //
4843 // Note: the original implementation in ReadBarrier::Barrier is
4844 // slightly more complex as it performs additional checks that we do
4845 // not do here for performance reasons.
4846
4847 Primitive::Type type = Primitive::kPrimNot;
4848 Register ref_reg = RegisterFrom(ref, type);
4849 DCHECK(obj.IsW());
4850 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4851
4852 // /* int32_t */ monitor = obj->monitor_
4853 __ Ldr(temp, HeapOperand(obj, monitor_offset));
4854 if (needs_null_check) {
4855 MaybeRecordImplicitNullCheck(instruction);
4856 }
4857 // /* LockWord */ lock_word = LockWord(monitor)
4858 static_assert(sizeof(LockWord) == sizeof(int32_t),
4859 "art::LockWord and int32_t have different sizes.");
4860 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
4861 __ Lsr(temp, temp, LockWord::kReadBarrierStateShift);
4862 __ And(temp, temp, Operand(LockWord::kReadBarrierStateMask));
4863 static_assert(
4864 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
4865 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
4866
4867 // Introduce a dependency on the high bits of rb_state, which shall
4868 // be all zeroes, to prevent load-load reordering, and without using
4869 // a memory barrier (which would be more expensive).
4870 // temp2 = rb_state & ~LockWord::kReadBarrierStateMask = 0
4871 Register temp2 = temps.AcquireW();
4872 __ Bic(temp2, temp, Operand(LockWord::kReadBarrierStateMask));
4873 // obj is unchanged by this operation, but its value now depends on
4874 // temp2, which depends on temp.
4875 __ Add(obj, obj, Operand(temp2));
4876 temps.Release(temp2);
4877
4878 // The actual reference load.
4879 if (index.IsValid()) {
4880 static_assert(
4881 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4882 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4883 temp2 = temps.AcquireW();
4884 // /* HeapReference<Object> */ ref =
4885 // *(obj + offset + index * sizeof(HeapReference<Object>))
4886 MemOperand source = HeapOperand(obj);
4887 if (index.IsConstant()) {
4888 uint32_t computed_offset =
4889 offset + (Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type));
4890 source = HeapOperand(obj, computed_offset);
4891 } else {
4892 __ Add(temp2, obj, offset);
4893 source = HeapOperand(temp2, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
4894 }
4895 Load(type, ref_reg, source);
4896 temps.Release(temp2);
4897 } else {
4898 // /* HeapReference<Object> */ ref = *(obj + offset)
4899 MemOperand field = HeapOperand(obj, offset);
4900 if (use_load_acquire) {
4901 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
4902 } else {
4903 Load(type, ref_reg, field);
4904 }
4905 }
4906
4907 // Object* ref = ref_addr->AsMirrorPtr()
4908 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
4909
4910 // Slow path used to mark the object `ref` when it is gray.
4911 SlowPathCodeARM64* slow_path =
4912 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
4913 AddSlowPath(slow_path);
4914
4915 // if (rb_state == ReadBarrier::gray_ptr_)
4916 // ref = ReadBarrier::Mark(ref);
4917 __ Cmp(temp, ReadBarrier::gray_ptr_);
4918 __ B(eq, slow_path->GetEntryLabel());
4919 __ Bind(slow_path->GetExitLabel());
4920}
4921
4922void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
4923 Location out,
4924 Location ref,
4925 Location obj,
4926 uint32_t offset,
4927 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004928 DCHECK(kEmitCompilerReadBarrier);
4929
Roland Levillain44015862016-01-22 11:47:17 +00004930 // Insert a slow path based read barrier *after* the reference load.
4931 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004932 // If heap poisoning is enabled, the unpoisoning of the loaded
4933 // reference will be carried out by the runtime within the slow
4934 // path.
4935 //
4936 // Note that `ref` currently does not get unpoisoned (when heap
4937 // poisoning is enabled), which is alright as the `ref` argument is
4938 // not used by the artReadBarrierSlow entry point.
4939 //
4940 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4941 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
4942 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
4943 AddSlowPath(slow_path);
4944
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004945 __ B(slow_path->GetEntryLabel());
4946 __ Bind(slow_path->GetExitLabel());
4947}
4948
Roland Levillain44015862016-01-22 11:47:17 +00004949void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4950 Location out,
4951 Location ref,
4952 Location obj,
4953 uint32_t offset,
4954 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004955 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004956 // Baker's read barriers shall be handled by the fast path
4957 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
4958 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004959 // If heap poisoning is enabled, unpoisoning will be taken care of
4960 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00004961 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004962 } else if (kPoisonHeapReferences) {
4963 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
4964 }
4965}
4966
Roland Levillain44015862016-01-22 11:47:17 +00004967void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4968 Location out,
4969 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004970 DCHECK(kEmitCompilerReadBarrier);
4971
Roland Levillain44015862016-01-22 11:47:17 +00004972 // Insert a slow path based read barrier *after* the GC root load.
4973 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004974 // Note that GC roots are not affected by heap poisoning, so we do
4975 // not need to do anything special for this here.
4976 SlowPathCodeARM64* slow_path =
4977 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
4978 AddSlowPath(slow_path);
4979
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004980 __ B(slow_path->GetEntryLabel());
4981 __ Bind(slow_path->GetExitLabel());
4982}
4983
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004984void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
4985 LocationSummary* locations =
4986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4987 locations->SetInAt(0, Location::RequiresRegister());
4988 locations->SetOut(Location::RequiresRegister());
4989}
4990
4991void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
4992 LocationSummary* locations = instruction->GetLocations();
4993 uint32_t method_offset = 0;
4994 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4995 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4996 instruction->GetIndex(), kArm64PointerSize).SizeValue();
4997 } else {
4998 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4999 instruction->GetIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
5000 }
5001 __ Ldr(XRegisterFrom(locations->Out()),
5002 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
5003}
5004
5005
5006
Alexandre Rames67555f72014-11-18 10:55:16 +00005007#undef __
5008#undef QUICK_ENTRY_POINT
5009
Alexandre Rames5319def2014-10-23 10:03:10 +01005010} // namespace arm64
5011} // namespace art