blob: b1e4f5c763bf3a409a7ab706cad71225a554ad43 [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));
1055 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001057 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1058 frame_size - GetCoreSpillSize());
1059 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1060 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001061 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001062}
1063
1064void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001065 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001066 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001067 if (!HasEmptyFrame()) {
1068 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001069 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1070 frame_size - FrameEntrySpillSize());
1071 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1072 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001073 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001074 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001075 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001076 __ Ret();
1077 GetAssembler()->cfi().RestoreState();
1078 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001079}
1080
Scott Wakeling97c72b72016-06-24 16:19:36 +01001081CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001082 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001083 return CPURegList(CPURegister::kRegister, kXRegSize,
1084 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001085}
1086
Scott Wakeling97c72b72016-06-24 16:19:36 +01001087CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001088 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1089 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001090 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1091 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001092}
1093
Alexandre Rames5319def2014-10-23 10:03:10 +01001094void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1095 __ Bind(GetLabelOf(block));
1096}
1097
Calin Juravle175dc732015-08-25 15:42:32 +01001098void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1099 DCHECK(location.IsRegister());
1100 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1101}
1102
Calin Juravlee460d1d2015-09-29 04:52:17 +01001103void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1104 if (location.IsRegister()) {
1105 locations->AddTemp(location);
1106 } else {
1107 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1108 }
1109}
1110
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001111void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001112 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001113 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001114 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001115 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001116 if (value_can_be_null) {
1117 __ Cbz(value, &done);
1118 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001119 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001120 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001121 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001122 if (value_can_be_null) {
1123 __ Bind(&done);
1124 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001125}
1126
David Brazdil58282f42016-01-14 12:45:10 +00001127void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001128 // Blocked core registers:
1129 // lr : Runtime reserved.
1130 // tr : Runtime reserved.
1131 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1132 // ip1 : VIXL core temp.
1133 // ip0 : VIXL core temp.
1134 //
1135 // Blocked fp registers:
1136 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001137 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1138 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001140 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001142
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001143 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001144 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001145 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001146 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001147
David Brazdil58282f42016-01-14 12:45:10 +00001148 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001149 // Stubs do not save callee-save floating point registers. If the graph
1150 // is debuggable, we need to deal with these registers differently. For
1151 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001152 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1153 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001154 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001155 }
1156 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001157}
1158
Alexandre Rames3e69f162014-12-10 10:36:50 +00001159size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1160 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1161 __ Str(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
1165size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1166 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1167 __ Ldr(reg, MemOperand(sp, stack_index));
1168 return kArm64WordSize;
1169}
1170
1171size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1172 FPRegister reg = FPRegister(reg_id, kDRegSize);
1173 __ Str(reg, MemOperand(sp, stack_index));
1174 return kArm64WordSize;
1175}
1176
1177size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1178 FPRegister reg = FPRegister(reg_id, kDRegSize);
1179 __ Ldr(reg, MemOperand(sp, stack_index));
1180 return kArm64WordSize;
1181}
1182
Alexandre Rames5319def2014-10-23 10:03:10 +01001183void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001184 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001185}
1186
1187void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001188 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001189}
1190
Alexandre Rames67555f72014-11-18 10:55:16 +00001191void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001192 if (constant->IsIntConstant()) {
1193 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1194 } else if (constant->IsLongConstant()) {
1195 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1196 } else if (constant->IsNullConstant()) {
1197 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001198 } else if (constant->IsFloatConstant()) {
1199 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1200 } else {
1201 DCHECK(constant->IsDoubleConstant());
1202 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1203 }
1204}
1205
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206
1207static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1208 DCHECK(constant.IsConstant());
1209 HConstant* cst = constant.GetConstant();
1210 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001211 // Null is mapped to a core W register, which we associate with kPrimInt.
1212 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1214 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1215 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1216}
1217
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218void CodeGeneratorARM64::MoveLocation(Location destination,
1219 Location source,
1220 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001221 if (source.Equals(destination)) {
1222 return;
1223 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224
1225 // A valid move can always be inferred from the destination and source
1226 // locations. When moving from and to a register, the argument type can be
1227 // used to generate 32bit instead of 64bit moves. In debug mode we also
1228 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230
1231 if (destination.IsRegister() || destination.IsFpuRegister()) {
1232 if (unspecified_type) {
1233 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1234 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001235 (src_cst != nullptr && (src_cst->IsIntConstant()
1236 || src_cst->IsFloatConstant()
1237 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001240 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001241 // If the source is a double stack slot or a 64bit constant, a 64bit
1242 // type is appropriate. Else the source is a register, and since the
1243 // type has not been specified, we chose a 64bit type to force a 64bit
1244 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001246 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1249 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1250 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001251 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1252 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1253 __ Ldr(dst, StackOperandFrom(source));
1254 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001256 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001260 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001261 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1263 ? Primitive::kPrimLong
1264 : Primitive::kPrimInt;
1265 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1266 }
1267 } else {
1268 DCHECK(source.IsFpuRegister());
1269 if (destination.IsRegister()) {
1270 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1271 ? Primitive::kPrimDouble
1272 : Primitive::kPrimFloat;
1273 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1274 } else {
1275 DCHECK(destination.IsFpuRegister());
1276 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 }
1278 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001279 } else { // The destination is not a register. It must be a stack slot.
1280 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1281 if (source.IsRegister() || source.IsFpuRegister()) {
1282 if (unspecified_type) {
1283 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001284 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001285 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001287 }
1288 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1290 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1291 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001292 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1294 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001295 UseScratchRegisterScope temps(GetVIXLAssembler());
1296 HConstant* src_cst = source.GetConstant();
1297 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001298 if (src_cst->IsZeroBitPattern()) {
1299 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001301 if (src_cst->IsIntConstant()) {
1302 temp = temps.AcquireW();
1303 } else if (src_cst->IsLongConstant()) {
1304 temp = temps.AcquireX();
1305 } else if (src_cst->IsFloatConstant()) {
1306 temp = temps.AcquireS();
1307 } else {
1308 DCHECK(src_cst->IsDoubleConstant());
1309 temp = temps.AcquireD();
1310 }
1311 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001312 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001315 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001316 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001317 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001318 // There is generally less pressure on FP registers.
1319 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 __ Ldr(temp, StackOperandFrom(source));
1321 __ Str(temp, StackOperandFrom(destination));
1322 }
1323 }
1324}
1325
1326void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001327 CPURegister dst,
1328 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001329 switch (type) {
1330 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001331 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332 break;
1333 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001334 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001335 break;
1336 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001337 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001338 break;
1339 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001340 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001341 break;
1342 case Primitive::kPrimInt:
1343 case Primitive::kPrimNot:
1344 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001347 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001348 __ Ldr(dst, src);
1349 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001350 case Primitive::kPrimVoid:
1351 LOG(FATAL) << "Unreachable type " << type;
1352 }
1353}
1354
Calin Juravle77520bc2015-01-12 18:45:46 +00001355void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001356 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001357 const MemOperand& src,
1358 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001359 MacroAssembler* masm = GetVIXLAssembler();
1360 BlockPoolsScope block_pools(masm);
1361 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001362 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001363 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001364
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001365 DCHECK(!src.IsPreIndex());
1366 DCHECK(!src.IsPostIndex());
1367
1368 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001369 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001370 MemOperand base = MemOperand(temp_base);
1371 switch (type) {
1372 case Primitive::kPrimBoolean:
1373 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001374 if (needs_null_check) {
1375 MaybeRecordImplicitNullCheck(instruction);
1376 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001377 break;
1378 case Primitive::kPrimByte:
1379 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001380 if (needs_null_check) {
1381 MaybeRecordImplicitNullCheck(instruction);
1382 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001383 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1384 break;
1385 case Primitive::kPrimChar:
1386 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001387 if (needs_null_check) {
1388 MaybeRecordImplicitNullCheck(instruction);
1389 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001390 break;
1391 case Primitive::kPrimShort:
1392 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001393 if (needs_null_check) {
1394 MaybeRecordImplicitNullCheck(instruction);
1395 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1397 break;
1398 case Primitive::kPrimInt:
1399 case Primitive::kPrimNot:
1400 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001401 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001403 if (needs_null_check) {
1404 MaybeRecordImplicitNullCheck(instruction);
1405 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001406 break;
1407 case Primitive::kPrimFloat:
1408 case Primitive::kPrimDouble: {
1409 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001410 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411
1412 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1413 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001414 if (needs_null_check) {
1415 MaybeRecordImplicitNullCheck(instruction);
1416 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001417 __ Fmov(FPRegister(dst), temp);
1418 break;
1419 }
1420 case Primitive::kPrimVoid:
1421 LOG(FATAL) << "Unreachable type " << type;
1422 }
1423}
1424
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001426 CPURegister src,
1427 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001428 switch (type) {
1429 case Primitive::kPrimBoolean:
1430 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001431 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432 break;
1433 case Primitive::kPrimChar:
1434 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001435 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001436 break;
1437 case Primitive::kPrimInt:
1438 case Primitive::kPrimNot:
1439 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001440 case Primitive::kPrimFloat:
1441 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001442 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001443 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001444 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001445 case Primitive::kPrimVoid:
1446 LOG(FATAL) << "Unreachable type " << type;
1447 }
1448}
1449
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001450void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1451 CPURegister src,
1452 const MemOperand& dst) {
1453 UseScratchRegisterScope temps(GetVIXLAssembler());
1454 Register temp_base = temps.AcquireX();
1455
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001456 DCHECK(!dst.IsPreIndex());
1457 DCHECK(!dst.IsPostIndex());
1458
1459 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001460 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001461 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001462 MemOperand base = MemOperand(temp_base);
1463 switch (type) {
1464 case Primitive::kPrimBoolean:
1465 case Primitive::kPrimByte:
1466 __ Stlrb(Register(src), base);
1467 break;
1468 case Primitive::kPrimChar:
1469 case Primitive::kPrimShort:
1470 __ Stlrh(Register(src), base);
1471 break;
1472 case Primitive::kPrimInt:
1473 case Primitive::kPrimNot:
1474 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001475 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001476 __ Stlr(Register(src), base);
1477 break;
1478 case Primitive::kPrimFloat:
1479 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001480 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001481 Register temp_src;
1482 if (src.IsZero()) {
1483 // The zero register is used to avoid synthesizing zero constants.
1484 temp_src = Register(src);
1485 } else {
1486 DCHECK(src.IsFPRegister());
1487 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1488 __ Fmov(temp_src, FPRegister(src));
1489 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001490
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001491 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001492 break;
1493 }
1494 case Primitive::kPrimVoid:
1495 LOG(FATAL) << "Unreachable type " << type;
1496 }
1497}
1498
Calin Juravle175dc732015-08-25 15:42:32 +01001499void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1500 HInstruction* instruction,
1501 uint32_t dex_pc,
1502 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001503 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001504 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001505 if (EntrypointRequiresStackMap(entrypoint)) {
1506 RecordPcInfo(instruction, dex_pc, slow_path);
1507 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001508}
1509
Roland Levillaindec8f632016-07-22 17:10:06 +01001510void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1511 HInstruction* instruction,
1512 SlowPathCode* slow_path) {
1513 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001514 GenerateInvokeRuntime(entry_point_offset);
1515}
1516
1517void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001518 BlockPoolsScope block_pools(GetVIXLAssembler());
1519 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1520 __ Blr(lr);
1521}
1522
Alexandre Rames67555f72014-11-18 10:55:16 +00001523void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001524 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001525 UseScratchRegisterScope temps(GetVIXLAssembler());
1526 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001527 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1528
Serban Constantinescu02164b32014-11-13 14:05:07 +00001529 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001530 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1531 __ Add(temp, class_reg, status_offset);
1532 __ Ldar(temp, HeapOperand(temp));
1533 __ Cmp(temp, mirror::Class::kStatusInitialized);
1534 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001535 __ Bind(slow_path->GetExitLabel());
1536}
Alexandre Rames5319def2014-10-23 10:03:10 +01001537
Roland Levillain44015862016-01-22 11:47:17 +00001538void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001539 BarrierType type = BarrierAll;
1540
1541 switch (kind) {
1542 case MemBarrierKind::kAnyAny:
1543 case MemBarrierKind::kAnyStore: {
1544 type = BarrierAll;
1545 break;
1546 }
1547 case MemBarrierKind::kLoadAny: {
1548 type = BarrierReads;
1549 break;
1550 }
1551 case MemBarrierKind::kStoreStore: {
1552 type = BarrierWrites;
1553 break;
1554 }
1555 default:
1556 LOG(FATAL) << "Unexpected memory barrier " << kind;
1557 }
1558 __ Dmb(InnerShareable, type);
1559}
1560
Serban Constantinescu02164b32014-11-13 14:05:07 +00001561void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1562 HBasicBlock* successor) {
1563 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001564 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1565 if (slow_path == nullptr) {
1566 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1567 instruction->SetSlowPath(slow_path);
1568 codegen_->AddSlowPath(slow_path);
1569 if (successor != nullptr) {
1570 DCHECK(successor->IsLoopHeader());
1571 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1572 }
1573 } else {
1574 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1575 }
1576
Serban Constantinescu02164b32014-11-13 14:05:07 +00001577 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1578 Register temp = temps.AcquireW();
1579
Andreas Gampe542451c2016-07-26 09:02:02 -07001580 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001581 if (successor == nullptr) {
1582 __ Cbnz(temp, slow_path->GetEntryLabel());
1583 __ Bind(slow_path->GetReturnLabel());
1584 } else {
1585 __ Cbz(temp, codegen_->GetLabelOf(successor));
1586 __ B(slow_path->GetEntryLabel());
1587 // slow_path will return to GetLabelOf(successor).
1588 }
1589}
1590
Alexandre Rames5319def2014-10-23 10:03:10 +01001591InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1592 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001593 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 assembler_(codegen->GetAssembler()),
1595 codegen_(codegen) {}
1596
1597#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001598 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001599
1600#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1601
1602enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001603 // Using a base helps identify when we hit such breakpoints.
1604 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001605#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1606 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1607#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1608};
1609
1610#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001611 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001612 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1613 } \
1614 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1616 locations->SetOut(Location::Any()); \
1617 }
1618 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1619#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1620
1621#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001622#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001623
Alexandre Rames67555f72014-11-18 10:55:16 +00001624void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001625 DCHECK_EQ(instr->InputCount(), 2U);
1626 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1627 Primitive::Type type = instr->GetResultType();
1628 switch (type) {
1629 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001630 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001631 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001632 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001634 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001635
1636 case Primitive::kPrimFloat:
1637 case Primitive::kPrimDouble:
1638 locations->SetInAt(0, Location::RequiresFpuRegister());
1639 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001640 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001641 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001642
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001644 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 }
1646}
1647
Alexandre Rames09a99962015-04-15 11:47:56 +01001648void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001649 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1650
1651 bool object_field_get_with_read_barrier =
1652 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001653 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001654 new (GetGraph()->GetArena()) LocationSummary(instruction,
1655 object_field_get_with_read_barrier ?
1656 LocationSummary::kCallOnSlowPath :
1657 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001658 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001659 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001660 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001661 locations->SetInAt(0, Location::RequiresRegister());
1662 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1663 locations->SetOut(Location::RequiresFpuRegister());
1664 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001665 // The output overlaps for an object field get when read barriers
1666 // are enabled: we do not want the load to overwrite the object's
1667 // location, as we need it to emit the read barrier.
1668 locations->SetOut(
1669 Location::RequiresRegister(),
1670 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001671 }
1672}
1673
1674void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1675 const FieldInfo& field_info) {
1676 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001677 LocationSummary* locations = instruction->GetLocations();
1678 Location base_loc = locations->InAt(0);
1679 Location out = locations->Out();
1680 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001681 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001682 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001683 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001684
Roland Levillain44015862016-01-22 11:47:17 +00001685 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1686 // Object FieldGet with Baker's read barrier case.
1687 MacroAssembler* masm = GetVIXLAssembler();
1688 UseScratchRegisterScope temps(masm);
1689 // /* HeapReference<Object> */ out = *(base + offset)
1690 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1691 Register temp = temps.AcquireW();
1692 // Note that potential implicit null checks are handled in this
1693 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1694 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1695 instruction,
1696 out,
1697 base,
1698 offset,
1699 temp,
1700 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001701 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001702 } else {
1703 // General case.
1704 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001705 // Note that a potential implicit null check is handled in this
1706 // CodeGeneratorARM64::LoadAcquire call.
1707 // NB: LoadAcquire will record the pc info if needed.
1708 codegen_->LoadAcquire(
1709 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001710 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001711 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001712 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001713 }
Roland Levillain44015862016-01-22 11:47:17 +00001714 if (field_type == Primitive::kPrimNot) {
1715 // If read barriers are enabled, emit read barriers other than
1716 // Baker's using a slow path (and also unpoison the loaded
1717 // reference, if heap poisoning is enabled).
1718 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1719 }
Roland Levillain4d027112015-07-01 15:41:14 +01001720 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001721}
1722
1723void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1724 LocationSummary* locations =
1725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1726 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001727 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1728 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1729 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001730 locations->SetInAt(1, Location::RequiresFpuRegister());
1731 } else {
1732 locations->SetInAt(1, Location::RequiresRegister());
1733 }
1734}
1735
1736void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001737 const FieldInfo& field_info,
1738 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001740 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001741
1742 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001743 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001744 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001745 Offset offset = field_info.GetFieldOffset();
1746 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001747
Roland Levillain4d027112015-07-01 15:41:14 +01001748 {
1749 // We use a block to end the scratch scope before the write barrier, thus
1750 // freeing the temporary registers so they can be used in `MarkGCCard`.
1751 UseScratchRegisterScope temps(GetVIXLAssembler());
1752
1753 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1754 DCHECK(value.IsW());
1755 Register temp = temps.AcquireW();
1756 __ Mov(temp, value.W());
1757 GetAssembler()->PoisonHeapReference(temp.W());
1758 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001759 }
Roland Levillain4d027112015-07-01 15:41:14 +01001760
1761 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001762 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1763 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001764 } else {
1765 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1766 codegen_->MaybeRecordImplicitNullCheck(instruction);
1767 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001768 }
1769
1770 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001771 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001772 }
1773}
1774
Alexandre Rames67555f72014-11-18 10:55:16 +00001775void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001776 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001777
1778 switch (type) {
1779 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001780 case Primitive::kPrimLong: {
1781 Register dst = OutputRegister(instr);
1782 Register lhs = InputRegisterAt(instr, 0);
1783 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001784 if (instr->IsAdd()) {
1785 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001786 } else if (instr->IsAnd()) {
1787 __ And(dst, lhs, rhs);
1788 } else if (instr->IsOr()) {
1789 __ Orr(dst, lhs, rhs);
1790 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001791 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001792 } else if (instr->IsRor()) {
1793 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001794 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001795 __ Ror(dst, lhs, shift);
1796 } else {
1797 // Ensure shift distance is in the same size register as the result. If
1798 // we are rotating a long and the shift comes in a w register originally,
1799 // we don't need to sxtw for use as an x since the shift distances are
1800 // all & reg_bits - 1.
1801 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1802 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001803 } else {
1804 DCHECK(instr->IsXor());
1805 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001806 }
1807 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001808 }
1809 case Primitive::kPrimFloat:
1810 case Primitive::kPrimDouble: {
1811 FPRegister dst = OutputFPRegister(instr);
1812 FPRegister lhs = InputFPRegisterAt(instr, 0);
1813 FPRegister rhs = InputFPRegisterAt(instr, 1);
1814 if (instr->IsAdd()) {
1815 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001816 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001817 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001818 } else {
1819 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001820 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001821 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001822 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001823 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001824 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001825 }
1826}
1827
Serban Constantinescu02164b32014-11-13 14:05:07 +00001828void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1829 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1830
1831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1832 Primitive::Type type = instr->GetResultType();
1833 switch (type) {
1834 case Primitive::kPrimInt:
1835 case Primitive::kPrimLong: {
1836 locations->SetInAt(0, Location::RequiresRegister());
1837 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1838 locations->SetOut(Location::RequiresRegister());
1839 break;
1840 }
1841 default:
1842 LOG(FATAL) << "Unexpected shift type " << type;
1843 }
1844}
1845
1846void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1847 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1848
1849 Primitive::Type type = instr->GetType();
1850 switch (type) {
1851 case Primitive::kPrimInt:
1852 case Primitive::kPrimLong: {
1853 Register dst = OutputRegister(instr);
1854 Register lhs = InputRegisterAt(instr, 0);
1855 Operand rhs = InputOperandAt(instr, 1);
1856 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001857 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001858 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001859 if (instr->IsShl()) {
1860 __ Lsl(dst, lhs, shift_value);
1861 } else if (instr->IsShr()) {
1862 __ Asr(dst, lhs, shift_value);
1863 } else {
1864 __ Lsr(dst, lhs, shift_value);
1865 }
1866 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001867 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001868
1869 if (instr->IsShl()) {
1870 __ Lsl(dst, lhs, rhs_reg);
1871 } else if (instr->IsShr()) {
1872 __ Asr(dst, lhs, rhs_reg);
1873 } else {
1874 __ Lsr(dst, lhs, rhs_reg);
1875 }
1876 }
1877 break;
1878 }
1879 default:
1880 LOG(FATAL) << "Unexpected shift operation type " << type;
1881 }
1882}
1883
Alexandre Rames5319def2014-10-23 10:03:10 +01001884void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001885 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001886}
1887
1888void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001889 HandleBinaryOp(instruction);
1890}
1891
1892void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1893 HandleBinaryOp(instruction);
1894}
1895
1896void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1897 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001898}
1899
Artem Serov7fc63502016-02-09 17:15:29 +00001900void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001901 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1903 locations->SetInAt(0, Location::RequiresRegister());
1904 // There is no immediate variant of negated bitwise instructions in AArch64.
1905 locations->SetInAt(1, Location::RequiresRegister());
1906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1907}
1908
Artem Serov7fc63502016-02-09 17:15:29 +00001909void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001910 Register dst = OutputRegister(instr);
1911 Register lhs = InputRegisterAt(instr, 0);
1912 Register rhs = InputRegisterAt(instr, 1);
1913
1914 switch (instr->GetOpKind()) {
1915 case HInstruction::kAnd:
1916 __ Bic(dst, lhs, rhs);
1917 break;
1918 case HInstruction::kOr:
1919 __ Orn(dst, lhs, rhs);
1920 break;
1921 case HInstruction::kXor:
1922 __ Eon(dst, lhs, rhs);
1923 break;
1924 default:
1925 LOG(FATAL) << "Unreachable";
1926 }
1927}
1928
Alexandre Rames8626b742015-11-25 16:28:08 +00001929void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1930 HArm64DataProcWithShifterOp* instruction) {
1931 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1932 instruction->GetType() == Primitive::kPrimLong);
1933 LocationSummary* locations =
1934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1935 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1936 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1937 } else {
1938 locations->SetInAt(0, Location::RequiresRegister());
1939 }
1940 locations->SetInAt(1, Location::RequiresRegister());
1941 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1942}
1943
1944void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1945 HArm64DataProcWithShifterOp* instruction) {
1946 Primitive::Type type = instruction->GetType();
1947 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1948 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1949 Register out = OutputRegister(instruction);
1950 Register left;
1951 if (kind != HInstruction::kNeg) {
1952 left = InputRegisterAt(instruction, 0);
1953 }
1954 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1955 // shifter operand operation, the IR generating `right_reg` (input to the type
1956 // conversion) can have a different type from the current instruction's type,
1957 // so we manually indicate the type.
1958 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001959 int64_t shift_amount = instruction->GetShiftAmount() &
1960 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001961
1962 Operand right_operand(0);
1963
1964 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1965 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1966 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1967 } else {
1968 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1969 }
1970
1971 // Logical binary operations do not support extension operations in the
1972 // operand. Note that VIXL would still manage if it was passed by generating
1973 // the extension as a separate instruction.
1974 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1975 DCHECK(!right_operand.IsExtendedRegister() ||
1976 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1977 kind != HInstruction::kNeg));
1978 switch (kind) {
1979 case HInstruction::kAdd:
1980 __ Add(out, left, right_operand);
1981 break;
1982 case HInstruction::kAnd:
1983 __ And(out, left, right_operand);
1984 break;
1985 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001986 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001987 __ Neg(out, right_operand);
1988 break;
1989 case HInstruction::kOr:
1990 __ Orr(out, left, right_operand);
1991 break;
1992 case HInstruction::kSub:
1993 __ Sub(out, left, right_operand);
1994 break;
1995 case HInstruction::kXor:
1996 __ Eor(out, left, right_operand);
1997 break;
1998 default:
1999 LOG(FATAL) << "Unexpected operation kind: " << kind;
2000 UNREACHABLE();
2001 }
2002}
2003
Artem Serov328429f2016-07-06 16:23:04 +01002004void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002005 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2006 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002007 LocationSummary* locations =
2008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2009 locations->SetInAt(0, Location::RequiresRegister());
2010 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2011 locations->SetOut(Location::RequiresRegister());
2012}
2013
Roland Levillain4a3aa572016-08-15 13:17:06 +00002014void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
2015 HIntermediateAddress* instruction) {
2016 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2017 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002018 __ Add(OutputRegister(instruction),
2019 InputRegisterAt(instruction, 0),
2020 Operand(InputOperandAt(instruction, 1)));
2021}
2022
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002023void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002024 LocationSummary* locations =
2025 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002026 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2027 if (instr->GetOpKind() == HInstruction::kSub &&
2028 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002029 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002030 // Don't allocate register for Mneg instruction.
2031 } else {
2032 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2033 Location::RequiresRegister());
2034 }
2035 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2036 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2038}
2039
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002040void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002041 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002042 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2043 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002044
2045 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2046 // This fixup should be carried out for all multiply-accumulate instructions:
2047 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2048 if (instr->GetType() == Primitive::kPrimLong &&
2049 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2050 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002051 vixl::aarch64::Instruction* prev =
2052 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002053 if (prev->IsLoadOrStore()) {
2054 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002055 vixl::aarch64::CodeBufferCheckScope scope(masm,
2056 kInstructionSize,
2057 vixl::aarch64::CodeBufferCheckScope::kCheck,
2058 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002059 __ nop();
2060 }
2061 }
2062
2063 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002064 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002065 __ Madd(res, mul_left, mul_right, accumulator);
2066 } else {
2067 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002068 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002069 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002070 __ Mneg(res, mul_left, mul_right);
2071 } else {
2072 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2073 __ Msub(res, mul_left, mul_right, accumulator);
2074 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002075 }
2076}
2077
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002078void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002079 bool object_array_get_with_read_barrier =
2080 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002081 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002082 new (GetGraph()->GetArena()) LocationSummary(instruction,
2083 object_array_get_with_read_barrier ?
2084 LocationSummary::kCallOnSlowPath :
2085 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002086 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002087 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002088 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002089 locations->SetInAt(0, Location::RequiresRegister());
2090 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002091 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2093 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002094 // The output overlaps in the case of an object array get with
2095 // read barriers enabled: we do not want the move to overwrite the
2096 // array's location, as we need it to emit the read barrier.
2097 locations->SetOut(
2098 Location::RequiresRegister(),
2099 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002100 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002101}
2102
2103void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002104 Primitive::Type type = instruction->GetType();
2105 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002106 LocationSummary* locations = instruction->GetLocations();
2107 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002108 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002109 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002110 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2111 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002112 MacroAssembler* masm = GetVIXLAssembler();
2113 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002114 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002115 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002116
Roland Levillain44015862016-01-22 11:47:17 +00002117 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2118 // Object ArrayGet with Baker's read barrier case.
2119 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002120 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2121 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002122 // Note that a potential implicit null check is handled in the
2123 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2124 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2125 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002126 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002127 // General case.
2128 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002129 Register length;
2130 if (maybe_compressed_char_at) {
2131 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2132 length = temps.AcquireW();
2133 __ Ldr(length, HeapOperand(obj, count_offset));
2134 codegen_->MaybeRecordImplicitNullCheck(instruction);
2135 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002136 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002137 if (maybe_compressed_char_at) {
2138 vixl::aarch64::Label uncompressed_load, done;
2139 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2140 __ Ldrb(Register(OutputCPURegister(instruction)),
2141 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2142 __ B(&done);
2143 __ Bind(&uncompressed_load);
2144 __ Ldrh(Register(OutputCPURegister(instruction)),
2145 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2146 __ Bind(&done);
2147 } else {
2148 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2149 source = HeapOperand(obj, offset);
2150 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002151 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002152 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002153 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002154 // The read barrier instrumentation does not support the
2155 // HIntermediateAddress instruction yet.
2156 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002157 // We do not need to compute the intermediate address from the array: the
2158 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002159 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002160 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002161 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002162 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2163 }
2164 temp = obj;
2165 } else {
2166 __ Add(temp, obj, offset);
2167 }
jessicahandojo05765752016-09-09 19:01:32 -07002168 if (maybe_compressed_char_at) {
2169 vixl::aarch64::Label uncompressed_load, done;
2170 __ Tbz(length.W(), kWRegSize - 1, &uncompressed_load);
2171 __ Ldrb(Register(OutputCPURegister(instruction)),
2172 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2173 __ B(&done);
2174 __ Bind(&uncompressed_load);
2175 __ Ldrh(Register(OutputCPURegister(instruction)),
2176 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2177 __ Bind(&done);
2178 } else {
2179 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2180 }
Roland Levillain44015862016-01-22 11:47:17 +00002181 }
jessicahandojo05765752016-09-09 19:01:32 -07002182 if (!maybe_compressed_char_at) {
2183 codegen_->Load(type, OutputCPURegister(instruction), source);
2184 codegen_->MaybeRecordImplicitNullCheck(instruction);
2185 }
Roland Levillain44015862016-01-22 11:47:17 +00002186
2187 if (type == Primitive::kPrimNot) {
2188 static_assert(
2189 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2190 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2191 Location obj_loc = locations->InAt(0);
2192 if (index.IsConstant()) {
2193 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2194 } else {
2195 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2196 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002197 }
Roland Levillain4d027112015-07-01 15:41:14 +01002198 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002199}
2200
Alexandre Rames5319def2014-10-23 10:03:10 +01002201void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2202 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2203 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002205}
2206
2207void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002208 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002209 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002210 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002211 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002212 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002213 // Mask out compression flag from String's array length.
2214 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2215 __ And(out.W(), out.W(), Operand(static_cast<int32_t>(INT32_MAX)));
2216 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002217}
2218
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002219void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002220 Primitive::Type value_type = instruction->GetComponentType();
2221
2222 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002223 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2224 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002225 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002226 LocationSummary::kCallOnSlowPath :
2227 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002228 locations->SetInAt(0, Location::RequiresRegister());
2229 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002230 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2231 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2232 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002233 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002234 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002235 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002236 }
2237}
2238
2239void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2240 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002241 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002242 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002243 bool needs_write_barrier =
2244 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002245
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002246 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002247 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002248 CPURegister source = value;
2249 Location index = locations->InAt(1);
2250 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2251 MemOperand destination = HeapOperand(array);
2252 MacroAssembler* masm = GetVIXLAssembler();
2253 BlockPoolsScope block_pools(masm);
2254
2255 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002256 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002257 if (index.IsConstant()) {
2258 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2259 destination = HeapOperand(array, offset);
2260 } else {
2261 UseScratchRegisterScope temps(masm);
2262 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002263 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002264 // The read barrier instrumentation does not support the
2265 // HIntermediateAddress instruction yet.
2266 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002267 // We do not need to compute the intermediate address from the array: the
2268 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002269 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002270 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002271 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002272 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2273 }
2274 temp = array;
2275 } else {
2276 __ Add(temp, array, offset);
2277 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002278 destination = HeapOperand(temp,
2279 XRegisterFrom(index),
2280 LSL,
2281 Primitive::ComponentSizeShift(value_type));
2282 }
2283 codegen_->Store(value_type, value, destination);
2284 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002285 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002286 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002287 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002288 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002289 {
2290 // We use a block to end the scratch scope before the write barrier, thus
2291 // freeing the temporary registers so they can be used in `MarkGCCard`.
2292 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002293 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002294 if (index.IsConstant()) {
2295 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002296 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002297 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002298 destination = HeapOperand(temp,
2299 XRegisterFrom(index),
2300 LSL,
2301 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002302 }
2303
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002304 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2305 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2306 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2307
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002308 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002309 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2310 codegen_->AddSlowPath(slow_path);
2311 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002312 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002313 __ Cbnz(Register(value), &non_zero);
2314 if (!index.IsConstant()) {
2315 __ Add(temp, array, offset);
2316 }
2317 __ Str(wzr, destination);
2318 codegen_->MaybeRecordImplicitNullCheck(instruction);
2319 __ B(&done);
2320 __ Bind(&non_zero);
2321 }
2322
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002323 // Note that when Baker read barriers are enabled, the type
2324 // checks are performed without read barriers. This is fine,
2325 // even in the case where a class object is in the from-space
2326 // after the flip, as a comparison involving such a type would
2327 // not produce a false positive; it may of course produce a
2328 // false negative, in which case we would take the ArraySet
2329 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002330
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002331 Register temp2 = temps.AcquireSameSizeAs(array);
2332 // /* HeapReference<Class> */ temp = array->klass_
2333 __ Ldr(temp, HeapOperand(array, class_offset));
2334 codegen_->MaybeRecordImplicitNullCheck(instruction);
2335 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002336
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002337 // /* HeapReference<Class> */ temp = temp->component_type_
2338 __ Ldr(temp, HeapOperand(temp, component_offset));
2339 // /* HeapReference<Class> */ temp2 = value->klass_
2340 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2341 // If heap poisoning is enabled, no need to unpoison `temp`
2342 // nor `temp2`, as we are comparing two poisoned references.
2343 __ Cmp(temp, temp2);
2344 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002345
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002346 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2347 vixl::aarch64::Label do_put;
2348 __ B(eq, &do_put);
2349 // If heap poisoning is enabled, the `temp` reference has
2350 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002351 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2352
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002353 // /* HeapReference<Class> */ temp = temp->super_class_
2354 __ Ldr(temp, HeapOperand(temp, super_offset));
2355 // If heap poisoning is enabled, no need to unpoison
2356 // `temp`, as we are comparing against null below.
2357 __ Cbnz(temp, slow_path->GetEntryLabel());
2358 __ Bind(&do_put);
2359 } else {
2360 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002361 }
2362 }
2363
2364 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002365 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002366 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002367 __ Mov(temp2, value.W());
2368 GetAssembler()->PoisonHeapReference(temp2);
2369 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002370 }
2371
2372 if (!index.IsConstant()) {
2373 __ Add(temp, array, offset);
2374 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002375 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002376
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002377 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002378 codegen_->MaybeRecordImplicitNullCheck(instruction);
2379 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002380 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002381
2382 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2383
2384 if (done.IsLinked()) {
2385 __ Bind(&done);
2386 }
2387
2388 if (slow_path != nullptr) {
2389 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002390 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002391 }
2392}
2393
Alexandre Rames67555f72014-11-18 10:55:16 +00002394void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002395 RegisterSet caller_saves = RegisterSet::Empty();
2396 InvokeRuntimeCallingConvention calling_convention;
2397 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2398 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2399 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002400 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002401 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002402}
2403
2404void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002405 BoundsCheckSlowPathARM64* slow_path =
2406 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002407 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002408 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2409 __ B(slow_path->GetEntryLabel(), hs);
2410}
2411
Alexandre Rames67555f72014-11-18 10:55:16 +00002412void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2413 LocationSummary* locations =
2414 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2415 locations->SetInAt(0, Location::RequiresRegister());
2416 if (check->HasUses()) {
2417 locations->SetOut(Location::SameAsFirstInput());
2418 }
2419}
2420
2421void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2422 // We assume the class is not null.
2423 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2424 check->GetLoadClass(), check, check->GetDexPc(), true);
2425 codegen_->AddSlowPath(slow_path);
2426 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2427}
2428
Roland Levillain1a653882016-03-18 18:05:57 +00002429static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2430 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2431 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2432}
2433
2434void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2435 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2436 Location rhs_loc = instruction->GetLocations()->InAt(1);
2437 if (rhs_loc.IsConstant()) {
2438 // 0.0 is the only immediate that can be encoded directly in
2439 // an FCMP instruction.
2440 //
2441 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2442 // specify that in a floating-point comparison, positive zero
2443 // and negative zero are considered equal, so we can use the
2444 // literal 0.0 for both cases here.
2445 //
2446 // Note however that some methods (Float.equal, Float.compare,
2447 // Float.compareTo, Double.equal, Double.compare,
2448 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2449 // StrictMath.min) consider 0.0 to be (strictly) greater than
2450 // -0.0. So if we ever translate calls to these methods into a
2451 // HCompare instruction, we must handle the -0.0 case with
2452 // care here.
2453 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2454 __ Fcmp(lhs_reg, 0.0);
2455 } else {
2456 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2457 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002458}
2459
Serban Constantinescu02164b32014-11-13 14:05:07 +00002460void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002461 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002462 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2463 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002464 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002465 case Primitive::kPrimBoolean:
2466 case Primitive::kPrimByte:
2467 case Primitive::kPrimShort:
2468 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002469 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002470 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002471 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002472 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2474 break;
2475 }
2476 case Primitive::kPrimFloat:
2477 case Primitive::kPrimDouble: {
2478 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002479 locations->SetInAt(1,
2480 IsFloatingPointZeroConstant(compare->InputAt(1))
2481 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2482 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002483 locations->SetOut(Location::RequiresRegister());
2484 break;
2485 }
2486 default:
2487 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2488 }
2489}
2490
2491void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2492 Primitive::Type in_type = compare->InputAt(0)->GetType();
2493
2494 // 0 if: left == right
2495 // 1 if: left > right
2496 // -1 if: left < right
2497 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002498 case Primitive::kPrimBoolean:
2499 case Primitive::kPrimByte:
2500 case Primitive::kPrimShort:
2501 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002502 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002503 case Primitive::kPrimLong: {
2504 Register result = OutputRegister(compare);
2505 Register left = InputRegisterAt(compare, 0);
2506 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002507 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002508 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2509 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002510 break;
2511 }
2512 case Primitive::kPrimFloat:
2513 case Primitive::kPrimDouble: {
2514 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002515 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002516 __ Cset(result, ne);
2517 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002518 break;
2519 }
2520 default:
2521 LOG(FATAL) << "Unimplemented compare type " << in_type;
2522 }
2523}
2524
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002525void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002526 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002527
2528 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2529 locations->SetInAt(0, Location::RequiresFpuRegister());
2530 locations->SetInAt(1,
2531 IsFloatingPointZeroConstant(instruction->InputAt(1))
2532 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2533 : Location::RequiresFpuRegister());
2534 } else {
2535 // Integer cases.
2536 locations->SetInAt(0, Location::RequiresRegister());
2537 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2538 }
2539
David Brazdilb3e773e2016-01-26 11:28:37 +00002540 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002541 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002542 }
2543}
2544
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002545void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002546 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002547 return;
2548 }
2549
2550 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002551 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002552 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002553
Roland Levillain7f63c522015-07-13 15:54:55 +00002554 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002555 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002556 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002557 } else {
2558 // Integer cases.
2559 Register lhs = InputRegisterAt(instruction, 0);
2560 Operand rhs = InputOperandAt(instruction, 1);
2561 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002562 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002563 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002564}
2565
2566#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2567 M(Equal) \
2568 M(NotEqual) \
2569 M(LessThan) \
2570 M(LessThanOrEqual) \
2571 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002572 M(GreaterThanOrEqual) \
2573 M(Below) \
2574 M(BelowOrEqual) \
2575 M(Above) \
2576 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002577#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002578void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2579void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002580FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002581#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002582#undef FOR_EACH_CONDITION_INSTRUCTION
2583
Zheng Xuc6667102015-05-15 16:08:45 +08002584void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2585 DCHECK(instruction->IsDiv() || instruction->IsRem());
2586
2587 LocationSummary* locations = instruction->GetLocations();
2588 Location second = locations->InAt(1);
2589 DCHECK(second.IsConstant());
2590
2591 Register out = OutputRegister(instruction);
2592 Register dividend = InputRegisterAt(instruction, 0);
2593 int64_t imm = Int64FromConstant(second.GetConstant());
2594 DCHECK(imm == 1 || imm == -1);
2595
2596 if (instruction->IsRem()) {
2597 __ Mov(out, 0);
2598 } else {
2599 if (imm == 1) {
2600 __ Mov(out, dividend);
2601 } else {
2602 __ Neg(out, dividend);
2603 }
2604 }
2605}
2606
2607void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2608 DCHECK(instruction->IsDiv() || instruction->IsRem());
2609
2610 LocationSummary* locations = instruction->GetLocations();
2611 Location second = locations->InAt(1);
2612 DCHECK(second.IsConstant());
2613
2614 Register out = OutputRegister(instruction);
2615 Register dividend = InputRegisterAt(instruction, 0);
2616 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002617 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002618 int ctz_imm = CTZ(abs_imm);
2619
2620 UseScratchRegisterScope temps(GetVIXLAssembler());
2621 Register temp = temps.AcquireSameSizeAs(out);
2622
2623 if (instruction->IsDiv()) {
2624 __ Add(temp, dividend, abs_imm - 1);
2625 __ Cmp(dividend, 0);
2626 __ Csel(out, temp, dividend, lt);
2627 if (imm > 0) {
2628 __ Asr(out, out, ctz_imm);
2629 } else {
2630 __ Neg(out, Operand(out, ASR, ctz_imm));
2631 }
2632 } else {
2633 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2634 __ Asr(temp, dividend, bits - 1);
2635 __ Lsr(temp, temp, bits - ctz_imm);
2636 __ Add(out, dividend, temp);
2637 __ And(out, out, abs_imm - 1);
2638 __ Sub(out, out, temp);
2639 }
2640}
2641
2642void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2643 DCHECK(instruction->IsDiv() || instruction->IsRem());
2644
2645 LocationSummary* locations = instruction->GetLocations();
2646 Location second = locations->InAt(1);
2647 DCHECK(second.IsConstant());
2648
2649 Register out = OutputRegister(instruction);
2650 Register dividend = InputRegisterAt(instruction, 0);
2651 int64_t imm = Int64FromConstant(second.GetConstant());
2652
2653 Primitive::Type type = instruction->GetResultType();
2654 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2655
2656 int64_t magic;
2657 int shift;
2658 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2659
2660 UseScratchRegisterScope temps(GetVIXLAssembler());
2661 Register temp = temps.AcquireSameSizeAs(out);
2662
2663 // temp = get_high(dividend * magic)
2664 __ Mov(temp, magic);
2665 if (type == Primitive::kPrimLong) {
2666 __ Smulh(temp, dividend, temp);
2667 } else {
2668 __ Smull(temp.X(), dividend, temp);
2669 __ Lsr(temp.X(), temp.X(), 32);
2670 }
2671
2672 if (imm > 0 && magic < 0) {
2673 __ Add(temp, temp, dividend);
2674 } else if (imm < 0 && magic > 0) {
2675 __ Sub(temp, temp, dividend);
2676 }
2677
2678 if (shift != 0) {
2679 __ Asr(temp, temp, shift);
2680 }
2681
2682 if (instruction->IsDiv()) {
2683 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2684 } else {
2685 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2686 // TODO: Strength reduction for msub.
2687 Register temp_imm = temps.AcquireSameSizeAs(out);
2688 __ Mov(temp_imm, imm);
2689 __ Msub(out, temp, temp_imm, dividend);
2690 }
2691}
2692
2693void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2694 DCHECK(instruction->IsDiv() || instruction->IsRem());
2695 Primitive::Type type = instruction->GetResultType();
2696 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2697
2698 LocationSummary* locations = instruction->GetLocations();
2699 Register out = OutputRegister(instruction);
2700 Location second = locations->InAt(1);
2701
2702 if (second.IsConstant()) {
2703 int64_t imm = Int64FromConstant(second.GetConstant());
2704
2705 if (imm == 0) {
2706 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2707 } else if (imm == 1 || imm == -1) {
2708 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002709 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002710 DivRemByPowerOfTwo(instruction);
2711 } else {
2712 DCHECK(imm <= -2 || imm >= 2);
2713 GenerateDivRemWithAnyConstant(instruction);
2714 }
2715 } else {
2716 Register dividend = InputRegisterAt(instruction, 0);
2717 Register divisor = InputRegisterAt(instruction, 1);
2718 if (instruction->IsDiv()) {
2719 __ Sdiv(out, dividend, divisor);
2720 } else {
2721 UseScratchRegisterScope temps(GetVIXLAssembler());
2722 Register temp = temps.AcquireSameSizeAs(out);
2723 __ Sdiv(temp, dividend, divisor);
2724 __ Msub(out, temp, divisor, dividend);
2725 }
2726 }
2727}
2728
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002729void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2730 LocationSummary* locations =
2731 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2732 switch (div->GetResultType()) {
2733 case Primitive::kPrimInt:
2734 case Primitive::kPrimLong:
2735 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002736 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2738 break;
2739
2740 case Primitive::kPrimFloat:
2741 case Primitive::kPrimDouble:
2742 locations->SetInAt(0, Location::RequiresFpuRegister());
2743 locations->SetInAt(1, Location::RequiresFpuRegister());
2744 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2745 break;
2746
2747 default:
2748 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2749 }
2750}
2751
2752void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2753 Primitive::Type type = div->GetResultType();
2754 switch (type) {
2755 case Primitive::kPrimInt:
2756 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002757 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002758 break;
2759
2760 case Primitive::kPrimFloat:
2761 case Primitive::kPrimDouble:
2762 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2763 break;
2764
2765 default:
2766 LOG(FATAL) << "Unexpected div type " << type;
2767 }
2768}
2769
Alexandre Rames67555f72014-11-18 10:55:16 +00002770void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002771 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002772 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00002773}
2774
2775void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2776 SlowPathCodeARM64* slow_path =
2777 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2778 codegen_->AddSlowPath(slow_path);
2779 Location value = instruction->GetLocations()->InAt(0);
2780
Alexandre Rames3e69f162014-12-10 10:36:50 +00002781 Primitive::Type type = instruction->GetType();
2782
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002783 if (!Primitive::IsIntegralType(type)) {
2784 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002785 return;
2786 }
2787
Alexandre Rames67555f72014-11-18 10:55:16 +00002788 if (value.IsConstant()) {
2789 int64_t divisor = Int64ConstantFrom(value);
2790 if (divisor == 0) {
2791 __ B(slow_path->GetEntryLabel());
2792 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002793 // A division by a non-null constant is valid. We don't need to perform
2794 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002795 }
2796 } else {
2797 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2798 }
2799}
2800
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002801void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2802 LocationSummary* locations =
2803 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2804 locations->SetOut(Location::ConstantLocation(constant));
2805}
2806
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002807void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2808 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002809 // Will be generated at use site.
2810}
2811
Alexandre Rames5319def2014-10-23 10:03:10 +01002812void LocationsBuilderARM64::VisitExit(HExit* exit) {
2813 exit->SetLocations(nullptr);
2814}
2815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002816void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002817}
2818
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002819void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2820 LocationSummary* locations =
2821 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2822 locations->SetOut(Location::ConstantLocation(constant));
2823}
2824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002825void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002826 // Will be generated at use site.
2827}
2828
David Brazdilfc6a86a2015-06-26 10:33:45 +00002829void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002830 DCHECK(!successor->IsExitBlock());
2831 HBasicBlock* block = got->GetBlock();
2832 HInstruction* previous = got->GetPrevious();
2833 HLoopInformation* info = block->GetLoopInformation();
2834
David Brazdil46e2a392015-03-16 17:31:52 +00002835 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002836 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2837 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2838 return;
2839 }
2840 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2841 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2842 }
2843 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002844 __ B(codegen_->GetLabelOf(successor));
2845 }
2846}
2847
David Brazdilfc6a86a2015-06-26 10:33:45 +00002848void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2849 got->SetLocations(nullptr);
2850}
2851
2852void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2853 HandleGoto(got, got->GetSuccessor());
2854}
2855
2856void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2857 try_boundary->SetLocations(nullptr);
2858}
2859
2860void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2861 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2862 if (!successor->IsExitBlock()) {
2863 HandleGoto(try_boundary, successor);
2864 }
2865}
2866
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002867void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002868 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002869 vixl::aarch64::Label* true_target,
2870 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002871 // FP branching requires both targets to be explicit. If either of the targets
2872 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002873 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002874 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002875
David Brazdil0debae72015-11-12 18:37:00 +00002876 if (true_target == nullptr && false_target == nullptr) {
2877 // Nothing to do. The code always falls through.
2878 return;
2879 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002880 // Constant condition, statically compared against "true" (integer value 1).
2881 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002882 if (true_target != nullptr) {
2883 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002884 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002885 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002886 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002887 if (false_target != nullptr) {
2888 __ B(false_target);
2889 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002890 }
David Brazdil0debae72015-11-12 18:37:00 +00002891 return;
2892 }
2893
2894 // The following code generates these patterns:
2895 // (1) true_target == nullptr && false_target != nullptr
2896 // - opposite condition true => branch to false_target
2897 // (2) true_target != nullptr && false_target == nullptr
2898 // - condition true => branch to true_target
2899 // (3) true_target != nullptr && false_target != nullptr
2900 // - condition true => branch to true_target
2901 // - branch to false_target
2902 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002903 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002904 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002905 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002906 if (true_target == nullptr) {
2907 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2908 } else {
2909 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2910 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002911 } else {
2912 // The condition instruction has not been materialized, use its inputs as
2913 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002914 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002915
David Brazdil0debae72015-11-12 18:37:00 +00002916 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002917 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002918 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002919 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002920 IfCondition opposite_condition = condition->GetOppositeCondition();
2921 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002922 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002923 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002924 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002925 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002926 // Integer cases.
2927 Register lhs = InputRegisterAt(condition, 0);
2928 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002929
2930 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002931 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002932 if (true_target == nullptr) {
2933 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2934 non_fallthrough_target = false_target;
2935 } else {
2936 arm64_cond = ARM64Condition(condition->GetCondition());
2937 non_fallthrough_target = true_target;
2938 }
2939
Aart Bik086d27e2016-01-20 17:02:00 -08002940 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002941 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002942 switch (arm64_cond) {
2943 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002944 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002945 break;
2946 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002947 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002948 break;
2949 case lt:
2950 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002951 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002952 break;
2953 case ge:
2954 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002955 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002956 break;
2957 default:
2958 // Without the `static_cast` the compiler throws an error for
2959 // `-Werror=sign-promo`.
2960 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2961 }
2962 } else {
2963 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002964 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002965 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002966 }
2967 }
David Brazdil0debae72015-11-12 18:37:00 +00002968
2969 // If neither branch falls through (case 3), the conditional branch to `true_target`
2970 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2971 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002972 __ B(false_target);
2973 }
David Brazdil0debae72015-11-12 18:37:00 +00002974
2975 if (fallthrough_target.IsLinked()) {
2976 __ Bind(&fallthrough_target);
2977 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002978}
2979
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002980void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002982 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002983 locations->SetInAt(0, Location::RequiresRegister());
2984 }
2985}
2986
2987void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002988 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2989 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002990 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2991 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2992 true_target = nullptr;
2993 }
2994 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2995 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2996 false_target = nullptr;
2997 }
David Brazdil0debae72015-11-12 18:37:00 +00002998 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002999}
3000
3001void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3002 LocationSummary* locations = new (GetGraph()->GetArena())
3003 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003004 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003005 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003006 locations->SetInAt(0, Location::RequiresRegister());
3007 }
3008}
3009
3010void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003011 SlowPathCodeARM64* slow_path =
3012 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003013 GenerateTestAndBranch(deoptimize,
3014 /* condition_input_index */ 0,
3015 slow_path->GetEntryLabel(),
3016 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003017}
3018
David Brazdilc0b601b2016-02-08 14:20:45 +00003019static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3020 return condition->IsCondition() &&
3021 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3022}
3023
Alexandre Rames880f1192016-06-13 16:04:50 +01003024static inline Condition GetConditionForSelect(HCondition* condition) {
3025 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003026 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3027 : ARM64Condition(cond);
3028}
3029
David Brazdil74eb1b22015-12-14 11:44:01 +00003030void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3031 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003032 if (Primitive::IsFloatingPointType(select->GetType())) {
3033 locations->SetInAt(0, Location::RequiresFpuRegister());
3034 locations->SetInAt(1, Location::RequiresFpuRegister());
3035 locations->SetOut(Location::RequiresFpuRegister());
3036 } else {
3037 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3038 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3039 bool is_true_value_constant = cst_true_value != nullptr;
3040 bool is_false_value_constant = cst_false_value != nullptr;
3041 // Ask VIXL whether we should synthesize constants in registers.
3042 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3043 Operand true_op = is_true_value_constant ?
3044 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3045 Operand false_op = is_false_value_constant ?
3046 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3047 bool true_value_in_register = false;
3048 bool false_value_in_register = false;
3049 MacroAssembler::GetCselSynthesisInformation(
3050 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3051 true_value_in_register |= !is_true_value_constant;
3052 false_value_in_register |= !is_false_value_constant;
3053
3054 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3055 : Location::ConstantLocation(cst_true_value));
3056 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3057 : Location::ConstantLocation(cst_false_value));
3058 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003059 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003060
David Brazdil74eb1b22015-12-14 11:44:01 +00003061 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3062 locations->SetInAt(2, Location::RequiresRegister());
3063 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003064}
3065
3066void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003067 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003068 Condition csel_cond;
3069
3070 if (IsBooleanValueOrMaterializedCondition(cond)) {
3071 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003072 // Use the condition flags set by the previous instruction.
3073 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003074 } else {
3075 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003076 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003077 }
3078 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003079 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003080 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003081 } else {
3082 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003083 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003084 }
3085
Alexandre Rames880f1192016-06-13 16:04:50 +01003086 if (Primitive::IsFloatingPointType(select->GetType())) {
3087 __ Fcsel(OutputFPRegister(select),
3088 InputFPRegisterAt(select, 1),
3089 InputFPRegisterAt(select, 0),
3090 csel_cond);
3091 } else {
3092 __ Csel(OutputRegister(select),
3093 InputOperandAt(select, 1),
3094 InputOperandAt(select, 0),
3095 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003096 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003097}
3098
David Srbecky0cf44932015-12-09 14:09:59 +00003099void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3100 new (GetGraph()->GetArena()) LocationSummary(info);
3101}
3102
David Srbeckyd28f4a02016-03-14 17:14:24 +00003103void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3104 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003105}
3106
3107void CodeGeneratorARM64::GenerateNop() {
3108 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003109}
3110
Alexandre Rames5319def2014-10-23 10:03:10 +01003111void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003112 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003113}
3114
3115void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003116 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003117}
3118
3119void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003120 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003121}
3122
3123void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003124 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003125}
3126
Roland Levillain44015862016-01-22 11:47:17 +00003127static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3128 return kEmitCompilerReadBarrier &&
3129 (kUseBakerReadBarrier ||
3130 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3131 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3132 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3133}
3134
Alexandre Rames67555f72014-11-18 10:55:16 +00003135void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003136 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003137 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003138 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003139 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003140 case TypeCheckKind::kExactCheck:
3141 case TypeCheckKind::kAbstractClassCheck:
3142 case TypeCheckKind::kClassHierarchyCheck:
3143 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003144 call_kind =
3145 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003146 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003147 break;
3148 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003149 case TypeCheckKind::kUnresolvedCheck:
3150 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003151 call_kind = LocationSummary::kCallOnSlowPath;
3152 break;
3153 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003154
Alexandre Rames67555f72014-11-18 10:55:16 +00003155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003156 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003157 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003158 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003159 locations->SetInAt(0, Location::RequiresRegister());
3160 locations->SetInAt(1, Location::RequiresRegister());
3161 // The "out" register is used as a temporary, so it overlaps with the inputs.
3162 // Note that TypeCheckSlowPathARM64 uses this register too.
3163 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3164 // When read barriers are enabled, we need a temporary register for
3165 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003166 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003167 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003168 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003169}
3170
3171void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003172 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003173 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003174 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003175 Register obj = InputRegisterAt(instruction, 0);
3176 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003177 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003178 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003179 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3180 locations->GetTemp(0) :
3181 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003182 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3183 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3184 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3185 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003186
Scott Wakeling97c72b72016-06-24 16:19:36 +01003187 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003188 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003189
3190 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003191 // Avoid null check if we know `obj` is not null.
3192 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003193 __ Cbz(obj, &zero);
3194 }
3195
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003196 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003197 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003198
Roland Levillain44015862016-01-22 11:47:17 +00003199 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 case TypeCheckKind::kExactCheck: {
3201 __ Cmp(out, cls);
3202 __ Cset(out, eq);
3203 if (zero.IsLinked()) {
3204 __ B(&done);
3205 }
3206 break;
3207 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003209 case TypeCheckKind::kAbstractClassCheck: {
3210 // If the class is abstract, we eagerly fetch the super class of the
3211 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003212 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003213 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003214 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003215 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003216 // If `out` is null, we use it for the result, and jump to `done`.
3217 __ Cbz(out, &done);
3218 __ Cmp(out, cls);
3219 __ B(ne, &loop);
3220 __ Mov(out, 1);
3221 if (zero.IsLinked()) {
3222 __ B(&done);
3223 }
3224 break;
3225 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003226
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003227 case TypeCheckKind::kClassHierarchyCheck: {
3228 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003229 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003230 __ Bind(&loop);
3231 __ Cmp(out, cls);
3232 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003233 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003234 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003235 __ Cbnz(out, &loop);
3236 // If `out` is null, we use it for the result, and jump to `done`.
3237 __ B(&done);
3238 __ Bind(&success);
3239 __ Mov(out, 1);
3240 if (zero.IsLinked()) {
3241 __ B(&done);
3242 }
3243 break;
3244 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003245
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003246 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003247 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003248 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003249 __ Cmp(out, cls);
3250 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003251 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003252 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003253 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003254 // If `out` is null, we use it for the result, and jump to `done`.
3255 __ Cbz(out, &done);
3256 __ Ldrh(out, HeapOperand(out, primitive_offset));
3257 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3258 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003259 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003260 __ Mov(out, 1);
3261 __ B(&done);
3262 break;
3263 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003265 case TypeCheckKind::kArrayCheck: {
3266 __ Cmp(out, cls);
3267 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003268 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3269 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003270 codegen_->AddSlowPath(slow_path);
3271 __ B(ne, slow_path->GetEntryLabel());
3272 __ Mov(out, 1);
3273 if (zero.IsLinked()) {
3274 __ B(&done);
3275 }
3276 break;
3277 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003278
Calin Juravle98893e12015-10-02 21:05:03 +01003279 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003280 case TypeCheckKind::kInterfaceCheck: {
3281 // Note that we indeed only call on slow path, but we always go
3282 // into the slow path for the unresolved and interface check
3283 // cases.
3284 //
3285 // We cannot directly call the InstanceofNonTrivial runtime
3286 // entry point without resorting to a type checking slow path
3287 // here (i.e. by calling InvokeRuntime directly), as it would
3288 // require to assign fixed registers for the inputs of this
3289 // HInstanceOf instruction (following the runtime calling
3290 // convention), which might be cluttered by the potential first
3291 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003292 //
3293 // TODO: Introduce a new runtime entry point taking the object
3294 // to test (instead of its class) as argument, and let it deal
3295 // with the read barrier issues. This will let us refactor this
3296 // case of the `switch` code as it was previously (with a direct
3297 // call to the runtime not using a type checking slow path).
3298 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003299 DCHECK(locations->OnlyCallsOnSlowPath());
3300 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3301 /* is_fatal */ false);
3302 codegen_->AddSlowPath(slow_path);
3303 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003304 if (zero.IsLinked()) {
3305 __ B(&done);
3306 }
3307 break;
3308 }
3309 }
3310
3311 if (zero.IsLinked()) {
3312 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003313 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003314 }
3315
3316 if (done.IsLinked()) {
3317 __ Bind(&done);
3318 }
3319
3320 if (slow_path != nullptr) {
3321 __ Bind(slow_path->GetExitLabel());
3322 }
3323}
3324
3325void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3326 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3327 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3328
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003329 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3330 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003331 case TypeCheckKind::kExactCheck:
3332 case TypeCheckKind::kAbstractClassCheck:
3333 case TypeCheckKind::kClassHierarchyCheck:
3334 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003335 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3336 LocationSummary::kCallOnSlowPath :
3337 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003338 break;
3339 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003340 case TypeCheckKind::kUnresolvedCheck:
3341 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003342 call_kind = LocationSummary::kCallOnSlowPath;
3343 break;
3344 }
3345
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3347 locations->SetInAt(0, Location::RequiresRegister());
3348 locations->SetInAt(1, Location::RequiresRegister());
3349 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3350 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003351 // When read barriers are enabled, we need an additional temporary
3352 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003353 if (TypeCheckNeedsATemporary(type_check_kind)) {
3354 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 }
3356}
3357
3358void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003359 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003360 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003361 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 Register obj = InputRegisterAt(instruction, 0);
3363 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003364 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003365 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3366 locations->GetTemp(1) :
3367 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003369 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3370 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3371 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3372 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003374 bool is_type_check_slow_path_fatal =
3375 (type_check_kind == TypeCheckKind::kExactCheck ||
3376 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3377 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3378 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3379 !instruction->CanThrowIntoCatchBlock();
3380 SlowPathCodeARM64* type_check_slow_path =
3381 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3382 is_type_check_slow_path_fatal);
3383 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384
Scott Wakeling97c72b72016-06-24 16:19:36 +01003385 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003386 // Avoid null check if we know obj is not null.
3387 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003388 __ Cbz(obj, &done);
3389 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003390
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003391 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003392 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003393
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003395 case TypeCheckKind::kExactCheck:
3396 case TypeCheckKind::kArrayCheck: {
3397 __ Cmp(temp, cls);
3398 // Jump to slow path for throwing the exception or doing a
3399 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003401 break;
3402 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003403
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003404 case TypeCheckKind::kAbstractClassCheck: {
3405 // If the class is abstract, we eagerly fetch the super class of the
3406 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003407 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003408 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003409 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003410 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003411
3412 // If the class reference currently in `temp` is not null, jump
3413 // to the `compare_classes` label to compare it with the checked
3414 // class.
3415 __ Cbnz(temp, &compare_classes);
3416 // Otherwise, jump to the slow path to throw the exception.
3417 //
3418 // But before, move back the object's class into `temp` before
3419 // going into the slow path, as it has been overwritten in the
3420 // meantime.
3421 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003422 GenerateReferenceLoadTwoRegisters(
3423 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003424 __ B(type_check_slow_path->GetEntryLabel());
3425
3426 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003427 __ Cmp(temp, cls);
3428 __ B(ne, &loop);
3429 break;
3430 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003432 case TypeCheckKind::kClassHierarchyCheck: {
3433 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003434 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435 __ Bind(&loop);
3436 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003437 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003439 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003440 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003441
3442 // If the class reference currently in `temp` is not null, jump
3443 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003444 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003445 // Otherwise, jump to the slow path to throw the exception.
3446 //
3447 // But before, move back the object's class into `temp` before
3448 // going into the slow path, as it has been overwritten in the
3449 // meantime.
3450 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003451 GenerateReferenceLoadTwoRegisters(
3452 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003454 break;
3455 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003456
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003457 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003458 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003459 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003460 __ Cmp(temp, cls);
3461 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003462
3463 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003464 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003465 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003466
3467 // If the component type is not null (i.e. the object is indeed
3468 // an array), jump to label `check_non_primitive_component_type`
3469 // to further check that this component type is not a primitive
3470 // type.
3471 __ Cbnz(temp, &check_non_primitive_component_type);
3472 // Otherwise, jump to the slow path to throw the exception.
3473 //
3474 // But before, move back the object's class into `temp` before
3475 // going into the slow path, as it has been overwritten in the
3476 // meantime.
3477 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003478 GenerateReferenceLoadTwoRegisters(
3479 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003480 __ B(type_check_slow_path->GetEntryLabel());
3481
3482 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003483 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3484 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003485 __ Cbz(temp, &done);
3486 // Same comment as above regarding `temp` and the slow path.
3487 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003488 GenerateReferenceLoadTwoRegisters(
3489 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003490 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003491 break;
3492 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003493
Calin Juravle98893e12015-10-02 21:05:03 +01003494 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003496 // We always go into the type check slow path for the unresolved
3497 // and interface check cases.
3498 //
3499 // We cannot directly call the CheckCast runtime entry point
3500 // without resorting to a type checking slow path here (i.e. by
3501 // calling InvokeRuntime directly), as it would require to
3502 // assign fixed registers for the inputs of this HInstanceOf
3503 // instruction (following the runtime calling convention), which
3504 // might be cluttered by the potential first read barrier
3505 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003506 //
3507 // TODO: Introduce a new runtime entry point taking the object
3508 // to test (instead of its class) as argument, and let it deal
3509 // with the read barrier issues. This will let us refactor this
3510 // case of the `switch` code as it was previously (with a direct
3511 // call to the runtime not using a type checking slow path).
3512 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003513 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003514 break;
3515 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003516 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003517
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003518 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003519}
3520
Alexandre Rames5319def2014-10-23 10:03:10 +01003521void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3523 locations->SetOut(Location::ConstantLocation(constant));
3524}
3525
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003526void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003527 // Will be generated at use site.
3528}
3529
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003530void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3531 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3532 locations->SetOut(Location::ConstantLocation(constant));
3533}
3534
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003535void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003536 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003537}
3538
Calin Juravle175dc732015-08-25 15:42:32 +01003539void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3540 // The trampoline uses the same calling convention as dex calling conventions,
3541 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3542 // the method_idx.
3543 HandleInvoke(invoke);
3544}
3545
3546void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3547 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3548}
3549
Alexandre Rames5319def2014-10-23 10:03:10 +01003550void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003551 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003552 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003553}
3554
Alexandre Rames67555f72014-11-18 10:55:16 +00003555void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3556 HandleInvoke(invoke);
3557}
3558
3559void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3560 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003561 LocationSummary* locations = invoke->GetLocations();
3562 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003563 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003564 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003565 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003566
3567 // The register ip1 is required to be used for the hidden argument in
3568 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003569 MacroAssembler* masm = GetVIXLAssembler();
3570 UseScratchRegisterScope scratch_scope(masm);
3571 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003572 scratch_scope.Exclude(ip1);
3573 __ Mov(ip1, invoke->GetDexMethodIndex());
3574
Alexandre Rames67555f72014-11-18 10:55:16 +00003575 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003576 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003577 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003578 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003579 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003580 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003581 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003582 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003583 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003584 // Instead of simply (possibly) unpoisoning `temp` here, we should
3585 // emit a read barrier for the previous class reference load.
3586 // However this is not required in practice, as this is an
3587 // intermediate/temporary reference and because the current
3588 // concurrent copying collector keeps the from-space memory
3589 // intact/accessible until the end of the marking phase (the
3590 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003591 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003592 __ Ldr(temp,
3593 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3594 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003595 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003596 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003597 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003598 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003599 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003600 // lr();
3601 __ Blr(lr);
3602 DCHECK(!codegen_->IsLeafMethod());
3603 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3604}
3605
3606void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003607 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3608 if (intrinsic.TryDispatch(invoke)) {
3609 return;
3610 }
3611
Alexandre Rames67555f72014-11-18 10:55:16 +00003612 HandleInvoke(invoke);
3613}
3614
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003615void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003616 // Explicit clinit checks triggered by static invokes must have been pruned by
3617 // art::PrepareForRegisterAllocation.
3618 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003619
Andreas Gampe878d58c2015-01-15 23:24:00 -08003620 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3621 if (intrinsic.TryDispatch(invoke)) {
3622 return;
3623 }
3624
Alexandre Rames67555f72014-11-18 10:55:16 +00003625 HandleInvoke(invoke);
3626}
3627
Andreas Gampe878d58c2015-01-15 23:24:00 -08003628static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3629 if (invoke->GetLocations()->Intrinsified()) {
3630 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3631 intrinsic.Dispatch(invoke);
3632 return true;
3633 }
3634 return false;
3635}
3636
Vladimir Markodc151b22015-10-15 18:02:30 +01003637HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3638 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003639 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003640 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003641 return desired_dispatch_info;
3642}
3643
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003644void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003645 // For better instruction scheduling we load the direct code pointer before the method pointer.
3646 bool direct_code_loaded = false;
3647 switch (invoke->GetCodePtrLocation()) {
3648 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3649 // LR = code address from literal pool with link-time patch.
3650 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3651 direct_code_loaded = true;
3652 break;
3653 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3654 // LR = invoke->GetDirectCodePtr();
3655 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3656 direct_code_loaded = true;
3657 break;
3658 default:
3659 break;
3660 }
3661
Andreas Gampe878d58c2015-01-15 23:24:00 -08003662 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003663 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3664 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003665 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
3666 uint32_t offset =
3667 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003668 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003669 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00003670 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003671 }
Vladimir Marko58155012015-08-19 12:49:41 +00003672 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003673 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003674 break;
3675 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3676 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003677 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003678 break;
3679 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3680 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003681 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003682 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3683 break;
3684 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3685 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003686 const DexFile& dex_file = invoke->GetDexFile();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003687 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003688 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003689 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003690 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003691 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003692 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00003693 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00003694 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003695 }
Vladimir Marko58155012015-08-19 12:49:41 +00003696 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003697 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003698 Register reg = XRegisterFrom(temp);
3699 Register method_reg;
3700 if (current_method.IsRegister()) {
3701 method_reg = XRegisterFrom(current_method);
3702 } else {
3703 DCHECK(invoke->GetLocations()->Intrinsified());
3704 DCHECK(!current_method.IsValid());
3705 method_reg = reg;
3706 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3707 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003708
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003709 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003710 __ Ldr(reg.X(),
3711 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003712 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003713 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003714 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3715 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003716 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3717 break;
3718 }
3719 }
3720
3721 switch (invoke->GetCodePtrLocation()) {
3722 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3723 __ Bl(&frame_entry_label_);
3724 break;
3725 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00003726 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
3727 invoke->GetTargetMethod().dex_method_index);
Scott Wakeling97c72b72016-06-24 16:19:36 +01003728 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3729 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003730 __ Bind(label);
3731 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003732 break;
3733 }
3734 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3735 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3736 // LR prepared above for better instruction scheduling.
3737 DCHECK(direct_code_loaded);
3738 // lr()
3739 __ Blr(lr);
3740 break;
3741 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3742 // LR = callee_method->entry_point_from_quick_compiled_code_;
3743 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003744 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003745 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003746 // lr()
3747 __ Blr(lr);
3748 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003749 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003750
Andreas Gampe878d58c2015-01-15 23:24:00 -08003751 DCHECK(!IsLeafMethod());
3752}
3753
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003754void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003755 // Use the calling convention instead of the location of the receiver, as
3756 // intrinsics may have put the receiver in a different register. In the intrinsics
3757 // slow path, the arguments have been moved to the right place, so here we are
3758 // guaranteed that the receiver is the first register of the calling convention.
3759 InvokeDexCallingConvention calling_convention;
3760 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003761 Register temp = XRegisterFrom(temp_in);
3762 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3763 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3764 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003765 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003766
3767 BlockPoolsScope block_pools(GetVIXLAssembler());
3768
3769 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003770 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003771 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003772 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003773 // Instead of simply (possibly) unpoisoning `temp` here, we should
3774 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003775 // intermediate/temporary reference and because the current
3776 // concurrent copying collector keeps the from-space memory
3777 // intact/accessible until the end of the marking phase (the
3778 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003779 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3780 // temp = temp->GetMethodAt(method_offset);
3781 __ Ldr(temp, MemOperand(temp, method_offset));
3782 // lr = temp->GetEntryPoint();
3783 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3784 // lr();
3785 __ Blr(lr);
3786}
3787
Scott Wakeling97c72b72016-06-24 16:19:36 +01003788vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3789 const DexFile& dex_file,
3790 uint32_t string_index,
3791 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003792 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3793}
3794
Scott Wakeling97c72b72016-06-24 16:19:36 +01003795vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3796 const DexFile& dex_file,
3797 uint32_t type_index,
3798 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003799 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3800}
3801
Scott Wakeling97c72b72016-06-24 16:19:36 +01003802vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3803 const DexFile& dex_file,
3804 uint32_t element_offset,
3805 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003806 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3807}
3808
Scott Wakeling97c72b72016-06-24 16:19:36 +01003809vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3810 const DexFile& dex_file,
3811 uint32_t offset_or_index,
3812 vixl::aarch64::Label* adrp_label,
3813 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003814 // Add a patch entry and return the label.
3815 patches->emplace_back(dex_file, offset_or_index);
3816 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003817 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003818 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3819 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3820 return label;
3821}
3822
Scott Wakeling97c72b72016-06-24 16:19:36 +01003823vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003824 const DexFile& dex_file, uint32_t string_index) {
3825 return boot_image_string_patches_.GetOrCreate(
3826 StringReference(&dex_file, string_index),
3827 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3828}
3829
Scott Wakeling97c72b72016-06-24 16:19:36 +01003830vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003831 const DexFile& dex_file, uint32_t type_index) {
3832 return boot_image_type_patches_.GetOrCreate(
3833 TypeReference(&dex_file, type_index),
3834 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3835}
3836
Scott Wakeling97c72b72016-06-24 16:19:36 +01003837vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3838 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003839 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3840 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3841 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3842}
3843
Scott Wakeling97c72b72016-06-24 16:19:36 +01003844vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3845 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003846 return DeduplicateUint64Literal(address);
3847}
3848
Vladimir Markoaad75c62016-10-03 08:46:48 +00003849void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
3850 vixl::aarch64::Register reg) {
3851 DCHECK(reg.IsX());
3852 SingleEmissionCheckScope guard(GetVIXLAssembler());
3853 __ Bind(fixup_label);
3854 __ adrp(reg, /* offset placeholder */ 0);
3855}
3856
3857void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
3858 vixl::aarch64::Register out,
3859 vixl::aarch64::Register base) {
3860 DCHECK(out.IsX());
3861 DCHECK(base.IsX());
3862 SingleEmissionCheckScope guard(GetVIXLAssembler());
3863 __ Bind(fixup_label);
3864 __ add(out, base, Operand(/* offset placeholder */ 0));
3865}
3866
3867void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
3868 vixl::aarch64::Register out,
3869 vixl::aarch64::Register base) {
3870 DCHECK(base.IsX());
3871 SingleEmissionCheckScope guard(GetVIXLAssembler());
3872 __ Bind(fixup_label);
3873 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
3874}
3875
3876template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
3877inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
3878 const ArenaDeque<PcRelativePatchInfo>& infos,
3879 ArenaVector<LinkerPatch>* linker_patches) {
3880 for (const PcRelativePatchInfo& info : infos) {
3881 linker_patches->push_back(Factory(info.label.GetLocation(),
3882 &info.target_dex_file,
3883 info.pc_insn_label->GetLocation(),
3884 info.offset_or_index));
3885 }
3886}
3887
Vladimir Marko58155012015-08-19 12:49:41 +00003888void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3889 DCHECK(linker_patches->empty());
3890 size_t size =
3891 method_patches_.size() +
3892 call_patches_.size() +
3893 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003894 pc_relative_dex_cache_patches_.size() +
3895 boot_image_string_patches_.size() +
3896 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003897 boot_image_type_patches_.size() +
3898 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003899 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003900 linker_patches->reserve(size);
3901 for (const auto& entry : method_patches_) {
3902 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003903 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3904 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003905 target_method.dex_file,
3906 target_method.dex_method_index));
3907 }
3908 for (const auto& entry : call_patches_) {
3909 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003910 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3911 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003912 target_method.dex_file,
3913 target_method.dex_method_index));
3914 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003915 for (const PatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3916 linker_patches->push_back(
3917 LinkerPatch::RelativeCodePatch(info.label.GetLocation(), &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00003918 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003919 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003920 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003921 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003922 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003923 info.offset_or_index));
3924 }
3925 for (const auto& entry : boot_image_string_patches_) {
3926 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003927 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3928 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003929 target_string.dex_file,
3930 target_string.string_index));
3931 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003932 if (!GetCompilerOptions().IsBootImage()) {
3933 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
3934 linker_patches);
3935 } else {
3936 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
3937 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003938 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003939 for (const auto& entry : boot_image_type_patches_) {
3940 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003941 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3942 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003943 target_type.dex_file,
3944 target_type.type_index));
3945 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00003946 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
3947 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003948 for (const auto& entry : boot_image_address_patches_) {
3949 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003950 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3951 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003952 }
3953}
3954
Scott Wakeling97c72b72016-06-24 16:19:36 +01003955vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003956 Uint32ToLiteralMap* map) {
3957 return map->GetOrCreate(
3958 value,
3959 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3960}
3961
Scott Wakeling97c72b72016-06-24 16:19:36 +01003962vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003963 return uint64_literals_.GetOrCreate(
3964 value,
3965 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003966}
3967
Scott Wakeling97c72b72016-06-24 16:19:36 +01003968vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003969 MethodReference target_method,
3970 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003971 return map->GetOrCreate(
3972 target_method,
3973 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003974}
3975
Scott Wakeling97c72b72016-06-24 16:19:36 +01003976vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003977 MethodReference target_method) {
3978 return DeduplicateMethodLiteral(target_method, &method_patches_);
3979}
3980
Scott Wakeling97c72b72016-06-24 16:19:36 +01003981vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003982 MethodReference target_method) {
3983 return DeduplicateMethodLiteral(target_method, &call_patches_);
3984}
3985
3986
Andreas Gampe878d58c2015-01-15 23:24:00 -08003987void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003988 // Explicit clinit checks triggered by static invokes must have been pruned by
3989 // art::PrepareForRegisterAllocation.
3990 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003991
Andreas Gampe878d58c2015-01-15 23:24:00 -08003992 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3993 return;
3994 }
3995
Alexandre Ramesd921d642015-04-16 15:07:16 +01003996 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003997 LocationSummary* locations = invoke->GetLocations();
3998 codegen_->GenerateStaticOrDirectCall(
3999 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004000 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004001}
4002
4003void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004004 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4005 return;
4006 }
4007
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004008 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004009 DCHECK(!codegen_->IsLeafMethod());
4010 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4011}
4012
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004013HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4014 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004015 switch (desired_class_load_kind) {
4016 case HLoadClass::LoadKind::kReferrersClass:
4017 break;
4018 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4019 DCHECK(!GetCompilerOptions().GetCompilePic());
4020 break;
4021 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4022 DCHECK(GetCompilerOptions().GetCompilePic());
4023 break;
4024 case HLoadClass::LoadKind::kBootImageAddress:
4025 break;
4026 case HLoadClass::LoadKind::kDexCacheAddress:
4027 DCHECK(Runtime::Current()->UseJitCompilation());
4028 break;
4029 case HLoadClass::LoadKind::kDexCachePcRelative:
4030 DCHECK(!Runtime::Current()->UseJitCompilation());
4031 break;
4032 case HLoadClass::LoadKind::kDexCacheViaMethod:
4033 break;
4034 }
4035 return desired_class_load_kind;
4036}
4037
Alexandre Rames67555f72014-11-18 10:55:16 +00004038void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004039 if (cls->NeedsAccessCheck()) {
4040 InvokeRuntimeCallingConvention calling_convention;
4041 CodeGenerator::CreateLoadClassLocationSummary(
4042 cls,
4043 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004044 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004045 /* code_generator_supports_read_barrier */ true);
4046 return;
4047 }
4048
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004049 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4050 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004051 ? LocationSummary::kCallOnSlowPath
4052 : LocationSummary::kNoCall;
4053 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004054 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004055 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004056 }
4057
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004058 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4059 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4060 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4061 locations->SetInAt(0, Location::RequiresRegister());
4062 }
4063 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004064}
4065
4066void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004067 if (cls->NeedsAccessCheck()) {
4068 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004069 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004070 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004071 return;
4072 }
4073
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004074 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004075 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004076
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004077 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004078 bool generate_null_check = false;
4079 switch (cls->GetLoadKind()) {
4080 case HLoadClass::LoadKind::kReferrersClass: {
4081 DCHECK(!cls->CanCallRuntime());
4082 DCHECK(!cls->MustGenerateClinitCheck());
4083 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4084 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004085 GenerateGcRootFieldLoad(cls,
4086 out_loc,
4087 current_method,
4088 ArtMethod::DeclaringClassOffset().Int32Value(),
4089 /*fixup_label*/ nullptr,
4090 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004091 break;
4092 }
4093 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004094 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004095 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4096 cls->GetTypeIndex()));
4097 break;
4098 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004099 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004100 // Add ADRP with its PC-relative type patch.
4101 const DexFile& dex_file = cls->GetDexFile();
4102 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004103 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004104 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004105 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004106 vixl::aarch64::Label* add_label =
4107 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004108 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004109 break;
4110 }
4111 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004112 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004113 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4114 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4115 break;
4116 }
4117 case HLoadClass::LoadKind::kDexCacheAddress: {
4118 DCHECK_NE(cls->GetAddress(), 0u);
4119 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4120 // that gives a 16KiB range. To try and reduce the number of literals if we load
4121 // multiple types, simply split the dex cache address to a 16KiB aligned base
4122 // loaded from a literal and the remaining offset embedded in the load.
4123 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4124 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4125 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4126 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4127 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4128 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4129 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004130 GenerateGcRootFieldLoad(cls,
4131 out_loc,
4132 out.X(),
4133 offset,
4134 /*fixup_label*/ nullptr,
4135 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004136 generate_null_check = !cls->IsInDexCache();
4137 break;
4138 }
4139 case HLoadClass::LoadKind::kDexCachePcRelative: {
4140 // Add ADRP with its PC-relative DexCache access patch.
4141 const DexFile& dex_file = cls->GetDexFile();
4142 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004143 vixl::aarch64::Label* adrp_label =
4144 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004145 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004146 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004147 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004148 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4149 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004150 GenerateGcRootFieldLoad(cls,
4151 out_loc,
4152 out.X(),
4153 /* offset placeholder */ 0,
4154 ldr_label,
4155 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004156 generate_null_check = !cls->IsInDexCache();
4157 break;
4158 }
4159 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4160 MemberOffset resolved_types_offset =
4161 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4162 // /* GcRoot<mirror::Class>[] */ out =
4163 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4164 Register current_method = InputRegisterAt(cls, 0);
4165 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4166 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004167 GenerateGcRootFieldLoad(cls,
4168 out_loc,
4169 out.X(),
4170 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4171 /*fixup_label*/ nullptr,
4172 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004173 generate_null_check = !cls->IsInDexCache();
4174 break;
4175 }
4176 }
4177
4178 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4179 DCHECK(cls->CanCallRuntime());
4180 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4181 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4182 codegen_->AddSlowPath(slow_path);
4183 if (generate_null_check) {
4184 __ Cbz(out, slow_path->GetEntryLabel());
4185 }
4186 if (cls->MustGenerateClinitCheck()) {
4187 GenerateClassInitializationCheck(slow_path, out);
4188 } else {
4189 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004190 }
4191 }
4192}
4193
David Brazdilcb1c0552015-08-04 16:22:25 +01004194static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004195 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004196}
4197
Alexandre Rames67555f72014-11-18 10:55:16 +00004198void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4199 LocationSummary* locations =
4200 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4201 locations->SetOut(Location::RequiresRegister());
4202}
4203
4204void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004205 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4206}
4207
4208void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4209 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4210}
4211
4212void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4213 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004214}
4215
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004216HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4217 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004218 switch (desired_string_load_kind) {
4219 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4220 DCHECK(!GetCompilerOptions().GetCompilePic());
4221 break;
4222 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4223 DCHECK(GetCompilerOptions().GetCompilePic());
4224 break;
4225 case HLoadString::LoadKind::kBootImageAddress:
4226 break;
4227 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004228 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004229 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004230 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004231 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004232 break;
4233 case HLoadString::LoadKind::kDexCacheViaMethod:
4234 break;
4235 }
4236 return desired_string_load_kind;
4237}
4238
Alexandre Rames67555f72014-11-18 10:55:16 +00004239void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004240 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00004241 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4242 ? LocationSummary::kCallOnMainOnly
4243 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004244 : LocationSummary::kNoCall;
4245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004246 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4247 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004248 InvokeRuntimeCallingConvention calling_convention;
4249 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4250 } else {
4251 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004252 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004253}
4254
4255void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004256 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004258 switch (load->GetLoadKind()) {
4259 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4261 load->GetStringIndex()));
4262 return; // No dex cache slow path.
4263 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004264 // Add ADRP with its PC-relative String patch.
4265 const DexFile& dex_file = load->GetDexFile();
4266 uint32_t string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004267 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004268 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004269 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004270 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004271 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004272 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004273 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004274 return; // No dex cache slow path.
4275 }
4276 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004277 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4278 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4279 return; // No dex cache slow path.
4280 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004281 case HLoadString::LoadKind::kBssEntry: {
4282 // Add ADRP with its PC-relative String .bss entry patch.
4283 const DexFile& dex_file = load->GetDexFile();
4284 uint32_t string_index = load->GetStringIndex();
4285 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4286 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4287 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4288 // Add LDR with its PC-relative String patch.
4289 vixl::aarch64::Label* ldr_label =
4290 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4291 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4292 GenerateGcRootFieldLoad(load,
4293 load->GetLocations()->Out(),
4294 out.X(),
4295 /* placeholder */ 0u,
4296 ldr_label);
4297 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4298 codegen_->AddSlowPath(slow_path);
4299 __ Cbz(out.X(), slow_path->GetEntryLabel());
4300 __ Bind(slow_path->GetExitLabel());
4301 return;
4302 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004303 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004304 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004305 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004306
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004307 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004308 InvokeRuntimeCallingConvention calling_convention;
4309 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex());
4310 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4311 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004312}
4313
Alexandre Rames5319def2014-10-23 10:03:10 +01004314void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4316 locations->SetOut(Location::ConstantLocation(constant));
4317}
4318
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004319void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004320 // Will be generated at use site.
4321}
4322
Alexandre Rames67555f72014-11-18 10:55:16 +00004323void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4324 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004326 InvokeRuntimeCallingConvention calling_convention;
4327 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4328}
4329
4330void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004331 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4332 instruction,
4333 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004334 if (instruction->IsEnter()) {
4335 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4336 } else {
4337 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4338 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004339}
4340
Alexandre Rames42d641b2014-10-27 14:00:51 +00004341void LocationsBuilderARM64::VisitMul(HMul* mul) {
4342 LocationSummary* locations =
4343 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4344 switch (mul->GetResultType()) {
4345 case Primitive::kPrimInt:
4346 case Primitive::kPrimLong:
4347 locations->SetInAt(0, Location::RequiresRegister());
4348 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004350 break;
4351
4352 case Primitive::kPrimFloat:
4353 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004354 locations->SetInAt(0, Location::RequiresFpuRegister());
4355 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004356 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004357 break;
4358
4359 default:
4360 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4361 }
4362}
4363
4364void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4365 switch (mul->GetResultType()) {
4366 case Primitive::kPrimInt:
4367 case Primitive::kPrimLong:
4368 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4369 break;
4370
4371 case Primitive::kPrimFloat:
4372 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004373 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004374 break;
4375
4376 default:
4377 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4378 }
4379}
4380
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004381void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4382 LocationSummary* locations =
4383 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4384 switch (neg->GetResultType()) {
4385 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004386 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004387 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004389 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004390
4391 case Primitive::kPrimFloat:
4392 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004393 locations->SetInAt(0, Location::RequiresFpuRegister());
4394 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004395 break;
4396
4397 default:
4398 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4399 }
4400}
4401
4402void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4403 switch (neg->GetResultType()) {
4404 case Primitive::kPrimInt:
4405 case Primitive::kPrimLong:
4406 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4407 break;
4408
4409 case Primitive::kPrimFloat:
4410 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004411 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004412 break;
4413
4414 default:
4415 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4416 }
4417}
4418
4419void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4420 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004422 InvokeRuntimeCallingConvention calling_convention;
4423 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004424 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004425 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004426 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004427}
4428
4429void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4430 LocationSummary* locations = instruction->GetLocations();
4431 InvokeRuntimeCallingConvention calling_convention;
4432 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4433 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004434 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004435 // Note: if heap poisoning is enabled, the entry point takes cares
4436 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004437 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004438 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004439}
4440
Alexandre Rames5319def2014-10-23 10:03:10 +01004441void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4442 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004444 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004445 if (instruction->IsStringAlloc()) {
4446 locations->AddTemp(LocationFrom(kArtMethodRegister));
4447 } else {
4448 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4449 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4450 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004451 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4452}
4453
4454void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004455 // Note: if heap poisoning is enabled, the entry point takes cares
4456 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004457 if (instruction->IsStringAlloc()) {
4458 // String is allocated through StringFactory. Call NewEmptyString entry point.
4459 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004460 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004461 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4462 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4463 __ Blr(lr);
4464 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4465 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004466 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004467 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4468 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004469}
4470
4471void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4472 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004473 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004475}
4476
4477void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004478 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004479 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004480 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004481 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004482 break;
4483
4484 default:
4485 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4486 }
4487}
4488
David Brazdil66d126e2015-04-03 16:02:44 +01004489void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4491 locations->SetInAt(0, Location::RequiresRegister());
4492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4493}
4494
4495void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004496 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004497}
4498
Alexandre Rames5319def2014-10-23 10:03:10 +01004499void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004500 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4501 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004502}
4503
Calin Juravle2ae48182016-03-16 14:05:09 +00004504void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4505 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004506 return;
4507 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004508
Alexandre Ramesd921d642015-04-16 15:07:16 +01004509 BlockPoolsScope block_pools(GetVIXLAssembler());
4510 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004511 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004512 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004513}
4514
Calin Juravle2ae48182016-03-16 14:05:09 +00004515void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004516 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004517 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004518
4519 LocationSummary* locations = instruction->GetLocations();
4520 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004521
4522 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004523}
4524
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004525void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004526 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004527}
4528
Alexandre Rames67555f72014-11-18 10:55:16 +00004529void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4530 HandleBinaryOp(instruction);
4531}
4532
4533void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4534 HandleBinaryOp(instruction);
4535}
4536
Alexandre Rames3e69f162014-12-10 10:36:50 +00004537void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4538 LOG(FATAL) << "Unreachable";
4539}
4540
4541void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4542 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4543}
4544
Alexandre Rames5319def2014-10-23 10:03:10 +01004545void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4547 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4548 if (location.IsStackSlot()) {
4549 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4550 } else if (location.IsDoubleStackSlot()) {
4551 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4552 }
4553 locations->SetOut(location);
4554}
4555
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004556void InstructionCodeGeneratorARM64::VisitParameterValue(
4557 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004558 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004559}
4560
4561void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4562 LocationSummary* locations =
4563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004564 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004565}
4566
4567void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4568 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4569 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004570}
4571
4572void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004574 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004575 locations->SetInAt(i, Location::Any());
4576 }
4577 locations->SetOut(Location::Any());
4578}
4579
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004580void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004581 LOG(FATAL) << "Unreachable";
4582}
4583
Serban Constantinescu02164b32014-11-13 14:05:07 +00004584void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004585 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004586 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004587 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4588 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4590
4591 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004592 case Primitive::kPrimInt:
4593 case Primitive::kPrimLong:
4594 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004595 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4597 break;
4598
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004599 case Primitive::kPrimFloat:
4600 case Primitive::kPrimDouble: {
4601 InvokeRuntimeCallingConvention calling_convention;
4602 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4603 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4604 locations->SetOut(calling_convention.GetReturnLocation(type));
4605
4606 break;
4607 }
4608
Serban Constantinescu02164b32014-11-13 14:05:07 +00004609 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004610 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004611 }
4612}
4613
4614void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4615 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004616
Serban Constantinescu02164b32014-11-13 14:05:07 +00004617 switch (type) {
4618 case Primitive::kPrimInt:
4619 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004620 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004621 break;
4622 }
4623
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004624 case Primitive::kPrimFloat:
4625 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004626 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4627 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004628 if (type == Primitive::kPrimFloat) {
4629 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4630 } else {
4631 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4632 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004633 break;
4634 }
4635
Serban Constantinescu02164b32014-11-13 14:05:07 +00004636 default:
4637 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004638 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004639 }
4640}
4641
Calin Juravle27df7582015-04-17 19:12:31 +01004642void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4643 memory_barrier->SetLocations(nullptr);
4644}
4645
4646void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004647 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004648}
4649
Alexandre Rames5319def2014-10-23 10:03:10 +01004650void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4652 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004653 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004654}
4655
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004656void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004657 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004658}
4659
4660void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4661 instruction->SetLocations(nullptr);
4662}
4663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004664void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004665 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004666}
4667
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004668void LocationsBuilderARM64::VisitRor(HRor* ror) {
4669 HandleBinaryOp(ror);
4670}
4671
4672void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4673 HandleBinaryOp(ror);
4674}
4675
Serban Constantinescu02164b32014-11-13 14:05:07 +00004676void LocationsBuilderARM64::VisitShl(HShl* shl) {
4677 HandleShift(shl);
4678}
4679
4680void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4681 HandleShift(shl);
4682}
4683
4684void LocationsBuilderARM64::VisitShr(HShr* shr) {
4685 HandleShift(shr);
4686}
4687
4688void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4689 HandleShift(shr);
4690}
4691
Alexandre Rames5319def2014-10-23 10:03:10 +01004692void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004693 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004694}
4695
4696void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004697 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004698}
4699
Alexandre Rames67555f72014-11-18 10:55:16 +00004700void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004701 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004702}
4703
4704void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004705 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004706}
4707
4708void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004709 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004710}
4711
Alexandre Rames67555f72014-11-18 10:55:16 +00004712void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004713 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004714}
4715
Calin Juravlee460d1d2015-09-29 04:52:17 +01004716void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4717 HUnresolvedInstanceFieldGet* instruction) {
4718 FieldAccessCallingConventionARM64 calling_convention;
4719 codegen_->CreateUnresolvedFieldLocationSummary(
4720 instruction, instruction->GetFieldType(), calling_convention);
4721}
4722
4723void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4724 HUnresolvedInstanceFieldGet* instruction) {
4725 FieldAccessCallingConventionARM64 calling_convention;
4726 codegen_->GenerateUnresolvedFieldAccess(instruction,
4727 instruction->GetFieldType(),
4728 instruction->GetFieldIndex(),
4729 instruction->GetDexPc(),
4730 calling_convention);
4731}
4732
4733void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4734 HUnresolvedInstanceFieldSet* instruction) {
4735 FieldAccessCallingConventionARM64 calling_convention;
4736 codegen_->CreateUnresolvedFieldLocationSummary(
4737 instruction, instruction->GetFieldType(), calling_convention);
4738}
4739
4740void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4741 HUnresolvedInstanceFieldSet* instruction) {
4742 FieldAccessCallingConventionARM64 calling_convention;
4743 codegen_->GenerateUnresolvedFieldAccess(instruction,
4744 instruction->GetFieldType(),
4745 instruction->GetFieldIndex(),
4746 instruction->GetDexPc(),
4747 calling_convention);
4748}
4749
4750void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4751 HUnresolvedStaticFieldGet* instruction) {
4752 FieldAccessCallingConventionARM64 calling_convention;
4753 codegen_->CreateUnresolvedFieldLocationSummary(
4754 instruction, instruction->GetFieldType(), calling_convention);
4755}
4756
4757void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4758 HUnresolvedStaticFieldGet* instruction) {
4759 FieldAccessCallingConventionARM64 calling_convention;
4760 codegen_->GenerateUnresolvedFieldAccess(instruction,
4761 instruction->GetFieldType(),
4762 instruction->GetFieldIndex(),
4763 instruction->GetDexPc(),
4764 calling_convention);
4765}
4766
4767void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4768 HUnresolvedStaticFieldSet* instruction) {
4769 FieldAccessCallingConventionARM64 calling_convention;
4770 codegen_->CreateUnresolvedFieldLocationSummary(
4771 instruction, instruction->GetFieldType(), calling_convention);
4772}
4773
4774void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4775 HUnresolvedStaticFieldSet* instruction) {
4776 FieldAccessCallingConventionARM64 calling_convention;
4777 codegen_->GenerateUnresolvedFieldAccess(instruction,
4778 instruction->GetFieldType(),
4779 instruction->GetFieldIndex(),
4780 instruction->GetDexPc(),
4781 calling_convention);
4782}
4783
Alexandre Rames5319def2014-10-23 10:03:10 +01004784void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004785 LocationSummary* locations =
4786 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004787 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004788}
4789
4790void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004791 HBasicBlock* block = instruction->GetBlock();
4792 if (block->GetLoopInformation() != nullptr) {
4793 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4794 // The back edge will generate the suspend check.
4795 return;
4796 }
4797 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4798 // The goto will generate the suspend check.
4799 return;
4800 }
4801 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004802}
4803
Alexandre Rames67555f72014-11-18 10:55:16 +00004804void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4805 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004807 InvokeRuntimeCallingConvention calling_convention;
4808 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4809}
4810
4811void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004812 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004813 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004814}
4815
4816void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4817 LocationSummary* locations =
4818 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4819 Primitive::Type input_type = conversion->GetInputType();
4820 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004821 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004822 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4823 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4824 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4825 }
4826
Alexandre Rames542361f2015-01-29 16:57:31 +00004827 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004828 locations->SetInAt(0, Location::RequiresFpuRegister());
4829 } else {
4830 locations->SetInAt(0, Location::RequiresRegister());
4831 }
4832
Alexandre Rames542361f2015-01-29 16:57:31 +00004833 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004834 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4835 } else {
4836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4837 }
4838}
4839
4840void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4841 Primitive::Type result_type = conversion->GetResultType();
4842 Primitive::Type input_type = conversion->GetInputType();
4843
4844 DCHECK_NE(input_type, result_type);
4845
Alexandre Rames542361f2015-01-29 16:57:31 +00004846 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004847 int result_size = Primitive::ComponentSize(result_type);
4848 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004849 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004850 Register output = OutputRegister(conversion);
4851 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004852 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004853 // 'int' values are used directly as W registers, discarding the top
4854 // bits, so we don't need to sign-extend and can just perform a move.
4855 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4856 // top 32 bits of the target register. We theoretically could leave those
4857 // bits unchanged, but we would have to make sure that no code uses a
4858 // 32bit input value as a 64bit value assuming that the top 32 bits are
4859 // zero.
4860 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004861 } else if (result_type == Primitive::kPrimChar ||
4862 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4863 __ Ubfx(output,
4864 output.IsX() ? source.X() : source.W(),
4865 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004866 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004867 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004868 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004869 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004870 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004871 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004872 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4873 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004874 } else if (Primitive::IsFloatingPointType(result_type) &&
4875 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004876 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4877 } else {
4878 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4879 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004880 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004881}
Alexandre Rames67555f72014-11-18 10:55:16 +00004882
Serban Constantinescu02164b32014-11-13 14:05:07 +00004883void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4884 HandleShift(ushr);
4885}
4886
4887void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4888 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004889}
4890
4891void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4892 HandleBinaryOp(instruction);
4893}
4894
4895void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4896 HandleBinaryOp(instruction);
4897}
4898
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004899void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004900 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004901 LOG(FATAL) << "Unreachable";
4902}
4903
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004904void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004905 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004906 LOG(FATAL) << "Unreachable";
4907}
4908
Mark Mendellfe57faa2015-09-18 09:26:15 -04004909// Simple implementation of packed switch - generate cascaded compare/jumps.
4910void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4911 LocationSummary* locations =
4912 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4913 locations->SetInAt(0, Location::RequiresRegister());
4914}
4915
4916void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4917 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004918 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004919 Register value_reg = InputRegisterAt(switch_instr, 0);
4920 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4921
Zheng Xu3927c8b2015-11-18 17:46:25 +08004922 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004923 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004924 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4925 // make sure we don't emit it if the target may run out of range.
4926 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4927 // ranges and emit the tables only as required.
4928 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004929
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004930 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004931 // Current instruction id is an upper bound of the number of HIRs in the graph.
4932 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4933 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004934 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4935 Register temp = temps.AcquireW();
4936 __ Subs(temp, value_reg, Operand(lower_bound));
4937
Zheng Xu3927c8b2015-11-18 17:46:25 +08004938 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004939 // Jump to successors[0] if value == lower_bound.
4940 __ B(eq, codegen_->GetLabelOf(successors[0]));
4941 int32_t last_index = 0;
4942 for (; num_entries - last_index > 2; last_index += 2) {
4943 __ Subs(temp, temp, Operand(2));
4944 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4945 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4946 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4947 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4948 }
4949 if (num_entries - last_index == 2) {
4950 // The last missing case_value.
4951 __ Cmp(temp, Operand(1));
4952 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004953 }
4954
4955 // And the default for any other value.
4956 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4957 __ B(codegen_->GetLabelOf(default_block));
4958 }
4959 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004960 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004961
4962 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4963
4964 // Below instructions should use at most one blocked register. Since there are two blocked
4965 // registers, we are free to block one.
4966 Register temp_w = temps.AcquireW();
4967 Register index;
4968 // Remove the bias.
4969 if (lower_bound != 0) {
4970 index = temp_w;
4971 __ Sub(index, value_reg, Operand(lower_bound));
4972 } else {
4973 index = value_reg;
4974 }
4975
4976 // Jump to default block if index is out of the range.
4977 __ Cmp(index, Operand(num_entries));
4978 __ B(hs, codegen_->GetLabelOf(default_block));
4979
4980 // In current VIXL implementation, it won't require any blocked registers to encode the
4981 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4982 // register pressure.
4983 Register table_base = temps.AcquireX();
4984 // Load jump offset from the table.
4985 __ Adr(table_base, jump_table->GetTableStartLabel());
4986 Register jump_offset = temp_w;
4987 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4988
4989 // Jump to target block by branching to table_base(pc related) + offset.
4990 Register target_address = table_base;
4991 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4992 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004993 }
4994}
4995
Roland Levillain44015862016-01-22 11:47:17 +00004996void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4997 Location out,
4998 uint32_t offset,
4999 Location maybe_temp) {
5000 Primitive::Type type = Primitive::kPrimNot;
5001 Register out_reg = RegisterFrom(out, type);
5002 if (kEmitCompilerReadBarrier) {
5003 Register temp_reg = RegisterFrom(maybe_temp, type);
5004 if (kUseBakerReadBarrier) {
5005 // Load with fast path based Baker's read barrier.
5006 // /* HeapReference<Object> */ out = *(out + offset)
5007 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5008 out,
5009 out_reg,
5010 offset,
5011 temp_reg,
5012 /* needs_null_check */ false,
5013 /* use_load_acquire */ false);
5014 } else {
5015 // Load with slow path based read barrier.
5016 // Save the value of `out` into `maybe_temp` before overwriting it
5017 // in the following move operation, as we will need it for the
5018 // read barrier below.
5019 __ Mov(temp_reg, out_reg);
5020 // /* HeapReference<Object> */ out = *(out + offset)
5021 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5022 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5023 }
5024 } else {
5025 // Plain load with no read barrier.
5026 // /* HeapReference<Object> */ out = *(out + offset)
5027 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5028 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5029 }
5030}
5031
5032void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5033 Location out,
5034 Location obj,
5035 uint32_t offset,
5036 Location maybe_temp) {
5037 Primitive::Type type = Primitive::kPrimNot;
5038 Register out_reg = RegisterFrom(out, type);
5039 Register obj_reg = RegisterFrom(obj, type);
5040 if (kEmitCompilerReadBarrier) {
5041 if (kUseBakerReadBarrier) {
5042 // Load with fast path based Baker's read barrier.
5043 Register temp_reg = RegisterFrom(maybe_temp, type);
5044 // /* HeapReference<Object> */ out = *(obj + offset)
5045 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5046 out,
5047 obj_reg,
5048 offset,
5049 temp_reg,
5050 /* needs_null_check */ false,
5051 /* use_load_acquire */ false);
5052 } else {
5053 // Load with slow path based read barrier.
5054 // /* HeapReference<Object> */ out = *(obj + offset)
5055 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5056 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5057 }
5058 } else {
5059 // Plain load with no read barrier.
5060 // /* HeapReference<Object> */ out = *(obj + offset)
5061 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5062 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5063 }
5064}
5065
5066void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5067 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005068 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005069 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005070 vixl::aarch64::Label* fixup_label,
5071 bool requires_read_barrier) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005072 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005073 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005074 if (requires_read_barrier) {
5075 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005076 if (kUseBakerReadBarrier) {
5077 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5078 // Baker's read barrier are used:
5079 //
5080 // root = obj.field;
5081 // if (Thread::Current()->GetIsGcMarking()) {
5082 // root = ReadBarrier::Mark(root)
5083 // }
5084
5085 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005086 if (fixup_label == nullptr) {
5087 __ Ldr(root_reg, MemOperand(obj, offset));
5088 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005089 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 }
Roland Levillain44015862016-01-22 11:47:17 +00005091 static_assert(
5092 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5093 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5094 "have different sizes.");
5095 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5096 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5097 "have different sizes.");
5098
Vladimir Marko953437b2016-08-24 08:30:46 +00005099 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005100 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005101 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005102 codegen_->AddSlowPath(slow_path);
5103
5104 MacroAssembler* masm = GetVIXLAssembler();
5105 UseScratchRegisterScope temps(masm);
5106 Register temp = temps.AcquireW();
5107 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005108 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005109 __ Cbnz(temp, slow_path->GetEntryLabel());
5110 __ Bind(slow_path->GetExitLabel());
5111 } else {
5112 // GC root loaded through a slow path for read barriers other
5113 // than Baker's.
5114 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005115 if (fixup_label == nullptr) {
5116 __ Add(root_reg.X(), obj.X(), offset);
5117 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005118 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005119 }
Roland Levillain44015862016-01-22 11:47:17 +00005120 // /* mirror::Object* */ root = root->Read()
5121 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5122 }
5123 } else {
5124 // Plain GC root load with no read barrier.
5125 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005126 if (fixup_label == nullptr) {
5127 __ Ldr(root_reg, MemOperand(obj, offset));
5128 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005129 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005130 }
Roland Levillain44015862016-01-22 11:47:17 +00005131 // Note that GC roots are not affected by heap poisoning, thus we
5132 // do not have to unpoison `root_reg` here.
5133 }
5134}
5135
5136void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5137 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005138 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005139 uint32_t offset,
5140 Register temp,
5141 bool needs_null_check,
5142 bool use_load_acquire) {
5143 DCHECK(kEmitCompilerReadBarrier);
5144 DCHECK(kUseBakerReadBarrier);
5145
5146 // /* HeapReference<Object> */ ref = *(obj + offset)
5147 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005148 size_t no_scale_factor = 0U;
5149 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5150 ref,
5151 obj,
5152 offset,
5153 no_index,
5154 no_scale_factor,
5155 temp,
5156 needs_null_check,
5157 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005158}
5159
5160void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5161 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005162 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005163 uint32_t data_offset,
5164 Location index,
5165 Register temp,
5166 bool needs_null_check) {
5167 DCHECK(kEmitCompilerReadBarrier);
5168 DCHECK(kUseBakerReadBarrier);
5169
5170 // Array cells are never volatile variables, therefore array loads
5171 // never use Load-Acquire instructions on ARM64.
5172 const bool use_load_acquire = false;
5173
Roland Levillainbfea3352016-06-23 13:48:47 +01005174 static_assert(
5175 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5176 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005177 // /* HeapReference<Object> */ ref =
5178 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005179 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5180 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5181 ref,
5182 obj,
5183 data_offset,
5184 index,
5185 scale_factor,
5186 temp,
5187 needs_null_check,
5188 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005189}
5190
5191void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5192 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005193 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005194 uint32_t offset,
5195 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005196 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005197 Register temp,
5198 bool needs_null_check,
5199 bool use_load_acquire) {
5200 DCHECK(kEmitCompilerReadBarrier);
5201 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005202 // If we are emitting an array load, we should not be using a
5203 // Load Acquire instruction. In other words:
5204 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5205 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005206
5207 MacroAssembler* masm = GetVIXLAssembler();
5208 UseScratchRegisterScope temps(masm);
5209
5210 // In slow path based read barriers, the read barrier call is
5211 // inserted after the original load. However, in fast path based
5212 // Baker's read barriers, we need to perform the load of
5213 // mirror::Object::monitor_ *before* the original reference load.
5214 // This load-load ordering is required by the read barrier.
5215 // The fast path/slow path (for Baker's algorithm) should look like:
5216 //
5217 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5218 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5219 // HeapReference<Object> ref = *src; // Original reference load.
5220 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5221 // if (is_gray) {
5222 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5223 // }
5224 //
5225 // Note: the original implementation in ReadBarrier::Barrier is
5226 // slightly more complex as it performs additional checks that we do
5227 // not do here for performance reasons.
5228
5229 Primitive::Type type = Primitive::kPrimNot;
5230 Register ref_reg = RegisterFrom(ref, type);
5231 DCHECK(obj.IsW());
5232 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5233
5234 // /* int32_t */ monitor = obj->monitor_
5235 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5236 if (needs_null_check) {
5237 MaybeRecordImplicitNullCheck(instruction);
5238 }
5239 // /* LockWord */ lock_word = LockWord(monitor)
5240 static_assert(sizeof(LockWord) == sizeof(int32_t),
5241 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005242
Vladimir Marko877a0332016-07-11 19:30:56 +01005243 // Introduce a dependency on the lock_word including rb_state,
5244 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005245 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005246 // `obj` is unchanged by this operation, but its value now depends
5247 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005248 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005249
5250 // The actual reference load.
5251 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005252 // Load types involving an "index".
5253 if (use_load_acquire) {
5254 // UnsafeGetObjectVolatile intrinsic case.
5255 // Register `index` is not an index in an object array, but an
5256 // offset to an object reference field within object `obj`.
5257 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5258 DCHECK(instruction->GetLocations()->Intrinsified());
5259 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5260 << instruction->AsInvoke()->GetIntrinsic();
5261 DCHECK_EQ(offset, 0U);
5262 DCHECK_EQ(scale_factor, 0U);
5263 DCHECK_EQ(needs_null_check, 0U);
5264 // /* HeapReference<Object> */ ref = *(obj + index)
5265 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5266 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005267 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005268 // ArrayGet and UnsafeGetObject intrinsics cases.
5269 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5270 if (index.IsConstant()) {
5271 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5272 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5273 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005274 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005275 __ Add(temp2, obj, offset);
5276 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5277 temps.Release(temp2);
5278 }
Roland Levillain44015862016-01-22 11:47:17 +00005279 }
Roland Levillain44015862016-01-22 11:47:17 +00005280 } else {
5281 // /* HeapReference<Object> */ ref = *(obj + offset)
5282 MemOperand field = HeapOperand(obj, offset);
5283 if (use_load_acquire) {
5284 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5285 } else {
5286 Load(type, ref_reg, field);
5287 }
5288 }
5289
5290 // Object* ref = ref_addr->AsMirrorPtr()
5291 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5292
Vladimir Marko953437b2016-08-24 08:30:46 +00005293 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005294 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005295 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005296 AddSlowPath(slow_path);
5297
5298 // if (rb_state == ReadBarrier::gray_ptr_)
5299 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005300 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5301 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5302 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5303 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5304 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005305 __ Bind(slow_path->GetExitLabel());
5306}
5307
5308void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5309 Location out,
5310 Location ref,
5311 Location obj,
5312 uint32_t offset,
5313 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005314 DCHECK(kEmitCompilerReadBarrier);
5315
Roland Levillain44015862016-01-22 11:47:17 +00005316 // Insert a slow path based read barrier *after* the reference load.
5317 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005318 // If heap poisoning is enabled, the unpoisoning of the loaded
5319 // reference will be carried out by the runtime within the slow
5320 // path.
5321 //
5322 // Note that `ref` currently does not get unpoisoned (when heap
5323 // poisoning is enabled), which is alright as the `ref` argument is
5324 // not used by the artReadBarrierSlow entry point.
5325 //
5326 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5327 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5328 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5329 AddSlowPath(slow_path);
5330
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005331 __ B(slow_path->GetEntryLabel());
5332 __ Bind(slow_path->GetExitLabel());
5333}
5334
Roland Levillain44015862016-01-22 11:47:17 +00005335void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5336 Location out,
5337 Location ref,
5338 Location obj,
5339 uint32_t offset,
5340 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005341 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005342 // Baker's read barriers shall be handled by the fast path
5343 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5344 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005345 // If heap poisoning is enabled, unpoisoning will be taken care of
5346 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005347 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005348 } else if (kPoisonHeapReferences) {
5349 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5350 }
5351}
5352
Roland Levillain44015862016-01-22 11:47:17 +00005353void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5354 Location out,
5355 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005356 DCHECK(kEmitCompilerReadBarrier);
5357
Roland Levillain44015862016-01-22 11:47:17 +00005358 // Insert a slow path based read barrier *after* the GC root load.
5359 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005360 // Note that GC roots are not affected by heap poisoning, so we do
5361 // not need to do anything special for this here.
5362 SlowPathCodeARM64* slow_path =
5363 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5364 AddSlowPath(slow_path);
5365
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005366 __ B(slow_path->GetEntryLabel());
5367 __ Bind(slow_path->GetExitLabel());
5368}
5369
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005370void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5371 LocationSummary* locations =
5372 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5373 locations->SetInAt(0, Location::RequiresRegister());
5374 locations->SetOut(Location::RequiresRegister());
5375}
5376
5377void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5378 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005379 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005380 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005381 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005382 __ Ldr(XRegisterFrom(locations->Out()),
5383 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005384 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005385 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005386 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005387 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5388 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005389 __ Ldr(XRegisterFrom(locations->Out()),
5390 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005391 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005392}
5393
5394
5395
Alexandre Rames67555f72014-11-18 10:55:16 +00005396#undef __
5397#undef QUICK_ENTRY_POINT
5398
Alexandre Rames5319def2014-10-23 10:03:10 +01005399} // namespace arm64
5400} // namespace art