blob: f02b02854128707d4813686c6025a9d180968079 [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
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100142 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800143 int64_t spill_offset,
144 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
146 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
147 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800148 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800150 codegen->GetNumberOfFloatingPointRegisters()));
151
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
153 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100188 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
189 for (uint32_t i : LowToHighBits(core_spills)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800193 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
199
Vladimir Marko804b03f2016-09-14 16:26:36 +0100200 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
201 for (uint32_t i : LowToHighBits(fp_spills)) {
202 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
203 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
204 saved_fpu_stack_offsets_[i] = stack_offset;
205 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800206 }
207
Vladimir Marko804b03f2016-09-14 16:26:36 +0100208 SaveRestoreLiveRegistersHelper(codegen,
209 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800210 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
211}
212
213void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 SaveRestoreLiveRegistersHelper(codegen,
215 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800216 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
217}
218
Alexandre Rames5319def2014-10-23 10:03:10 +0100219class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
220 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000221 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100222
Alexandre Rames67555f72014-11-18 10:55:16 +0000223 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100224 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000225 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226
Alexandre Rames5319def2014-10-23 10:03:10 +0100227 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000228 if (instruction_->CanThrowIntoCatchBlock()) {
229 // Live registers will be restored in the catch block if caught.
230 SaveLiveRegisters(codegen, instruction_->GetLocations());
231 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000232 // We're moving two locations to locations that could overlap, so we need a parallel
233 // move resolver.
234 InvokeRuntimeCallingConvention calling_convention;
235 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100236 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
237 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000238 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
239 ? kQuickThrowStringBounds
240 : kQuickThrowArrayBounds;
241 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100242 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800243 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100244 }
245
Alexandre Rames8158f282015-08-07 10:26:17 +0100246 bool IsFatal() const OVERRIDE { return true; }
247
Alexandre Rames9931f312015-06-19 14:47:01 +0100248 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
249
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 private:
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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000256 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000257
258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
260 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000261 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800262 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000263 }
264
Alexandre Rames8158f282015-08-07 10:26:17 +0100265 bool IsFatal() const OVERRIDE { return true; }
266
Alexandre Rames9931f312015-06-19 14:47:01 +0100267 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
268
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
271};
272
273class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
274 public:
275 LoadClassSlowPathARM64(HLoadClass* cls,
276 HInstruction* at,
277 uint32_t dex_pc,
278 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000280 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
281 }
282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = at_->GetLocations();
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286
287 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000289
290 InvokeRuntimeCallingConvention calling_convention;
291 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
293 : kQuickInitializeType;
294 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800295 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100296 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800297 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100298 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000300
301 // Move the class to the desired location.
302 Location out = locations->Out();
303 if (out.IsValid()) {
304 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
305 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000306 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 __ B(GetExitLabel());
311 }
312
Alexandre Rames9931f312015-06-19 14:47:01 +0100313 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
314
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
319 // The instruction where this slow path is happening.
320 // (Might be the load class or an initialization check).
321 HInstruction* const at_;
322
323 // The dex PC of `at_`.
324 const uint32_t dex_pc_;
325
326 // Whether to initialize the class.
327 const bool do_clinit_;
328
329 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
330};
331
Vladimir Markoaad75c62016-10-03 08:46:48 +0000332class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
333 public:
334 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 LocationSummary* locations = instruction_->GetLocations();
338 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
340
341 __ Bind(GetEntryLabel());
342 SaveLiveRegisters(codegen, locations);
343
344 InvokeRuntimeCallingConvention calling_convention;
345 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
346 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
347 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
348 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
349 Primitive::Type type = instruction_->GetType();
350 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
351
352 RestoreLiveRegisters(codegen, locations);
353
354 // Store the resolved String to the BSS entry.
355 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
356 Register temp = temps.AcquireX();
357 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
358 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary
359 // for the ADRP in the fast path, so that we can avoid the ADRP here.
360 vixl::aarch64::Label* adrp_label =
361 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
362 arm64_codegen->EmitAdrpPlaceholder(adrp_label, temp);
363 vixl::aarch64::Label* strp_label =
364 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
365 {
366 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
367 __ Bind(strp_label);
368 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
369 MemOperand(temp, /* offset placeholder */ 0));
370 }
371
372 __ B(GetExitLabel());
373 }
374
375 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
376
377 private:
378 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
379};
380
Alexandre Rames5319def2014-10-23 10:03:10 +0100381class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
382 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100384
Alexandre Rames67555f72014-11-18 10:55:16 +0000385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
386 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000388 if (instruction_->CanThrowIntoCatchBlock()) {
389 // Live registers will be restored in the catch block if caught.
390 SaveLiveRegisters(codegen, instruction_->GetLocations());
391 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000392 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
393 instruction_,
394 instruction_->GetDexPc(),
395 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800396 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100397 }
398
Alexandre Rames8158f282015-08-07 10:26:17 +0100399 bool IsFatal() const OVERRIDE { return true; }
400
Alexandre Rames9931f312015-06-19 14:47:01 +0100401 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
402
Alexandre Rames5319def2014-10-23 10:03:10 +0100403 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100404 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
405};
406
407class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
408 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100409 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000410 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100411
Alexandre Rames67555f72014-11-18 10:55:16 +0000412 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
413 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000415 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800416 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000417 if (successor_ == nullptr) {
418 __ B(GetReturnLabel());
419 } else {
420 __ B(arm64_codegen->GetLabelOf(successor_));
421 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 }
423
Scott Wakeling97c72b72016-06-24 16:19:36 +0100424 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 DCHECK(successor_ == nullptr);
426 return &return_label_;
427 }
428
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100429 HBasicBlock* GetSuccessor() const {
430 return successor_;
431 }
432
Alexandre Rames9931f312015-06-19 14:47:01 +0100433 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
434
Alexandre Rames5319def2014-10-23 10:03:10 +0100435 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100436 // If not null, the block to branch to after the suspend check.
437 HBasicBlock* const successor_;
438
439 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100440 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100441
442 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
443};
444
Alexandre Rames67555f72014-11-18 10:55:16 +0000445class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
446 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000447 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000448 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000451 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100452 Location class_to_check = locations->InAt(1);
453 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
454 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000455 DCHECK(instruction_->IsCheckCast()
456 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
457 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459
Alexandre Rames67555f72014-11-18 10:55:16 +0000460 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000462 if (!is_fatal_) {
463 SaveLiveRegisters(codegen, locations);
464 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465
466 // We're moving two locations to locations that could overlap, so we need a parallel
467 // move resolver.
468 InvokeRuntimeCallingConvention calling_convention;
469 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100470 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
471 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472
473 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000474 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700475 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000476 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000477 Primitive::Type ret_type = instruction_->GetType();
478 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
479 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
480 } else {
481 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000482 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800483 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000484 }
485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000486 if (!is_fatal_) {
487 RestoreLiveRegisters(codegen, locations);
488 __ B(GetExitLabel());
489 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 }
491
Alexandre Rames9931f312015-06-19 14:47:01 +0100492 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100493 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100494
Alexandre Rames67555f72014-11-18 10:55:16 +0000495 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000496 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000497
Alexandre Rames67555f72014-11-18 10:55:16 +0000498 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
499};
500
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700501class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
502 public:
Aart Bik42249c32016-01-07 15:33:50 -0800503 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000504 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800507 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700508 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000509 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000510 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700511 }
512
Alexandre Rames9931f312015-06-19 14:47:01 +0100513 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
514
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700515 private:
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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100522
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);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000548 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100549 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
550 RestoreLiveRegisters(codegen, locations);
551 __ B(GetExitLabel());
552 }
553
554 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
555
556 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100557 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
558};
559
Zheng Xu3927c8b2015-11-18 17:46:25 +0800560void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
561 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000562 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800563
564 // We are about to use the assembler to place literals directly. Make sure we have enough
565 // underlying code buffer and we have generated the jump table with right size.
566 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
567 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
568
569 __ Bind(&table_start_);
570 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
571 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100572 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800573 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100574 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800575 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
576 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
577 Literal<int32_t> literal(jump_offset);
578 __ place(&literal);
579 }
580}
581
Roland Levillain44015862016-01-22 11:47:17 +0000582// Slow path marking an object during a read barrier.
583class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
584 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100585 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
586 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000587 DCHECK(kEmitCompilerReadBarrier);
588 }
589
590 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
591
592 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
593 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000594 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100595 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000596 DCHECK(instruction_->IsInstanceFieldGet() ||
597 instruction_->IsStaticFieldGet() ||
598 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100599 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000600 instruction_->IsLoadClass() ||
601 instruction_->IsLoadString() ||
602 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100603 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100604 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
605 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000606 << "Unexpected instruction in read barrier marking slow path: "
607 << instruction_->DebugName();
608
609 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100610 // No need to save live registers; it's taken care of by the
611 // entrypoint. Also, there is no need to update the stack mask,
612 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000613 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100614 DCHECK_NE(obj_.reg(), LR);
615 DCHECK_NE(obj_.reg(), WSP);
616 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100617 // IP0 is used internally by the ReadBarrierMarkRegX entry point
618 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700619 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100620 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
621 // "Compact" slow path, saving two moves.
622 //
623 // Instead of using the standard runtime calling convention (input
624 // and output in W0):
625 //
626 // W0 <- obj
627 // W0 <- ReadBarrierMark(W0)
628 // obj <- W0
629 //
630 // we just use rX (the register holding `obj`) as input and output
631 // of a dedicated entrypoint:
632 //
633 // rX <- ReadBarrierMarkRegX(rX)
634 //
635 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700636 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100637 // This runtime call does not require a stack map.
638 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000639 __ B(GetExitLabel());
640 }
641
642 private:
Roland Levillain44015862016-01-22 11:47:17 +0000643 const Location obj_;
644
645 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
646};
647
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000648// Slow path generating a read barrier for a heap reference.
649class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
650 public:
651 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
652 Location out,
653 Location ref,
654 Location obj,
655 uint32_t offset,
656 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000657 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000658 out_(out),
659 ref_(ref),
660 obj_(obj),
661 offset_(offset),
662 index_(index) {
663 DCHECK(kEmitCompilerReadBarrier);
664 // If `obj` is equal to `out` or `ref`, it means the initial object
665 // has been overwritten by (or after) the heap object reference load
666 // to be instrumented, e.g.:
667 //
668 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000669 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670 //
671 // In that case, we have lost the information about the original
672 // object, and the emitted read barrier cannot work properly.
673 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
674 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
675 }
676
677 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
678 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
679 LocationSummary* locations = instruction_->GetLocations();
680 Primitive::Type type = Primitive::kPrimNot;
681 DCHECK(locations->CanCall());
682 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100683 DCHECK(instruction_->IsInstanceFieldGet() ||
684 instruction_->IsStaticFieldGet() ||
685 instruction_->IsArrayGet() ||
686 instruction_->IsInstanceOf() ||
687 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100688 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000689 << "Unexpected instruction in read barrier for heap reference slow path: "
690 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000691 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000692 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100693 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000694
695 __ Bind(GetEntryLabel());
696
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000697 SaveLiveRegisters(codegen, locations);
698
699 // We may have to change the index's value, but as `index_` is a
700 // constant member (like other "inputs" of this slow path),
701 // introduce a copy of it, `index`.
702 Location index = index_;
703 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100704 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000705 if (instruction_->IsArrayGet()) {
706 // Compute the actual memory offset and store it in `index`.
707 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
708 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
709 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
710 // We are about to change the value of `index_reg` (see the
711 // calls to vixl::MacroAssembler::Lsl and
712 // vixl::MacroAssembler::Mov below), but it has
713 // not been saved by the previous call to
714 // art::SlowPathCode::SaveLiveRegisters, as it is a
715 // callee-save register --
716 // art::SlowPathCode::SaveLiveRegisters does not consider
717 // callee-save registers, as it has been designed with the
718 // assumption that callee-save registers are supposed to be
719 // handled by the called function. So, as a callee-save
720 // register, `index_reg` _would_ eventually be saved onto
721 // the stack, but it would be too late: we would have
722 // changed its value earlier. Therefore, we manually save
723 // it here into another freely available register,
724 // `free_reg`, chosen of course among the caller-save
725 // registers (as a callee-save `free_reg` register would
726 // exhibit the same problem).
727 //
728 // Note we could have requested a temporary register from
729 // the register allocator instead; but we prefer not to, as
730 // this is a slow path, and we know we can find a
731 // caller-save register that is available.
732 Register free_reg = FindAvailableCallerSaveRegister(codegen);
733 __ Mov(free_reg.W(), index_reg);
734 index_reg = free_reg;
735 index = LocationFrom(index_reg);
736 } else {
737 // The initial register stored in `index_` has already been
738 // saved in the call to art::SlowPathCode::SaveLiveRegisters
739 // (as it is not a callee-save register), so we can freely
740 // use it.
741 }
742 // Shifting the index value contained in `index_reg` by the scale
743 // factor (2) cannot overflow in practice, as the runtime is
744 // unable to allocate object arrays with a size larger than
745 // 2^26 - 1 (that is, 2^28 - 4 bytes).
746 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
747 static_assert(
748 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
749 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
750 __ Add(index_reg, index_reg, Operand(offset_));
751 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
753 // intrinsics, `index_` is not shifted by a scale factor of 2
754 // (as in the case of ArrayGet), as it is actually an offset
755 // to an object field within an object.
756 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000757 DCHECK(instruction_->GetLocations()->Intrinsified());
758 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
759 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
760 << instruction_->AsInvoke()->GetIntrinsic();
761 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100762 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000763 }
764 }
765
766 // We're moving two or three locations to locations that could
767 // overlap, so we need a parallel move resolver.
768 InvokeRuntimeCallingConvention calling_convention;
769 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
770 parallel_move.AddMove(ref_,
771 LocationFrom(calling_convention.GetRegisterAt(0)),
772 type,
773 nullptr);
774 parallel_move.AddMove(obj_,
775 LocationFrom(calling_convention.GetRegisterAt(1)),
776 type,
777 nullptr);
778 if (index.IsValid()) {
779 parallel_move.AddMove(index,
780 LocationFrom(calling_convention.GetRegisterAt(2)),
781 Primitive::kPrimInt,
782 nullptr);
783 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
784 } else {
785 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
786 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
787 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000788 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000789 instruction_,
790 instruction_->GetDexPc(),
791 this);
792 CheckEntrypointTypes<
793 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
794 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
795
796 RestoreLiveRegisters(codegen, locations);
797
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000798 __ B(GetExitLabel());
799 }
800
801 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
802
803 private:
804 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100805 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
806 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
808 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
809 return Register(VIXLRegCodeFromART(i), kXRegSize);
810 }
811 }
812 // We shall never fail to find a free caller-save register, as
813 // there are more than two core caller-save registers on ARM64
814 // (meaning it is possible to find one which is different from
815 // `ref` and `obj`).
816 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
817 LOG(FATAL) << "Could not find a free register";
818 UNREACHABLE();
819 }
820
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000821 const Location out_;
822 const Location ref_;
823 const Location obj_;
824 const uint32_t offset_;
825 // An additional location containing an index to an array.
826 // Only used for HArrayGet and the UnsafeGetObject &
827 // UnsafeGetObjectVolatile intrinsics.
828 const Location index_;
829
830 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
831};
832
833// Slow path generating a read barrier for a GC root.
834class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
835 public:
836 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000837 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000838 DCHECK(kEmitCompilerReadBarrier);
839 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000840
841 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
842 LocationSummary* locations = instruction_->GetLocations();
843 Primitive::Type type = Primitive::kPrimNot;
844 DCHECK(locations->CanCall());
845 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000846 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
847 << "Unexpected instruction in read barrier for GC root slow path: "
848 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000849
850 __ Bind(GetEntryLabel());
851 SaveLiveRegisters(codegen, locations);
852
853 InvokeRuntimeCallingConvention calling_convention;
854 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
855 // The argument of the ReadBarrierForRootSlow is not a managed
856 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
857 // thus we need a 64-bit move here, and we cannot use
858 //
859 // arm64_codegen->MoveLocation(
860 // LocationFrom(calling_convention.GetRegisterAt(0)),
861 // root_,
862 // type);
863 //
864 // which would emit a 32-bit move, as `type` is a (32-bit wide)
865 // reference type (`Primitive::kPrimNot`).
866 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000867 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000868 instruction_,
869 instruction_->GetDexPc(),
870 this);
871 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
872 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
873
874 RestoreLiveRegisters(codegen, locations);
875 __ B(GetExitLabel());
876 }
877
878 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
879
880 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000881 const Location out_;
882 const Location root_;
883
884 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
885};
886
Alexandre Rames5319def2014-10-23 10:03:10 +0100887#undef __
888
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100889Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100890 Location next_location;
891 if (type == Primitive::kPrimVoid) {
892 LOG(FATAL) << "Unreachable type " << type;
893 }
894
Alexandre Rames542361f2015-01-29 16:57:31 +0000895 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100896 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
897 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000898 } else if (!Primitive::IsFloatingPointType(type) &&
899 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000900 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
901 } else {
902 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
904 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100905 }
906
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000907 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000908 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100909 return next_location;
910}
911
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100912Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100913 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100914}
915
Serban Constantinescu579885a2015-02-22 20:51:33 +0000916CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
917 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100918 const CompilerOptions& compiler_options,
919 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100920 : CodeGenerator(graph,
921 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000922 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000923 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100924 callee_saved_core_registers.GetList(),
925 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100926 compiler_options,
927 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100928 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800929 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100930 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000931 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000932 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100933 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000934 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000935 uint32_literals_(std::less<uint32_t>(),
936 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100937 uint64_literals_(std::less<uint64_t>(),
938 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
939 method_patches_(MethodReferenceComparator(),
940 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
941 call_patches_(MethodReferenceComparator(),
942 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
943 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000944 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 boot_image_string_patches_(StringReferenceValueComparator(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
947 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100948 boot_image_type_patches_(TypeReferenceValueComparator(),
949 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
950 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000951 boot_image_address_patches_(std::less<uint32_t>(),
952 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000953 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000954 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000955}
Alexandre Rames5319def2014-10-23 10:03:10 +0100956
Alexandre Rames67555f72014-11-18 10:55:16 +0000957#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100958
Zheng Xu3927c8b2015-11-18 17:46:25 +0800959void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100960 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800961 jump_table->EmitTable(this);
962 }
963}
964
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000965void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800966 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000967 // Ensure we emit the literal pool.
968 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000969
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000970 CodeGenerator::Finalize(allocator);
971}
972
Zheng Xuad4450e2015-04-17 18:48:56 +0800973void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
974 // Note: There are 6 kinds of moves:
975 // 1. constant -> GPR/FPR (non-cycle)
976 // 2. constant -> stack (non-cycle)
977 // 3. GPR/FPR -> GPR/FPR
978 // 4. GPR/FPR -> stack
979 // 5. stack -> GPR/FPR
980 // 6. stack -> stack (non-cycle)
981 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
982 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
983 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
984 // dependency.
985 vixl_temps_.Open(GetVIXLAssembler());
986}
987
988void ParallelMoveResolverARM64::FinishEmitNativeCode() {
989 vixl_temps_.Close();
990}
991
992Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
993 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
994 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
995 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
996 Location scratch = GetScratchLocation(kind);
997 if (!scratch.Equals(Location::NoLocation())) {
998 return scratch;
999 }
1000 // Allocate from VIXL temp registers.
1001 if (kind == Location::kRegister) {
1002 scratch = LocationFrom(vixl_temps_.AcquireX());
1003 } else {
1004 DCHECK(kind == Location::kFpuRegister);
1005 scratch = LocationFrom(vixl_temps_.AcquireD());
1006 }
1007 AddScratchLocation(scratch);
1008 return scratch;
1009}
1010
1011void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1012 if (loc.IsRegister()) {
1013 vixl_temps_.Release(XRegisterFrom(loc));
1014 } else {
1015 DCHECK(loc.IsFpuRegister());
1016 vixl_temps_.Release(DRegisterFrom(loc));
1017 }
1018 RemoveScratchLocation(loc);
1019}
1020
Alexandre Rames3e69f162014-12-10 10:36:50 +00001021void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001022 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001023 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001024}
1025
Alexandre Rames5319def2014-10-23 10:03:10 +01001026void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001027 MacroAssembler* masm = GetVIXLAssembler();
1028 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001029 __ Bind(&frame_entry_label_);
1030
Serban Constantinescu02164b32014-11-13 14:05:07 +00001031 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1032 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001033 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001034 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001035 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001036 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001037 __ Ldr(wzr, MemOperand(temp, 0));
1038 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001039 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001040
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001041 if (!HasEmptyFrame()) {
1042 int frame_size = GetFrameSize();
1043 // Stack layout:
1044 // sp[frame_size - 8] : lr.
1045 // ... : other preserved core registers.
1046 // ... : other preserved fp registers.
1047 // ... : reserved frame space.
1048 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001049
1050 // Save the current method if we need it. Note that we do not
1051 // do this in HCurrentMethod, as the instruction might have been removed
1052 // in the SSA graph.
1053 if (RequiresCurrentMethod()) {
1054 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001055 } else {
1056 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001057 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001059 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1060 frame_size - GetCoreSpillSize());
1061 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1062 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001063 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001064}
1065
1066void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001067 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001068 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001069 if (!HasEmptyFrame()) {
1070 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001071 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1072 frame_size - FrameEntrySpillSize());
1073 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1074 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001075 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001077 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001078 __ Ret();
1079 GetAssembler()->cfi().RestoreState();
1080 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001081}
1082
Scott Wakeling97c72b72016-06-24 16:19:36 +01001083CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001084 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001085 return CPURegList(CPURegister::kRegister, kXRegSize,
1086 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001087}
1088
Scott Wakeling97c72b72016-06-24 16:19:36 +01001089CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001090 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1091 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001092 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1093 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001094}
1095
Alexandre Rames5319def2014-10-23 10:03:10 +01001096void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1097 __ Bind(GetLabelOf(block));
1098}
1099
Calin Juravle175dc732015-08-25 15:42:32 +01001100void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1101 DCHECK(location.IsRegister());
1102 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1103}
1104
Calin Juravlee460d1d2015-09-29 04:52:17 +01001105void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1106 if (location.IsRegister()) {
1107 locations->AddTemp(location);
1108 } else {
1109 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1110 }
1111}
1112
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001113void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001114 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001115 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001116 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001117 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001118 if (value_can_be_null) {
1119 __ Cbz(value, &done);
1120 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001121 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001122 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001123 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001124 if (value_can_be_null) {
1125 __ Bind(&done);
1126 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001127}
1128
David Brazdil58282f42016-01-14 12:45:10 +00001129void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001130 // Blocked core registers:
1131 // lr : Runtime reserved.
1132 // tr : Runtime reserved.
1133 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1134 // ip1 : VIXL core temp.
1135 // ip0 : VIXL core temp.
1136 //
1137 // Blocked fp registers:
1138 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1140 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001142 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001143 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001144
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001145 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001146 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001147 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001148 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001149
David Brazdil58282f42016-01-14 12:45:10 +00001150 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001151 // Stubs do not save callee-save floating point registers. If the graph
1152 // is debuggable, we need to deal with these registers differently. For
1153 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001154 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1155 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001156 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001157 }
1158 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001159}
1160
Alexandre Rames3e69f162014-12-10 10:36:50 +00001161size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1162 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1163 __ Str(reg, MemOperand(sp, stack_index));
1164 return kArm64WordSize;
1165}
1166
1167size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1168 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1169 __ Ldr(reg, MemOperand(sp, stack_index));
1170 return kArm64WordSize;
1171}
1172
1173size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1174 FPRegister reg = FPRegister(reg_id, kDRegSize);
1175 __ Str(reg, MemOperand(sp, stack_index));
1176 return kArm64WordSize;
1177}
1178
1179size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1180 FPRegister reg = FPRegister(reg_id, kDRegSize);
1181 __ Ldr(reg, MemOperand(sp, stack_index));
1182 return kArm64WordSize;
1183}
1184
Alexandre Rames5319def2014-10-23 10:03:10 +01001185void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001186 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001187}
1188
1189void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001190 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001191}
1192
Alexandre Rames67555f72014-11-18 10:55:16 +00001193void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001194 if (constant->IsIntConstant()) {
1195 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1196 } else if (constant->IsLongConstant()) {
1197 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1198 } else if (constant->IsNullConstant()) {
1199 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001200 } else if (constant->IsFloatConstant()) {
1201 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1202 } else {
1203 DCHECK(constant->IsDoubleConstant());
1204 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1205 }
1206}
1207
Alexandre Rames3e69f162014-12-10 10:36:50 +00001208
1209static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1210 DCHECK(constant.IsConstant());
1211 HConstant* cst = constant.GetConstant();
1212 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001213 // Null is mapped to a core W register, which we associate with kPrimInt.
1214 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001215 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1216 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1217 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1218}
1219
Calin Juravlee460d1d2015-09-29 04:52:17 +01001220void CodeGeneratorARM64::MoveLocation(Location destination,
1221 Location source,
1222 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001223 if (source.Equals(destination)) {
1224 return;
1225 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001226
1227 // A valid move can always be inferred from the destination and source
1228 // locations. When moving from and to a register, the argument type can be
1229 // used to generate 32bit instead of 64bit moves. In debug mode we also
1230 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001232
1233 if (destination.IsRegister() || destination.IsFpuRegister()) {
1234 if (unspecified_type) {
1235 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1236 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001237 (src_cst != nullptr && (src_cst->IsIntConstant()
1238 || src_cst->IsFloatConstant()
1239 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001242 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001243 // If the source is a double stack slot or a 64bit constant, a 64bit
1244 // type is appropriate. Else the source is a register, and since the
1245 // type has not been specified, we chose a 64bit type to force a 64bit
1246 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001248 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1251 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1252 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001253 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1254 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1255 __ Ldr(dst, StackOperandFrom(source));
1256 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001260 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001261 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001262 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001263 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001264 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1265 ? Primitive::kPrimLong
1266 : Primitive::kPrimInt;
1267 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1268 }
1269 } else {
1270 DCHECK(source.IsFpuRegister());
1271 if (destination.IsRegister()) {
1272 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1273 ? Primitive::kPrimDouble
1274 : Primitive::kPrimFloat;
1275 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1276 } else {
1277 DCHECK(destination.IsFpuRegister());
1278 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001279 }
1280 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 } else { // The destination is not a register. It must be a stack slot.
1282 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1283 if (source.IsRegister() || source.IsFpuRegister()) {
1284 if (unspecified_type) {
1285 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001287 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001289 }
1290 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1292 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1293 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001295 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1296 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001297 UseScratchRegisterScope temps(GetVIXLAssembler());
1298 HConstant* src_cst = source.GetConstant();
1299 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001300 if (src_cst->IsZeroBitPattern()) {
1301 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001302 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001303 if (src_cst->IsIntConstant()) {
1304 temp = temps.AcquireW();
1305 } else if (src_cst->IsLongConstant()) {
1306 temp = temps.AcquireX();
1307 } else if (src_cst->IsFloatConstant()) {
1308 temp = temps.AcquireS();
1309 } else {
1310 DCHECK(src_cst->IsDoubleConstant());
1311 temp = temps.AcquireD();
1312 }
1313 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001314 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001317 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001318 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001320 // There is generally less pressure on FP registers.
1321 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001322 __ Ldr(temp, StackOperandFrom(source));
1323 __ Str(temp, StackOperandFrom(destination));
1324 }
1325 }
1326}
1327
1328void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001329 CPURegister dst,
1330 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001331 switch (type) {
1332 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001333 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 break;
1335 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001336 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001337 break;
1338 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001339 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001340 break;
1341 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001342 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001343 break;
1344 case Primitive::kPrimInt:
1345 case Primitive::kPrimNot:
1346 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001347 case Primitive::kPrimFloat:
1348 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001349 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001350 __ Ldr(dst, src);
1351 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001352 case Primitive::kPrimVoid:
1353 LOG(FATAL) << "Unreachable type " << type;
1354 }
1355}
1356
Calin Juravle77520bc2015-01-12 18:45:46 +00001357void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001359 const MemOperand& src,
1360 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001361 MacroAssembler* masm = GetVIXLAssembler();
1362 BlockPoolsScope block_pools(masm);
1363 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001364 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001365 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001366
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001367 DCHECK(!src.IsPreIndex());
1368 DCHECK(!src.IsPostIndex());
1369
1370 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001371 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001372 MemOperand base = MemOperand(temp_base);
1373 switch (type) {
1374 case Primitive::kPrimBoolean:
1375 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001376 if (needs_null_check) {
1377 MaybeRecordImplicitNullCheck(instruction);
1378 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001379 break;
1380 case Primitive::kPrimByte:
1381 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001382 if (needs_null_check) {
1383 MaybeRecordImplicitNullCheck(instruction);
1384 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001385 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1386 break;
1387 case Primitive::kPrimChar:
1388 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001389 if (needs_null_check) {
1390 MaybeRecordImplicitNullCheck(instruction);
1391 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001392 break;
1393 case Primitive::kPrimShort:
1394 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001395 if (needs_null_check) {
1396 MaybeRecordImplicitNullCheck(instruction);
1397 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001398 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1399 break;
1400 case Primitive::kPrimInt:
1401 case Primitive::kPrimNot:
1402 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001403 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001404 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001405 if (needs_null_check) {
1406 MaybeRecordImplicitNullCheck(instruction);
1407 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001408 break;
1409 case Primitive::kPrimFloat:
1410 case Primitive::kPrimDouble: {
1411 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001412 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001413
1414 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1415 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001416 if (needs_null_check) {
1417 MaybeRecordImplicitNullCheck(instruction);
1418 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001419 __ Fmov(FPRegister(dst), temp);
1420 break;
1421 }
1422 case Primitive::kPrimVoid:
1423 LOG(FATAL) << "Unreachable type " << type;
1424 }
1425}
1426
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001427void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428 CPURegister src,
1429 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001430 switch (type) {
1431 case Primitive::kPrimBoolean:
1432 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001433 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001434 break;
1435 case Primitive::kPrimChar:
1436 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001437 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001438 break;
1439 case Primitive::kPrimInt:
1440 case Primitive::kPrimNot:
1441 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001442 case Primitive::kPrimFloat:
1443 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001444 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001445 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001446 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001447 case Primitive::kPrimVoid:
1448 LOG(FATAL) << "Unreachable type " << type;
1449 }
1450}
1451
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001452void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1453 CPURegister src,
1454 const MemOperand& dst) {
1455 UseScratchRegisterScope temps(GetVIXLAssembler());
1456 Register temp_base = temps.AcquireX();
1457
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001458 DCHECK(!dst.IsPreIndex());
1459 DCHECK(!dst.IsPostIndex());
1460
1461 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001462 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001463 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001464 MemOperand base = MemOperand(temp_base);
1465 switch (type) {
1466 case Primitive::kPrimBoolean:
1467 case Primitive::kPrimByte:
1468 __ Stlrb(Register(src), base);
1469 break;
1470 case Primitive::kPrimChar:
1471 case Primitive::kPrimShort:
1472 __ Stlrh(Register(src), base);
1473 break;
1474 case Primitive::kPrimInt:
1475 case Primitive::kPrimNot:
1476 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001477 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001478 __ Stlr(Register(src), base);
1479 break;
1480 case Primitive::kPrimFloat:
1481 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001482 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001483 Register temp_src;
1484 if (src.IsZero()) {
1485 // The zero register is used to avoid synthesizing zero constants.
1486 temp_src = Register(src);
1487 } else {
1488 DCHECK(src.IsFPRegister());
1489 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1490 __ Fmov(temp_src, FPRegister(src));
1491 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001492
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001493 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001494 break;
1495 }
1496 case Primitive::kPrimVoid:
1497 LOG(FATAL) << "Unreachable type " << type;
1498 }
1499}
1500
Calin Juravle175dc732015-08-25 15:42:32 +01001501void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1502 HInstruction* instruction,
1503 uint32_t dex_pc,
1504 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001505 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001506 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001507 if (EntrypointRequiresStackMap(entrypoint)) {
1508 RecordPcInfo(instruction, dex_pc, slow_path);
1509 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001510}
1511
Roland Levillaindec8f632016-07-22 17:10:06 +01001512void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1513 HInstruction* instruction,
1514 SlowPathCode* slow_path) {
1515 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001516 GenerateInvokeRuntime(entry_point_offset);
1517}
1518
1519void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001520 BlockPoolsScope block_pools(GetVIXLAssembler());
1521 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1522 __ Blr(lr);
1523}
1524
Alexandre Rames67555f72014-11-18 10:55:16 +00001525void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001526 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001527 UseScratchRegisterScope temps(GetVIXLAssembler());
1528 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001529 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1530
Serban Constantinescu02164b32014-11-13 14:05:07 +00001531 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001532 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1533 __ Add(temp, class_reg, status_offset);
1534 __ Ldar(temp, HeapOperand(temp));
1535 __ Cmp(temp, mirror::Class::kStatusInitialized);
1536 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001537 __ Bind(slow_path->GetExitLabel());
1538}
Alexandre Rames5319def2014-10-23 10:03:10 +01001539
Roland Levillain44015862016-01-22 11:47:17 +00001540void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001541 BarrierType type = BarrierAll;
1542
1543 switch (kind) {
1544 case MemBarrierKind::kAnyAny:
1545 case MemBarrierKind::kAnyStore: {
1546 type = BarrierAll;
1547 break;
1548 }
1549 case MemBarrierKind::kLoadAny: {
1550 type = BarrierReads;
1551 break;
1552 }
1553 case MemBarrierKind::kStoreStore: {
1554 type = BarrierWrites;
1555 break;
1556 }
1557 default:
1558 LOG(FATAL) << "Unexpected memory barrier " << kind;
1559 }
1560 __ Dmb(InnerShareable, type);
1561}
1562
Serban Constantinescu02164b32014-11-13 14:05:07 +00001563void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1564 HBasicBlock* successor) {
1565 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001566 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1567 if (slow_path == nullptr) {
1568 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1569 instruction->SetSlowPath(slow_path);
1570 codegen_->AddSlowPath(slow_path);
1571 if (successor != nullptr) {
1572 DCHECK(successor->IsLoopHeader());
1573 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1574 }
1575 } else {
1576 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1577 }
1578
Serban Constantinescu02164b32014-11-13 14:05:07 +00001579 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1580 Register temp = temps.AcquireW();
1581
Andreas Gampe542451c2016-07-26 09:02:02 -07001582 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001583 if (successor == nullptr) {
1584 __ Cbnz(temp, slow_path->GetEntryLabel());
1585 __ Bind(slow_path->GetReturnLabel());
1586 } else {
1587 __ Cbz(temp, codegen_->GetLabelOf(successor));
1588 __ B(slow_path->GetEntryLabel());
1589 // slow_path will return to GetLabelOf(successor).
1590 }
1591}
1592
Alexandre Rames5319def2014-10-23 10:03:10 +01001593InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1594 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001595 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001596 assembler_(codegen->GetAssembler()),
1597 codegen_(codegen) {}
1598
1599#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001600 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001601
1602#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1603
1604enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001605 // Using a base helps identify when we hit such breakpoints.
1606 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001607#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1608 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1609#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1610};
1611
1612#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001613 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001614 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1615 } \
1616 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1618 locations->SetOut(Location::Any()); \
1619 }
1620 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1621#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1622
1623#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001624#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001625
Alexandre Rames67555f72014-11-18 10:55:16 +00001626void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001627 DCHECK_EQ(instr->InputCount(), 2U);
1628 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1629 Primitive::Type type = instr->GetResultType();
1630 switch (type) {
1631 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001632 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001633 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001634 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001635 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001636 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001637
1638 case Primitive::kPrimFloat:
1639 case Primitive::kPrimDouble:
1640 locations->SetInAt(0, Location::RequiresFpuRegister());
1641 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001642 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001644
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001646 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001647 }
1648}
1649
Alexandre Rames09a99962015-04-15 11:47:56 +01001650void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001651 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1652
1653 bool object_field_get_with_read_barrier =
1654 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001655 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001656 new (GetGraph()->GetArena()) LocationSummary(instruction,
1657 object_field_get_with_read_barrier ?
1658 LocationSummary::kCallOnSlowPath :
1659 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001660 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001661 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001662 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001663 locations->SetInAt(0, Location::RequiresRegister());
1664 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1665 locations->SetOut(Location::RequiresFpuRegister());
1666 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001667 // The output overlaps for an object field get when read barriers
1668 // are enabled: we do not want the load to overwrite the object's
1669 // location, as we need it to emit the read barrier.
1670 locations->SetOut(
1671 Location::RequiresRegister(),
1672 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001673 }
1674}
1675
1676void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1677 const FieldInfo& field_info) {
1678 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001679 LocationSummary* locations = instruction->GetLocations();
1680 Location base_loc = locations->InAt(0);
1681 Location out = locations->Out();
1682 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001683 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001684 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001685 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001686
Roland Levillain44015862016-01-22 11:47:17 +00001687 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1688 // Object FieldGet with Baker's read barrier case.
1689 MacroAssembler* masm = GetVIXLAssembler();
1690 UseScratchRegisterScope temps(masm);
1691 // /* HeapReference<Object> */ out = *(base + offset)
1692 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1693 Register temp = temps.AcquireW();
1694 // Note that potential implicit null checks are handled in this
1695 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1696 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1697 instruction,
1698 out,
1699 base,
1700 offset,
1701 temp,
1702 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001703 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001704 } else {
1705 // General case.
1706 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001707 // Note that a potential implicit null check is handled in this
1708 // CodeGeneratorARM64::LoadAcquire call.
1709 // NB: LoadAcquire will record the pc info if needed.
1710 codegen_->LoadAcquire(
1711 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001712 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001713 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001714 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001715 }
Roland Levillain44015862016-01-22 11:47:17 +00001716 if (field_type == Primitive::kPrimNot) {
1717 // If read barriers are enabled, emit read barriers other than
1718 // Baker's using a slow path (and also unpoison the loaded
1719 // reference, if heap poisoning is enabled).
1720 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1721 }
Roland Levillain4d027112015-07-01 15:41:14 +01001722 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001723}
1724
1725void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1726 LocationSummary* locations =
1727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1728 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001729 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1730 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1731 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001732 locations->SetInAt(1, Location::RequiresFpuRegister());
1733 } else {
1734 locations->SetInAt(1, Location::RequiresRegister());
1735 }
1736}
1737
1738void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001739 const FieldInfo& field_info,
1740 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001741 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001742 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001743
1744 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001745 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001746 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001747 Offset offset = field_info.GetFieldOffset();
1748 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001749
Roland Levillain4d027112015-07-01 15:41:14 +01001750 {
1751 // We use a block to end the scratch scope before the write barrier, thus
1752 // freeing the temporary registers so they can be used in `MarkGCCard`.
1753 UseScratchRegisterScope temps(GetVIXLAssembler());
1754
1755 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1756 DCHECK(value.IsW());
1757 Register temp = temps.AcquireW();
1758 __ Mov(temp, value.W());
1759 GetAssembler()->PoisonHeapReference(temp.W());
1760 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001761 }
Roland Levillain4d027112015-07-01 15:41:14 +01001762
1763 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001764 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1765 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001766 } else {
1767 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1768 codegen_->MaybeRecordImplicitNullCheck(instruction);
1769 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001770 }
1771
1772 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001773 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001774 }
1775}
1776
Alexandre Rames67555f72014-11-18 10:55:16 +00001777void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001778 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001779
1780 switch (type) {
1781 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001782 case Primitive::kPrimLong: {
1783 Register dst = OutputRegister(instr);
1784 Register lhs = InputRegisterAt(instr, 0);
1785 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001786 if (instr->IsAdd()) {
1787 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001788 } else if (instr->IsAnd()) {
1789 __ And(dst, lhs, rhs);
1790 } else if (instr->IsOr()) {
1791 __ Orr(dst, lhs, rhs);
1792 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001793 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001794 } else if (instr->IsRor()) {
1795 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001796 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001797 __ Ror(dst, lhs, shift);
1798 } else {
1799 // Ensure shift distance is in the same size register as the result. If
1800 // we are rotating a long and the shift comes in a w register originally,
1801 // we don't need to sxtw for use as an x since the shift distances are
1802 // all & reg_bits - 1.
1803 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1804 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001805 } else {
1806 DCHECK(instr->IsXor());
1807 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001808 }
1809 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001810 }
1811 case Primitive::kPrimFloat:
1812 case Primitive::kPrimDouble: {
1813 FPRegister dst = OutputFPRegister(instr);
1814 FPRegister lhs = InputFPRegisterAt(instr, 0);
1815 FPRegister rhs = InputFPRegisterAt(instr, 1);
1816 if (instr->IsAdd()) {
1817 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001818 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001819 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001820 } else {
1821 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001822 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001823 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001824 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001825 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001826 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001827 }
1828}
1829
Serban Constantinescu02164b32014-11-13 14:05:07 +00001830void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1831 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1832
1833 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1834 Primitive::Type type = instr->GetResultType();
1835 switch (type) {
1836 case Primitive::kPrimInt:
1837 case Primitive::kPrimLong: {
1838 locations->SetInAt(0, Location::RequiresRegister());
1839 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1840 locations->SetOut(Location::RequiresRegister());
1841 break;
1842 }
1843 default:
1844 LOG(FATAL) << "Unexpected shift type " << type;
1845 }
1846}
1847
1848void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1849 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1850
1851 Primitive::Type type = instr->GetType();
1852 switch (type) {
1853 case Primitive::kPrimInt:
1854 case Primitive::kPrimLong: {
1855 Register dst = OutputRegister(instr);
1856 Register lhs = InputRegisterAt(instr, 0);
1857 Operand rhs = InputOperandAt(instr, 1);
1858 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001859 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001860 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001861 if (instr->IsShl()) {
1862 __ Lsl(dst, lhs, shift_value);
1863 } else if (instr->IsShr()) {
1864 __ Asr(dst, lhs, shift_value);
1865 } else {
1866 __ Lsr(dst, lhs, shift_value);
1867 }
1868 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001869 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001870
1871 if (instr->IsShl()) {
1872 __ Lsl(dst, lhs, rhs_reg);
1873 } else if (instr->IsShr()) {
1874 __ Asr(dst, lhs, rhs_reg);
1875 } else {
1876 __ Lsr(dst, lhs, rhs_reg);
1877 }
1878 }
1879 break;
1880 }
1881 default:
1882 LOG(FATAL) << "Unexpected shift operation type " << type;
1883 }
1884}
1885
Alexandre Rames5319def2014-10-23 10:03:10 +01001886void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001887 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001888}
1889
1890void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001891 HandleBinaryOp(instruction);
1892}
1893
1894void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1895 HandleBinaryOp(instruction);
1896}
1897
1898void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1899 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001900}
1901
Artem Serov7fc63502016-02-09 17:15:29 +00001902void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001903 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1905 locations->SetInAt(0, Location::RequiresRegister());
1906 // There is no immediate variant of negated bitwise instructions in AArch64.
1907 locations->SetInAt(1, Location::RequiresRegister());
1908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1909}
1910
Artem Serov7fc63502016-02-09 17:15:29 +00001911void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001912 Register dst = OutputRegister(instr);
1913 Register lhs = InputRegisterAt(instr, 0);
1914 Register rhs = InputRegisterAt(instr, 1);
1915
1916 switch (instr->GetOpKind()) {
1917 case HInstruction::kAnd:
1918 __ Bic(dst, lhs, rhs);
1919 break;
1920 case HInstruction::kOr:
1921 __ Orn(dst, lhs, rhs);
1922 break;
1923 case HInstruction::kXor:
1924 __ Eon(dst, lhs, rhs);
1925 break;
1926 default:
1927 LOG(FATAL) << "Unreachable";
1928 }
1929}
1930
Alexandre Rames8626b742015-11-25 16:28:08 +00001931void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1932 HArm64DataProcWithShifterOp* instruction) {
1933 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1934 instruction->GetType() == Primitive::kPrimLong);
1935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1937 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1938 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1939 } else {
1940 locations->SetInAt(0, Location::RequiresRegister());
1941 }
1942 locations->SetInAt(1, Location::RequiresRegister());
1943 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1944}
1945
1946void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1947 HArm64DataProcWithShifterOp* instruction) {
1948 Primitive::Type type = instruction->GetType();
1949 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1950 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1951 Register out = OutputRegister(instruction);
1952 Register left;
1953 if (kind != HInstruction::kNeg) {
1954 left = InputRegisterAt(instruction, 0);
1955 }
1956 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1957 // shifter operand operation, the IR generating `right_reg` (input to the type
1958 // conversion) can have a different type from the current instruction's type,
1959 // so we manually indicate the type.
1960 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001961 int64_t shift_amount = instruction->GetShiftAmount() &
1962 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001963
1964 Operand right_operand(0);
1965
1966 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1967 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1968 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1969 } else {
1970 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1971 }
1972
1973 // Logical binary operations do not support extension operations in the
1974 // operand. Note that VIXL would still manage if it was passed by generating
1975 // the extension as a separate instruction.
1976 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1977 DCHECK(!right_operand.IsExtendedRegister() ||
1978 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1979 kind != HInstruction::kNeg));
1980 switch (kind) {
1981 case HInstruction::kAdd:
1982 __ Add(out, left, right_operand);
1983 break;
1984 case HInstruction::kAnd:
1985 __ And(out, left, right_operand);
1986 break;
1987 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001988 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001989 __ Neg(out, right_operand);
1990 break;
1991 case HInstruction::kOr:
1992 __ Orr(out, left, right_operand);
1993 break;
1994 case HInstruction::kSub:
1995 __ Sub(out, left, right_operand);
1996 break;
1997 case HInstruction::kXor:
1998 __ Eor(out, left, right_operand);
1999 break;
2000 default:
2001 LOG(FATAL) << "Unexpected operation kind: " << kind;
2002 UNREACHABLE();
2003 }
2004}
2005
Artem Serov328429f2016-07-06 16:23:04 +01002006void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002007 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2008 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2011 locations->SetInAt(0, Location::RequiresRegister());
2012 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2013 locations->SetOut(Location::RequiresRegister());
2014}
2015
Roland Levillain4a3aa572016-08-15 13:17:06 +00002016void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
2017 HIntermediateAddress* instruction) {
2018 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2019 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002020 __ Add(OutputRegister(instruction),
2021 InputRegisterAt(instruction, 0),
2022 Operand(InputOperandAt(instruction, 1)));
2023}
2024
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002025void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002026 LocationSummary* locations =
2027 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002028 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2029 if (instr->GetOpKind() == HInstruction::kSub &&
2030 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002031 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002032 // Don't allocate register for Mneg instruction.
2033 } else {
2034 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2035 Location::RequiresRegister());
2036 }
2037 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2038 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2040}
2041
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002042void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002043 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002044 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2045 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002046
2047 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2048 // This fixup should be carried out for all multiply-accumulate instructions:
2049 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2050 if (instr->GetType() == Primitive::kPrimLong &&
2051 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2052 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002053 vixl::aarch64::Instruction* prev =
2054 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002055 if (prev->IsLoadOrStore()) {
2056 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002057 vixl::aarch64::CodeBufferCheckScope scope(masm,
2058 kInstructionSize,
2059 vixl::aarch64::CodeBufferCheckScope::kCheck,
2060 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002061 __ nop();
2062 }
2063 }
2064
2065 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002066 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002067 __ Madd(res, mul_left, mul_right, accumulator);
2068 } else {
2069 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002070 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002071 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002072 __ Mneg(res, mul_left, mul_right);
2073 } else {
2074 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2075 __ Msub(res, mul_left, mul_right, accumulator);
2076 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002077 }
2078}
2079
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002080void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002081 bool object_array_get_with_read_barrier =
2082 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002083 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002084 new (GetGraph()->GetArena()) LocationSummary(instruction,
2085 object_array_get_with_read_barrier ?
2086 LocationSummary::kCallOnSlowPath :
2087 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002088 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002089 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002090 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002091 locations->SetInAt(0, Location::RequiresRegister());
2092 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002093 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2094 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2095 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002096 // The output overlaps in the case of an object array get with
2097 // read barriers enabled: we do not want the move to overwrite the
2098 // array's location, as we need it to emit the read barrier.
2099 locations->SetOut(
2100 Location::RequiresRegister(),
2101 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002102 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002103}
2104
2105void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002106 Primitive::Type type = instruction->GetType();
2107 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002108 LocationSummary* locations = instruction->GetLocations();
2109 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002110 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002111 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002112 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2113 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002114 MacroAssembler* masm = GetVIXLAssembler();
2115 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002116 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002117 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002118
Roland Levillain44015862016-01-22 11:47:17 +00002119 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2120 // Object ArrayGet with Baker's read barrier case.
2121 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002122 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2123 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002124 // Note that a potential implicit null check is handled in the
2125 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2126 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2127 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002128 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002129 // General case.
2130 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002131 Register length;
2132 if (maybe_compressed_char_at) {
2133 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2134 length = temps.AcquireW();
2135 __ Ldr(length, HeapOperand(obj, count_offset));
2136 codegen_->MaybeRecordImplicitNullCheck(instruction);
2137 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002138 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002139 if (maybe_compressed_char_at) {
2140 vixl::aarch64::Label uncompressed_load, done;
2141 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2142 __ Ldrb(Register(OutputCPURegister(instruction)),
2143 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2144 __ B(&done);
2145 __ Bind(&uncompressed_load);
2146 __ Ldrh(Register(OutputCPURegister(instruction)),
2147 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2148 __ Bind(&done);
2149 } else {
2150 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2151 source = HeapOperand(obj, offset);
2152 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002153 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002154 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002155 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002156 // The read barrier instrumentation does not support the
2157 // HIntermediateAddress instruction yet.
2158 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002159 // We do not need to compute the intermediate address from the array: the
2160 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002161 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002162 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002163 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002164 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2165 }
2166 temp = obj;
2167 } else {
2168 __ Add(temp, obj, offset);
2169 }
jessicahandojo05765752016-09-09 19:01:32 -07002170 if (maybe_compressed_char_at) {
2171 vixl::aarch64::Label uncompressed_load, done;
2172 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2173 __ Ldrb(Register(OutputCPURegister(instruction)),
2174 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2175 __ B(&done);
2176 __ Bind(&uncompressed_load);
2177 __ Ldrh(Register(OutputCPURegister(instruction)),
2178 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2179 __ Bind(&done);
2180 } else {
2181 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2182 }
Roland Levillain44015862016-01-22 11:47:17 +00002183 }
jessicahandojo05765752016-09-09 19:01:32 -07002184 if (!maybe_compressed_char_at) {
2185 codegen_->Load(type, OutputCPURegister(instruction), source);
2186 codegen_->MaybeRecordImplicitNullCheck(instruction);
2187 }
Roland Levillain44015862016-01-22 11:47:17 +00002188
2189 if (type == Primitive::kPrimNot) {
2190 static_assert(
2191 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2192 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2193 Location obj_loc = locations->InAt(0);
2194 if (index.IsConstant()) {
2195 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2196 } else {
2197 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2198 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002199 }
Roland Levillain4d027112015-07-01 15:41:14 +01002200 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002201}
2202
Alexandre Rames5319def2014-10-23 10:03:10 +01002203void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2204 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2205 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002207}
2208
2209void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002210 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002211 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002212 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002213 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002214 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002215 // Mask out compression flag from String's array length.
2216 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2217 __ And(out.W(), out.W(), Operand(static_cast<int32_t>(INT32_MAX)));
2218 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002219}
2220
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002221void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002222 Primitive::Type value_type = instruction->GetComponentType();
2223
2224 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2226 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002227 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002228 LocationSummary::kCallOnSlowPath :
2229 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002230 locations->SetInAt(0, Location::RequiresRegister());
2231 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002232 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2233 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2234 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002235 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002236 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002237 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002238 }
2239}
2240
2241void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2242 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002243 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002244 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002245 bool needs_write_barrier =
2246 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002247
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002248 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002249 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002250 CPURegister source = value;
2251 Location index = locations->InAt(1);
2252 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2253 MemOperand destination = HeapOperand(array);
2254 MacroAssembler* masm = GetVIXLAssembler();
2255 BlockPoolsScope block_pools(masm);
2256
2257 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002258 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002259 if (index.IsConstant()) {
2260 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2261 destination = HeapOperand(array, offset);
2262 } else {
2263 UseScratchRegisterScope temps(masm);
2264 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002265 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002266 // The read barrier instrumentation does not support the
2267 // HIntermediateAddress instruction yet.
2268 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002269 // We do not need to compute the intermediate address from the array: the
2270 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002271 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002272 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002273 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002274 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2275 }
2276 temp = array;
2277 } else {
2278 __ Add(temp, array, offset);
2279 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002280 destination = HeapOperand(temp,
2281 XRegisterFrom(index),
2282 LSL,
2283 Primitive::ComponentSizeShift(value_type));
2284 }
2285 codegen_->Store(value_type, value, destination);
2286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002287 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002288 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002289 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002290 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002291 {
2292 // We use a block to end the scratch scope before the write barrier, thus
2293 // freeing the temporary registers so they can be used in `MarkGCCard`.
2294 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002295 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002296 if (index.IsConstant()) {
2297 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002298 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002299 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002300 destination = HeapOperand(temp,
2301 XRegisterFrom(index),
2302 LSL,
2303 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002304 }
2305
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002306 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2307 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2308 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2309
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002310 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002311 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2312 codegen_->AddSlowPath(slow_path);
2313 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002314 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002315 __ Cbnz(Register(value), &non_zero);
2316 if (!index.IsConstant()) {
2317 __ Add(temp, array, offset);
2318 }
2319 __ Str(wzr, destination);
2320 codegen_->MaybeRecordImplicitNullCheck(instruction);
2321 __ B(&done);
2322 __ Bind(&non_zero);
2323 }
2324
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002325 // Note that when Baker read barriers are enabled, the type
2326 // checks are performed without read barriers. This is fine,
2327 // even in the case where a class object is in the from-space
2328 // after the flip, as a comparison involving such a type would
2329 // not produce a false positive; it may of course produce a
2330 // false negative, in which case we would take the ArraySet
2331 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002332
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002333 Register temp2 = temps.AcquireSameSizeAs(array);
2334 // /* HeapReference<Class> */ temp = array->klass_
2335 __ Ldr(temp, HeapOperand(array, class_offset));
2336 codegen_->MaybeRecordImplicitNullCheck(instruction);
2337 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002338
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002339 // /* HeapReference<Class> */ temp = temp->component_type_
2340 __ Ldr(temp, HeapOperand(temp, component_offset));
2341 // /* HeapReference<Class> */ temp2 = value->klass_
2342 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2343 // If heap poisoning is enabled, no need to unpoison `temp`
2344 // nor `temp2`, as we are comparing two poisoned references.
2345 __ Cmp(temp, temp2);
2346 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002347
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002348 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2349 vixl::aarch64::Label do_put;
2350 __ B(eq, &do_put);
2351 // If heap poisoning is enabled, the `temp` reference has
2352 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002353 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2354
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002355 // /* HeapReference<Class> */ temp = temp->super_class_
2356 __ Ldr(temp, HeapOperand(temp, super_offset));
2357 // If heap poisoning is enabled, no need to unpoison
2358 // `temp`, as we are comparing against null below.
2359 __ Cbnz(temp, slow_path->GetEntryLabel());
2360 __ Bind(&do_put);
2361 } else {
2362 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002363 }
2364 }
2365
2366 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002367 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002368 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002369 __ Mov(temp2, value.W());
2370 GetAssembler()->PoisonHeapReference(temp2);
2371 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002372 }
2373
2374 if (!index.IsConstant()) {
2375 __ Add(temp, array, offset);
2376 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002377 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002378
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002379 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002380 codegen_->MaybeRecordImplicitNullCheck(instruction);
2381 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002382 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002383
2384 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2385
2386 if (done.IsLinked()) {
2387 __ Bind(&done);
2388 }
2389
2390 if (slow_path != nullptr) {
2391 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002392 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002393 }
2394}
2395
Alexandre Rames67555f72014-11-18 10:55:16 +00002396void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002397 RegisterSet caller_saves = RegisterSet::Empty();
2398 InvokeRuntimeCallingConvention calling_convention;
2399 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2400 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2401 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002402 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002403 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002404}
2405
2406void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002407 BoundsCheckSlowPathARM64* slow_path =
2408 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002409 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002410 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2411 __ B(slow_path->GetEntryLabel(), hs);
2412}
2413
Alexandre Rames67555f72014-11-18 10:55:16 +00002414void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2415 LocationSummary* locations =
2416 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2417 locations->SetInAt(0, Location::RequiresRegister());
2418 if (check->HasUses()) {
2419 locations->SetOut(Location::SameAsFirstInput());
2420 }
2421}
2422
2423void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2424 // We assume the class is not null.
2425 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2426 check->GetLoadClass(), check, check->GetDexPc(), true);
2427 codegen_->AddSlowPath(slow_path);
2428 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2429}
2430
Roland Levillain1a653882016-03-18 18:05:57 +00002431static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2432 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2433 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2434}
2435
2436void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2437 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2438 Location rhs_loc = instruction->GetLocations()->InAt(1);
2439 if (rhs_loc.IsConstant()) {
2440 // 0.0 is the only immediate that can be encoded directly in
2441 // an FCMP instruction.
2442 //
2443 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2444 // specify that in a floating-point comparison, positive zero
2445 // and negative zero are considered equal, so we can use the
2446 // literal 0.0 for both cases here.
2447 //
2448 // Note however that some methods (Float.equal, Float.compare,
2449 // Float.compareTo, Double.equal, Double.compare,
2450 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2451 // StrictMath.min) consider 0.0 to be (strictly) greater than
2452 // -0.0. So if we ever translate calls to these methods into a
2453 // HCompare instruction, we must handle the -0.0 case with
2454 // care here.
2455 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2456 __ Fcmp(lhs_reg, 0.0);
2457 } else {
2458 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2459 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002460}
2461
Serban Constantinescu02164b32014-11-13 14:05:07 +00002462void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002463 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002464 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2465 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002466 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002467 case Primitive::kPrimBoolean:
2468 case Primitive::kPrimByte:
2469 case Primitive::kPrimShort:
2470 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002471 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002472 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002473 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002474 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2476 break;
2477 }
2478 case Primitive::kPrimFloat:
2479 case Primitive::kPrimDouble: {
2480 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002481 locations->SetInAt(1,
2482 IsFloatingPointZeroConstant(compare->InputAt(1))
2483 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2484 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002485 locations->SetOut(Location::RequiresRegister());
2486 break;
2487 }
2488 default:
2489 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2490 }
2491}
2492
2493void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2494 Primitive::Type in_type = compare->InputAt(0)->GetType();
2495
2496 // 0 if: left == right
2497 // 1 if: left > right
2498 // -1 if: left < right
2499 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002500 case Primitive::kPrimBoolean:
2501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002504 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002505 case Primitive::kPrimLong: {
2506 Register result = OutputRegister(compare);
2507 Register left = InputRegisterAt(compare, 0);
2508 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002509 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002510 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2511 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002512 break;
2513 }
2514 case Primitive::kPrimFloat:
2515 case Primitive::kPrimDouble: {
2516 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002517 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002518 __ Cset(result, ne);
2519 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002520 break;
2521 }
2522 default:
2523 LOG(FATAL) << "Unimplemented compare type " << in_type;
2524 }
2525}
2526
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002527void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002529
2530 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2531 locations->SetInAt(0, Location::RequiresFpuRegister());
2532 locations->SetInAt(1,
2533 IsFloatingPointZeroConstant(instruction->InputAt(1))
2534 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2535 : Location::RequiresFpuRegister());
2536 } else {
2537 // Integer cases.
2538 locations->SetInAt(0, Location::RequiresRegister());
2539 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2540 }
2541
David Brazdilb3e773e2016-01-26 11:28:37 +00002542 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002544 }
2545}
2546
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002547void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002548 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002549 return;
2550 }
2551
2552 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002553 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002554 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002555
Roland Levillain7f63c522015-07-13 15:54:55 +00002556 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002557 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002558 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002559 } else {
2560 // Integer cases.
2561 Register lhs = InputRegisterAt(instruction, 0);
2562 Operand rhs = InputOperandAt(instruction, 1);
2563 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002564 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002565 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002566}
2567
2568#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2569 M(Equal) \
2570 M(NotEqual) \
2571 M(LessThan) \
2572 M(LessThanOrEqual) \
2573 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002574 M(GreaterThanOrEqual) \
2575 M(Below) \
2576 M(BelowOrEqual) \
2577 M(Above) \
2578 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002579#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002580void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2581void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002582FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002583#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002584#undef FOR_EACH_CONDITION_INSTRUCTION
2585
Zheng Xuc6667102015-05-15 16:08:45 +08002586void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2587 DCHECK(instruction->IsDiv() || instruction->IsRem());
2588
2589 LocationSummary* locations = instruction->GetLocations();
2590 Location second = locations->InAt(1);
2591 DCHECK(second.IsConstant());
2592
2593 Register out = OutputRegister(instruction);
2594 Register dividend = InputRegisterAt(instruction, 0);
2595 int64_t imm = Int64FromConstant(second.GetConstant());
2596 DCHECK(imm == 1 || imm == -1);
2597
2598 if (instruction->IsRem()) {
2599 __ Mov(out, 0);
2600 } else {
2601 if (imm == 1) {
2602 __ Mov(out, dividend);
2603 } else {
2604 __ Neg(out, dividend);
2605 }
2606 }
2607}
2608
2609void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2610 DCHECK(instruction->IsDiv() || instruction->IsRem());
2611
2612 LocationSummary* locations = instruction->GetLocations();
2613 Location second = locations->InAt(1);
2614 DCHECK(second.IsConstant());
2615
2616 Register out = OutputRegister(instruction);
2617 Register dividend = InputRegisterAt(instruction, 0);
2618 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002619 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002620 int ctz_imm = CTZ(abs_imm);
2621
2622 UseScratchRegisterScope temps(GetVIXLAssembler());
2623 Register temp = temps.AcquireSameSizeAs(out);
2624
2625 if (instruction->IsDiv()) {
2626 __ Add(temp, dividend, abs_imm - 1);
2627 __ Cmp(dividend, 0);
2628 __ Csel(out, temp, dividend, lt);
2629 if (imm > 0) {
2630 __ Asr(out, out, ctz_imm);
2631 } else {
2632 __ Neg(out, Operand(out, ASR, ctz_imm));
2633 }
2634 } else {
2635 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2636 __ Asr(temp, dividend, bits - 1);
2637 __ Lsr(temp, temp, bits - ctz_imm);
2638 __ Add(out, dividend, temp);
2639 __ And(out, out, abs_imm - 1);
2640 __ Sub(out, out, temp);
2641 }
2642}
2643
2644void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2645 DCHECK(instruction->IsDiv() || instruction->IsRem());
2646
2647 LocationSummary* locations = instruction->GetLocations();
2648 Location second = locations->InAt(1);
2649 DCHECK(second.IsConstant());
2650
2651 Register out = OutputRegister(instruction);
2652 Register dividend = InputRegisterAt(instruction, 0);
2653 int64_t imm = Int64FromConstant(second.GetConstant());
2654
2655 Primitive::Type type = instruction->GetResultType();
2656 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2657
2658 int64_t magic;
2659 int shift;
2660 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2661
2662 UseScratchRegisterScope temps(GetVIXLAssembler());
2663 Register temp = temps.AcquireSameSizeAs(out);
2664
2665 // temp = get_high(dividend * magic)
2666 __ Mov(temp, magic);
2667 if (type == Primitive::kPrimLong) {
2668 __ Smulh(temp, dividend, temp);
2669 } else {
2670 __ Smull(temp.X(), dividend, temp);
2671 __ Lsr(temp.X(), temp.X(), 32);
2672 }
2673
2674 if (imm > 0 && magic < 0) {
2675 __ Add(temp, temp, dividend);
2676 } else if (imm < 0 && magic > 0) {
2677 __ Sub(temp, temp, dividend);
2678 }
2679
2680 if (shift != 0) {
2681 __ Asr(temp, temp, shift);
2682 }
2683
2684 if (instruction->IsDiv()) {
2685 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2686 } else {
2687 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2688 // TODO: Strength reduction for msub.
2689 Register temp_imm = temps.AcquireSameSizeAs(out);
2690 __ Mov(temp_imm, imm);
2691 __ Msub(out, temp, temp_imm, dividend);
2692 }
2693}
2694
2695void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2696 DCHECK(instruction->IsDiv() || instruction->IsRem());
2697 Primitive::Type type = instruction->GetResultType();
2698 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2699
2700 LocationSummary* locations = instruction->GetLocations();
2701 Register out = OutputRegister(instruction);
2702 Location second = locations->InAt(1);
2703
2704 if (second.IsConstant()) {
2705 int64_t imm = Int64FromConstant(second.GetConstant());
2706
2707 if (imm == 0) {
2708 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2709 } else if (imm == 1 || imm == -1) {
2710 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002711 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002712 DivRemByPowerOfTwo(instruction);
2713 } else {
2714 DCHECK(imm <= -2 || imm >= 2);
2715 GenerateDivRemWithAnyConstant(instruction);
2716 }
2717 } else {
2718 Register dividend = InputRegisterAt(instruction, 0);
2719 Register divisor = InputRegisterAt(instruction, 1);
2720 if (instruction->IsDiv()) {
2721 __ Sdiv(out, dividend, divisor);
2722 } else {
2723 UseScratchRegisterScope temps(GetVIXLAssembler());
2724 Register temp = temps.AcquireSameSizeAs(out);
2725 __ Sdiv(temp, dividend, divisor);
2726 __ Msub(out, temp, divisor, dividend);
2727 }
2728 }
2729}
2730
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002731void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2732 LocationSummary* locations =
2733 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2734 switch (div->GetResultType()) {
2735 case Primitive::kPrimInt:
2736 case Primitive::kPrimLong:
2737 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002738 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2740 break;
2741
2742 case Primitive::kPrimFloat:
2743 case Primitive::kPrimDouble:
2744 locations->SetInAt(0, Location::RequiresFpuRegister());
2745 locations->SetInAt(1, Location::RequiresFpuRegister());
2746 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2747 break;
2748
2749 default:
2750 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2751 }
2752}
2753
2754void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2755 Primitive::Type type = div->GetResultType();
2756 switch (type) {
2757 case Primitive::kPrimInt:
2758 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002759 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002760 break;
2761
2762 case Primitive::kPrimFloat:
2763 case Primitive::kPrimDouble:
2764 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2765 break;
2766
2767 default:
2768 LOG(FATAL) << "Unexpected div type " << type;
2769 }
2770}
2771
Alexandre Rames67555f72014-11-18 10:55:16 +00002772void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002773 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002774 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002775}
2776
2777void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2778 SlowPathCodeARM64* slow_path =
2779 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2780 codegen_->AddSlowPath(slow_path);
2781 Location value = instruction->GetLocations()->InAt(0);
2782
Alexandre Rames3e69f162014-12-10 10:36:50 +00002783 Primitive::Type type = instruction->GetType();
2784
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002785 if (!Primitive::IsIntegralType(type)) {
2786 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002787 return;
2788 }
2789
Alexandre Rames67555f72014-11-18 10:55:16 +00002790 if (value.IsConstant()) {
2791 int64_t divisor = Int64ConstantFrom(value);
2792 if (divisor == 0) {
2793 __ B(slow_path->GetEntryLabel());
2794 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002795 // A division by a non-null constant is valid. We don't need to perform
2796 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002797 }
2798 } else {
2799 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2800 }
2801}
2802
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002803void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2804 LocationSummary* locations =
2805 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2806 locations->SetOut(Location::ConstantLocation(constant));
2807}
2808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002809void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2810 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002811 // Will be generated at use site.
2812}
2813
Alexandre Rames5319def2014-10-23 10:03:10 +01002814void LocationsBuilderARM64::VisitExit(HExit* exit) {
2815 exit->SetLocations(nullptr);
2816}
2817
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002818void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002819}
2820
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002821void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2822 LocationSummary* locations =
2823 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2824 locations->SetOut(Location::ConstantLocation(constant));
2825}
2826
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002827void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002828 // Will be generated at use site.
2829}
2830
David Brazdilfc6a86a2015-06-26 10:33:45 +00002831void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002832 DCHECK(!successor->IsExitBlock());
2833 HBasicBlock* block = got->GetBlock();
2834 HInstruction* previous = got->GetPrevious();
2835 HLoopInformation* info = block->GetLoopInformation();
2836
David Brazdil46e2a392015-03-16 17:31:52 +00002837 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002838 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2839 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2840 return;
2841 }
2842 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2843 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2844 }
2845 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002846 __ B(codegen_->GetLabelOf(successor));
2847 }
2848}
2849
David Brazdilfc6a86a2015-06-26 10:33:45 +00002850void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2851 got->SetLocations(nullptr);
2852}
2853
2854void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2855 HandleGoto(got, got->GetSuccessor());
2856}
2857
2858void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2859 try_boundary->SetLocations(nullptr);
2860}
2861
2862void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2863 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2864 if (!successor->IsExitBlock()) {
2865 HandleGoto(try_boundary, successor);
2866 }
2867}
2868
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002869void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002870 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002871 vixl::aarch64::Label* true_target,
2872 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002873 // FP branching requires both targets to be explicit. If either of the targets
2874 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002875 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002876 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002877
David Brazdil0debae72015-11-12 18:37:00 +00002878 if (true_target == nullptr && false_target == nullptr) {
2879 // Nothing to do. The code always falls through.
2880 return;
2881 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002882 // Constant condition, statically compared against "true" (integer value 1).
2883 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002884 if (true_target != nullptr) {
2885 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002886 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002887 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002888 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002889 if (false_target != nullptr) {
2890 __ B(false_target);
2891 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002892 }
David Brazdil0debae72015-11-12 18:37:00 +00002893 return;
2894 }
2895
2896 // The following code generates these patterns:
2897 // (1) true_target == nullptr && false_target != nullptr
2898 // - opposite condition true => branch to false_target
2899 // (2) true_target != nullptr && false_target == nullptr
2900 // - condition true => branch to true_target
2901 // (3) true_target != nullptr && false_target != nullptr
2902 // - condition true => branch to true_target
2903 // - branch to false_target
2904 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002905 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002906 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002907 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002908 if (true_target == nullptr) {
2909 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2910 } else {
2911 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2912 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002913 } else {
2914 // The condition instruction has not been materialized, use its inputs as
2915 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002916 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002917
David Brazdil0debae72015-11-12 18:37:00 +00002918 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002919 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002920 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002921 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002922 IfCondition opposite_condition = condition->GetOppositeCondition();
2923 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002924 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002925 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002926 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002927 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002928 // Integer cases.
2929 Register lhs = InputRegisterAt(condition, 0);
2930 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002931
2932 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002933 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002934 if (true_target == nullptr) {
2935 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2936 non_fallthrough_target = false_target;
2937 } else {
2938 arm64_cond = ARM64Condition(condition->GetCondition());
2939 non_fallthrough_target = true_target;
2940 }
2941
Aart Bik086d27e2016-01-20 17:02:00 -08002942 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002943 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002944 switch (arm64_cond) {
2945 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002946 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002947 break;
2948 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002949 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002950 break;
2951 case lt:
2952 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002953 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002954 break;
2955 case ge:
2956 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002957 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002958 break;
2959 default:
2960 // Without the `static_cast` the compiler throws an error for
2961 // `-Werror=sign-promo`.
2962 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2963 }
2964 } else {
2965 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002966 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002967 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002968 }
2969 }
David Brazdil0debae72015-11-12 18:37:00 +00002970
2971 // If neither branch falls through (case 3), the conditional branch to `true_target`
2972 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2973 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002974 __ B(false_target);
2975 }
David Brazdil0debae72015-11-12 18:37:00 +00002976
2977 if (fallthrough_target.IsLinked()) {
2978 __ Bind(&fallthrough_target);
2979 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002980}
2981
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002982void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002984 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002985 locations->SetInAt(0, Location::RequiresRegister());
2986 }
2987}
2988
2989void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002990 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2991 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002992 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2993 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2994 true_target = nullptr;
2995 }
2996 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2997 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2998 false_target = nullptr;
2999 }
David Brazdil0debae72015-11-12 18:37:00 +00003000 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003001}
3002
3003void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3004 LocationSummary* locations = new (GetGraph()->GetArena())
3005 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003006 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003007 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003008 locations->SetInAt(0, Location::RequiresRegister());
3009 }
3010}
3011
3012void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003013 SlowPathCodeARM64* slow_path =
3014 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003015 GenerateTestAndBranch(deoptimize,
3016 /* condition_input_index */ 0,
3017 slow_path->GetEntryLabel(),
3018 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003019}
3020
David Brazdilc0b601b2016-02-08 14:20:45 +00003021static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3022 return condition->IsCondition() &&
3023 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3024}
3025
Alexandre Rames880f1192016-06-13 16:04:50 +01003026static inline Condition GetConditionForSelect(HCondition* condition) {
3027 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003028 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3029 : ARM64Condition(cond);
3030}
3031
David Brazdil74eb1b22015-12-14 11:44:01 +00003032void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3033 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003034 if (Primitive::IsFloatingPointType(select->GetType())) {
3035 locations->SetInAt(0, Location::RequiresFpuRegister());
3036 locations->SetInAt(1, Location::RequiresFpuRegister());
3037 locations->SetOut(Location::RequiresFpuRegister());
3038 } else {
3039 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3040 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3041 bool is_true_value_constant = cst_true_value != nullptr;
3042 bool is_false_value_constant = cst_false_value != nullptr;
3043 // Ask VIXL whether we should synthesize constants in registers.
3044 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3045 Operand true_op = is_true_value_constant ?
3046 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3047 Operand false_op = is_false_value_constant ?
3048 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3049 bool true_value_in_register = false;
3050 bool false_value_in_register = false;
3051 MacroAssembler::GetCselSynthesisInformation(
3052 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3053 true_value_in_register |= !is_true_value_constant;
3054 false_value_in_register |= !is_false_value_constant;
3055
3056 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3057 : Location::ConstantLocation(cst_true_value));
3058 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3059 : Location::ConstantLocation(cst_false_value));
3060 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003061 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003062
David Brazdil74eb1b22015-12-14 11:44:01 +00003063 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3064 locations->SetInAt(2, Location::RequiresRegister());
3065 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003066}
3067
3068void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003069 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003070 Condition csel_cond;
3071
3072 if (IsBooleanValueOrMaterializedCondition(cond)) {
3073 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003074 // Use the condition flags set by the previous instruction.
3075 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003076 } else {
3077 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003078 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003079 }
3080 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003081 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003082 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003083 } else {
3084 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003085 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003086 }
3087
Alexandre Rames880f1192016-06-13 16:04:50 +01003088 if (Primitive::IsFloatingPointType(select->GetType())) {
3089 __ Fcsel(OutputFPRegister(select),
3090 InputFPRegisterAt(select, 1),
3091 InputFPRegisterAt(select, 0),
3092 csel_cond);
3093 } else {
3094 __ Csel(OutputRegister(select),
3095 InputOperandAt(select, 1),
3096 InputOperandAt(select, 0),
3097 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003098 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003099}
3100
David Srbecky0cf44932015-12-09 14:09:59 +00003101void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3102 new (GetGraph()->GetArena()) LocationSummary(info);
3103}
3104
David Srbeckyd28f4a02016-03-14 17:14:24 +00003105void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3106 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003107}
3108
3109void CodeGeneratorARM64::GenerateNop() {
3110 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003111}
3112
Alexandre Rames5319def2014-10-23 10:03:10 +01003113void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003114 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003115}
3116
3117void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003118 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003119}
3120
3121void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003122 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003123}
3124
3125void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003126 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003127}
3128
Roland Levillain44015862016-01-22 11:47:17 +00003129static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3130 return kEmitCompilerReadBarrier &&
3131 (kUseBakerReadBarrier ||
3132 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3133 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3134 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3135}
3136
Alexandre Rames67555f72014-11-18 10:55:16 +00003137void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003138 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003139 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003140 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003141 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003142 case TypeCheckKind::kExactCheck:
3143 case TypeCheckKind::kAbstractClassCheck:
3144 case TypeCheckKind::kClassHierarchyCheck:
3145 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003146 call_kind =
3147 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003148 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003149 break;
3150 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003151 case TypeCheckKind::kUnresolvedCheck:
3152 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003153 call_kind = LocationSummary::kCallOnSlowPath;
3154 break;
3155 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003156
Alexandre Rames67555f72014-11-18 10:55:16 +00003157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003158 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003159 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003160 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003161 locations->SetInAt(0, Location::RequiresRegister());
3162 locations->SetInAt(1, Location::RequiresRegister());
3163 // The "out" register is used as a temporary, so it overlaps with the inputs.
3164 // Note that TypeCheckSlowPathARM64 uses this register too.
3165 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3166 // When read barriers are enabled, we need a temporary register for
3167 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003168 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003169 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003170 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003171}
3172
3173void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003174 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003175 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003176 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003177 Register obj = InputRegisterAt(instruction, 0);
3178 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003179 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003180 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003181 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3182 locations->GetTemp(0) :
3183 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3185 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3186 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3187 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003188
Scott Wakeling97c72b72016-06-24 16:19:36 +01003189 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003190 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003191
3192 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003193 // Avoid null check if we know `obj` is not null.
3194 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003195 __ Cbz(obj, &zero);
3196 }
3197
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003198 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003199 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200
Roland Levillain44015862016-01-22 11:47:17 +00003201 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003202 case TypeCheckKind::kExactCheck: {
3203 __ Cmp(out, cls);
3204 __ Cset(out, eq);
3205 if (zero.IsLinked()) {
3206 __ B(&done);
3207 }
3208 break;
3209 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003211 case TypeCheckKind::kAbstractClassCheck: {
3212 // If the class is abstract, we eagerly fetch the super class of the
3213 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003214 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003215 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003216 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003217 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003218 // If `out` is null, we use it for the result, and jump to `done`.
3219 __ Cbz(out, &done);
3220 __ Cmp(out, cls);
3221 __ B(ne, &loop);
3222 __ Mov(out, 1);
3223 if (zero.IsLinked()) {
3224 __ B(&done);
3225 }
3226 break;
3227 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003229 case TypeCheckKind::kClassHierarchyCheck: {
3230 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003231 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003232 __ Bind(&loop);
3233 __ Cmp(out, cls);
3234 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003235 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003236 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003237 __ Cbnz(out, &loop);
3238 // If `out` is null, we use it for the result, and jump to `done`.
3239 __ B(&done);
3240 __ Bind(&success);
3241 __ Mov(out, 1);
3242 if (zero.IsLinked()) {
3243 __ B(&done);
3244 }
3245 break;
3246 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003248 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003249 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003250 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003251 __ Cmp(out, cls);
3252 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003253 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003254 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003255 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003256 // If `out` is null, we use it for the result, and jump to `done`.
3257 __ Cbz(out, &done);
3258 __ Ldrh(out, HeapOperand(out, primitive_offset));
3259 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3260 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003261 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003262 __ Mov(out, 1);
3263 __ B(&done);
3264 break;
3265 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003266
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003267 case TypeCheckKind::kArrayCheck: {
3268 __ Cmp(out, cls);
3269 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003270 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3271 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 codegen_->AddSlowPath(slow_path);
3273 __ B(ne, slow_path->GetEntryLabel());
3274 __ Mov(out, 1);
3275 if (zero.IsLinked()) {
3276 __ B(&done);
3277 }
3278 break;
3279 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003280
Calin Juravle98893e12015-10-02 21:05:03 +01003281 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003282 case TypeCheckKind::kInterfaceCheck: {
3283 // Note that we indeed only call on slow path, but we always go
3284 // into the slow path for the unresolved and interface check
3285 // cases.
3286 //
3287 // We cannot directly call the InstanceofNonTrivial runtime
3288 // entry point without resorting to a type checking slow path
3289 // here (i.e. by calling InvokeRuntime directly), as it would
3290 // require to assign fixed registers for the inputs of this
3291 // HInstanceOf instruction (following the runtime calling
3292 // convention), which might be cluttered by the potential first
3293 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003294 //
3295 // TODO: Introduce a new runtime entry point taking the object
3296 // to test (instead of its class) as argument, and let it deal
3297 // with the read barrier issues. This will let us refactor this
3298 // case of the `switch` code as it was previously (with a direct
3299 // call to the runtime not using a type checking slow path).
3300 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003301 DCHECK(locations->OnlyCallsOnSlowPath());
3302 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3303 /* is_fatal */ false);
3304 codegen_->AddSlowPath(slow_path);
3305 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306 if (zero.IsLinked()) {
3307 __ B(&done);
3308 }
3309 break;
3310 }
3311 }
3312
3313 if (zero.IsLinked()) {
3314 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003315 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003316 }
3317
3318 if (done.IsLinked()) {
3319 __ Bind(&done);
3320 }
3321
3322 if (slow_path != nullptr) {
3323 __ Bind(slow_path->GetExitLabel());
3324 }
3325}
3326
3327void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3328 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3329 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3330
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003331 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3332 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003333 case TypeCheckKind::kExactCheck:
3334 case TypeCheckKind::kAbstractClassCheck:
3335 case TypeCheckKind::kClassHierarchyCheck:
3336 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3338 LocationSummary::kCallOnSlowPath :
3339 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003340 break;
3341 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003342 case TypeCheckKind::kUnresolvedCheck:
3343 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003344 call_kind = LocationSummary::kCallOnSlowPath;
3345 break;
3346 }
3347
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3349 locations->SetInAt(0, Location::RequiresRegister());
3350 locations->SetInAt(1, Location::RequiresRegister());
3351 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3352 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003353 // When read barriers are enabled, we need an additional temporary
3354 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003355 if (TypeCheckNeedsATemporary(type_check_kind)) {
3356 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003357 }
3358}
3359
3360void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003361 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003363 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003364 Register obj = InputRegisterAt(instruction, 0);
3365 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003366 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003367 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3368 locations->GetTemp(1) :
3369 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003370 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003371 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3372 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3373 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3374 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003375
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003376 bool is_type_check_slow_path_fatal =
3377 (type_check_kind == TypeCheckKind::kExactCheck ||
3378 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3379 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3380 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3381 !instruction->CanThrowIntoCatchBlock();
3382 SlowPathCodeARM64* type_check_slow_path =
3383 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3384 is_type_check_slow_path_fatal);
3385 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003386
Scott Wakeling97c72b72016-06-24 16:19:36 +01003387 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003388 // Avoid null check if we know obj is not null.
3389 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003390 __ Cbz(obj, &done);
3391 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003392
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003393 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003394 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003395
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003397 case TypeCheckKind::kExactCheck:
3398 case TypeCheckKind::kArrayCheck: {
3399 __ Cmp(temp, cls);
3400 // Jump to slow path for throwing the exception or doing a
3401 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003403 break;
3404 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003405
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003406 case TypeCheckKind::kAbstractClassCheck: {
3407 // If the class is abstract, we eagerly fetch the super class of the
3408 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003409 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003410 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003411 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003412 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003413
3414 // If the class reference currently in `temp` is not null, jump
3415 // to the `compare_classes` label to compare it with the checked
3416 // class.
3417 __ Cbnz(temp, &compare_classes);
3418 // Otherwise, jump to the slow path to throw the exception.
3419 //
3420 // But before, move back the object's class into `temp` before
3421 // going into the slow path, as it has been overwritten in the
3422 // meantime.
3423 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003424 GenerateReferenceLoadTwoRegisters(
3425 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426 __ B(type_check_slow_path->GetEntryLabel());
3427
3428 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003429 __ Cmp(temp, cls);
3430 __ B(ne, &loop);
3431 break;
3432 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003433
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003434 case TypeCheckKind::kClassHierarchyCheck: {
3435 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003436 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003437 __ Bind(&loop);
3438 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003439 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003440
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003442 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003443
3444 // If the class reference currently in `temp` is not null, jump
3445 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003447 // Otherwise, jump to the slow path to throw the exception.
3448 //
3449 // But before, move back the object's class into `temp` before
3450 // going into the slow path, as it has been overwritten in the
3451 // meantime.
3452 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003453 GenerateReferenceLoadTwoRegisters(
3454 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003455 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003456 break;
3457 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003458
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003460 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003461 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003462 __ Cmp(temp, cls);
3463 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003464
3465 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003466 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003467 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003468
3469 // If the component type is not null (i.e. the object is indeed
3470 // an array), jump to label `check_non_primitive_component_type`
3471 // to further check that this component type is not a primitive
3472 // type.
3473 __ Cbnz(temp, &check_non_primitive_component_type);
3474 // Otherwise, jump to the slow path to throw the exception.
3475 //
3476 // But before, move back the object's class into `temp` before
3477 // going into the slow path, as it has been overwritten in the
3478 // meantime.
3479 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003480 GenerateReferenceLoadTwoRegisters(
3481 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003482 __ B(type_check_slow_path->GetEntryLabel());
3483
3484 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003485 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3486 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003487 __ Cbz(temp, &done);
3488 // Same comment as above regarding `temp` and the slow path.
3489 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003490 GenerateReferenceLoadTwoRegisters(
3491 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003492 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003493 break;
3494 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003495
Calin Juravle98893e12015-10-02 21:05:03 +01003496 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003497 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003498 // We always go into the type check slow path for the unresolved
3499 // and interface check cases.
3500 //
3501 // We cannot directly call the CheckCast runtime entry point
3502 // without resorting to a type checking slow path here (i.e. by
3503 // calling InvokeRuntime directly), as it would require to
3504 // assign fixed registers for the inputs of this HInstanceOf
3505 // instruction (following the runtime calling convention), which
3506 // might be cluttered by the potential first read barrier
3507 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003508 //
3509 // TODO: Introduce a new runtime entry point taking the object
3510 // to test (instead of its class) as argument, and let it deal
3511 // with the read barrier issues. This will let us refactor this
3512 // case of the `switch` code as it was previously (with a direct
3513 // call to the runtime not using a type checking slow path).
3514 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003515 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003516 break;
3517 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003518 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003519
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003520 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003521}
3522
Alexandre Rames5319def2014-10-23 10:03:10 +01003523void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3525 locations->SetOut(Location::ConstantLocation(constant));
3526}
3527
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003528void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003529 // Will be generated at use site.
3530}
3531
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003532void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3534 locations->SetOut(Location::ConstantLocation(constant));
3535}
3536
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003537void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003538 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003539}
3540
Calin Juravle175dc732015-08-25 15:42:32 +01003541void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3542 // The trampoline uses the same calling convention as dex calling conventions,
3543 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3544 // the method_idx.
3545 HandleInvoke(invoke);
3546}
3547
3548void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3549 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3550}
3551
Alexandre Rames5319def2014-10-23 10:03:10 +01003552void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003553 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003554 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003555}
3556
Alexandre Rames67555f72014-11-18 10:55:16 +00003557void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3558 HandleInvoke(invoke);
3559}
3560
3561void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3562 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003563 LocationSummary* locations = invoke->GetLocations();
3564 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003565 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003566 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003567 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003568
3569 // The register ip1 is required to be used for the hidden argument in
3570 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003571 MacroAssembler* masm = GetVIXLAssembler();
3572 UseScratchRegisterScope scratch_scope(masm);
3573 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003574 scratch_scope.Exclude(ip1);
3575 __ Mov(ip1, invoke->GetDexMethodIndex());
3576
Alexandre Rames67555f72014-11-18 10:55:16 +00003577 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003578 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003579 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003580 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003581 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003582 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003583 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003584 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003585 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003586 // Instead of simply (possibly) unpoisoning `temp` here, we should
3587 // emit a read barrier for the previous class reference load.
3588 // However this is not required in practice, as this is an
3589 // intermediate/temporary reference and because the current
3590 // concurrent copying collector keeps the from-space memory
3591 // intact/accessible until the end of the marking phase (the
3592 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003593 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003594 __ Ldr(temp,
3595 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3596 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003597 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003598 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003599 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003600 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003601 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003602 // lr();
3603 __ Blr(lr);
3604 DCHECK(!codegen_->IsLeafMethod());
3605 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3606}
3607
3608void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003609 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3610 if (intrinsic.TryDispatch(invoke)) {
3611 return;
3612 }
3613
Alexandre Rames67555f72014-11-18 10:55:16 +00003614 HandleInvoke(invoke);
3615}
3616
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003617void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003618 // Explicit clinit checks triggered by static invokes must have been pruned by
3619 // art::PrepareForRegisterAllocation.
3620 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003621
Andreas Gampe878d58c2015-01-15 23:24:00 -08003622 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3623 if (intrinsic.TryDispatch(invoke)) {
3624 return;
3625 }
3626
Alexandre Rames67555f72014-11-18 10:55:16 +00003627 HandleInvoke(invoke);
3628}
3629
Andreas Gampe878d58c2015-01-15 23:24:00 -08003630static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3631 if (invoke->GetLocations()->Intrinsified()) {
3632 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3633 intrinsic.Dispatch(invoke);
3634 return true;
3635 }
3636 return false;
3637}
3638
Vladimir Markodc151b22015-10-15 18:02:30 +01003639HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3640 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003641 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003642 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003643 return desired_dispatch_info;
3644}
3645
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003646void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003647 // For better instruction scheduling we load the direct code pointer before the method pointer.
3648 bool direct_code_loaded = false;
3649 switch (invoke->GetCodePtrLocation()) {
3650 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3651 // LR = code address from literal pool with link-time patch.
3652 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3653 direct_code_loaded = true;
3654 break;
3655 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3656 // LR = invoke->GetDirectCodePtr();
3657 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3658 direct_code_loaded = true;
3659 break;
3660 default:
3661 break;
3662 }
3663
Andreas Gampe878d58c2015-01-15 23:24:00 -08003664 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003665 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3666 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003667 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3668 uint32_t offset =
3669 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003670 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003671 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003672 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003673 }
Vladimir Marko58155012015-08-19 12:49:41 +00003674 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003675 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003676 break;
3677 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3678 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003679 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003680 break;
3681 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3682 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003683 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003684 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3685 break;
3686 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3687 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003688 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003689 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003690 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003691 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003692 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003693 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003694 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003695 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003696 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003697 }
Vladimir Marko58155012015-08-19 12:49:41 +00003698 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003699 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003700 Register reg = XRegisterFrom(temp);
3701 Register method_reg;
3702 if (current_method.IsRegister()) {
3703 method_reg = XRegisterFrom(current_method);
3704 } else {
3705 DCHECK(invoke->GetLocations()->Intrinsified());
3706 DCHECK(!current_method.IsValid());
3707 method_reg = reg;
3708 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3709 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003710
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003711 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003712 __ Ldr(reg.X(),
3713 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003714 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003715 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003716 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3717 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003718 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3719 break;
3720 }
3721 }
3722
3723 switch (invoke->GetCodePtrLocation()) {
3724 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3725 __ Bl(&frame_entry_label_);
3726 break;
3727 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00003728 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3729 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01003730 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3731 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003732 __ Bind(label);
3733 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003734 break;
3735 }
3736 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3737 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3738 // LR prepared above for better instruction scheduling.
3739 DCHECK(direct_code_loaded);
3740 // lr()
3741 __ Blr(lr);
3742 break;
3743 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3744 // LR = callee_method->entry_point_from_quick_compiled_code_;
3745 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003746 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003747 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003748 // lr()
3749 __ Blr(lr);
3750 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003751 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003752
Andreas Gampe878d58c2015-01-15 23:24:00 -08003753 DCHECK(!IsLeafMethod());
3754}
3755
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003756void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003757 // Use the calling convention instead of the location of the receiver, as
3758 // intrinsics may have put the receiver in a different register. In the intrinsics
3759 // slow path, the arguments have been moved to the right place, so here we are
3760 // guaranteed that the receiver is the first register of the calling convention.
3761 InvokeDexCallingConvention calling_convention;
3762 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003763 Register temp = XRegisterFrom(temp_in);
3764 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3765 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3766 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003767 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003768
3769 BlockPoolsScope block_pools(GetVIXLAssembler());
3770
3771 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003772 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003773 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003774 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003775 // Instead of simply (possibly) unpoisoning `temp` here, we should
3776 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003777 // intermediate/temporary reference and because the current
3778 // concurrent copying collector keeps the from-space memory
3779 // intact/accessible until the end of the marking phase (the
3780 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003781 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3782 // temp = temp->GetMethodAt(method_offset);
3783 __ Ldr(temp, MemOperand(temp, method_offset));
3784 // lr = temp->GetEntryPoint();
3785 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3786 // lr();
3787 __ Blr(lr);
3788}
3789
Scott Wakeling97c72b72016-06-24 16:19:36 +01003790vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3791 const DexFile& dex_file,
3792 uint32_t string_index,
3793 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003794 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3795}
3796
Scott Wakeling97c72b72016-06-24 16:19:36 +01003797vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3798 const DexFile& dex_file,
3799 uint32_t type_index,
3800 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003801 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3802}
3803
Scott Wakeling97c72b72016-06-24 16:19:36 +01003804vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3805 const DexFile& dex_file,
3806 uint32_t element_offset,
3807 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003808 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3809}
3810
Scott Wakeling97c72b72016-06-24 16:19:36 +01003811vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3812 const DexFile& dex_file,
3813 uint32_t offset_or_index,
3814 vixl::aarch64::Label* adrp_label,
3815 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003816 // Add a patch entry and return the label.
3817 patches->emplace_back(dex_file, offset_or_index);
3818 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003819 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003820 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3821 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3822 return label;
3823}
3824
Scott Wakeling97c72b72016-06-24 16:19:36 +01003825vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003826 const DexFile& dex_file, uint32_t string_index) {
3827 return boot_image_string_patches_.GetOrCreate(
3828 StringReference(&dex_file, string_index),
3829 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3830}
3831
Scott Wakeling97c72b72016-06-24 16:19:36 +01003832vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003833 const DexFile& dex_file, uint32_t type_index) {
3834 return boot_image_type_patches_.GetOrCreate(
3835 TypeReference(&dex_file, type_index),
3836 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3837}
3838
Scott Wakeling97c72b72016-06-24 16:19:36 +01003839vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3840 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003841 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3842 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3843 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3844}
3845
Scott Wakeling97c72b72016-06-24 16:19:36 +01003846vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3847 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003848 return DeduplicateUint64Literal(address);
3849}
3850
Vladimir Markoaad75c62016-10-03 08:46:48 +00003851void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
3852 vixl::aarch64::Register reg) {
3853 DCHECK(reg.IsX());
3854 SingleEmissionCheckScope guard(GetVIXLAssembler());
3855 __ Bind(fixup_label);
3856 __ adrp(reg, /* offset placeholder */ 0);
3857}
3858
3859void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
3860 vixl::aarch64::Register out,
3861 vixl::aarch64::Register base) {
3862 DCHECK(out.IsX());
3863 DCHECK(base.IsX());
3864 SingleEmissionCheckScope guard(GetVIXLAssembler());
3865 __ Bind(fixup_label);
3866 __ add(out, base, Operand(/* offset placeholder */ 0));
3867}
3868
3869void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
3870 vixl::aarch64::Register out,
3871 vixl::aarch64::Register base) {
3872 DCHECK(base.IsX());
3873 SingleEmissionCheckScope guard(GetVIXLAssembler());
3874 __ Bind(fixup_label);
3875 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
3876}
3877
3878template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
3879inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
3880 const ArenaDeque<PcRelativePatchInfo>& infos,
3881 ArenaVector<LinkerPatch>* linker_patches) {
3882 for (const PcRelativePatchInfo& info : infos) {
3883 linker_patches->push_back(Factory(info.label.GetLocation(),
3884 &info.target_dex_file,
3885 info.pc_insn_label->GetLocation(),
3886 info.offset_or_index));
3887 }
3888}
3889
Vladimir Marko58155012015-08-19 12:49:41 +00003890void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3891 DCHECK(linker_patches->empty());
3892 size_t size =
3893 method_patches_.size() +
3894 call_patches_.size() +
3895 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003896 pc_relative_dex_cache_patches_.size() +
3897 boot_image_string_patches_.size() +
3898 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003899 boot_image_type_patches_.size() +
3900 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003901 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003902 linker_patches->reserve(size);
3903 for (const auto& entry : method_patches_) {
3904 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003905 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3906 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003907 target_method.dex_file,
3908 target_method.dex_method_index));
3909 }
3910 for (const auto& entry : call_patches_) {
3911 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003912 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3913 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003914 target_method.dex_file,
3915 target_method.dex_method_index));
3916 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003917 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3918 linker_patches->push_back(
3919 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00003920 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003921 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003922 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003923 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003924 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003925 info.offset_or_index));
3926 }
3927 for (const auto& entry : boot_image_string_patches_) {
3928 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003929 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3930 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003931 target_string.dex_file,
3932 target_string.string_index));
3933 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003934 if (!GetCompilerOptions().IsBootImage()) {
3935 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
3936 linker_patches);
3937 } else {
3938 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
3939 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003940 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003941 for (const auto& entry : boot_image_type_patches_) {
3942 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003943 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3944 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003945 target_type.dex_file,
3946 target_type.type_index));
3947 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003948 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
3949 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003950 for (const auto& entry : boot_image_address_patches_) {
3951 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003952 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3953 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003954 }
3955}
3956
Scott Wakeling97c72b72016-06-24 16:19:36 +01003957vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003958 Uint32ToLiteralMap* map) {
3959 return map->GetOrCreate(
3960 value,
3961 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3962}
3963
Scott Wakeling97c72b72016-06-24 16:19:36 +01003964vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003965 return uint64_literals_.GetOrCreate(
3966 value,
3967 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003968}
3969
Scott Wakeling97c72b72016-06-24 16:19:36 +01003970vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003971 MethodReference target_method,
3972 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003973 return map->GetOrCreate(
3974 target_method,
3975 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003976}
3977
Scott Wakeling97c72b72016-06-24 16:19:36 +01003978vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003979 MethodReference target_method) {
3980 return DeduplicateMethodLiteral(target_method, &method_patches_);
3981}
3982
Scott Wakeling97c72b72016-06-24 16:19:36 +01003983vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003984 MethodReference target_method) {
3985 return DeduplicateMethodLiteral(target_method, &call_patches_);
3986}
3987
3988
Andreas Gampe878d58c2015-01-15 23:24:00 -08003989void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003990 // Explicit clinit checks triggered by static invokes must have been pruned by
3991 // art::PrepareForRegisterAllocation.
3992 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003993
Andreas Gampe878d58c2015-01-15 23:24:00 -08003994 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3995 return;
3996 }
3997
Alexandre Ramesd921d642015-04-16 15:07:16 +01003998 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003999 LocationSummary* locations = invoke->GetLocations();
4000 codegen_->GenerateStaticOrDirectCall(
4001 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004002 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004003}
4004
4005void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004006 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4007 return;
4008 }
4009
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004010 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004011 DCHECK(!codegen_->IsLeafMethod());
4012 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4013}
4014
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004015HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4016 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004017 switch (desired_class_load_kind) {
4018 case HLoadClass::LoadKind::kReferrersClass:
4019 break;
4020 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4021 DCHECK(!GetCompilerOptions().GetCompilePic());
4022 break;
4023 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4024 DCHECK(GetCompilerOptions().GetCompilePic());
4025 break;
4026 case HLoadClass::LoadKind::kBootImageAddress:
4027 break;
4028 case HLoadClass::LoadKind::kDexCacheAddress:
4029 DCHECK(Runtime::Current()->UseJitCompilation());
4030 break;
4031 case HLoadClass::LoadKind::kDexCachePcRelative:
4032 DCHECK(!Runtime::Current()->UseJitCompilation());
4033 break;
4034 case HLoadClass::LoadKind::kDexCacheViaMethod:
4035 break;
4036 }
4037 return desired_class_load_kind;
4038}
4039
Alexandre Rames67555f72014-11-18 10:55:16 +00004040void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004041 if (cls->NeedsAccessCheck()) {
4042 InvokeRuntimeCallingConvention calling_convention;
4043 CodeGenerator::CreateLoadClassLocationSummary(
4044 cls,
4045 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004046 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004047 /* code_generator_supports_read_barrier */ true);
4048 return;
4049 }
4050
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004051 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4052 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004053 ? LocationSummary::kCallOnSlowPath
4054 : LocationSummary::kNoCall;
4055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004056 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004057 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004058 }
4059
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004060 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4061 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4062 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4063 locations->SetInAt(0, Location::RequiresRegister());
4064 }
4065 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004066}
4067
4068void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004069 if (cls->NeedsAccessCheck()) {
4070 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004071 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004072 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004073 return;
4074 }
4075
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004076 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004077 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004078
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004079 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004080 bool generate_null_check = false;
4081 switch (cls->GetLoadKind()) {
4082 case HLoadClass::LoadKind::kReferrersClass: {
4083 DCHECK(!cls->CanCallRuntime());
4084 DCHECK(!cls->MustGenerateClinitCheck());
4085 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4086 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004087 GenerateGcRootFieldLoad(cls,
4088 out_loc,
4089 current_method,
4090 ArtMethod::DeclaringClassOffset().Int32Value(),
4091 /*fixup_label*/ nullptr,
4092 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004093 break;
4094 }
4095 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004096 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004097 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4098 cls->GetTypeIndex()));
4099 break;
4100 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004101 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004102 // Add ADRP with its PC-relative type patch.
4103 const DexFile& dex_file = cls->GetDexFile();
4104 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004105 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004106 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004107 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004108 vixl::aarch64::Label* add_label =
4109 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004110 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004111 break;
4112 }
4113 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004114 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004115 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4116 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4117 break;
4118 }
4119 case HLoadClass::LoadKind::kDexCacheAddress: {
4120 DCHECK_NE(cls->GetAddress(), 0u);
4121 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4122 // that gives a 16KiB range. To try and reduce the number of literals if we load
4123 // multiple types, simply split the dex cache address to a 16KiB aligned base
4124 // loaded from a literal and the remaining offset embedded in the load.
4125 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4126 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4127 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4128 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4129 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4130 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4131 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004132 GenerateGcRootFieldLoad(cls,
4133 out_loc,
4134 out.X(),
4135 offset,
4136 /*fixup_label*/ nullptr,
4137 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004138 generate_null_check = !cls->IsInDexCache();
4139 break;
4140 }
4141 case HLoadClass::LoadKind::kDexCachePcRelative: {
4142 // Add ADRP with its PC-relative DexCache access patch.
4143 const DexFile& dex_file = cls->GetDexFile();
4144 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004145 vixl::aarch64::Label* adrp_label =
4146 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004147 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004148 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004149 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004150 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4151 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004152 GenerateGcRootFieldLoad(cls,
4153 out_loc,
4154 out.X(),
4155 /* offset placeholder */ 0,
4156 ldr_label,
4157 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004158 generate_null_check = !cls->IsInDexCache();
4159 break;
4160 }
4161 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4162 MemberOffset resolved_types_offset =
4163 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4164 // /* GcRoot<mirror::Class>[] */ out =
4165 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4166 Register current_method = InputRegisterAt(cls, 0);
4167 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4168 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004169 GenerateGcRootFieldLoad(cls,
4170 out_loc,
4171 out.X(),
4172 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4173 /*fixup_label*/ nullptr,
4174 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004175 generate_null_check = !cls->IsInDexCache();
4176 break;
4177 }
4178 }
4179
4180 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4181 DCHECK(cls->CanCallRuntime());
4182 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4183 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4184 codegen_->AddSlowPath(slow_path);
4185 if (generate_null_check) {
4186 __ Cbz(out, slow_path->GetEntryLabel());
4187 }
4188 if (cls->MustGenerateClinitCheck()) {
4189 GenerateClassInitializationCheck(slow_path, out);
4190 } else {
4191 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004192 }
4193 }
4194}
4195
David Brazdilcb1c0552015-08-04 16:22:25 +01004196static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004197 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004198}
4199
Alexandre Rames67555f72014-11-18 10:55:16 +00004200void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4201 LocationSummary* locations =
4202 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4203 locations->SetOut(Location::RequiresRegister());
4204}
4205
4206void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004207 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4208}
4209
4210void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4211 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4212}
4213
4214void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4215 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004216}
4217
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004218HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4219 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004220 switch (desired_string_load_kind) {
4221 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4222 DCHECK(!GetCompilerOptions().GetCompilePic());
4223 break;
4224 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4225 DCHECK(GetCompilerOptions().GetCompilePic());
4226 break;
4227 case HLoadString::LoadKind::kBootImageAddress:
4228 break;
4229 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004230 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004231 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004232 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004233 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004234 break;
4235 case HLoadString::LoadKind::kDexCacheViaMethod:
4236 break;
4237 }
4238 return desired_string_load_kind;
4239}
4240
Alexandre Rames67555f72014-11-18 10:55:16 +00004241void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004242 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00004243 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4244 ? LocationSummary::kCallOnMainOnly
4245 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004246 : LocationSummary::kNoCall;
4247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004248 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4249 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004250 InvokeRuntimeCallingConvention calling_convention;
4251 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4252 } else {
4253 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004254 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004255}
4256
4257void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004258 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004259
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260 switch (load->GetLoadKind()) {
4261 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4263 load->GetStringIndex()));
4264 return; // No dex cache slow path.
4265 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004266 // Add ADRP with its PC-relative String patch.
4267 const DexFile& dex_file = load->GetDexFile();
4268 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004269 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004270 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004271 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004273 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004274 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004275 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004276 return; // No dex cache slow path.
4277 }
4278 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004279 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4280 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4281 return; // No dex cache slow path.
4282 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004283 case HLoadString::LoadKind::kBssEntry: {
4284 // Add ADRP with its PC-relative String .bss entry patch.
4285 const DexFile& dex_file = load->GetDexFile();
4286 uint32_t string_index = load->GetStringIndex();
4287 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4288 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4289 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4290 // Add LDR with its PC-relative String patch.
4291 vixl::aarch64::Label* ldr_label =
4292 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4293 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4294 GenerateGcRootFieldLoad(load,
4295 load->GetLocations()->Out(),
4296 out.X(),
4297 /* placeholder */ 0u,
4298 ldr_label);
4299 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4300 codegen_->AddSlowPath(slow_path);
4301 __ Cbz(out.X(), slow_path->GetEntryLabel());
4302 __ Bind(slow_path->GetExitLabel());
4303 return;
4304 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004305 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004306 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004307 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004308
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004309 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004310 InvokeRuntimeCallingConvention calling_convention;
4311 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4312 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4313 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004314}
4315
Alexandre Rames5319def2014-10-23 10:03:10 +01004316void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4318 locations->SetOut(Location::ConstantLocation(constant));
4319}
4320
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004321void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004322 // Will be generated at use site.
4323}
4324
Alexandre Rames67555f72014-11-18 10:55:16 +00004325void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4326 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004328 InvokeRuntimeCallingConvention calling_convention;
4329 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4330}
4331
4332void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004333 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4334 instruction,
4335 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004336 if (instruction->IsEnter()) {
4337 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4338 } else {
4339 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4340 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004341}
4342
Alexandre Rames42d641b2014-10-27 14:00:51 +00004343void LocationsBuilderARM64::VisitMul(HMul* mul) {
4344 LocationSummary* locations =
4345 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4346 switch (mul->GetResultType()) {
4347 case Primitive::kPrimInt:
4348 case Primitive::kPrimLong:
4349 locations->SetInAt(0, Location::RequiresRegister());
4350 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004352 break;
4353
4354 case Primitive::kPrimFloat:
4355 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004356 locations->SetInAt(0, Location::RequiresFpuRegister());
4357 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004358 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004359 break;
4360
4361 default:
4362 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4363 }
4364}
4365
4366void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4367 switch (mul->GetResultType()) {
4368 case Primitive::kPrimInt:
4369 case Primitive::kPrimLong:
4370 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4371 break;
4372
4373 case Primitive::kPrimFloat:
4374 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004375 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004376 break;
4377
4378 default:
4379 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4380 }
4381}
4382
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004383void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4384 LocationSummary* locations =
4385 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4386 switch (neg->GetResultType()) {
4387 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004388 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004389 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004391 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004392
4393 case Primitive::kPrimFloat:
4394 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004395 locations->SetInAt(0, Location::RequiresFpuRegister());
4396 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004397 break;
4398
4399 default:
4400 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4401 }
4402}
4403
4404void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4405 switch (neg->GetResultType()) {
4406 case Primitive::kPrimInt:
4407 case Primitive::kPrimLong:
4408 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4409 break;
4410
4411 case Primitive::kPrimFloat:
4412 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004413 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004414 break;
4415
4416 default:
4417 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4418 }
4419}
4420
4421void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4422 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004424 InvokeRuntimeCallingConvention calling_convention;
4425 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004426 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004427 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004428 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004429}
4430
4431void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4432 LocationSummary* locations = instruction->GetLocations();
4433 InvokeRuntimeCallingConvention calling_convention;
4434 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4435 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004436 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004437 // Note: if heap poisoning is enabled, the entry point takes cares
4438 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004439 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004440 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004441}
4442
Alexandre Rames5319def2014-10-23 10:03:10 +01004443void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4444 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004446 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004447 if (instruction->IsStringAlloc()) {
4448 locations->AddTemp(LocationFrom(kArtMethodRegister));
4449 } else {
4450 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4451 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4452 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004453 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4454}
4455
4456void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004457 // Note: if heap poisoning is enabled, the entry point takes cares
4458 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004459 if (instruction->IsStringAlloc()) {
4460 // String is allocated through StringFactory. Call NewEmptyString entry point.
4461 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004462 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004463 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4464 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4465 __ Blr(lr);
4466 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4467 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004468 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004469 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4470 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004471}
4472
4473void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004475 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004476 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004477}
4478
4479void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004480 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004481 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004482 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004483 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004484 break;
4485
4486 default:
4487 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4488 }
4489}
4490
David Brazdil66d126e2015-04-03 16:02:44 +01004491void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4493 locations->SetInAt(0, Location::RequiresRegister());
4494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4495}
4496
4497void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004498 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004499}
4500
Alexandre Rames5319def2014-10-23 10:03:10 +01004501void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004502 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4503 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004504}
4505
Calin Juravle2ae48182016-03-16 14:05:09 +00004506void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4507 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004508 return;
4509 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004510
Alexandre Ramesd921d642015-04-16 15:07:16 +01004511 BlockPoolsScope block_pools(GetVIXLAssembler());
4512 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004513 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004514 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004515}
4516
Calin Juravle2ae48182016-03-16 14:05:09 +00004517void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004518 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004519 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004520
4521 LocationSummary* locations = instruction->GetLocations();
4522 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004523
4524 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004525}
4526
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004527void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004528 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004529}
4530
Alexandre Rames67555f72014-11-18 10:55:16 +00004531void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4532 HandleBinaryOp(instruction);
4533}
4534
4535void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4536 HandleBinaryOp(instruction);
4537}
4538
Alexandre Rames3e69f162014-12-10 10:36:50 +00004539void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4540 LOG(FATAL) << "Unreachable";
4541}
4542
4543void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4544 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4545}
4546
Alexandre Rames5319def2014-10-23 10:03:10 +01004547void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4548 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4549 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4550 if (location.IsStackSlot()) {
4551 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4552 } else if (location.IsDoubleStackSlot()) {
4553 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4554 }
4555 locations->SetOut(location);
4556}
4557
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004558void InstructionCodeGeneratorARM64::VisitParameterValue(
4559 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004560 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004561}
4562
4563void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4564 LocationSummary* locations =
4565 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004566 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004567}
4568
4569void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4570 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4571 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004572}
4573
4574void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004576 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004577 locations->SetInAt(i, Location::Any());
4578 }
4579 locations->SetOut(Location::Any());
4580}
4581
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004582void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004583 LOG(FATAL) << "Unreachable";
4584}
4585
Serban Constantinescu02164b32014-11-13 14:05:07 +00004586void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004587 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004588 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004589 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4590 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4592
4593 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004594 case Primitive::kPrimInt:
4595 case Primitive::kPrimLong:
4596 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004597 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4599 break;
4600
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004601 case Primitive::kPrimFloat:
4602 case Primitive::kPrimDouble: {
4603 InvokeRuntimeCallingConvention calling_convention;
4604 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4605 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4606 locations->SetOut(calling_convention.GetReturnLocation(type));
4607
4608 break;
4609 }
4610
Serban Constantinescu02164b32014-11-13 14:05:07 +00004611 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004612 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004613 }
4614}
4615
4616void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4617 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004618
Serban Constantinescu02164b32014-11-13 14:05:07 +00004619 switch (type) {
4620 case Primitive::kPrimInt:
4621 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004622 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004623 break;
4624 }
4625
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004626 case Primitive::kPrimFloat:
4627 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004628 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4629 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004630 if (type == Primitive::kPrimFloat) {
4631 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4632 } else {
4633 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4634 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004635 break;
4636 }
4637
Serban Constantinescu02164b32014-11-13 14:05:07 +00004638 default:
4639 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004640 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004641 }
4642}
4643
Calin Juravle27df7582015-04-17 19:12:31 +01004644void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4645 memory_barrier->SetLocations(nullptr);
4646}
4647
4648void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004649 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004650}
4651
Alexandre Rames5319def2014-10-23 10:03:10 +01004652void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4653 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4654 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004655 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004656}
4657
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004658void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004659 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004660}
4661
4662void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4663 instruction->SetLocations(nullptr);
4664}
4665
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004666void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004667 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004668}
4669
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004670void LocationsBuilderARM64::VisitRor(HRor* ror) {
4671 HandleBinaryOp(ror);
4672}
4673
4674void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4675 HandleBinaryOp(ror);
4676}
4677
Serban Constantinescu02164b32014-11-13 14:05:07 +00004678void LocationsBuilderARM64::VisitShl(HShl* shl) {
4679 HandleShift(shl);
4680}
4681
4682void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4683 HandleShift(shl);
4684}
4685
4686void LocationsBuilderARM64::VisitShr(HShr* shr) {
4687 HandleShift(shr);
4688}
4689
4690void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4691 HandleShift(shr);
4692}
4693
Alexandre Rames5319def2014-10-23 10:03:10 +01004694void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004695 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004696}
4697
4698void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004699 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004700}
4701
Alexandre Rames67555f72014-11-18 10:55:16 +00004702void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004703 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004704}
4705
4706void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004707 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004708}
4709
4710void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004711 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004712}
4713
Alexandre Rames67555f72014-11-18 10:55:16 +00004714void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004715 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004716}
4717
Calin Juravlee460d1d2015-09-29 04:52:17 +01004718void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4719 HUnresolvedInstanceFieldGet* instruction) {
4720 FieldAccessCallingConventionARM64 calling_convention;
4721 codegen_->CreateUnresolvedFieldLocationSummary(
4722 instruction, instruction->GetFieldType(), calling_convention);
4723}
4724
4725void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4726 HUnresolvedInstanceFieldGet* instruction) {
4727 FieldAccessCallingConventionARM64 calling_convention;
4728 codegen_->GenerateUnresolvedFieldAccess(instruction,
4729 instruction->GetFieldType(),
4730 instruction->GetFieldIndex(),
4731 instruction->GetDexPc(),
4732 calling_convention);
4733}
4734
4735void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4736 HUnresolvedInstanceFieldSet* instruction) {
4737 FieldAccessCallingConventionARM64 calling_convention;
4738 codegen_->CreateUnresolvedFieldLocationSummary(
4739 instruction, instruction->GetFieldType(), calling_convention);
4740}
4741
4742void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4743 HUnresolvedInstanceFieldSet* instruction) {
4744 FieldAccessCallingConventionARM64 calling_convention;
4745 codegen_->GenerateUnresolvedFieldAccess(instruction,
4746 instruction->GetFieldType(),
4747 instruction->GetFieldIndex(),
4748 instruction->GetDexPc(),
4749 calling_convention);
4750}
4751
4752void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4753 HUnresolvedStaticFieldGet* instruction) {
4754 FieldAccessCallingConventionARM64 calling_convention;
4755 codegen_->CreateUnresolvedFieldLocationSummary(
4756 instruction, instruction->GetFieldType(), calling_convention);
4757}
4758
4759void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4760 HUnresolvedStaticFieldGet* instruction) {
4761 FieldAccessCallingConventionARM64 calling_convention;
4762 codegen_->GenerateUnresolvedFieldAccess(instruction,
4763 instruction->GetFieldType(),
4764 instruction->GetFieldIndex(),
4765 instruction->GetDexPc(),
4766 calling_convention);
4767}
4768
4769void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4770 HUnresolvedStaticFieldSet* instruction) {
4771 FieldAccessCallingConventionARM64 calling_convention;
4772 codegen_->CreateUnresolvedFieldLocationSummary(
4773 instruction, instruction->GetFieldType(), calling_convention);
4774}
4775
4776void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4777 HUnresolvedStaticFieldSet* instruction) {
4778 FieldAccessCallingConventionARM64 calling_convention;
4779 codegen_->GenerateUnresolvedFieldAccess(instruction,
4780 instruction->GetFieldType(),
4781 instruction->GetFieldIndex(),
4782 instruction->GetDexPc(),
4783 calling_convention);
4784}
4785
Alexandre Rames5319def2014-10-23 10:03:10 +01004786void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004787 LocationSummary* locations =
4788 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004789 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004790}
4791
4792void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004793 HBasicBlock* block = instruction->GetBlock();
4794 if (block->GetLoopInformation() != nullptr) {
4795 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4796 // The back edge will generate the suspend check.
4797 return;
4798 }
4799 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4800 // The goto will generate the suspend check.
4801 return;
4802 }
4803 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004804}
4805
Alexandre Rames67555f72014-11-18 10:55:16 +00004806void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4807 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004809 InvokeRuntimeCallingConvention calling_convention;
4810 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4811}
4812
4813void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004814 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004815 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004816}
4817
4818void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4819 LocationSummary* locations =
4820 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4821 Primitive::Type input_type = conversion->GetInputType();
4822 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004823 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004824 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4825 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4826 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4827 }
4828
Alexandre Rames542361f2015-01-29 16:57:31 +00004829 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004830 locations->SetInAt(0, Location::RequiresFpuRegister());
4831 } else {
4832 locations->SetInAt(0, Location::RequiresRegister());
4833 }
4834
Alexandre Rames542361f2015-01-29 16:57:31 +00004835 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004836 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4837 } else {
4838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4839 }
4840}
4841
4842void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4843 Primitive::Type result_type = conversion->GetResultType();
4844 Primitive::Type input_type = conversion->GetInputType();
4845
4846 DCHECK_NE(input_type, result_type);
4847
Alexandre Rames542361f2015-01-29 16:57:31 +00004848 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004849 int result_size = Primitive::ComponentSize(result_type);
4850 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004851 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004852 Register output = OutputRegister(conversion);
4853 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004854 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004855 // 'int' values are used directly as W registers, discarding the top
4856 // bits, so we don't need to sign-extend and can just perform a move.
4857 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4858 // top 32 bits of the target register. We theoretically could leave those
4859 // bits unchanged, but we would have to make sure that no code uses a
4860 // 32bit input value as a 64bit value assuming that the top 32 bits are
4861 // zero.
4862 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004863 } else if (result_type == Primitive::kPrimChar ||
4864 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4865 __ Ubfx(output,
4866 output.IsX() ? source.X() : source.W(),
4867 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004868 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004869 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004870 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004871 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004872 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004873 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004874 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4875 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004876 } else if (Primitive::IsFloatingPointType(result_type) &&
4877 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004878 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4879 } else {
4880 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4881 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004882 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004883}
Alexandre Rames67555f72014-11-18 10:55:16 +00004884
Serban Constantinescu02164b32014-11-13 14:05:07 +00004885void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4886 HandleShift(ushr);
4887}
4888
4889void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4890 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004891}
4892
4893void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4894 HandleBinaryOp(instruction);
4895}
4896
4897void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4898 HandleBinaryOp(instruction);
4899}
4900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004901void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004902 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004903 LOG(FATAL) << "Unreachable";
4904}
4905
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004906void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004907 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004908 LOG(FATAL) << "Unreachable";
4909}
4910
Mark Mendellfe57faa2015-09-18 09:26:15 -04004911// Simple implementation of packed switch - generate cascaded compare/jumps.
4912void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4913 LocationSummary* locations =
4914 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4915 locations->SetInAt(0, Location::RequiresRegister());
4916}
4917
4918void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4919 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004920 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004921 Register value_reg = InputRegisterAt(switch_instr, 0);
4922 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4923
Zheng Xu3927c8b2015-11-18 17:46:25 +08004924 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004925 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004926 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4927 // make sure we don't emit it if the target may run out of range.
4928 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4929 // ranges and emit the tables only as required.
4930 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004931
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004932 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004933 // Current instruction id is an upper bound of the number of HIRs in the graph.
4934 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4935 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004936 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4937 Register temp = temps.AcquireW();
4938 __ Subs(temp, value_reg, Operand(lower_bound));
4939
Zheng Xu3927c8b2015-11-18 17:46:25 +08004940 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004941 // Jump to successors[0] if value == lower_bound.
4942 __ B(eq, codegen_->GetLabelOf(successors[0]));
4943 int32_t last_index = 0;
4944 for (; num_entries - last_index > 2; last_index += 2) {
4945 __ Subs(temp, temp, Operand(2));
4946 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4947 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4948 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4949 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4950 }
4951 if (num_entries - last_index == 2) {
4952 // The last missing case_value.
4953 __ Cmp(temp, Operand(1));
4954 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004955 }
4956
4957 // And the default for any other value.
4958 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4959 __ B(codegen_->GetLabelOf(default_block));
4960 }
4961 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004962 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004963
4964 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4965
4966 // Below instructions should use at most one blocked register. Since there are two blocked
4967 // registers, we are free to block one.
4968 Register temp_w = temps.AcquireW();
4969 Register index;
4970 // Remove the bias.
4971 if (lower_bound != 0) {
4972 index = temp_w;
4973 __ Sub(index, value_reg, Operand(lower_bound));
4974 } else {
4975 index = value_reg;
4976 }
4977
4978 // Jump to default block if index is out of the range.
4979 __ Cmp(index, Operand(num_entries));
4980 __ B(hs, codegen_->GetLabelOf(default_block));
4981
4982 // In current VIXL implementation, it won't require any blocked registers to encode the
4983 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4984 // register pressure.
4985 Register table_base = temps.AcquireX();
4986 // Load jump offset from the table.
4987 __ Adr(table_base, jump_table->GetTableStartLabel());
4988 Register jump_offset = temp_w;
4989 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4990
4991 // Jump to target block by branching to table_base(pc related) + offset.
4992 Register target_address = table_base;
4993 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4994 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004995 }
4996}
4997
Roland Levillain44015862016-01-22 11:47:17 +00004998void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4999 Location out,
5000 uint32_t offset,
5001 Location maybe_temp) {
5002 Primitive::Type type = Primitive::kPrimNot;
5003 Register out_reg = RegisterFrom(out, type);
5004 if (kEmitCompilerReadBarrier) {
5005 Register temp_reg = RegisterFrom(maybe_temp, type);
5006 if (kUseBakerReadBarrier) {
5007 // Load with fast path based Baker's read barrier.
5008 // /* HeapReference<Object> */ out = *(out + offset)
5009 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5010 out,
5011 out_reg,
5012 offset,
5013 temp_reg,
5014 /* needs_null_check */ false,
5015 /* use_load_acquire */ false);
5016 } else {
5017 // Load with slow path based read barrier.
5018 // Save the value of `out` into `maybe_temp` before overwriting it
5019 // in the following move operation, as we will need it for the
5020 // read barrier below.
5021 __ Mov(temp_reg, out_reg);
5022 // /* HeapReference<Object> */ out = *(out + offset)
5023 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5024 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5025 }
5026 } else {
5027 // Plain load with no read barrier.
5028 // /* HeapReference<Object> */ out = *(out + offset)
5029 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5030 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5031 }
5032}
5033
5034void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5035 Location out,
5036 Location obj,
5037 uint32_t offset,
5038 Location maybe_temp) {
5039 Primitive::Type type = Primitive::kPrimNot;
5040 Register out_reg = RegisterFrom(out, type);
5041 Register obj_reg = RegisterFrom(obj, type);
5042 if (kEmitCompilerReadBarrier) {
5043 if (kUseBakerReadBarrier) {
5044 // Load with fast path based Baker's read barrier.
5045 Register temp_reg = RegisterFrom(maybe_temp, type);
5046 // /* HeapReference<Object> */ out = *(obj + offset)
5047 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5048 out,
5049 obj_reg,
5050 offset,
5051 temp_reg,
5052 /* needs_null_check */ false,
5053 /* use_load_acquire */ false);
5054 } else {
5055 // Load with slow path based read barrier.
5056 // /* HeapReference<Object> */ out = *(obj + offset)
5057 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5058 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5059 }
5060 } else {
5061 // Plain load with no read barrier.
5062 // /* HeapReference<Object> */ out = *(obj + offset)
5063 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5064 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5065 }
5066}
5067
5068void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5069 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005070 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005071 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005072 vixl::aarch64::Label* fixup_label,
5073 bool requires_read_barrier) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005074 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005075 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005076 if (requires_read_barrier) {
5077 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005078 if (kUseBakerReadBarrier) {
5079 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5080 // Baker's read barrier are used:
5081 //
5082 // root = obj.field;
5083 // if (Thread::Current()->GetIsGcMarking()) {
5084 // root = ReadBarrier::Mark(root)
5085 // }
5086
5087 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 if (fixup_label == nullptr) {
5089 __ Ldr(root_reg, MemOperand(obj, offset));
5090 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005091 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 }
Roland Levillain44015862016-01-22 11:47:17 +00005093 static_assert(
5094 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5095 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5096 "have different sizes.");
5097 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5098 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5099 "have different sizes.");
5100
Vladimir Marko953437b2016-08-24 08:30:46 +00005101 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005102 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005103 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005104 codegen_->AddSlowPath(slow_path);
5105
5106 MacroAssembler* masm = GetVIXLAssembler();
5107 UseScratchRegisterScope temps(masm);
5108 Register temp = temps.AcquireW();
5109 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005110 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005111 __ Cbnz(temp, slow_path->GetEntryLabel());
5112 __ Bind(slow_path->GetExitLabel());
5113 } else {
5114 // GC root loaded through a slow path for read barriers other
5115 // than Baker's.
5116 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005117 if (fixup_label == nullptr) {
5118 __ Add(root_reg.X(), obj.X(), offset);
5119 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005120 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005121 }
Roland Levillain44015862016-01-22 11:47:17 +00005122 // /* mirror::Object* */ root = root->Read()
5123 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5124 }
5125 } else {
5126 // Plain GC root load with no read barrier.
5127 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005128 if (fixup_label == nullptr) {
5129 __ Ldr(root_reg, MemOperand(obj, offset));
5130 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005131 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005132 }
Roland Levillain44015862016-01-22 11:47:17 +00005133 // Note that GC roots are not affected by heap poisoning, thus we
5134 // do not have to unpoison `root_reg` here.
5135 }
5136}
5137
5138void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5139 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005140 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005141 uint32_t offset,
5142 Register temp,
5143 bool needs_null_check,
5144 bool use_load_acquire) {
5145 DCHECK(kEmitCompilerReadBarrier);
5146 DCHECK(kUseBakerReadBarrier);
5147
5148 // /* HeapReference<Object> */ ref = *(obj + offset)
5149 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005150 size_t no_scale_factor = 0U;
5151 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5152 ref,
5153 obj,
5154 offset,
5155 no_index,
5156 no_scale_factor,
5157 temp,
5158 needs_null_check,
5159 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005160}
5161
5162void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5163 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005164 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005165 uint32_t data_offset,
5166 Location index,
5167 Register temp,
5168 bool needs_null_check) {
5169 DCHECK(kEmitCompilerReadBarrier);
5170 DCHECK(kUseBakerReadBarrier);
5171
5172 // Array cells are never volatile variables, therefore array loads
5173 // never use Load-Acquire instructions on ARM64.
5174 const bool use_load_acquire = false;
5175
Roland Levillainbfea3352016-06-23 13:48:47 +01005176 static_assert(
5177 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5178 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005179 // /* HeapReference<Object> */ ref =
5180 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005181 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5182 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5183 ref,
5184 obj,
5185 data_offset,
5186 index,
5187 scale_factor,
5188 temp,
5189 needs_null_check,
5190 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005191}
5192
5193void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5194 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005195 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005196 uint32_t offset,
5197 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005198 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005199 Register temp,
5200 bool needs_null_check,
5201 bool use_load_acquire) {
5202 DCHECK(kEmitCompilerReadBarrier);
5203 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005204 // If we are emitting an array load, we should not be using a
5205 // Load Acquire instruction. In other words:
5206 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5207 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005208
5209 MacroAssembler* masm = GetVIXLAssembler();
5210 UseScratchRegisterScope temps(masm);
5211
5212 // In slow path based read barriers, the read barrier call is
5213 // inserted after the original load. However, in fast path based
5214 // Baker's read barriers, we need to perform the load of
5215 // mirror::Object::monitor_ *before* the original reference load.
5216 // This load-load ordering is required by the read barrier.
5217 // The fast path/slow path (for Baker's algorithm) should look like:
5218 //
5219 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5220 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5221 // HeapReference<Object> ref = *src; // Original reference load.
5222 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5223 // if (is_gray) {
5224 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5225 // }
5226 //
5227 // Note: the original implementation in ReadBarrier::Barrier is
5228 // slightly more complex as it performs additional checks that we do
5229 // not do here for performance reasons.
5230
5231 Primitive::Type type = Primitive::kPrimNot;
5232 Register ref_reg = RegisterFrom(ref, type);
5233 DCHECK(obj.IsW());
5234 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5235
5236 // /* int32_t */ monitor = obj->monitor_
5237 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5238 if (needs_null_check) {
5239 MaybeRecordImplicitNullCheck(instruction);
5240 }
5241 // /* LockWord */ lock_word = LockWord(monitor)
5242 static_assert(sizeof(LockWord) == sizeof(int32_t),
5243 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005244
Vladimir Marko877a0332016-07-11 19:30:56 +01005245 // Introduce a dependency on the lock_word including rb_state,
5246 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005247 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005248 // `obj` is unchanged by this operation, but its value now depends
5249 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005250 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005251
5252 // The actual reference load.
5253 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005254 // Load types involving an "index".
5255 if (use_load_acquire) {
5256 // UnsafeGetObjectVolatile intrinsic case.
5257 // Register `index` is not an index in an object array, but an
5258 // offset to an object reference field within object `obj`.
5259 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5260 DCHECK(instruction->GetLocations()->Intrinsified());
5261 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5262 << instruction->AsInvoke()->GetIntrinsic();
5263 DCHECK_EQ(offset, 0U);
5264 DCHECK_EQ(scale_factor, 0U);
5265 DCHECK_EQ(needs_null_check, 0U);
5266 // /* HeapReference<Object> */ ref = *(obj + index)
5267 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5268 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005269 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005270 // ArrayGet and UnsafeGetObject intrinsics cases.
5271 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5272 if (index.IsConstant()) {
5273 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5274 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5275 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005276 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005277 __ Add(temp2, obj, offset);
5278 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5279 temps.Release(temp2);
5280 }
Roland Levillain44015862016-01-22 11:47:17 +00005281 }
Roland Levillain44015862016-01-22 11:47:17 +00005282 } else {
5283 // /* HeapReference<Object> */ ref = *(obj + offset)
5284 MemOperand field = HeapOperand(obj, offset);
5285 if (use_load_acquire) {
5286 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5287 } else {
5288 Load(type, ref_reg, field);
5289 }
5290 }
5291
5292 // Object* ref = ref_addr->AsMirrorPtr()
5293 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5294
Vladimir Marko953437b2016-08-24 08:30:46 +00005295 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005296 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005297 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005298 AddSlowPath(slow_path);
5299
5300 // if (rb_state == ReadBarrier::gray_ptr_)
5301 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005302 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5303 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5304 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5305 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5306 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005307 __ Bind(slow_path->GetExitLabel());
5308}
5309
5310void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5311 Location out,
5312 Location ref,
5313 Location obj,
5314 uint32_t offset,
5315 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005316 DCHECK(kEmitCompilerReadBarrier);
5317
Roland Levillain44015862016-01-22 11:47:17 +00005318 // Insert a slow path based read barrier *after* the reference load.
5319 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005320 // If heap poisoning is enabled, the unpoisoning of the loaded
5321 // reference will be carried out by the runtime within the slow
5322 // path.
5323 //
5324 // Note that `ref` currently does not get unpoisoned (when heap
5325 // poisoning is enabled), which is alright as the `ref` argument is
5326 // not used by the artReadBarrierSlow entry point.
5327 //
5328 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5329 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5330 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5331 AddSlowPath(slow_path);
5332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005333 __ B(slow_path->GetEntryLabel());
5334 __ Bind(slow_path->GetExitLabel());
5335}
5336
Roland Levillain44015862016-01-22 11:47:17 +00005337void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5338 Location out,
5339 Location ref,
5340 Location obj,
5341 uint32_t offset,
5342 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005343 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005344 // Baker's read barriers shall be handled by the fast path
5345 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5346 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005347 // If heap poisoning is enabled, unpoisoning will be taken care of
5348 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005349 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005350 } else if (kPoisonHeapReferences) {
5351 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5352 }
5353}
5354
Roland Levillain44015862016-01-22 11:47:17 +00005355void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5356 Location out,
5357 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005358 DCHECK(kEmitCompilerReadBarrier);
5359
Roland Levillain44015862016-01-22 11:47:17 +00005360 // Insert a slow path based read barrier *after* the GC root load.
5361 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005362 // Note that GC roots are not affected by heap poisoning, so we do
5363 // not need to do anything special for this here.
5364 SlowPathCodeARM64* slow_path =
5365 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5366 AddSlowPath(slow_path);
5367
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005368 __ B(slow_path->GetEntryLabel());
5369 __ Bind(slow_path->GetExitLabel());
5370}
5371
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005372void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5373 LocationSummary* locations =
5374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5375 locations->SetInAt(0, Location::RequiresRegister());
5376 locations->SetOut(Location::RequiresRegister());
5377}
5378
5379void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5380 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005381 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005382 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005383 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005384 __ Ldr(XRegisterFrom(locations->Out()),
5385 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005386 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005387 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005388 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005389 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5390 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005391 __ Ldr(XRegisterFrom(locations->Out()),
5392 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005393 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005394}
5395
5396
5397
Alexandre Rames67555f72014-11-18 10:55:16 +00005398#undef __
5399#undef QUICK_ENTRY_POINT
5400
Alexandre Rames5319def2014-10-23 10:03:10 +01005401} // namespace arm64
5402} // namespace art