blob: 368c5439e1362803ee93799cefa250d313186860 [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)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 : SlowPathCodeARM64(at), cls_(cls), 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 {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000284 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000285 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;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000291 dex::TypeIndex type_index = cls_->GetTypeIndex();
292 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000293 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
294 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800296 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100297 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100299 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800300 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000301
302 // Move the class to the desired location.
303 Location out = locations->Out();
304 if (out.IsValid()) {
305 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000306 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000307 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000310 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
311 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
312 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
313 DCHECK(out.IsValid());
314 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
315 Register temp = temps.AcquireX();
316 const DexFile& dex_file = cls_->GetDexFile();
317 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
318 // kSaveEverything and use a temporary for the ADRP in the fast path, so that we
319 // can avoid the ADRP here.
320 vixl::aarch64::Label* adrp_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000321 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000322 arm64_codegen->EmitAdrpPlaceholder(adrp_label, temp);
323 vixl::aarch64::Label* strp_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000324 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000325 {
326 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
327 __ Bind(strp_label);
328 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
329 MemOperand(temp, /* offset placeholder */ 0));
330 }
331 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 __ B(GetExitLabel());
333 }
334
Alexandre Rames9931f312015-06-19 14:47:01 +0100335 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 private:
338 // The class this slow path will load.
339 HLoadClass* const cls_;
340
Alexandre Rames67555f72014-11-18 10:55:16 +0000341 // The dex PC of `at_`.
342 const uint32_t dex_pc_;
343
344 // Whether to initialize the class.
345 const bool do_clinit_;
346
347 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
348};
349
Vladimir Markoaad75c62016-10-03 08:46:48 +0000350class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
351 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100352 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
353 : SlowPathCodeARM64(instruction),
354 temp_(temp),
355 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000356
357 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
358 LocationSummary* locations = instruction_->GetLocations();
359 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
360 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
361
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100362 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
363 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
364 temps.Exclude(temp_);
365
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366 __ Bind(GetEntryLabel());
367 SaveLiveRegisters(codegen, locations);
368
369 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000370 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
371 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
373 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
374 Primitive::Type type = instruction_->GetType();
375 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
376
377 RestoreLiveRegisters(codegen, locations);
378
379 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100381 if (!kUseReadBarrier || kUseBakerReadBarrier) {
382 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
383 } else {
384 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
385 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
386 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
387 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000388 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100389 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000390 {
391 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
392 __ Bind(strp_label);
393 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100394 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000395 }
396
397 __ B(GetExitLabel());
398 }
399
400 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
401
402 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100403 const Register temp_;
404 vixl::aarch64::Label* adrp_label_;
405
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
407};
408
Alexandre Rames5319def2014-10-23 10:03:10 +0100409class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
410 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000411 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
414 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100415 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000416 if (instruction_->CanThrowIntoCatchBlock()) {
417 // Live registers will be restored in the catch block if caught.
418 SaveLiveRegisters(codegen, instruction_->GetLocations());
419 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000420 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
421 instruction_,
422 instruction_->GetDexPc(),
423 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800424 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 }
426
Alexandre Rames8158f282015-08-07 10:26:17 +0100427 bool IsFatal() const OVERRIDE { return true; }
428
Alexandre Rames9931f312015-06-19 14:47:01 +0100429 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
430
Alexandre Rames5319def2014-10-23 10:03:10 +0100431 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100432 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
433};
434
435class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100437 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100439
Alexandre Rames67555f72014-11-18 10:55:16 +0000440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100442 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000443 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800444 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000445 if (successor_ == nullptr) {
446 __ B(GetReturnLabel());
447 } else {
448 __ B(arm64_codegen->GetLabelOf(successor_));
449 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100450 }
451
Scott Wakeling97c72b72016-06-24 16:19:36 +0100452 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 DCHECK(successor_ == nullptr);
454 return &return_label_;
455 }
456
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100457 HBasicBlock* GetSuccessor() const {
458 return successor_;
459 }
460
Alexandre Rames9931f312015-06-19 14:47:01 +0100461 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
462
Alexandre Rames5319def2014-10-23 10:03:10 +0100463 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100464 // If not null, the block to branch to after the suspend check.
465 HBasicBlock* const successor_;
466
467 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100468 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100469
470 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
471};
472
Alexandre Rames67555f72014-11-18 10:55:16 +0000473class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
474 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000476 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800480
Alexandre Rames3e69f162014-12-10 10:36:50 +0000481 DCHECK(instruction_->IsCheckCast()
482 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
483 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100484 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000488 if (!is_fatal_) {
489 SaveLiveRegisters(codegen, locations);
490 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000491
492 // We're moving two locations to locations that could overlap, so we need a parallel
493 // move resolver.
494 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800495 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800496 LocationFrom(calling_convention.GetRegisterAt(0)),
497 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800498 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800499 LocationFrom(calling_convention.GetRegisterAt(1)),
500 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000501 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000502 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800503 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000504 Primitive::Type ret_type = instruction_->GetType();
505 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
506 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
507 } else {
508 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800509 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
510 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511 }
512
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 if (!is_fatal_) {
514 RestoreLiveRegisters(codegen, locations);
515 __ B(GetExitLabel());
516 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000517 }
518
Alexandre Rames9931f312015-06-19 14:47:01 +0100519 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100520 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100521
Alexandre Rames67555f72014-11-18 10:55:16 +0000522 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000523 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000524
Alexandre Rames67555f72014-11-18 10:55:16 +0000525 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
526};
527
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700528class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
529 public:
Aart Bik42249c32016-01-07 15:33:50 -0800530 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000531 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700532
533 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800534 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000536 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000537 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700538 }
539
Alexandre Rames9931f312015-06-19 14:47:01 +0100540 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
541
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700542 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
544};
545
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100546class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
547 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000548 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
551 LocationSummary* locations = instruction_->GetLocations();
552 __ Bind(GetEntryLabel());
553 SaveLiveRegisters(codegen, locations);
554
555 InvokeRuntimeCallingConvention calling_convention;
556 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
557 parallel_move.AddMove(
558 locations->InAt(0),
559 LocationFrom(calling_convention.GetRegisterAt(0)),
560 Primitive::kPrimNot,
561 nullptr);
562 parallel_move.AddMove(
563 locations->InAt(1),
564 LocationFrom(calling_convention.GetRegisterAt(1)),
565 Primitive::kPrimInt,
566 nullptr);
567 parallel_move.AddMove(
568 locations->InAt(2),
569 LocationFrom(calling_convention.GetRegisterAt(2)),
570 Primitive::kPrimNot,
571 nullptr);
572 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
573
574 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000575 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
577 RestoreLiveRegisters(codegen, locations);
578 __ B(GetExitLabel());
579 }
580
581 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
582
583 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100584 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
585};
586
Zheng Xu3927c8b2015-11-18 17:46:25 +0800587void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
588 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000589 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800590
591 // We are about to use the assembler to place literals directly. Make sure we have enough
592 // underlying code buffer and we have generated the jump table with right size.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000593 vixl::CodeBufferCheckScope scope(codegen->GetVIXLAssembler(),
594 num_entries * sizeof(int32_t),
595 vixl::CodeBufferCheckScope::kReserveBufferSpace,
596 vixl::CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800597
598 __ Bind(&table_start_);
599 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
600 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100601 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800602 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800604 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
605 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
606 Literal<int32_t> literal(jump_offset);
607 __ place(&literal);
608 }
609}
610
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100611// Slow path marking an object reference `ref` during a read
612// barrier. The field `obj.field` in the object `obj` holding this
613// reference does not get updated by this slow path after marking (see
614// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
615//
616// This means that after the execution of this slow path, `ref` will
617// always be up-to-date, but `obj.field` may not; i.e., after the
618// flip, `ref` will be a to-space reference, but `obj.field` will
619// probably still be a from-space reference (unless it gets updated by
620// another thread, or if another thread installed another object
621// reference (different from `ref`) in `obj.field`).
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800622// If entrypoint is a valid location it is assumed to already be holding the entrypoint. The case
623// where the entrypoint is passed in is for the GcRoot read barrier.
Roland Levillain44015862016-01-22 11:47:17 +0000624class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
625 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800626 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
627 Location ref,
628 Location entrypoint = Location::NoLocation())
629 : SlowPathCodeARM64(instruction),
630 ref_(ref),
631 entrypoint_(entrypoint) {
Roland Levillain44015862016-01-22 11:47:17 +0000632 DCHECK(kEmitCompilerReadBarrier);
633 }
634
635 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
636
637 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
638 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000639 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 DCHECK(ref_.IsRegister()) << ref_;
641 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain44015862016-01-22 11:47:17 +0000642 DCHECK(instruction_->IsInstanceFieldGet() ||
643 instruction_->IsStaticFieldGet() ||
644 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100645 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000646 instruction_->IsLoadClass() ||
647 instruction_->IsLoadString() ||
648 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100649 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100650 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
651 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000652 << "Unexpected instruction in read barrier marking slow path: "
653 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000654 // The read barrier instrumentation of object ArrayGet
655 // instructions does not support the HIntermediateAddress
656 // instruction.
657 DCHECK(!(instruction_->IsArrayGet() &&
658 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain44015862016-01-22 11:47:17 +0000659
660 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100661 // No need to save live registers; it's taken care of by the
662 // entrypoint. Also, there is no need to update the stack mask,
663 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000664 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100665 DCHECK_NE(ref_.reg(), LR);
666 DCHECK_NE(ref_.reg(), WSP);
667 DCHECK_NE(ref_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100668 // IP0 is used internally by the ReadBarrierMarkRegX entry point
669 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 DCHECK_NE(ref_.reg(), IP0);
671 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
Roland Levillain02b75802016-07-13 11:54:35 +0100672 // "Compact" slow path, saving two moves.
673 //
674 // Instead of using the standard runtime calling convention (input
675 // and output in W0):
676 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100677 // W0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100678 // W0 <- ReadBarrierMark(W0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100679 // ref <- W0
Roland Levillain02b75802016-07-13 11:54:35 +0100680 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100681 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100682 // of a dedicated entrypoint:
683 //
684 // rX <- ReadBarrierMarkRegX(rX)
685 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800686 if (entrypoint_.IsValid()) {
687 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
688 __ Blr(XRegisterFrom(entrypoint_));
689 } else {
690 // Entrypoint is not already loaded, load from the thread.
691 int32_t entry_point_offset =
692 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
693 // This runtime call does not require a stack map.
694 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
695 }
Roland Levillain44015862016-01-22 11:47:17 +0000696 __ B(GetExitLabel());
697 }
698
699 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100700 // The location (register) of the marked object reference.
701 const Location ref_;
Roland Levillain44015862016-01-22 11:47:17 +0000702
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800703 // The location of the entrypoint if it is already loaded.
704 const Location entrypoint_;
705
Roland Levillain44015862016-01-22 11:47:17 +0000706 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
707};
708
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100709// Slow path marking an object reference `ref` during a read barrier,
710// and if needed, atomically updating the field `obj.field` in the
711// object `obj` holding this reference after marking (contrary to
712// ReadBarrierMarkSlowPathARM64 above, which never tries to update
713// `obj.field`).
714//
715// This means that after the execution of this slow path, both `ref`
716// and `obj.field` will be up-to-date; i.e., after the flip, both will
717// hold the same to-space reference (unless another thread installed
718// another object reference (different from `ref`) in `obj.field`).
719class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
720 public:
721 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
722 Location ref,
723 Register obj,
724 Location field_offset,
725 Register temp)
726 : SlowPathCodeARM64(instruction),
727 ref_(ref),
728 obj_(obj),
729 field_offset_(field_offset),
730 temp_(temp) {
731 DCHECK(kEmitCompilerReadBarrier);
732 }
733
734 const char* GetDescription() const OVERRIDE {
735 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
736 }
737
738 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
739 LocationSummary* locations = instruction_->GetLocations();
740 Register ref_reg = WRegisterFrom(ref_);
741 DCHECK(locations->CanCall());
742 DCHECK(ref_.IsRegister()) << ref_;
743 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
744 // This slow path is only used by the UnsafeCASObject intrinsic.
745 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
746 << "Unexpected instruction in read barrier marking and field updating slow path: "
747 << instruction_->DebugName();
748 DCHECK(instruction_->GetLocations()->Intrinsified());
749 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
750 DCHECK(field_offset_.IsRegister()) << field_offset_;
751
752 __ Bind(GetEntryLabel());
753
754 // Save the old reference.
755 // Note that we cannot use IP to save the old reference, as IP is
756 // used internally by the ReadBarrierMarkRegX entry point, and we
757 // need the old reference after the call to that entry point.
758 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
759 __ Mov(temp_.W(), ref_reg);
760
761 // No need to save live registers; it's taken care of by the
762 // entrypoint. Also, there is no need to update the stack mask,
763 // as this runtime call will not trigger a garbage collection.
764 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
765 DCHECK_NE(ref_.reg(), LR);
766 DCHECK_NE(ref_.reg(), WSP);
767 DCHECK_NE(ref_.reg(), WZR);
768 // IP0 is used internally by the ReadBarrierMarkRegX entry point
769 // as a temporary, it cannot be the entry point's input/output.
770 DCHECK_NE(ref_.reg(), IP0);
771 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
772 // "Compact" slow path, saving two moves.
773 //
774 // Instead of using the standard runtime calling convention (input
775 // and output in W0):
776 //
777 // W0 <- ref
778 // W0 <- ReadBarrierMark(W0)
779 // ref <- W0
780 //
781 // we just use rX (the register containing `ref`) as input and output
782 // of a dedicated entrypoint:
783 //
784 // rX <- ReadBarrierMarkRegX(rX)
785 //
786 int32_t entry_point_offset =
787 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
788 // This runtime call does not require a stack map.
789 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
790
791 // If the new reference is different from the old reference,
792 // update the field in the holder (`*(obj_ + field_offset_)`).
793 //
794 // Note that this field could also hold a different object, if
795 // another thread had concurrently changed it. In that case, the
796 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
797 // (CAS) operation below would abort the CAS, leaving the field
798 // as-is.
799 vixl::aarch64::Label done;
800 __ Cmp(temp_.W(), ref_reg);
801 __ B(eq, &done);
802
803 // Update the the holder's field atomically. This may fail if
804 // mutator updates before us, but it's OK. This is achieved
805 // using a strong compare-and-set (CAS) operation with relaxed
806 // memory synchronization ordering, where the expected value is
807 // the old reference and the desired value is the new reference.
808
809 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
810 UseScratchRegisterScope temps(masm);
811
812 // Convenience aliases.
813 Register base = obj_.W();
814 Register offset = XRegisterFrom(field_offset_);
815 Register expected = temp_.W();
816 Register value = ref_reg;
817 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
818 Register tmp_value = temps.AcquireW(); // Value in memory.
819
820 __ Add(tmp_ptr, base.X(), Operand(offset));
821
822 if (kPoisonHeapReferences) {
823 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
824 if (value.Is(expected)) {
825 // Do not poison `value`, as it is the same register as
826 // `expected`, which has just been poisoned.
827 } else {
828 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
829 }
830 }
831
832 // do {
833 // tmp_value = [tmp_ptr] - expected;
834 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
835
Roland Levillain24a4d112016-10-26 13:10:46 +0100836 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100837 __ Bind(&loop_head);
838 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
839 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100840 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
842 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100843 __ B(&exit_loop);
844 __ Bind(&comparison_failed);
845 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100846 __ Bind(&exit_loop);
847
848 if (kPoisonHeapReferences) {
849 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
850 if (value.Is(expected)) {
851 // Do not unpoison `value`, as it is the same register as
852 // `expected`, which has just been unpoisoned.
853 } else {
854 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
855 }
856 }
857
858 __ Bind(&done);
859 __ B(GetExitLabel());
860 }
861
862 private:
863 // The location (register) of the marked object reference.
864 const Location ref_;
865 // The register containing the object holding the marked object reference field.
866 const Register obj_;
867 // The location of the offset of the marked reference field within `obj_`.
868 Location field_offset_;
869
870 const Register temp_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
873};
874
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000875// Slow path generating a read barrier for a heap reference.
876class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
877 public:
878 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
879 Location out,
880 Location ref,
881 Location obj,
882 uint32_t offset,
883 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000885 out_(out),
886 ref_(ref),
887 obj_(obj),
888 offset_(offset),
889 index_(index) {
890 DCHECK(kEmitCompilerReadBarrier);
891 // If `obj` is equal to `out` or `ref`, it means the initial object
892 // has been overwritten by (or after) the heap object reference load
893 // to be instrumented, e.g.:
894 //
895 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000896 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000897 //
898 // In that case, we have lost the information about the original
899 // object, and the emitted read barrier cannot work properly.
900 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
901 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
902 }
903
904 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
905 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
906 LocationSummary* locations = instruction_->GetLocations();
907 Primitive::Type type = Primitive::kPrimNot;
908 DCHECK(locations->CanCall());
909 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100910 DCHECK(instruction_->IsInstanceFieldGet() ||
911 instruction_->IsStaticFieldGet() ||
912 instruction_->IsArrayGet() ||
913 instruction_->IsInstanceOf() ||
914 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100915 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000916 << "Unexpected instruction in read barrier for heap reference slow path: "
917 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000918 // The read barrier instrumentation of object ArrayGet
919 // instructions does not support the HIntermediateAddress
920 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000921 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100922 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000923
924 __ Bind(GetEntryLabel());
925
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000926 SaveLiveRegisters(codegen, locations);
927
928 // We may have to change the index's value, but as `index_` is a
929 // constant member (like other "inputs" of this slow path),
930 // introduce a copy of it, `index`.
931 Location index = index_;
932 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100933 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000934 if (instruction_->IsArrayGet()) {
935 // Compute the actual memory offset and store it in `index`.
936 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
937 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
938 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
939 // We are about to change the value of `index_reg` (see the
940 // calls to vixl::MacroAssembler::Lsl and
941 // vixl::MacroAssembler::Mov below), but it has
942 // not been saved by the previous call to
943 // art::SlowPathCode::SaveLiveRegisters, as it is a
944 // callee-save register --
945 // art::SlowPathCode::SaveLiveRegisters does not consider
946 // callee-save registers, as it has been designed with the
947 // assumption that callee-save registers are supposed to be
948 // handled by the called function. So, as a callee-save
949 // register, `index_reg` _would_ eventually be saved onto
950 // the stack, but it would be too late: we would have
951 // changed its value earlier. Therefore, we manually save
952 // it here into another freely available register,
953 // `free_reg`, chosen of course among the caller-save
954 // registers (as a callee-save `free_reg` register would
955 // exhibit the same problem).
956 //
957 // Note we could have requested a temporary register from
958 // the register allocator instead; but we prefer not to, as
959 // this is a slow path, and we know we can find a
960 // caller-save register that is available.
961 Register free_reg = FindAvailableCallerSaveRegister(codegen);
962 __ Mov(free_reg.W(), index_reg);
963 index_reg = free_reg;
964 index = LocationFrom(index_reg);
965 } else {
966 // The initial register stored in `index_` has already been
967 // saved in the call to art::SlowPathCode::SaveLiveRegisters
968 // (as it is not a callee-save register), so we can freely
969 // use it.
970 }
971 // Shifting the index value contained in `index_reg` by the scale
972 // factor (2) cannot overflow in practice, as the runtime is
973 // unable to allocate object arrays with a size larger than
974 // 2^26 - 1 (that is, 2^28 - 4 bytes).
975 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
976 static_assert(
977 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
978 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
979 __ Add(index_reg, index_reg, Operand(offset_));
980 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100981 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
982 // intrinsics, `index_` is not shifted by a scale factor of 2
983 // (as in the case of ArrayGet), as it is actually an offset
984 // to an object field within an object.
985 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000986 DCHECK(instruction_->GetLocations()->Intrinsified());
987 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
988 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
989 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100990 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100991 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000992 }
993 }
994
995 // We're moving two or three locations to locations that could
996 // overlap, so we need a parallel move resolver.
997 InvokeRuntimeCallingConvention calling_convention;
998 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
999 parallel_move.AddMove(ref_,
1000 LocationFrom(calling_convention.GetRegisterAt(0)),
1001 type,
1002 nullptr);
1003 parallel_move.AddMove(obj_,
1004 LocationFrom(calling_convention.GetRegisterAt(1)),
1005 type,
1006 nullptr);
1007 if (index.IsValid()) {
1008 parallel_move.AddMove(index,
1009 LocationFrom(calling_convention.GetRegisterAt(2)),
1010 Primitive::kPrimInt,
1011 nullptr);
1012 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1013 } else {
1014 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1015 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1016 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001017 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001018 instruction_,
1019 instruction_->GetDexPc(),
1020 this);
1021 CheckEntrypointTypes<
1022 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1023 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1024
1025 RestoreLiveRegisters(codegen, locations);
1026
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001027 __ B(GetExitLabel());
1028 }
1029
1030 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1031
1032 private:
1033 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001034 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1035 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001036 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1037 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1038 return Register(VIXLRegCodeFromART(i), kXRegSize);
1039 }
1040 }
1041 // We shall never fail to find a free caller-save register, as
1042 // there are more than two core caller-save registers on ARM64
1043 // (meaning it is possible to find one which is different from
1044 // `ref` and `obj`).
1045 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1046 LOG(FATAL) << "Could not find a free register";
1047 UNREACHABLE();
1048 }
1049
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001050 const Location out_;
1051 const Location ref_;
1052 const Location obj_;
1053 const uint32_t offset_;
1054 // An additional location containing an index to an array.
1055 // Only used for HArrayGet and the UnsafeGetObject &
1056 // UnsafeGetObjectVolatile intrinsics.
1057 const Location index_;
1058
1059 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1060};
1061
1062// Slow path generating a read barrier for a GC root.
1063class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1064 public:
1065 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001066 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001067 DCHECK(kEmitCompilerReadBarrier);
1068 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001069
1070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1071 LocationSummary* locations = instruction_->GetLocations();
1072 Primitive::Type type = Primitive::kPrimNot;
1073 DCHECK(locations->CanCall());
1074 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001075 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1076 << "Unexpected instruction in read barrier for GC root slow path: "
1077 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001078
1079 __ Bind(GetEntryLabel());
1080 SaveLiveRegisters(codegen, locations);
1081
1082 InvokeRuntimeCallingConvention calling_convention;
1083 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1084 // The argument of the ReadBarrierForRootSlow is not a managed
1085 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1086 // thus we need a 64-bit move here, and we cannot use
1087 //
1088 // arm64_codegen->MoveLocation(
1089 // LocationFrom(calling_convention.GetRegisterAt(0)),
1090 // root_,
1091 // type);
1092 //
1093 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1094 // reference type (`Primitive::kPrimNot`).
1095 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001096 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001097 instruction_,
1098 instruction_->GetDexPc(),
1099 this);
1100 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1101 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1102
1103 RestoreLiveRegisters(codegen, locations);
1104 __ B(GetExitLabel());
1105 }
1106
1107 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1108
1109 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001110 const Location out_;
1111 const Location root_;
1112
1113 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1114};
1115
Alexandre Rames5319def2014-10-23 10:03:10 +01001116#undef __
1117
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001118Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 Location next_location;
1120 if (type == Primitive::kPrimVoid) {
1121 LOG(FATAL) << "Unreachable type " << type;
1122 }
1123
Alexandre Rames542361f2015-01-29 16:57:31 +00001124 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001125 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1126 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001127 } else if (!Primitive::IsFloatingPointType(type) &&
1128 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001129 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1130 } else {
1131 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001132 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1133 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001134 }
1135
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001136 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001137 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001138 return next_location;
1139}
1140
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001141Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001142 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143}
1144
Serban Constantinescu579885a2015-02-22 20:51:33 +00001145CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1146 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001147 const CompilerOptions& compiler_options,
1148 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001149 : CodeGenerator(graph,
1150 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001151 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001152 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001153 callee_saved_core_registers.GetList(),
1154 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001155 compiler_options,
1156 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001157 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001158 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001159 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001160 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001161 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001162 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001163 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001164 uint32_literals_(std::less<uint32_t>(),
1165 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001166 uint64_literals_(std::less<uint64_t>(),
1167 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001168 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1169 boot_image_string_patches_(StringReferenceValueComparator(),
1170 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1171 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001172 boot_image_type_patches_(TypeReferenceValueComparator(),
1173 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1174 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001175 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001176 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001177 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1178 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001179 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1180 jit_class_patches_(TypeReferenceValueComparator(),
1181 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001182 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001183 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001184}
Alexandre Rames5319def2014-10-23 10:03:10 +01001185
Alexandre Rames67555f72014-11-18 10:55:16 +00001186#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001187
Zheng Xu3927c8b2015-11-18 17:46:25 +08001188void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001189 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001190 jump_table->EmitTable(this);
1191 }
1192}
1193
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001194void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001195 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001196 // Ensure we emit the literal pool.
1197 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001198
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001199 CodeGenerator::Finalize(allocator);
1200}
1201
Zheng Xuad4450e2015-04-17 18:48:56 +08001202void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1203 // Note: There are 6 kinds of moves:
1204 // 1. constant -> GPR/FPR (non-cycle)
1205 // 2. constant -> stack (non-cycle)
1206 // 3. GPR/FPR -> GPR/FPR
1207 // 4. GPR/FPR -> stack
1208 // 5. stack -> GPR/FPR
1209 // 6. stack -> stack (non-cycle)
1210 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1211 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1212 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1213 // dependency.
1214 vixl_temps_.Open(GetVIXLAssembler());
1215}
1216
1217void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1218 vixl_temps_.Close();
1219}
1220
1221Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1222 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1223 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1224 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1225 Location scratch = GetScratchLocation(kind);
1226 if (!scratch.Equals(Location::NoLocation())) {
1227 return scratch;
1228 }
1229 // Allocate from VIXL temp registers.
1230 if (kind == Location::kRegister) {
1231 scratch = LocationFrom(vixl_temps_.AcquireX());
1232 } else {
1233 DCHECK(kind == Location::kFpuRegister);
1234 scratch = LocationFrom(vixl_temps_.AcquireD());
1235 }
1236 AddScratchLocation(scratch);
1237 return scratch;
1238}
1239
1240void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1241 if (loc.IsRegister()) {
1242 vixl_temps_.Release(XRegisterFrom(loc));
1243 } else {
1244 DCHECK(loc.IsFpuRegister());
1245 vixl_temps_.Release(DRegisterFrom(loc));
1246 }
1247 RemoveScratchLocation(loc);
1248}
1249
Alexandre Rames3e69f162014-12-10 10:36:50 +00001250void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001251 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001253}
1254
Alexandre Rames5319def2014-10-23 10:03:10 +01001255void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001256 MacroAssembler* masm = GetVIXLAssembler();
1257 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001258 __ Bind(&frame_entry_label_);
1259
Serban Constantinescu02164b32014-11-13 14:05:07 +00001260 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1261 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001262 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001263 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001264 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001265 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001266 __ Ldr(wzr, MemOperand(temp, 0));
1267 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001268 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001269
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001270 if (!HasEmptyFrame()) {
1271 int frame_size = GetFrameSize();
1272 // Stack layout:
1273 // sp[frame_size - 8] : lr.
1274 // ... : other preserved core registers.
1275 // ... : other preserved fp registers.
1276 // ... : reserved frame space.
1277 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001278
1279 // Save the current method if we need it. Note that we do not
1280 // do this in HCurrentMethod, as the instruction might have been removed
1281 // in the SSA graph.
1282 if (RequiresCurrentMethod()) {
1283 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001284 } else {
1285 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001286 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001287 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001288 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1289 frame_size - GetCoreSpillSize());
1290 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1291 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001292
1293 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1294 // Initialize should_deoptimize flag to 0.
1295 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1296 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1297 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001298 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001299}
1300
1301void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001302 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001303 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001304 if (!HasEmptyFrame()) {
1305 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001306 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1307 frame_size - FrameEntrySpillSize());
1308 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1309 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001310 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001311 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001312 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001313 __ Ret();
1314 GetAssembler()->cfi().RestoreState();
1315 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001316}
1317
Scott Wakeling97c72b72016-06-24 16:19:36 +01001318CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001319 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001320 return CPURegList(CPURegister::kRegister, kXRegSize,
1321 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001322}
1323
Scott Wakeling97c72b72016-06-24 16:19:36 +01001324CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001325 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1326 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1328 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001329}
1330
Alexandre Rames5319def2014-10-23 10:03:10 +01001331void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1332 __ Bind(GetLabelOf(block));
1333}
1334
Calin Juravle175dc732015-08-25 15:42:32 +01001335void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1336 DCHECK(location.IsRegister());
1337 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1338}
1339
Calin Juravlee460d1d2015-09-29 04:52:17 +01001340void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1341 if (location.IsRegister()) {
1342 locations->AddTemp(location);
1343 } else {
1344 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1345 }
1346}
1347
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001348void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001349 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001350 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001351 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001352 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001353 if (value_can_be_null) {
1354 __ Cbz(value, &done);
1355 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001356 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001357 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001358 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001359 if (value_can_be_null) {
1360 __ Bind(&done);
1361 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001362}
1363
David Brazdil58282f42016-01-14 12:45:10 +00001364void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001365 // Blocked core registers:
1366 // lr : Runtime reserved.
1367 // tr : Runtime reserved.
1368 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1369 // ip1 : VIXL core temp.
1370 // ip0 : VIXL core temp.
1371 //
1372 // Blocked fp registers:
1373 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001374 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1375 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001376 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001377 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001378 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001379
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001380 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001381 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001382 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001383 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001384
David Brazdil58282f42016-01-14 12:45:10 +00001385 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001386 // Stubs do not save callee-save floating point registers. If the graph
1387 // is debuggable, we need to deal with these registers differently. For
1388 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001389 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1390 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001391 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001392 }
1393 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001394}
1395
Alexandre Rames3e69f162014-12-10 10:36:50 +00001396size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1397 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1398 __ Str(reg, MemOperand(sp, stack_index));
1399 return kArm64WordSize;
1400}
1401
1402size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1403 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1404 __ Ldr(reg, MemOperand(sp, stack_index));
1405 return kArm64WordSize;
1406}
1407
1408size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1409 FPRegister reg = FPRegister(reg_id, kDRegSize);
1410 __ Str(reg, MemOperand(sp, stack_index));
1411 return kArm64WordSize;
1412}
1413
1414size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1415 FPRegister reg = FPRegister(reg_id, kDRegSize);
1416 __ Ldr(reg, MemOperand(sp, stack_index));
1417 return kArm64WordSize;
1418}
1419
Alexandre Rames5319def2014-10-23 10:03:10 +01001420void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001421 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001422}
1423
1424void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001425 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001426}
1427
Alexandre Rames67555f72014-11-18 10:55:16 +00001428void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001429 if (constant->IsIntConstant()) {
1430 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1431 } else if (constant->IsLongConstant()) {
1432 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1433 } else if (constant->IsNullConstant()) {
1434 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001435 } else if (constant->IsFloatConstant()) {
1436 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1437 } else {
1438 DCHECK(constant->IsDoubleConstant());
1439 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1440 }
1441}
1442
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443
1444static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1445 DCHECK(constant.IsConstant());
1446 HConstant* cst = constant.GetConstant();
1447 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001448 // Null is mapped to a core W register, which we associate with kPrimInt.
1449 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001450 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1451 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1452 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1453}
1454
Calin Juravlee460d1d2015-09-29 04:52:17 +01001455void CodeGeneratorARM64::MoveLocation(Location destination,
1456 Location source,
1457 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001458 if (source.Equals(destination)) {
1459 return;
1460 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461
1462 // A valid move can always be inferred from the destination and source
1463 // locations. When moving from and to a register, the argument type can be
1464 // used to generate 32bit instead of 64bit moves. In debug mode we also
1465 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001466 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001467
1468 if (destination.IsRegister() || destination.IsFpuRegister()) {
1469 if (unspecified_type) {
1470 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1471 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001472 (src_cst != nullptr && (src_cst->IsIntConstant()
1473 || src_cst->IsFloatConstant()
1474 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001475 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001476 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001477 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001478 // If the source is a double stack slot or a 64bit constant, a 64bit
1479 // type is appropriate. Else the source is a register, and since the
1480 // type has not been specified, we chose a 64bit type to force a 64bit
1481 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001482 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001483 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001484 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001485 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1486 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1487 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1489 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1490 __ Ldr(dst, StackOperandFrom(source));
1491 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001492 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001493 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001494 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001495 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001496 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001498 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001499 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1500 ? Primitive::kPrimLong
1501 : Primitive::kPrimInt;
1502 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1503 }
1504 } else {
1505 DCHECK(source.IsFpuRegister());
1506 if (destination.IsRegister()) {
1507 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1508 ? Primitive::kPrimDouble
1509 : Primitive::kPrimFloat;
1510 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1511 } else {
1512 DCHECK(destination.IsFpuRegister());
1513 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001514 }
1515 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516 } else { // The destination is not a register. It must be a stack slot.
1517 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1518 if (source.IsRegister() || source.IsFpuRegister()) {
1519 if (unspecified_type) {
1520 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001521 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001522 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001523 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001524 }
1525 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1527 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1528 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001530 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1531 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001532 UseScratchRegisterScope temps(GetVIXLAssembler());
1533 HConstant* src_cst = source.GetConstant();
1534 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001535 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001536 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1537 ? Register(xzr)
1538 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001539 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001540 if (src_cst->IsIntConstant()) {
1541 temp = temps.AcquireW();
1542 } else if (src_cst->IsLongConstant()) {
1543 temp = temps.AcquireX();
1544 } else if (src_cst->IsFloatConstant()) {
1545 temp = temps.AcquireS();
1546 } else {
1547 DCHECK(src_cst->IsDoubleConstant());
1548 temp = temps.AcquireD();
1549 }
1550 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001552 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001553 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001554 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001555 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001556 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001557 // Use any scratch register (a core or a floating-point one)
1558 // from VIXL scratch register pools as a temporary.
1559 //
1560 // We used to only use the FP scratch register pool, but in some
1561 // rare cases the only register from this pool (D31) would
1562 // already be used (e.g. within a ParallelMove instruction, when
1563 // a move is blocked by a another move requiring a scratch FP
1564 // register, which would reserve D31). To prevent this issue, we
1565 // ask for a scratch register of any type (core or FP).
1566 CPURegister temp =
1567 temps.AcquireCPURegisterOfSize(destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 __ Ldr(temp, StackOperandFrom(source));
1569 __ Str(temp, StackOperandFrom(destination));
1570 }
1571 }
1572}
1573
1574void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001575 CPURegister dst,
1576 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001577 switch (type) {
1578 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001579 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001580 break;
1581 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001582 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001583 break;
1584 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001585 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001586 break;
1587 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001588 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 break;
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimNot:
1592 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001593 case Primitive::kPrimFloat:
1594 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001595 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001596 __ Ldr(dst, src);
1597 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001598 case Primitive::kPrimVoid:
1599 LOG(FATAL) << "Unreachable type " << type;
1600 }
1601}
1602
Calin Juravle77520bc2015-01-12 18:45:46 +00001603void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001604 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001605 const MemOperand& src,
1606 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001607 MacroAssembler* masm = GetVIXLAssembler();
1608 BlockPoolsScope block_pools(masm);
1609 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001610 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001611 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001612
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001613 DCHECK(!src.IsPreIndex());
1614 DCHECK(!src.IsPostIndex());
1615
1616 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001617 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001618 MemOperand base = MemOperand(temp_base);
1619 switch (type) {
1620 case Primitive::kPrimBoolean:
1621 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001622 if (needs_null_check) {
1623 MaybeRecordImplicitNullCheck(instruction);
1624 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001625 break;
1626 case Primitive::kPrimByte:
1627 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001628 if (needs_null_check) {
1629 MaybeRecordImplicitNullCheck(instruction);
1630 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001631 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1632 break;
1633 case Primitive::kPrimChar:
1634 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001635 if (needs_null_check) {
1636 MaybeRecordImplicitNullCheck(instruction);
1637 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001638 break;
1639 case Primitive::kPrimShort:
1640 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001641 if (needs_null_check) {
1642 MaybeRecordImplicitNullCheck(instruction);
1643 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001644 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1645 break;
1646 case Primitive::kPrimInt:
1647 case Primitive::kPrimNot:
1648 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001649 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001650 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001651 if (needs_null_check) {
1652 MaybeRecordImplicitNullCheck(instruction);
1653 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001654 break;
1655 case Primitive::kPrimFloat:
1656 case Primitive::kPrimDouble: {
1657 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001658 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001659
1660 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1661 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001662 if (needs_null_check) {
1663 MaybeRecordImplicitNullCheck(instruction);
1664 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001665 __ Fmov(FPRegister(dst), temp);
1666 break;
1667 }
1668 case Primitive::kPrimVoid:
1669 LOG(FATAL) << "Unreachable type " << type;
1670 }
1671}
1672
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001673void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001674 CPURegister src,
1675 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001676 switch (type) {
1677 case Primitive::kPrimBoolean:
1678 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001680 break;
1681 case Primitive::kPrimChar:
1682 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001683 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001684 break;
1685 case Primitive::kPrimInt:
1686 case Primitive::kPrimNot:
1687 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001688 case Primitive::kPrimFloat:
1689 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001690 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001691 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001692 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001693 case Primitive::kPrimVoid:
1694 LOG(FATAL) << "Unreachable type " << type;
1695 }
1696}
1697
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001698void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1699 CPURegister src,
1700 const MemOperand& dst) {
1701 UseScratchRegisterScope temps(GetVIXLAssembler());
1702 Register temp_base = temps.AcquireX();
1703
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001704 DCHECK(!dst.IsPreIndex());
1705 DCHECK(!dst.IsPostIndex());
1706
1707 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001708 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001709 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001710 MemOperand base = MemOperand(temp_base);
1711 switch (type) {
1712 case Primitive::kPrimBoolean:
1713 case Primitive::kPrimByte:
1714 __ Stlrb(Register(src), base);
1715 break;
1716 case Primitive::kPrimChar:
1717 case Primitive::kPrimShort:
1718 __ Stlrh(Register(src), base);
1719 break;
1720 case Primitive::kPrimInt:
1721 case Primitive::kPrimNot:
1722 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001723 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001724 __ Stlr(Register(src), base);
1725 break;
1726 case Primitive::kPrimFloat:
1727 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001728 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001729 Register temp_src;
1730 if (src.IsZero()) {
1731 // The zero register is used to avoid synthesizing zero constants.
1732 temp_src = Register(src);
1733 } else {
1734 DCHECK(src.IsFPRegister());
1735 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1736 __ Fmov(temp_src, FPRegister(src));
1737 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001738
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001739 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001740 break;
1741 }
1742 case Primitive::kPrimVoid:
1743 LOG(FATAL) << "Unreachable type " << type;
1744 }
1745}
1746
Calin Juravle175dc732015-08-25 15:42:32 +01001747void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1748 HInstruction* instruction,
1749 uint32_t dex_pc,
1750 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001751 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001752 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001753 if (EntrypointRequiresStackMap(entrypoint)) {
1754 RecordPcInfo(instruction, dex_pc, slow_path);
1755 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001756}
1757
Roland Levillaindec8f632016-07-22 17:10:06 +01001758void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1759 HInstruction* instruction,
1760 SlowPathCode* slow_path) {
1761 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001762 GenerateInvokeRuntime(entry_point_offset);
1763}
1764
1765void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001766 BlockPoolsScope block_pools(GetVIXLAssembler());
1767 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1768 __ Blr(lr);
1769}
1770
Alexandre Rames67555f72014-11-18 10:55:16 +00001771void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001772 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001773 UseScratchRegisterScope temps(GetVIXLAssembler());
1774 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001775 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1776
Serban Constantinescu02164b32014-11-13 14:05:07 +00001777 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001778 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1779 __ Add(temp, class_reg, status_offset);
1780 __ Ldar(temp, HeapOperand(temp));
1781 __ Cmp(temp, mirror::Class::kStatusInitialized);
1782 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 __ Bind(slow_path->GetExitLabel());
1784}
Alexandre Rames5319def2014-10-23 10:03:10 +01001785
Roland Levillain44015862016-01-22 11:47:17 +00001786void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787 BarrierType type = BarrierAll;
1788
1789 switch (kind) {
1790 case MemBarrierKind::kAnyAny:
1791 case MemBarrierKind::kAnyStore: {
1792 type = BarrierAll;
1793 break;
1794 }
1795 case MemBarrierKind::kLoadAny: {
1796 type = BarrierReads;
1797 break;
1798 }
1799 case MemBarrierKind::kStoreStore: {
1800 type = BarrierWrites;
1801 break;
1802 }
1803 default:
1804 LOG(FATAL) << "Unexpected memory barrier " << kind;
1805 }
1806 __ Dmb(InnerShareable, type);
1807}
1808
Serban Constantinescu02164b32014-11-13 14:05:07 +00001809void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1810 HBasicBlock* successor) {
1811 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001812 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1813 if (slow_path == nullptr) {
1814 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1815 instruction->SetSlowPath(slow_path);
1816 codegen_->AddSlowPath(slow_path);
1817 if (successor != nullptr) {
1818 DCHECK(successor->IsLoopHeader());
1819 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1820 }
1821 } else {
1822 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1823 }
1824
Serban Constantinescu02164b32014-11-13 14:05:07 +00001825 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1826 Register temp = temps.AcquireW();
1827
Andreas Gampe542451c2016-07-26 09:02:02 -07001828 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001829 if (successor == nullptr) {
1830 __ Cbnz(temp, slow_path->GetEntryLabel());
1831 __ Bind(slow_path->GetReturnLabel());
1832 } else {
1833 __ Cbz(temp, codegen_->GetLabelOf(successor));
1834 __ B(slow_path->GetEntryLabel());
1835 // slow_path will return to GetLabelOf(successor).
1836 }
1837}
1838
Alexandre Rames5319def2014-10-23 10:03:10 +01001839InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1840 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001841 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001842 assembler_(codegen->GetAssembler()),
1843 codegen_(codegen) {}
1844
1845#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001846 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001847
1848#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1849
1850enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001851 // Using a base helps identify when we hit such breakpoints.
1852 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001853#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1854 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1855#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1856};
1857
1858#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001859 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001860 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1861 } \
1862 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1864 locations->SetOut(Location::Any()); \
1865 }
1866 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1867#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1868
1869#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001870#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001871
Alexandre Rames67555f72014-11-18 10:55:16 +00001872void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001873 DCHECK_EQ(instr->InputCount(), 2U);
1874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1875 Primitive::Type type = instr->GetResultType();
1876 switch (type) {
1877 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001878 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001879 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001880 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001882 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001883
1884 case Primitive::kPrimFloat:
1885 case Primitive::kPrimDouble:
1886 locations->SetInAt(0, Location::RequiresFpuRegister());
1887 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001888 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001889 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001890
Alexandre Rames5319def2014-10-23 10:03:10 +01001891 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001892 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001893 }
1894}
1895
Alexandre Rames09a99962015-04-15 11:47:56 +01001896void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001897 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1898
1899 bool object_field_get_with_read_barrier =
1900 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001901 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001902 new (GetGraph()->GetArena()) LocationSummary(instruction,
1903 object_field_get_with_read_barrier ?
1904 LocationSummary::kCallOnSlowPath :
1905 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001906 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001907 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001908 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001909 locations->SetInAt(0, Location::RequiresRegister());
1910 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1911 locations->SetOut(Location::RequiresFpuRegister());
1912 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001913 // The output overlaps for an object field get when read barriers
1914 // are enabled: we do not want the load to overwrite the object's
1915 // location, as we need it to emit the read barrier.
1916 locations->SetOut(
1917 Location::RequiresRegister(),
1918 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001919 }
1920}
1921
1922void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1923 const FieldInfo& field_info) {
1924 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001925 LocationSummary* locations = instruction->GetLocations();
1926 Location base_loc = locations->InAt(0);
1927 Location out = locations->Out();
1928 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001929 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001930 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001931 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001932
Roland Levillain44015862016-01-22 11:47:17 +00001933 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1934 // Object FieldGet with Baker's read barrier case.
1935 MacroAssembler* masm = GetVIXLAssembler();
1936 UseScratchRegisterScope temps(masm);
1937 // /* HeapReference<Object> */ out = *(base + offset)
1938 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1939 Register temp = temps.AcquireW();
1940 // Note that potential implicit null checks are handled in this
1941 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1942 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1943 instruction,
1944 out,
1945 base,
1946 offset,
1947 temp,
1948 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001949 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001950 } else {
1951 // General case.
1952 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001953 // Note that a potential implicit null check is handled in this
1954 // CodeGeneratorARM64::LoadAcquire call.
1955 // NB: LoadAcquire will record the pc info if needed.
1956 codegen_->LoadAcquire(
1957 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001958 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001959 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001960 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001961 }
Roland Levillain44015862016-01-22 11:47:17 +00001962 if (field_type == Primitive::kPrimNot) {
1963 // If read barriers are enabled, emit read barriers other than
1964 // Baker's using a slow path (and also unpoison the loaded
1965 // reference, if heap poisoning is enabled).
1966 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1967 }
Roland Levillain4d027112015-07-01 15:41:14 +01001968 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001969}
1970
1971void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1972 LocationSummary* locations =
1973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1974 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001975 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1976 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1977 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001978 locations->SetInAt(1, Location::RequiresFpuRegister());
1979 } else {
1980 locations->SetInAt(1, Location::RequiresRegister());
1981 }
1982}
1983
1984void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001985 const FieldInfo& field_info,
1986 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001987 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001988 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001989
1990 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001991 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001992 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001993 Offset offset = field_info.GetFieldOffset();
1994 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001995
Roland Levillain4d027112015-07-01 15:41:14 +01001996 {
1997 // We use a block to end the scratch scope before the write barrier, thus
1998 // freeing the temporary registers so they can be used in `MarkGCCard`.
1999 UseScratchRegisterScope temps(GetVIXLAssembler());
2000
2001 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2002 DCHECK(value.IsW());
2003 Register temp = temps.AcquireW();
2004 __ Mov(temp, value.W());
2005 GetAssembler()->PoisonHeapReference(temp.W());
2006 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002007 }
Roland Levillain4d027112015-07-01 15:41:14 +01002008
2009 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002010 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
2011 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002012 } else {
2013 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2014 codegen_->MaybeRecordImplicitNullCheck(instruction);
2015 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002016 }
2017
2018 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002019 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002020 }
2021}
2022
Alexandre Rames67555f72014-11-18 10:55:16 +00002023void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002024 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002025
2026 switch (type) {
2027 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002028 case Primitive::kPrimLong: {
2029 Register dst = OutputRegister(instr);
2030 Register lhs = InputRegisterAt(instr, 0);
2031 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002032 if (instr->IsAdd()) {
2033 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002034 } else if (instr->IsAnd()) {
2035 __ And(dst, lhs, rhs);
2036 } else if (instr->IsOr()) {
2037 __ Orr(dst, lhs, rhs);
2038 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002039 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002040 } else if (instr->IsRor()) {
2041 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002042 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002043 __ Ror(dst, lhs, shift);
2044 } else {
2045 // Ensure shift distance is in the same size register as the result. If
2046 // we are rotating a long and the shift comes in a w register originally,
2047 // we don't need to sxtw for use as an x since the shift distances are
2048 // all & reg_bits - 1.
2049 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2050 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002051 } else {
2052 DCHECK(instr->IsXor());
2053 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002054 }
2055 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002056 }
2057 case Primitive::kPrimFloat:
2058 case Primitive::kPrimDouble: {
2059 FPRegister dst = OutputFPRegister(instr);
2060 FPRegister lhs = InputFPRegisterAt(instr, 0);
2061 FPRegister rhs = InputFPRegisterAt(instr, 1);
2062 if (instr->IsAdd()) {
2063 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002064 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002065 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002066 } else {
2067 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002068 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002069 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002070 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002071 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002072 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002073 }
2074}
2075
Serban Constantinescu02164b32014-11-13 14:05:07 +00002076void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2077 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2078
2079 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2080 Primitive::Type type = instr->GetResultType();
2081 switch (type) {
2082 case Primitive::kPrimInt:
2083 case Primitive::kPrimLong: {
2084 locations->SetInAt(0, Location::RequiresRegister());
2085 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2086 locations->SetOut(Location::RequiresRegister());
2087 break;
2088 }
2089 default:
2090 LOG(FATAL) << "Unexpected shift type " << type;
2091 }
2092}
2093
2094void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2095 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2096
2097 Primitive::Type type = instr->GetType();
2098 switch (type) {
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimLong: {
2101 Register dst = OutputRegister(instr);
2102 Register lhs = InputRegisterAt(instr, 0);
2103 Operand rhs = InputOperandAt(instr, 1);
2104 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002105 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002106 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002107 if (instr->IsShl()) {
2108 __ Lsl(dst, lhs, shift_value);
2109 } else if (instr->IsShr()) {
2110 __ Asr(dst, lhs, shift_value);
2111 } else {
2112 __ Lsr(dst, lhs, shift_value);
2113 }
2114 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002115 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002116
2117 if (instr->IsShl()) {
2118 __ Lsl(dst, lhs, rhs_reg);
2119 } else if (instr->IsShr()) {
2120 __ Asr(dst, lhs, rhs_reg);
2121 } else {
2122 __ Lsr(dst, lhs, rhs_reg);
2123 }
2124 }
2125 break;
2126 }
2127 default:
2128 LOG(FATAL) << "Unexpected shift operation type " << type;
2129 }
2130}
2131
Alexandre Rames5319def2014-10-23 10:03:10 +01002132void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002133 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002134}
2135
2136void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002137 HandleBinaryOp(instruction);
2138}
2139
2140void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2141 HandleBinaryOp(instruction);
2142}
2143
2144void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2145 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002146}
2147
Artem Serov7fc63502016-02-09 17:15:29 +00002148void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002149 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2151 locations->SetInAt(0, Location::RequiresRegister());
2152 // There is no immediate variant of negated bitwise instructions in AArch64.
2153 locations->SetInAt(1, Location::RequiresRegister());
2154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2155}
2156
Artem Serov7fc63502016-02-09 17:15:29 +00002157void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002158 Register dst = OutputRegister(instr);
2159 Register lhs = InputRegisterAt(instr, 0);
2160 Register rhs = InputRegisterAt(instr, 1);
2161
2162 switch (instr->GetOpKind()) {
2163 case HInstruction::kAnd:
2164 __ Bic(dst, lhs, rhs);
2165 break;
2166 case HInstruction::kOr:
2167 __ Orn(dst, lhs, rhs);
2168 break;
2169 case HInstruction::kXor:
2170 __ Eon(dst, lhs, rhs);
2171 break;
2172 default:
2173 LOG(FATAL) << "Unreachable";
2174 }
2175}
2176
Alexandre Rames8626b742015-11-25 16:28:08 +00002177void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2178 HArm64DataProcWithShifterOp* instruction) {
2179 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2180 instruction->GetType() == Primitive::kPrimLong);
2181 LocationSummary* locations =
2182 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2183 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2184 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2185 } else {
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 }
2188 locations->SetInAt(1, Location::RequiresRegister());
2189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190}
2191
2192void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2193 HArm64DataProcWithShifterOp* instruction) {
2194 Primitive::Type type = instruction->GetType();
2195 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2196 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2197 Register out = OutputRegister(instruction);
2198 Register left;
2199 if (kind != HInstruction::kNeg) {
2200 left = InputRegisterAt(instruction, 0);
2201 }
2202 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2203 // shifter operand operation, the IR generating `right_reg` (input to the type
2204 // conversion) can have a different type from the current instruction's type,
2205 // so we manually indicate the type.
2206 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002207 int64_t shift_amount = instruction->GetShiftAmount() &
2208 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002209
2210 Operand right_operand(0);
2211
2212 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2213 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2214 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2215 } else {
2216 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2217 }
2218
2219 // Logical binary operations do not support extension operations in the
2220 // operand. Note that VIXL would still manage if it was passed by generating
2221 // the extension as a separate instruction.
2222 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2223 DCHECK(!right_operand.IsExtendedRegister() ||
2224 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2225 kind != HInstruction::kNeg));
2226 switch (kind) {
2227 case HInstruction::kAdd:
2228 __ Add(out, left, right_operand);
2229 break;
2230 case HInstruction::kAnd:
2231 __ And(out, left, right_operand);
2232 break;
2233 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002234 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002235 __ Neg(out, right_operand);
2236 break;
2237 case HInstruction::kOr:
2238 __ Orr(out, left, right_operand);
2239 break;
2240 case HInstruction::kSub:
2241 __ Sub(out, left, right_operand);
2242 break;
2243 case HInstruction::kXor:
2244 __ Eor(out, left, right_operand);
2245 break;
2246 default:
2247 LOG(FATAL) << "Unexpected operation kind: " << kind;
2248 UNREACHABLE();
2249 }
2250}
2251
Artem Serov328429f2016-07-06 16:23:04 +01002252void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002253 LocationSummary* locations =
2254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2257 locations->SetOut(Location::RequiresRegister());
2258}
2259
Roland Levillain19c54192016-11-04 13:44:09 +00002260void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002261 __ Add(OutputRegister(instruction),
2262 InputRegisterAt(instruction, 0),
2263 Operand(InputOperandAt(instruction, 1)));
2264}
2265
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002266void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002269 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2270 if (instr->GetOpKind() == HInstruction::kSub &&
2271 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002272 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002273 // Don't allocate register for Mneg instruction.
2274 } else {
2275 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2276 Location::RequiresRegister());
2277 }
2278 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2279 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2281}
2282
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002283void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002284 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002285 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2286 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002287
2288 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2289 // This fixup should be carried out for all multiply-accumulate instructions:
2290 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2291 if (instr->GetType() == Primitive::kPrimLong &&
2292 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2293 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002294 vixl::aarch64::Instruction* prev =
2295 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002296 if (prev->IsLoadOrStore()) {
2297 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002298 vixl::CodeBufferCheckScope scope(masm,
2299 kInstructionSize,
2300 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2301 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002302 __ nop();
2303 }
2304 }
2305
2306 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002307 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002308 __ Madd(res, mul_left, mul_right, accumulator);
2309 } else {
2310 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002311 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002312 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002313 __ Mneg(res, mul_left, mul_right);
2314 } else {
2315 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2316 __ Msub(res, mul_left, mul_right, accumulator);
2317 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002318 }
2319}
2320
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002321void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002322 bool object_array_get_with_read_barrier =
2323 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002324 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002325 new (GetGraph()->GetArena()) LocationSummary(instruction,
2326 object_array_get_with_read_barrier ?
2327 LocationSummary::kCallOnSlowPath :
2328 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002329 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002330 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002331 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002332 locations->SetInAt(0, Location::RequiresRegister());
2333 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002334 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2335 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2336 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002337 // The output overlaps in the case of an object array get with
2338 // read barriers enabled: we do not want the move to overwrite the
2339 // array's location, as we need it to emit the read barrier.
2340 locations->SetOut(
2341 Location::RequiresRegister(),
2342 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002343 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002344}
2345
2346void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002347 Primitive::Type type = instruction->GetType();
2348 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002349 LocationSummary* locations = instruction->GetLocations();
2350 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002351 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002352 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002353 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2354 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002355 MacroAssembler* masm = GetVIXLAssembler();
2356 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002357 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002358 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002359
Roland Levillain19c54192016-11-04 13:44:09 +00002360 // The read barrier instrumentation of object ArrayGet instructions
2361 // does not support the HIntermediateAddress instruction.
2362 DCHECK(!((type == Primitive::kPrimNot) &&
2363 instruction->GetArray()->IsIntermediateAddress() &&
2364 kEmitCompilerReadBarrier));
2365
Roland Levillain44015862016-01-22 11:47:17 +00002366 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2367 // Object ArrayGet with Baker's read barrier case.
2368 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002369 // Note that a potential implicit null check is handled in the
2370 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2371 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2372 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002373 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002374 // General case.
2375 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002376 Register length;
2377 if (maybe_compressed_char_at) {
2378 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2379 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002380 if (instruction->GetArray()->IsIntermediateAddress()) {
2381 DCHECK_LT(count_offset, offset);
2382 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2383 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2384 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2385 } else {
2386 __ Ldr(length, HeapOperand(obj, count_offset));
2387 }
jessicahandojo05765752016-09-09 19:01:32 -07002388 codegen_->MaybeRecordImplicitNullCheck(instruction);
2389 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002390 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002391 if (maybe_compressed_char_at) {
2392 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002393 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2394 "Expecting 0=compressed, 1=uncompressed");
2395 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002396 __ Ldrb(Register(OutputCPURegister(instruction)),
2397 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2398 __ B(&done);
2399 __ Bind(&uncompressed_load);
2400 __ Ldrh(Register(OutputCPURegister(instruction)),
2401 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2402 __ Bind(&done);
2403 } else {
2404 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2405 source = HeapOperand(obj, offset);
2406 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002407 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002408 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002409 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002410 // We do not need to compute the intermediate address from the array: the
2411 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002412 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002413 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002414 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002415 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2416 }
2417 temp = obj;
2418 } else {
2419 __ Add(temp, obj, offset);
2420 }
jessicahandojo05765752016-09-09 19:01:32 -07002421 if (maybe_compressed_char_at) {
2422 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002423 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2424 "Expecting 0=compressed, 1=uncompressed");
2425 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002426 __ Ldrb(Register(OutputCPURegister(instruction)),
2427 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2428 __ B(&done);
2429 __ Bind(&uncompressed_load);
2430 __ Ldrh(Register(OutputCPURegister(instruction)),
2431 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2432 __ Bind(&done);
2433 } else {
2434 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2435 }
Roland Levillain44015862016-01-22 11:47:17 +00002436 }
jessicahandojo05765752016-09-09 19:01:32 -07002437 if (!maybe_compressed_char_at) {
2438 codegen_->Load(type, OutputCPURegister(instruction), source);
2439 codegen_->MaybeRecordImplicitNullCheck(instruction);
2440 }
Roland Levillain44015862016-01-22 11:47:17 +00002441
2442 if (type == Primitive::kPrimNot) {
2443 static_assert(
2444 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2445 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2446 Location obj_loc = locations->InAt(0);
2447 if (index.IsConstant()) {
2448 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2449 } else {
2450 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2451 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002452 }
Roland Levillain4d027112015-07-01 15:41:14 +01002453 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002454}
2455
Alexandre Rames5319def2014-10-23 10:03:10 +01002456void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2458 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002460}
2461
2462void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002463 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002464 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002465 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002466 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002467 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002468 // Mask out compression flag from String's array length.
2469 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002470 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002471 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002472}
2473
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002474void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002475 Primitive::Type value_type = instruction->GetComponentType();
2476
2477 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2479 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002480 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002481 LocationSummary::kCallOnSlowPath :
2482 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002483 locations->SetInAt(0, Location::RequiresRegister());
2484 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002485 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2486 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2487 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002488 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002489 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002490 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002491 }
2492}
2493
2494void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2495 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002496 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002497 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002498 bool needs_write_barrier =
2499 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002500
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002501 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002502 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002503 CPURegister source = value;
2504 Location index = locations->InAt(1);
2505 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2506 MemOperand destination = HeapOperand(array);
2507 MacroAssembler* masm = GetVIXLAssembler();
2508 BlockPoolsScope block_pools(masm);
2509
2510 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002511 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002512 if (index.IsConstant()) {
2513 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2514 destination = HeapOperand(array, offset);
2515 } else {
2516 UseScratchRegisterScope temps(masm);
2517 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002518 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002519 // We do not need to compute the intermediate address from the array: the
2520 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002521 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002522 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002523 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002524 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2525 }
2526 temp = array;
2527 } else {
2528 __ Add(temp, array, offset);
2529 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002530 destination = HeapOperand(temp,
2531 XRegisterFrom(index),
2532 LSL,
2533 Primitive::ComponentSizeShift(value_type));
2534 }
2535 codegen_->Store(value_type, value, destination);
2536 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002537 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002538 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002539 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002540 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002541 {
2542 // We use a block to end the scratch scope before the write barrier, thus
2543 // freeing the temporary registers so they can be used in `MarkGCCard`.
2544 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002545 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002546 if (index.IsConstant()) {
2547 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002548 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002549 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002550 destination = HeapOperand(temp,
2551 XRegisterFrom(index),
2552 LSL,
2553 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002554 }
2555
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002556 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2557 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2558 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2559
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002560 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002561 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2562 codegen_->AddSlowPath(slow_path);
2563 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002564 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002565 __ Cbnz(Register(value), &non_zero);
2566 if (!index.IsConstant()) {
2567 __ Add(temp, array, offset);
2568 }
2569 __ Str(wzr, destination);
2570 codegen_->MaybeRecordImplicitNullCheck(instruction);
2571 __ B(&done);
2572 __ Bind(&non_zero);
2573 }
2574
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002575 // Note that when Baker read barriers are enabled, the type
2576 // checks are performed without read barriers. This is fine,
2577 // even in the case where a class object is in the from-space
2578 // after the flip, as a comparison involving such a type would
2579 // not produce a false positive; it may of course produce a
2580 // false negative, in which case we would take the ArraySet
2581 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002582
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002583 Register temp2 = temps.AcquireSameSizeAs(array);
2584 // /* HeapReference<Class> */ temp = array->klass_
2585 __ Ldr(temp, HeapOperand(array, class_offset));
2586 codegen_->MaybeRecordImplicitNullCheck(instruction);
2587 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002588
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002589 // /* HeapReference<Class> */ temp = temp->component_type_
2590 __ Ldr(temp, HeapOperand(temp, component_offset));
2591 // /* HeapReference<Class> */ temp2 = value->klass_
2592 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2593 // If heap poisoning is enabled, no need to unpoison `temp`
2594 // nor `temp2`, as we are comparing two poisoned references.
2595 __ Cmp(temp, temp2);
2596 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002597
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002598 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2599 vixl::aarch64::Label do_put;
2600 __ B(eq, &do_put);
2601 // If heap poisoning is enabled, the `temp` reference has
2602 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002603 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2604
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002605 // /* HeapReference<Class> */ temp = temp->super_class_
2606 __ Ldr(temp, HeapOperand(temp, super_offset));
2607 // If heap poisoning is enabled, no need to unpoison
2608 // `temp`, as we are comparing against null below.
2609 __ Cbnz(temp, slow_path->GetEntryLabel());
2610 __ Bind(&do_put);
2611 } else {
2612 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002613 }
2614 }
2615
2616 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002617 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002618 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002619 __ Mov(temp2, value.W());
2620 GetAssembler()->PoisonHeapReference(temp2);
2621 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002622 }
2623
2624 if (!index.IsConstant()) {
2625 __ Add(temp, array, offset);
2626 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002627 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002628
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002629 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002630 codegen_->MaybeRecordImplicitNullCheck(instruction);
2631 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002632 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002633
2634 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2635
2636 if (done.IsLinked()) {
2637 __ Bind(&done);
2638 }
2639
2640 if (slow_path != nullptr) {
2641 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002642 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002643 }
2644}
2645
Alexandre Rames67555f72014-11-18 10:55:16 +00002646void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002647 RegisterSet caller_saves = RegisterSet::Empty();
2648 InvokeRuntimeCallingConvention calling_convention;
2649 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2650 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2651 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002652 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002653 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002654}
2655
2656void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002657 BoundsCheckSlowPathARM64* slow_path =
2658 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002659 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002660 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2661 __ B(slow_path->GetEntryLabel(), hs);
2662}
2663
Alexandre Rames67555f72014-11-18 10:55:16 +00002664void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2665 LocationSummary* locations =
2666 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2667 locations->SetInAt(0, Location::RequiresRegister());
2668 if (check->HasUses()) {
2669 locations->SetOut(Location::SameAsFirstInput());
2670 }
2671}
2672
2673void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2674 // We assume the class is not null.
2675 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2676 check->GetLoadClass(), check, check->GetDexPc(), true);
2677 codegen_->AddSlowPath(slow_path);
2678 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2679}
2680
Roland Levillain1a653882016-03-18 18:05:57 +00002681static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2682 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2683 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2684}
2685
2686void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2687 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2688 Location rhs_loc = instruction->GetLocations()->InAt(1);
2689 if (rhs_loc.IsConstant()) {
2690 // 0.0 is the only immediate that can be encoded directly in
2691 // an FCMP instruction.
2692 //
2693 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2694 // specify that in a floating-point comparison, positive zero
2695 // and negative zero are considered equal, so we can use the
2696 // literal 0.0 for both cases here.
2697 //
2698 // Note however that some methods (Float.equal, Float.compare,
2699 // Float.compareTo, Double.equal, Double.compare,
2700 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2701 // StrictMath.min) consider 0.0 to be (strictly) greater than
2702 // -0.0. So if we ever translate calls to these methods into a
2703 // HCompare instruction, we must handle the -0.0 case with
2704 // care here.
2705 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2706 __ Fcmp(lhs_reg, 0.0);
2707 } else {
2708 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2709 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002710}
2711
Serban Constantinescu02164b32014-11-13 14:05:07 +00002712void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002713 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002714 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2715 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002716 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002717 case Primitive::kPrimBoolean:
2718 case Primitive::kPrimByte:
2719 case Primitive::kPrimShort:
2720 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002721 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002722 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002723 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002724 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2726 break;
2727 }
2728 case Primitive::kPrimFloat:
2729 case Primitive::kPrimDouble: {
2730 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002731 locations->SetInAt(1,
2732 IsFloatingPointZeroConstant(compare->InputAt(1))
2733 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2734 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002735 locations->SetOut(Location::RequiresRegister());
2736 break;
2737 }
2738 default:
2739 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2740 }
2741}
2742
2743void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2744 Primitive::Type in_type = compare->InputAt(0)->GetType();
2745
2746 // 0 if: left == right
2747 // 1 if: left > right
2748 // -1 if: left < right
2749 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002750 case Primitive::kPrimBoolean:
2751 case Primitive::kPrimByte:
2752 case Primitive::kPrimShort:
2753 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002754 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002755 case Primitive::kPrimLong: {
2756 Register result = OutputRegister(compare);
2757 Register left = InputRegisterAt(compare, 0);
2758 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002759 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002760 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2761 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002762 break;
2763 }
2764 case Primitive::kPrimFloat:
2765 case Primitive::kPrimDouble: {
2766 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002767 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002768 __ Cset(result, ne);
2769 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002770 break;
2771 }
2772 default:
2773 LOG(FATAL) << "Unimplemented compare type " << in_type;
2774 }
2775}
2776
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002777void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002778 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002779
2780 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2781 locations->SetInAt(0, Location::RequiresFpuRegister());
2782 locations->SetInAt(1,
2783 IsFloatingPointZeroConstant(instruction->InputAt(1))
2784 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2785 : Location::RequiresFpuRegister());
2786 } else {
2787 // Integer cases.
2788 locations->SetInAt(0, Location::RequiresRegister());
2789 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2790 }
2791
David Brazdilb3e773e2016-01-26 11:28:37 +00002792 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002794 }
2795}
2796
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002797void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002798 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002799 return;
2800 }
2801
2802 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002803 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002804 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002805
Roland Levillain7f63c522015-07-13 15:54:55 +00002806 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002807 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002808 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002809 } else {
2810 // Integer cases.
2811 Register lhs = InputRegisterAt(instruction, 0);
2812 Operand rhs = InputOperandAt(instruction, 1);
2813 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002814 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002815 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002816}
2817
2818#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2819 M(Equal) \
2820 M(NotEqual) \
2821 M(LessThan) \
2822 M(LessThanOrEqual) \
2823 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002824 M(GreaterThanOrEqual) \
2825 M(Below) \
2826 M(BelowOrEqual) \
2827 M(Above) \
2828 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002829#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002830void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2831void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002832FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002833#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002834#undef FOR_EACH_CONDITION_INSTRUCTION
2835
Zheng Xuc6667102015-05-15 16:08:45 +08002836void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2837 DCHECK(instruction->IsDiv() || instruction->IsRem());
2838
2839 LocationSummary* locations = instruction->GetLocations();
2840 Location second = locations->InAt(1);
2841 DCHECK(second.IsConstant());
2842
2843 Register out = OutputRegister(instruction);
2844 Register dividend = InputRegisterAt(instruction, 0);
2845 int64_t imm = Int64FromConstant(second.GetConstant());
2846 DCHECK(imm == 1 || imm == -1);
2847
2848 if (instruction->IsRem()) {
2849 __ Mov(out, 0);
2850 } else {
2851 if (imm == 1) {
2852 __ Mov(out, dividend);
2853 } else {
2854 __ Neg(out, dividend);
2855 }
2856 }
2857}
2858
2859void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2860 DCHECK(instruction->IsDiv() || instruction->IsRem());
2861
2862 LocationSummary* locations = instruction->GetLocations();
2863 Location second = locations->InAt(1);
2864 DCHECK(second.IsConstant());
2865
2866 Register out = OutputRegister(instruction);
2867 Register dividend = InputRegisterAt(instruction, 0);
2868 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002869 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002870 int ctz_imm = CTZ(abs_imm);
2871
2872 UseScratchRegisterScope temps(GetVIXLAssembler());
2873 Register temp = temps.AcquireSameSizeAs(out);
2874
2875 if (instruction->IsDiv()) {
2876 __ Add(temp, dividend, abs_imm - 1);
2877 __ Cmp(dividend, 0);
2878 __ Csel(out, temp, dividend, lt);
2879 if (imm > 0) {
2880 __ Asr(out, out, ctz_imm);
2881 } else {
2882 __ Neg(out, Operand(out, ASR, ctz_imm));
2883 }
2884 } else {
2885 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2886 __ Asr(temp, dividend, bits - 1);
2887 __ Lsr(temp, temp, bits - ctz_imm);
2888 __ Add(out, dividend, temp);
2889 __ And(out, out, abs_imm - 1);
2890 __ Sub(out, out, temp);
2891 }
2892}
2893
2894void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2895 DCHECK(instruction->IsDiv() || instruction->IsRem());
2896
2897 LocationSummary* locations = instruction->GetLocations();
2898 Location second = locations->InAt(1);
2899 DCHECK(second.IsConstant());
2900
2901 Register out = OutputRegister(instruction);
2902 Register dividend = InputRegisterAt(instruction, 0);
2903 int64_t imm = Int64FromConstant(second.GetConstant());
2904
2905 Primitive::Type type = instruction->GetResultType();
2906 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2907
2908 int64_t magic;
2909 int shift;
2910 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2911
2912 UseScratchRegisterScope temps(GetVIXLAssembler());
2913 Register temp = temps.AcquireSameSizeAs(out);
2914
2915 // temp = get_high(dividend * magic)
2916 __ Mov(temp, magic);
2917 if (type == Primitive::kPrimLong) {
2918 __ Smulh(temp, dividend, temp);
2919 } else {
2920 __ Smull(temp.X(), dividend, temp);
2921 __ Lsr(temp.X(), temp.X(), 32);
2922 }
2923
2924 if (imm > 0 && magic < 0) {
2925 __ Add(temp, temp, dividend);
2926 } else if (imm < 0 && magic > 0) {
2927 __ Sub(temp, temp, dividend);
2928 }
2929
2930 if (shift != 0) {
2931 __ Asr(temp, temp, shift);
2932 }
2933
2934 if (instruction->IsDiv()) {
2935 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2936 } else {
2937 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2938 // TODO: Strength reduction for msub.
2939 Register temp_imm = temps.AcquireSameSizeAs(out);
2940 __ Mov(temp_imm, imm);
2941 __ Msub(out, temp, temp_imm, dividend);
2942 }
2943}
2944
2945void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2946 DCHECK(instruction->IsDiv() || instruction->IsRem());
2947 Primitive::Type type = instruction->GetResultType();
2948 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2949
2950 LocationSummary* locations = instruction->GetLocations();
2951 Register out = OutputRegister(instruction);
2952 Location second = locations->InAt(1);
2953
2954 if (second.IsConstant()) {
2955 int64_t imm = Int64FromConstant(second.GetConstant());
2956
2957 if (imm == 0) {
2958 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2959 } else if (imm == 1 || imm == -1) {
2960 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002961 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002962 DivRemByPowerOfTwo(instruction);
2963 } else {
2964 DCHECK(imm <= -2 || imm >= 2);
2965 GenerateDivRemWithAnyConstant(instruction);
2966 }
2967 } else {
2968 Register dividend = InputRegisterAt(instruction, 0);
2969 Register divisor = InputRegisterAt(instruction, 1);
2970 if (instruction->IsDiv()) {
2971 __ Sdiv(out, dividend, divisor);
2972 } else {
2973 UseScratchRegisterScope temps(GetVIXLAssembler());
2974 Register temp = temps.AcquireSameSizeAs(out);
2975 __ Sdiv(temp, dividend, divisor);
2976 __ Msub(out, temp, divisor, dividend);
2977 }
2978 }
2979}
2980
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002981void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2982 LocationSummary* locations =
2983 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2984 switch (div->GetResultType()) {
2985 case Primitive::kPrimInt:
2986 case Primitive::kPrimLong:
2987 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002988 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2990 break;
2991
2992 case Primitive::kPrimFloat:
2993 case Primitive::kPrimDouble:
2994 locations->SetInAt(0, Location::RequiresFpuRegister());
2995 locations->SetInAt(1, Location::RequiresFpuRegister());
2996 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2997 break;
2998
2999 default:
3000 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3001 }
3002}
3003
3004void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3005 Primitive::Type type = div->GetResultType();
3006 switch (type) {
3007 case Primitive::kPrimInt:
3008 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003009 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003010 break;
3011
3012 case Primitive::kPrimFloat:
3013 case Primitive::kPrimDouble:
3014 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3015 break;
3016
3017 default:
3018 LOG(FATAL) << "Unexpected div type " << type;
3019 }
3020}
3021
Alexandre Rames67555f72014-11-18 10:55:16 +00003022void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003023 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003024 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003025}
3026
3027void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3028 SlowPathCodeARM64* slow_path =
3029 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3030 codegen_->AddSlowPath(slow_path);
3031 Location value = instruction->GetLocations()->InAt(0);
3032
Alexandre Rames3e69f162014-12-10 10:36:50 +00003033 Primitive::Type type = instruction->GetType();
3034
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003035 if (!Primitive::IsIntegralType(type)) {
3036 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003037 return;
3038 }
3039
Alexandre Rames67555f72014-11-18 10:55:16 +00003040 if (value.IsConstant()) {
3041 int64_t divisor = Int64ConstantFrom(value);
3042 if (divisor == 0) {
3043 __ B(slow_path->GetEntryLabel());
3044 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003045 // A division by a non-null constant is valid. We don't need to perform
3046 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003047 }
3048 } else {
3049 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3050 }
3051}
3052
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003053void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3054 LocationSummary* locations =
3055 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3056 locations->SetOut(Location::ConstantLocation(constant));
3057}
3058
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003059void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3060 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003061 // Will be generated at use site.
3062}
3063
Alexandre Rames5319def2014-10-23 10:03:10 +01003064void LocationsBuilderARM64::VisitExit(HExit* exit) {
3065 exit->SetLocations(nullptr);
3066}
3067
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003068void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003069}
3070
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003071void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3072 LocationSummary* locations =
3073 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3074 locations->SetOut(Location::ConstantLocation(constant));
3075}
3076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003077void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003078 // Will be generated at use site.
3079}
3080
David Brazdilfc6a86a2015-06-26 10:33:45 +00003081void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003082 DCHECK(!successor->IsExitBlock());
3083 HBasicBlock* block = got->GetBlock();
3084 HInstruction* previous = got->GetPrevious();
3085 HLoopInformation* info = block->GetLoopInformation();
3086
David Brazdil46e2a392015-03-16 17:31:52 +00003087 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003088 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3089 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3090 return;
3091 }
3092 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3093 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3094 }
3095 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003096 __ B(codegen_->GetLabelOf(successor));
3097 }
3098}
3099
David Brazdilfc6a86a2015-06-26 10:33:45 +00003100void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3101 got->SetLocations(nullptr);
3102}
3103
3104void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3105 HandleGoto(got, got->GetSuccessor());
3106}
3107
3108void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3109 try_boundary->SetLocations(nullptr);
3110}
3111
3112void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3113 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3114 if (!successor->IsExitBlock()) {
3115 HandleGoto(try_boundary, successor);
3116 }
3117}
3118
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003119void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003120 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003121 vixl::aarch64::Label* true_target,
3122 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003123 // FP branching requires both targets to be explicit. If either of the targets
3124 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003125 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003126 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003127
David Brazdil0debae72015-11-12 18:37:00 +00003128 if (true_target == nullptr && false_target == nullptr) {
3129 // Nothing to do. The code always falls through.
3130 return;
3131 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003132 // Constant condition, statically compared against "true" (integer value 1).
3133 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003134 if (true_target != nullptr) {
3135 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003136 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003137 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003138 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003139 if (false_target != nullptr) {
3140 __ B(false_target);
3141 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003142 }
David Brazdil0debae72015-11-12 18:37:00 +00003143 return;
3144 }
3145
3146 // The following code generates these patterns:
3147 // (1) true_target == nullptr && false_target != nullptr
3148 // - opposite condition true => branch to false_target
3149 // (2) true_target != nullptr && false_target == nullptr
3150 // - condition true => branch to true_target
3151 // (3) true_target != nullptr && false_target != nullptr
3152 // - condition true => branch to true_target
3153 // - branch to false_target
3154 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003155 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003156 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003157 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003158 if (true_target == nullptr) {
3159 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3160 } else {
3161 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3162 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003163 } else {
3164 // The condition instruction has not been materialized, use its inputs as
3165 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003166 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003167
David Brazdil0debae72015-11-12 18:37:00 +00003168 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003169 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003170 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003171 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003172 IfCondition opposite_condition = condition->GetOppositeCondition();
3173 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003174 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003175 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003176 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003177 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003178 // Integer cases.
3179 Register lhs = InputRegisterAt(condition, 0);
3180 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003181
3182 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003183 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003184 if (true_target == nullptr) {
3185 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3186 non_fallthrough_target = false_target;
3187 } else {
3188 arm64_cond = ARM64Condition(condition->GetCondition());
3189 non_fallthrough_target = true_target;
3190 }
3191
Aart Bik086d27e2016-01-20 17:02:00 -08003192 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003193 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003194 switch (arm64_cond) {
3195 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003196 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003197 break;
3198 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003199 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003200 break;
3201 case lt:
3202 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003203 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003204 break;
3205 case ge:
3206 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003207 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003208 break;
3209 default:
3210 // Without the `static_cast` the compiler throws an error for
3211 // `-Werror=sign-promo`.
3212 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3213 }
3214 } else {
3215 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003216 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003217 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003218 }
3219 }
David Brazdil0debae72015-11-12 18:37:00 +00003220
3221 // If neither branch falls through (case 3), the conditional branch to `true_target`
3222 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3223 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003224 __ B(false_target);
3225 }
David Brazdil0debae72015-11-12 18:37:00 +00003226
3227 if (fallthrough_target.IsLinked()) {
3228 __ Bind(&fallthrough_target);
3229 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003230}
3231
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003234 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003235 locations->SetInAt(0, Location::RequiresRegister());
3236 }
3237}
3238
3239void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003240 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3241 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003242 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3243 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3244 true_target = nullptr;
3245 }
3246 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3247 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3248 false_target = nullptr;
3249 }
David Brazdil0debae72015-11-12 18:37:00 +00003250 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003251}
3252
3253void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3254 LocationSummary* locations = new (GetGraph()->GetArena())
3255 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003256 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003257 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003258 locations->SetInAt(0, Location::RequiresRegister());
3259 }
3260}
3261
3262void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003263 SlowPathCodeARM64* slow_path =
3264 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003265 GenerateTestAndBranch(deoptimize,
3266 /* condition_input_index */ 0,
3267 slow_path->GetEntryLabel(),
3268 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003269}
3270
Mingyao Yang063fc772016-08-02 11:02:54 -07003271void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3272 LocationSummary* locations = new (GetGraph()->GetArena())
3273 LocationSummary(flag, LocationSummary::kNoCall);
3274 locations->SetOut(Location::RequiresRegister());
3275}
3276
3277void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3278 __ Ldr(OutputRegister(flag),
3279 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3280}
3281
David Brazdilc0b601b2016-02-08 14:20:45 +00003282static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3283 return condition->IsCondition() &&
3284 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3285}
3286
Alexandre Rames880f1192016-06-13 16:04:50 +01003287static inline Condition GetConditionForSelect(HCondition* condition) {
3288 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003289 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3290 : ARM64Condition(cond);
3291}
3292
David Brazdil74eb1b22015-12-14 11:44:01 +00003293void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3294 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003295 if (Primitive::IsFloatingPointType(select->GetType())) {
3296 locations->SetInAt(0, Location::RequiresFpuRegister());
3297 locations->SetInAt(1, Location::RequiresFpuRegister());
3298 locations->SetOut(Location::RequiresFpuRegister());
3299 } else {
3300 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3301 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3302 bool is_true_value_constant = cst_true_value != nullptr;
3303 bool is_false_value_constant = cst_false_value != nullptr;
3304 // Ask VIXL whether we should synthesize constants in registers.
3305 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3306 Operand true_op = is_true_value_constant ?
3307 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3308 Operand false_op = is_false_value_constant ?
3309 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3310 bool true_value_in_register = false;
3311 bool false_value_in_register = false;
3312 MacroAssembler::GetCselSynthesisInformation(
3313 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3314 true_value_in_register |= !is_true_value_constant;
3315 false_value_in_register |= !is_false_value_constant;
3316
3317 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3318 : Location::ConstantLocation(cst_true_value));
3319 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3320 : Location::ConstantLocation(cst_false_value));
3321 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003322 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003323
David Brazdil74eb1b22015-12-14 11:44:01 +00003324 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3325 locations->SetInAt(2, Location::RequiresRegister());
3326 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003327}
3328
3329void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003330 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003331 Condition csel_cond;
3332
3333 if (IsBooleanValueOrMaterializedCondition(cond)) {
3334 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003335 // Use the condition flags set by the previous instruction.
3336 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003337 } else {
3338 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003339 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003340 }
3341 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003342 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003343 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003344 } else {
3345 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003346 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003347 }
3348
Alexandre Rames880f1192016-06-13 16:04:50 +01003349 if (Primitive::IsFloatingPointType(select->GetType())) {
3350 __ Fcsel(OutputFPRegister(select),
3351 InputFPRegisterAt(select, 1),
3352 InputFPRegisterAt(select, 0),
3353 csel_cond);
3354 } else {
3355 __ Csel(OutputRegister(select),
3356 InputOperandAt(select, 1),
3357 InputOperandAt(select, 0),
3358 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003359 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003360}
3361
David Srbecky0cf44932015-12-09 14:09:59 +00003362void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3363 new (GetGraph()->GetArena()) LocationSummary(info);
3364}
3365
David Srbeckyd28f4a02016-03-14 17:14:24 +00003366void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3367 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003368}
3369
3370void CodeGeneratorARM64::GenerateNop() {
3371 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003372}
3373
Alexandre Rames5319def2014-10-23 10:03:10 +01003374void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003375 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003376}
3377
3378void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003379 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003380}
3381
3382void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003383 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003384}
3385
3386void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003387 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003388}
3389
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003390// Temp is used for read barrier.
3391static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3392 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003393 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003394 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3395 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3396 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3397 return 1;
3398 }
3399 return 0;
3400}
3401
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003402// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003403// interface pointer, one for loading the current interface.
3404// The other checks have one temp for loading the object's class.
3405static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3406 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3407 return 3;
3408 }
3409 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003410}
3411
Alexandre Rames67555f72014-11-18 10:55:16 +00003412void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003413 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003414 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003415 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003416 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003417 case TypeCheckKind::kExactCheck:
3418 case TypeCheckKind::kAbstractClassCheck:
3419 case TypeCheckKind::kClassHierarchyCheck:
3420 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003421 call_kind =
3422 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003423 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003424 break;
3425 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426 case TypeCheckKind::kUnresolvedCheck:
3427 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003428 call_kind = LocationSummary::kCallOnSlowPath;
3429 break;
3430 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431
Alexandre Rames67555f72014-11-18 10:55:16 +00003432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003433 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003434 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003435 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436 locations->SetInAt(0, Location::RequiresRegister());
3437 locations->SetInAt(1, Location::RequiresRegister());
3438 // The "out" register is used as a temporary, so it overlaps with the inputs.
3439 // Note that TypeCheckSlowPathARM64 uses this register too.
3440 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003441 // Add temps if necessary for read barriers.
3442 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003443}
3444
3445void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003446 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003447 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003449 Register obj = InputRegisterAt(instruction, 0);
3450 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003451 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003452 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003453 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3454 DCHECK_LE(num_temps, 1u);
3455 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003456 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3457 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3458 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3459 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003460
Scott Wakeling97c72b72016-06-24 16:19:36 +01003461 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003462 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003463
3464 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003465 // Avoid null check if we know `obj` is not null.
3466 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003467 __ Cbz(obj, &zero);
3468 }
3469
Roland Levillain44015862016-01-22 11:47:17 +00003470 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003471 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003472 // /* HeapReference<Class> */ out = obj->klass_
3473 GenerateReferenceLoadTwoRegisters(instruction,
3474 out_loc,
3475 obj_loc,
3476 class_offset,
3477 maybe_temp_loc,
3478 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003479 __ Cmp(out, cls);
3480 __ Cset(out, eq);
3481 if (zero.IsLinked()) {
3482 __ B(&done);
3483 }
3484 break;
3485 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003486
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003487 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003488 // /* HeapReference<Class> */ out = obj->klass_
3489 GenerateReferenceLoadTwoRegisters(instruction,
3490 out_loc,
3491 obj_loc,
3492 class_offset,
3493 maybe_temp_loc,
3494 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 // If the class is abstract, we eagerly fetch the super class of the
3496 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003497 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003498 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003499 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003500 GenerateReferenceLoadOneRegister(instruction,
3501 out_loc,
3502 super_offset,
3503 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003504 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003505 // If `out` is null, we use it for the result, and jump to `done`.
3506 __ Cbz(out, &done);
3507 __ Cmp(out, cls);
3508 __ B(ne, &loop);
3509 __ Mov(out, 1);
3510 if (zero.IsLinked()) {
3511 __ B(&done);
3512 }
3513 break;
3514 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003516 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003517 // /* HeapReference<Class> */ out = obj->klass_
3518 GenerateReferenceLoadTwoRegisters(instruction,
3519 out_loc,
3520 obj_loc,
3521 class_offset,
3522 maybe_temp_loc,
3523 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003524 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003525 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003526 __ Bind(&loop);
3527 __ Cmp(out, cls);
3528 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003530 GenerateReferenceLoadOneRegister(instruction,
3531 out_loc,
3532 super_offset,
3533 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003534 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003535 __ Cbnz(out, &loop);
3536 // If `out` is null, we use it for the result, and jump to `done`.
3537 __ B(&done);
3538 __ Bind(&success);
3539 __ Mov(out, 1);
3540 if (zero.IsLinked()) {
3541 __ B(&done);
3542 }
3543 break;
3544 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003545
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003547 // /* HeapReference<Class> */ out = obj->klass_
3548 GenerateReferenceLoadTwoRegisters(instruction,
3549 out_loc,
3550 obj_loc,
3551 class_offset,
3552 maybe_temp_loc,
3553 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003554 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003555 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003556 __ Cmp(out, cls);
3557 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003558 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003559 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003560 GenerateReferenceLoadOneRegister(instruction,
3561 out_loc,
3562 component_offset,
3563 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003564 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003565 // If `out` is null, we use it for the result, and jump to `done`.
3566 __ Cbz(out, &done);
3567 __ Ldrh(out, HeapOperand(out, primitive_offset));
3568 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3569 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003570 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003571 __ Mov(out, 1);
3572 __ B(&done);
3573 break;
3574 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003575
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003576 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003577 // No read barrier since the slow path will retry upon failure.
3578 // /* HeapReference<Class> */ out = obj->klass_
3579 GenerateReferenceLoadTwoRegisters(instruction,
3580 out_loc,
3581 obj_loc,
3582 class_offset,
3583 maybe_temp_loc,
3584 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003585 __ Cmp(out, cls);
3586 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003587 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3588 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003589 codegen_->AddSlowPath(slow_path);
3590 __ B(ne, slow_path->GetEntryLabel());
3591 __ Mov(out, 1);
3592 if (zero.IsLinked()) {
3593 __ B(&done);
3594 }
3595 break;
3596 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003597
Calin Juravle98893e12015-10-02 21:05:03 +01003598 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003599 case TypeCheckKind::kInterfaceCheck: {
3600 // Note that we indeed only call on slow path, but we always go
3601 // into the slow path for the unresolved and interface check
3602 // cases.
3603 //
3604 // We cannot directly call the InstanceofNonTrivial runtime
3605 // entry point without resorting to a type checking slow path
3606 // here (i.e. by calling InvokeRuntime directly), as it would
3607 // require to assign fixed registers for the inputs of this
3608 // HInstanceOf instruction (following the runtime calling
3609 // convention), which might be cluttered by the potential first
3610 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003611 //
3612 // TODO: Introduce a new runtime entry point taking the object
3613 // to test (instead of its class) as argument, and let it deal
3614 // with the read barrier issues. This will let us refactor this
3615 // case of the `switch` code as it was previously (with a direct
3616 // call to the runtime not using a type checking slow path).
3617 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003618 DCHECK(locations->OnlyCallsOnSlowPath());
3619 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3620 /* is_fatal */ false);
3621 codegen_->AddSlowPath(slow_path);
3622 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003623 if (zero.IsLinked()) {
3624 __ B(&done);
3625 }
3626 break;
3627 }
3628 }
3629
3630 if (zero.IsLinked()) {
3631 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003632 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003633 }
3634
3635 if (done.IsLinked()) {
3636 __ Bind(&done);
3637 }
3638
3639 if (slow_path != nullptr) {
3640 __ Bind(slow_path->GetExitLabel());
3641 }
3642}
3643
3644void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3645 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3646 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3647
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003648 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3649 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003650 case TypeCheckKind::kExactCheck:
3651 case TypeCheckKind::kAbstractClassCheck:
3652 case TypeCheckKind::kClassHierarchyCheck:
3653 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003654 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3655 LocationSummary::kCallOnSlowPath :
3656 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003657 break;
3658 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003659 case TypeCheckKind::kUnresolvedCheck:
3660 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003661 call_kind = LocationSummary::kCallOnSlowPath;
3662 break;
3663 }
3664
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3666 locations->SetInAt(0, Location::RequiresRegister());
3667 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003668 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3669 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003670}
3671
3672void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003673 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003674 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003675 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003676 Register obj = InputRegisterAt(instruction, 0);
3677 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003678 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3679 DCHECK_GE(num_temps, 1u);
3680 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003681 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003682 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3683 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003684 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003685 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3686 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3687 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3688 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3689 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3690 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3691 const uint32_t object_array_data_offset =
3692 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003693
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003694 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003695 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3696 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3697 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003698 if (!kEmitCompilerReadBarrier) {
3699 is_type_check_slow_path_fatal =
3700 (type_check_kind == TypeCheckKind::kExactCheck ||
3701 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3702 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3703 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3704 !instruction->CanThrowIntoCatchBlock();
3705 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706 SlowPathCodeARM64* type_check_slow_path =
3707 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3708 is_type_check_slow_path_fatal);
3709 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003710
Scott Wakeling97c72b72016-06-24 16:19:36 +01003711 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003712 // Avoid null check if we know obj is not null.
3713 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003714 __ Cbz(obj, &done);
3715 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003716
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003717 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003718 case TypeCheckKind::kExactCheck:
3719 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003720 // /* HeapReference<Class> */ temp = obj->klass_
3721 GenerateReferenceLoadTwoRegisters(instruction,
3722 temp_loc,
3723 obj_loc,
3724 class_offset,
3725 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003726 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003727
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003728 __ Cmp(temp, cls);
3729 // Jump to slow path for throwing the exception or doing a
3730 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003731 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003732 break;
3733 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003734
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003735 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003736 // /* HeapReference<Class> */ temp = obj->klass_
3737 GenerateReferenceLoadTwoRegisters(instruction,
3738 temp_loc,
3739 obj_loc,
3740 class_offset,
3741 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003742 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003744 // If the class is abstract, we eagerly fetch the super class of the
3745 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003746 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003747 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003748 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003749 GenerateReferenceLoadOneRegister(instruction,
3750 temp_loc,
3751 super_offset,
3752 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003753 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003754
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003755 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3756 // exception.
3757 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3758 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003759 __ Cmp(temp, cls);
3760 __ B(ne, &loop);
3761 break;
3762 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003763
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003764 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003765 // /* HeapReference<Class> */ temp = obj->klass_
3766 GenerateReferenceLoadTwoRegisters(instruction,
3767 temp_loc,
3768 obj_loc,
3769 class_offset,
3770 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003771 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003773 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003774 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003775 __ Bind(&loop);
3776 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003777 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003778
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003779 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003780 GenerateReferenceLoadOneRegister(instruction,
3781 temp_loc,
3782 super_offset,
3783 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003784 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785
3786 // If the class reference currently in `temp` is not null, jump
3787 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003788 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003789 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003790 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003791 break;
3792 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003793
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003794 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003795 // /* HeapReference<Class> */ temp = obj->klass_
3796 GenerateReferenceLoadTwoRegisters(instruction,
3797 temp_loc,
3798 obj_loc,
3799 class_offset,
3800 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003801 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003802
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003803 // Do an exact check.
3804 __ Cmp(temp, cls);
3805 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003806
3807 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003808 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003809 GenerateReferenceLoadOneRegister(instruction,
3810 temp_loc,
3811 component_offset,
3812 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003813 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003814
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003815 // If the component type is null, jump to the slow path to throw the exception.
3816 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3817 // Otherwise, the object is indeed an array. Further check that this component type is not a
3818 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003819 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3820 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003821 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003822 break;
3823 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003824
Calin Juravle98893e12015-10-02 21:05:03 +01003825 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003826 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003827 //
3828 // We cannot directly call the CheckCast runtime entry point
3829 // without resorting to a type checking slow path here (i.e. by
3830 // calling InvokeRuntime directly), as it would require to
3831 // assign fixed registers for the inputs of this HInstanceOf
3832 // instruction (following the runtime calling convention), which
3833 // might be cluttered by the potential first read barrier
3834 // emission at the beginning of this method.
3835 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003836 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003837 case TypeCheckKind::kInterfaceCheck: {
3838 // /* HeapReference<Class> */ temp = obj->klass_
3839 GenerateReferenceLoadTwoRegisters(instruction,
3840 temp_loc,
3841 obj_loc,
3842 class_offset,
3843 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003844 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003845
3846 // /* HeapReference<Class> */ temp = temp->iftable_
3847 GenerateReferenceLoadTwoRegisters(instruction,
3848 temp_loc,
3849 temp_loc,
3850 iftable_offset,
3851 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003852 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003853 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003854 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003855 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003856 vixl::aarch64::Label start_loop;
3857 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003858 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003859 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3860 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003861 // Go to next interface.
3862 __ Add(temp, temp, 2 * kHeapReferenceSize);
3863 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003864 // Compare the classes and continue the loop if they do not match.
3865 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3866 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003867 break;
3868 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003869 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003870 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003871
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003872 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003873}
3874
Alexandre Rames5319def2014-10-23 10:03:10 +01003875void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3877 locations->SetOut(Location::ConstantLocation(constant));
3878}
3879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003880void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003881 // Will be generated at use site.
3882}
3883
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003884void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3886 locations->SetOut(Location::ConstantLocation(constant));
3887}
3888
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003889void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003890 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003891}
3892
Calin Juravle175dc732015-08-25 15:42:32 +01003893void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3894 // The trampoline uses the same calling convention as dex calling conventions,
3895 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3896 // the method_idx.
3897 HandleInvoke(invoke);
3898}
3899
3900void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3901 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3902}
3903
Alexandre Rames5319def2014-10-23 10:03:10 +01003904void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003905 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003906 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003907}
3908
Alexandre Rames67555f72014-11-18 10:55:16 +00003909void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3910 HandleInvoke(invoke);
3911}
3912
3913void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3914 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915 LocationSummary* locations = invoke->GetLocations();
3916 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003917 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003918 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003919 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003920
3921 // The register ip1 is required to be used for the hidden argument in
3922 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003923 MacroAssembler* masm = GetVIXLAssembler();
3924 UseScratchRegisterScope scratch_scope(masm);
3925 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003926 scratch_scope.Exclude(ip1);
3927 __ Mov(ip1, invoke->GetDexMethodIndex());
3928
Alexandre Rames67555f72014-11-18 10:55:16 +00003929 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003930 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003931 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003932 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003933 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003934 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003935 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003936 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003937 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003938 // Instead of simply (possibly) unpoisoning `temp` here, we should
3939 // emit a read barrier for the previous class reference load.
3940 // However this is not required in practice, as this is an
3941 // intermediate/temporary reference and because the current
3942 // concurrent copying collector keeps the from-space memory
3943 // intact/accessible until the end of the marking phase (the
3944 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003945 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003946 __ Ldr(temp,
3947 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3948 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003949 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003950 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003951 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003952 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003953 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003954 // lr();
3955 __ Blr(lr);
3956 DCHECK(!codegen_->IsLeafMethod());
3957 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3958}
3959
3960void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003961 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3962 if (intrinsic.TryDispatch(invoke)) {
3963 return;
3964 }
3965
Alexandre Rames67555f72014-11-18 10:55:16 +00003966 HandleInvoke(invoke);
3967}
3968
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003969void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003970 // Explicit clinit checks triggered by static invokes must have been pruned by
3971 // art::PrepareForRegisterAllocation.
3972 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003973
Andreas Gampe878d58c2015-01-15 23:24:00 -08003974 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3975 if (intrinsic.TryDispatch(invoke)) {
3976 return;
3977 }
3978
Alexandre Rames67555f72014-11-18 10:55:16 +00003979 HandleInvoke(invoke);
3980}
3981
Andreas Gampe878d58c2015-01-15 23:24:00 -08003982static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3983 if (invoke->GetLocations()->Intrinsified()) {
3984 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3985 intrinsic.Dispatch(invoke);
3986 return true;
3987 }
3988 return false;
3989}
3990
Vladimir Markodc151b22015-10-15 18:02:30 +01003991HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3992 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003993 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003994 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003995 return desired_dispatch_info;
3996}
3997
TatWai Chongd8c052a2016-11-02 16:12:48 +08003998Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
3999 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004000 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004001 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4002 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004003 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4004 uint32_t offset =
4005 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004006 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004007 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004008 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004009 }
Vladimir Marko58155012015-08-19 12:49:41 +00004010 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004011 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004012 break;
4013 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4014 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004015 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004016 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004017 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4018 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004019 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004020 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004021 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004022 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004023 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004024 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004025 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004026 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004027 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004028 }
Vladimir Marko58155012015-08-19 12:49:41 +00004029 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004030 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004031 Register reg = XRegisterFrom(temp);
4032 Register method_reg;
4033 if (current_method.IsRegister()) {
4034 method_reg = XRegisterFrom(current_method);
4035 } else {
4036 DCHECK(invoke->GetLocations()->Intrinsified());
4037 DCHECK(!current_method.IsValid());
4038 method_reg = reg;
4039 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4040 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004041
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004042 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004043 __ Ldr(reg.X(),
4044 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004045 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004046 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004047 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4048 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004049 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4050 break;
4051 }
4052 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004053 return callee_method;
4054}
4055
4056void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4057 // All registers are assumed to be correctly set up.
4058 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004059
4060 switch (invoke->GetCodePtrLocation()) {
4061 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4062 __ Bl(&frame_entry_label_);
4063 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004064 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4065 // LR = callee_method->entry_point_from_quick_compiled_code_;
4066 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004067 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004068 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004069 // lr()
4070 __ Blr(lr);
4071 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004072 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004073
Andreas Gampe878d58c2015-01-15 23:24:00 -08004074 DCHECK(!IsLeafMethod());
4075}
4076
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004077void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004078 // Use the calling convention instead of the location of the receiver, as
4079 // intrinsics may have put the receiver in a different register. In the intrinsics
4080 // slow path, the arguments have been moved to the right place, so here we are
4081 // guaranteed that the receiver is the first register of the calling convention.
4082 InvokeDexCallingConvention calling_convention;
4083 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004084 Register temp = XRegisterFrom(temp_in);
4085 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4086 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4087 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004088 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004089
4090 BlockPoolsScope block_pools(GetVIXLAssembler());
4091
4092 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004093 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004094 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004095 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004096 // Instead of simply (possibly) unpoisoning `temp` here, we should
4097 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004098 // intermediate/temporary reference and because the current
4099 // concurrent copying collector keeps the from-space memory
4100 // intact/accessible until the end of the marking phase (the
4101 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004102 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4103 // temp = temp->GetMethodAt(method_offset);
4104 __ Ldr(temp, MemOperand(temp, method_offset));
4105 // lr = temp->GetEntryPoint();
4106 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4107 // lr();
4108 __ Blr(lr);
4109}
4110
Orion Hodsonac141392017-01-13 11:53:47 +00004111void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4112 HandleInvoke(invoke);
4113}
4114
4115void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4116 codegen_->GenerateInvokePolymorphicCall(invoke);
4117}
4118
Scott Wakeling97c72b72016-06-24 16:19:36 +01004119vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4120 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004121 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004122 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004123 return
4124 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004125}
4126
Scott Wakeling97c72b72016-06-24 16:19:36 +01004127vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4128 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004129 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004130 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004131 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004132}
4133
Vladimir Marko1998cd02017-01-13 13:02:58 +00004134vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4135 const DexFile& dex_file,
4136 dex::TypeIndex type_index,
4137 vixl::aarch64::Label* adrp_label) {
4138 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4139}
4140
Scott Wakeling97c72b72016-06-24 16:19:36 +01004141vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4142 const DexFile& dex_file,
4143 uint32_t element_offset,
4144 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004145 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4146}
4147
Scott Wakeling97c72b72016-06-24 16:19:36 +01004148vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4149 const DexFile& dex_file,
4150 uint32_t offset_or_index,
4151 vixl::aarch64::Label* adrp_label,
4152 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004153 // Add a patch entry and return the label.
4154 patches->emplace_back(dex_file, offset_or_index);
4155 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004156 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004157 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4158 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4159 return label;
4160}
4161
Scott Wakeling97c72b72016-06-24 16:19:36 +01004162vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004163 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004164 return boot_image_string_patches_.GetOrCreate(
4165 StringReference(&dex_file, string_index),
4166 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4167}
4168
Scott Wakeling97c72b72016-06-24 16:19:36 +01004169vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004170 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004171 return boot_image_type_patches_.GetOrCreate(
4172 TypeReference(&dex_file, type_index),
4173 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4174}
4175
Scott Wakeling97c72b72016-06-24 16:19:36 +01004176vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4177 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004178 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4179 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4180 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4181}
4182
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004183vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004184 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4185 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4186 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004187 return jit_string_patches_.GetOrCreate(
4188 StringReference(&dex_file, string_index),
4189 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4190}
4191
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004192vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004193 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4194 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4195 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004196 return jit_class_patches_.GetOrCreate(
4197 TypeReference(&dex_file, type_index),
4198 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4199}
4200
Vladimir Markoaad75c62016-10-03 08:46:48 +00004201void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4202 vixl::aarch64::Register reg) {
4203 DCHECK(reg.IsX());
4204 SingleEmissionCheckScope guard(GetVIXLAssembler());
4205 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004206 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004207}
4208
4209void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4210 vixl::aarch64::Register out,
4211 vixl::aarch64::Register base) {
4212 DCHECK(out.IsX());
4213 DCHECK(base.IsX());
4214 SingleEmissionCheckScope guard(GetVIXLAssembler());
4215 __ Bind(fixup_label);
4216 __ add(out, base, Operand(/* offset placeholder */ 0));
4217}
4218
4219void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4220 vixl::aarch64::Register out,
4221 vixl::aarch64::Register base) {
4222 DCHECK(base.IsX());
4223 SingleEmissionCheckScope guard(GetVIXLAssembler());
4224 __ Bind(fixup_label);
4225 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4226}
4227
4228template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4229inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4230 const ArenaDeque<PcRelativePatchInfo>& infos,
4231 ArenaVector<LinkerPatch>* linker_patches) {
4232 for (const PcRelativePatchInfo& info : infos) {
4233 linker_patches->push_back(Factory(info.label.GetLocation(),
4234 &info.target_dex_file,
4235 info.pc_insn_label->GetLocation(),
4236 info.offset_or_index));
4237 }
4238}
4239
Vladimir Marko58155012015-08-19 12:49:41 +00004240void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4241 DCHECK(linker_patches->empty());
4242 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004243 pc_relative_dex_cache_patches_.size() +
4244 boot_image_string_patches_.size() +
4245 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004246 boot_image_type_patches_.size() +
4247 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004248 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004249 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004250 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004251 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004252 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004253 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004254 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004255 info.offset_or_index));
4256 }
4257 for (const auto& entry : boot_image_string_patches_) {
4258 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004259 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4260 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004261 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004262 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004263 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004264 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004265 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004266 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4267 linker_patches);
4268 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004269 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4270 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004271 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4272 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004273 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004274 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4275 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004276 for (const auto& entry : boot_image_type_patches_) {
4277 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004278 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4279 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004280 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004281 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004282 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004283 for (const auto& entry : boot_image_address_patches_) {
4284 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004285 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4286 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004287 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004288 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004289}
4290
Scott Wakeling97c72b72016-06-24 16:19:36 +01004291vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004292 Uint32ToLiteralMap* map) {
4293 return map->GetOrCreate(
4294 value,
4295 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4296}
4297
Scott Wakeling97c72b72016-06-24 16:19:36 +01004298vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004299 return uint64_literals_.GetOrCreate(
4300 value,
4301 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004302}
4303
Scott Wakeling97c72b72016-06-24 16:19:36 +01004304vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004305 MethodReference target_method,
4306 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004307 return map->GetOrCreate(
4308 target_method,
4309 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004310}
4311
Andreas Gampe878d58c2015-01-15 23:24:00 -08004312void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004313 // Explicit clinit checks triggered by static invokes must have been pruned by
4314 // art::PrepareForRegisterAllocation.
4315 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004316
Andreas Gampe878d58c2015-01-15 23:24:00 -08004317 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4318 return;
4319 }
4320
Alexandre Ramesd921d642015-04-16 15:07:16 +01004321 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004322 LocationSummary* locations = invoke->GetLocations();
4323 codegen_->GenerateStaticOrDirectCall(
4324 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004325 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004326}
4327
4328void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004329 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4330 return;
4331 }
4332
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004333 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004334 DCHECK(!codegen_->IsLeafMethod());
4335 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4336}
4337
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004338HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4339 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004340 switch (desired_class_load_kind) {
4341 case HLoadClass::LoadKind::kReferrersClass:
4342 break;
4343 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4344 DCHECK(!GetCompilerOptions().GetCompilePic());
4345 break;
4346 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4347 DCHECK(GetCompilerOptions().GetCompilePic());
4348 break;
4349 case HLoadClass::LoadKind::kBootImageAddress:
4350 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004351 case HLoadClass::LoadKind::kBssEntry:
4352 DCHECK(!Runtime::Current()->UseJitCompilation());
4353 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004354 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004355 DCHECK(Runtime::Current()->UseJitCompilation());
4356 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004357 case HLoadClass::LoadKind::kDexCacheViaMethod:
4358 break;
4359 }
4360 return desired_class_load_kind;
4361}
4362
Alexandre Rames67555f72014-11-18 10:55:16 +00004363void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004364 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4365 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004366 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004367 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004368 cls,
4369 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004370 LocationFrom(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004371 return;
4372 }
Vladimir Marko41559982017-01-06 14:04:23 +00004373 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004374
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004375 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4376 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004377 ? LocationSummary::kCallOnSlowPath
4378 : LocationSummary::kNoCall;
4379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004380 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004381 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004382 }
4383
Vladimir Marko41559982017-01-06 14:04:23 +00004384 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004385 locations->SetInAt(0, Location::RequiresRegister());
4386 }
4387 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004388}
4389
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004390// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4391// move.
4392void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004393 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4394 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4395 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004396 return;
4397 }
Vladimir Marko41559982017-01-06 14:04:23 +00004398 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004399
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004400 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004401 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004402
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004403 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4404 ? kWithoutReadBarrier
4405 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004406 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004407 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004408 case HLoadClass::LoadKind::kReferrersClass: {
4409 DCHECK(!cls->CanCallRuntime());
4410 DCHECK(!cls->MustGenerateClinitCheck());
4411 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4412 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004413 GenerateGcRootFieldLoad(cls,
4414 out_loc,
4415 current_method,
4416 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004417 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004418 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004419 break;
4420 }
4421 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004422 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004423 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4424 cls->GetTypeIndex()));
4425 break;
4426 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004427 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004428 // Add ADRP with its PC-relative type patch.
4429 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004430 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004431 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004432 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004433 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004434 vixl::aarch64::Label* add_label =
4435 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004436 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004437 break;
4438 }
4439 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004440 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004441 uint32_t address = dchecked_integral_cast<uint32_t>(
4442 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4443 DCHECK_NE(address, 0u);
4444 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004445 break;
4446 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004447 case HLoadClass::LoadKind::kBssEntry: {
4448 // Add ADRP with its PC-relative Class .bss entry patch.
4449 const DexFile& dex_file = cls->GetDexFile();
4450 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko1998cd02017-01-13 13:02:58 +00004451 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004452 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4453 // Add LDR with its PC-relative Class patch.
4454 vixl::aarch64::Label* ldr_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004455 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004456 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4457 GenerateGcRootFieldLoad(cls,
4458 cls->GetLocations()->Out(),
4459 out.X(),
4460 /* placeholder */ 0u,
4461 ldr_label,
4462 kCompilerReadBarrierOption);
4463 generate_null_check = true;
4464 break;
4465 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004466 case HLoadClass::LoadKind::kJitTableAddress: {
4467 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4468 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004469 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004470 GenerateGcRootFieldLoad(cls,
4471 out_loc,
4472 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004473 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004474 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004475 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004476 break;
4477 }
Vladimir Marko41559982017-01-06 14:04:23 +00004478 case HLoadClass::LoadKind::kDexCacheViaMethod:
4479 LOG(FATAL) << "UNREACHABLE";
4480 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004481 }
4482
4483 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4484 DCHECK(cls->CanCallRuntime());
4485 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4486 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4487 codegen_->AddSlowPath(slow_path);
4488 if (generate_null_check) {
4489 __ Cbz(out, slow_path->GetEntryLabel());
4490 }
4491 if (cls->MustGenerateClinitCheck()) {
4492 GenerateClassInitializationCheck(slow_path, out);
4493 } else {
4494 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004495 }
4496 }
4497}
4498
David Brazdilcb1c0552015-08-04 16:22:25 +01004499static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004500 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004501}
4502
Alexandre Rames67555f72014-11-18 10:55:16 +00004503void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4504 LocationSummary* locations =
4505 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4506 locations->SetOut(Location::RequiresRegister());
4507}
4508
4509void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004510 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4511}
4512
4513void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4514 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4515}
4516
4517void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4518 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004519}
4520
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004521HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4522 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004523 switch (desired_string_load_kind) {
4524 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4525 DCHECK(!GetCompilerOptions().GetCompilePic());
4526 break;
4527 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4528 DCHECK(GetCompilerOptions().GetCompilePic());
4529 break;
4530 case HLoadString::LoadKind::kBootImageAddress:
4531 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004532 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004533 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004534 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004535 case HLoadString::LoadKind::kJitTableAddress:
4536 DCHECK(Runtime::Current()->UseJitCompilation());
4537 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004538 case HLoadString::LoadKind::kDexCacheViaMethod:
4539 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004540 }
4541 return desired_string_load_kind;
4542}
4543
Alexandre Rames67555f72014-11-18 10:55:16 +00004544void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004545 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004547 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004548 InvokeRuntimeCallingConvention calling_convention;
4549 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4550 } else {
4551 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004552 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4553 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4554 // Rely on the pResolveString and/or marking to save everything, including temps.
4555 RegisterSet caller_saves = RegisterSet::Empty();
4556 InvokeRuntimeCallingConvention calling_convention;
4557 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4558 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4559 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4560 Primitive::kPrimNot).GetCode());
4561 locations->SetCustomSlowPathCallerSaves(caller_saves);
4562 } else {
4563 // For non-Baker read barrier we have a temp-clobbering call.
4564 }
4565 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004566 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004567}
4568
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004569// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4570// move.
4571void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004572 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004573 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004574
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004575 switch (load->GetLoadKind()) {
4576 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004577 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4578 load->GetStringIndex()));
4579 return; // No dex cache slow path.
4580 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004581 // Add ADRP with its PC-relative String patch.
4582 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004583 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004584 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004585 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004586 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004587 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004588 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004589 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004590 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004591 return; // No dex cache slow path.
4592 }
4593 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004594 uint32_t address = dchecked_integral_cast<uint32_t>(
4595 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4596 DCHECK_NE(address, 0u);
4597 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004598 return; // No dex cache slow path.
4599 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004600 case HLoadString::LoadKind::kBssEntry: {
4601 // Add ADRP with its PC-relative String .bss entry patch.
4602 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004603 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004604 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004605 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4606 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004607 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004608 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004609 // Add LDR with its PC-relative String patch.
4610 vixl::aarch64::Label* ldr_label =
4611 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004612 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004613 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004614 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004615 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004616 /* offset placeholder */ 0u,
4617 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004618 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004619 SlowPathCodeARM64* slow_path =
4620 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004621 codegen_->AddSlowPath(slow_path);
4622 __ Cbz(out.X(), slow_path->GetEntryLabel());
4623 __ Bind(slow_path->GetExitLabel());
4624 return;
4625 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004626 case HLoadString::LoadKind::kJitTableAddress: {
4627 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004628 load->GetStringIndex(),
4629 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004630 GenerateGcRootFieldLoad(load,
4631 out_loc,
4632 out.X(),
4633 /* offset */ 0,
4634 /* fixup_label */ nullptr,
4635 kCompilerReadBarrierOption);
4636 return;
4637 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004638 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004639 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004640 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004641
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004642 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004643 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004644 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004645 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004646 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4647 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004648}
4649
Alexandre Rames5319def2014-10-23 10:03:10 +01004650void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4652 locations->SetOut(Location::ConstantLocation(constant));
4653}
4654
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004655void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004656 // Will be generated at use site.
4657}
4658
Alexandre Rames67555f72014-11-18 10:55:16 +00004659void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4660 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004662 InvokeRuntimeCallingConvention calling_convention;
4663 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4664}
4665
4666void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004667 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004668 instruction,
4669 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004670 if (instruction->IsEnter()) {
4671 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4672 } else {
4673 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4674 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004675}
4676
Alexandre Rames42d641b2014-10-27 14:00:51 +00004677void LocationsBuilderARM64::VisitMul(HMul* mul) {
4678 LocationSummary* locations =
4679 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4680 switch (mul->GetResultType()) {
4681 case Primitive::kPrimInt:
4682 case Primitive::kPrimLong:
4683 locations->SetInAt(0, Location::RequiresRegister());
4684 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004685 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004686 break;
4687
4688 case Primitive::kPrimFloat:
4689 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004690 locations->SetInAt(0, Location::RequiresFpuRegister());
4691 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004692 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004693 break;
4694
4695 default:
4696 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4697 }
4698}
4699
4700void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4701 switch (mul->GetResultType()) {
4702 case Primitive::kPrimInt:
4703 case Primitive::kPrimLong:
4704 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4705 break;
4706
4707 case Primitive::kPrimFloat:
4708 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004709 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004710 break;
4711
4712 default:
4713 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4714 }
4715}
4716
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004717void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4718 LocationSummary* locations =
4719 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4720 switch (neg->GetResultType()) {
4721 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004722 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004723 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004724 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004725 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004726
4727 case Primitive::kPrimFloat:
4728 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004729 locations->SetInAt(0, Location::RequiresFpuRegister());
4730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004731 break;
4732
4733 default:
4734 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4735 }
4736}
4737
4738void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4739 switch (neg->GetResultType()) {
4740 case Primitive::kPrimInt:
4741 case Primitive::kPrimLong:
4742 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4743 break;
4744
4745 case Primitive::kPrimFloat:
4746 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004747 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004748 break;
4749
4750 default:
4751 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4752 }
4753}
4754
4755void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4756 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004758 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004759 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004760 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4761 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004762}
4763
4764void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004765 // Note: if heap poisoning is enabled, the entry point takes cares
4766 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004767 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4768 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004769}
4770
Alexandre Rames5319def2014-10-23 10:03:10 +01004771void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4772 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004773 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004774 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004775 if (instruction->IsStringAlloc()) {
4776 locations->AddTemp(LocationFrom(kArtMethodRegister));
4777 } else {
4778 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004779 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004780 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4781}
4782
4783void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004784 // Note: if heap poisoning is enabled, the entry point takes cares
4785 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004786 if (instruction->IsStringAlloc()) {
4787 // String is allocated through StringFactory. Call NewEmptyString entry point.
4788 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004789 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004790 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4791 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4792 __ Blr(lr);
4793 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4794 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004795 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004796 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004797 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004798}
4799
4800void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004802 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004804}
4805
4806void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004807 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004808 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004809 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004810 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004811 break;
4812
4813 default:
4814 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4815 }
4816}
4817
David Brazdil66d126e2015-04-03 16:02:44 +01004818void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4819 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4820 locations->SetInAt(0, Location::RequiresRegister());
4821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4822}
4823
4824void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004825 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004826}
4827
Alexandre Rames5319def2014-10-23 10:03:10 +01004828void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004829 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4830 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004831}
4832
Calin Juravle2ae48182016-03-16 14:05:09 +00004833void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4834 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004835 return;
4836 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004837
Alexandre Ramesd921d642015-04-16 15:07:16 +01004838 BlockPoolsScope block_pools(GetVIXLAssembler());
4839 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004840 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004841 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004842}
4843
Calin Juravle2ae48182016-03-16 14:05:09 +00004844void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004845 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004846 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004847
4848 LocationSummary* locations = instruction->GetLocations();
4849 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004850
4851 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004852}
4853
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004854void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004855 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004856}
4857
Alexandre Rames67555f72014-11-18 10:55:16 +00004858void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4859 HandleBinaryOp(instruction);
4860}
4861
4862void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4863 HandleBinaryOp(instruction);
4864}
4865
Alexandre Rames3e69f162014-12-10 10:36:50 +00004866void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4867 LOG(FATAL) << "Unreachable";
4868}
4869
4870void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4871 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4872}
4873
Alexandre Rames5319def2014-10-23 10:03:10 +01004874void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4876 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4877 if (location.IsStackSlot()) {
4878 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4879 } else if (location.IsDoubleStackSlot()) {
4880 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4881 }
4882 locations->SetOut(location);
4883}
4884
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004885void InstructionCodeGeneratorARM64::VisitParameterValue(
4886 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004887 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004888}
4889
4890void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004893 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004894}
4895
4896void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4897 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4898 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004899}
4900
4901void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004903 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004904 locations->SetInAt(i, Location::Any());
4905 }
4906 locations->SetOut(Location::Any());
4907}
4908
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004909void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004910 LOG(FATAL) << "Unreachable";
4911}
4912
Serban Constantinescu02164b32014-11-13 14:05:07 +00004913void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004914 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004915 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004916 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4917 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4919
4920 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004921 case Primitive::kPrimInt:
4922 case Primitive::kPrimLong:
4923 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004924 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4926 break;
4927
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004928 case Primitive::kPrimFloat:
4929 case Primitive::kPrimDouble: {
4930 InvokeRuntimeCallingConvention calling_convention;
4931 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4932 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4933 locations->SetOut(calling_convention.GetReturnLocation(type));
4934
4935 break;
4936 }
4937
Serban Constantinescu02164b32014-11-13 14:05:07 +00004938 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004939 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004940 }
4941}
4942
4943void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4944 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004945
Serban Constantinescu02164b32014-11-13 14:05:07 +00004946 switch (type) {
4947 case Primitive::kPrimInt:
4948 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004949 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004950 break;
4951 }
4952
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004953 case Primitive::kPrimFloat:
4954 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004955 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4956 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004957 if (type == Primitive::kPrimFloat) {
4958 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4959 } else {
4960 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4961 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004962 break;
4963 }
4964
Serban Constantinescu02164b32014-11-13 14:05:07 +00004965 default:
4966 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004967 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004968 }
4969}
4970
Calin Juravle27df7582015-04-17 19:12:31 +01004971void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4972 memory_barrier->SetLocations(nullptr);
4973}
4974
4975void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004976 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004977}
4978
Alexandre Rames5319def2014-10-23 10:03:10 +01004979void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4981 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004982 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004983}
4984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004985void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004986 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004987}
4988
4989void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4990 instruction->SetLocations(nullptr);
4991}
4992
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004993void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004994 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004995}
4996
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004997void LocationsBuilderARM64::VisitRor(HRor* ror) {
4998 HandleBinaryOp(ror);
4999}
5000
5001void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5002 HandleBinaryOp(ror);
5003}
5004
Serban Constantinescu02164b32014-11-13 14:05:07 +00005005void LocationsBuilderARM64::VisitShl(HShl* shl) {
5006 HandleShift(shl);
5007}
5008
5009void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5010 HandleShift(shl);
5011}
5012
5013void LocationsBuilderARM64::VisitShr(HShr* shr) {
5014 HandleShift(shr);
5015}
5016
5017void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5018 HandleShift(shr);
5019}
5020
Alexandre Rames5319def2014-10-23 10:03:10 +01005021void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005022 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005023}
5024
5025void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005026 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005027}
5028
Alexandre Rames67555f72014-11-18 10:55:16 +00005029void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005030 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005031}
5032
5033void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005034 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005035}
5036
5037void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005038 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005039}
5040
Alexandre Rames67555f72014-11-18 10:55:16 +00005041void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005042 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005043}
5044
Calin Juravlee460d1d2015-09-29 04:52:17 +01005045void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5046 HUnresolvedInstanceFieldGet* instruction) {
5047 FieldAccessCallingConventionARM64 calling_convention;
5048 codegen_->CreateUnresolvedFieldLocationSummary(
5049 instruction, instruction->GetFieldType(), calling_convention);
5050}
5051
5052void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5053 HUnresolvedInstanceFieldGet* instruction) {
5054 FieldAccessCallingConventionARM64 calling_convention;
5055 codegen_->GenerateUnresolvedFieldAccess(instruction,
5056 instruction->GetFieldType(),
5057 instruction->GetFieldIndex(),
5058 instruction->GetDexPc(),
5059 calling_convention);
5060}
5061
5062void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5063 HUnresolvedInstanceFieldSet* instruction) {
5064 FieldAccessCallingConventionARM64 calling_convention;
5065 codegen_->CreateUnresolvedFieldLocationSummary(
5066 instruction, instruction->GetFieldType(), calling_convention);
5067}
5068
5069void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5070 HUnresolvedInstanceFieldSet* instruction) {
5071 FieldAccessCallingConventionARM64 calling_convention;
5072 codegen_->GenerateUnresolvedFieldAccess(instruction,
5073 instruction->GetFieldType(),
5074 instruction->GetFieldIndex(),
5075 instruction->GetDexPc(),
5076 calling_convention);
5077}
5078
5079void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5080 HUnresolvedStaticFieldGet* instruction) {
5081 FieldAccessCallingConventionARM64 calling_convention;
5082 codegen_->CreateUnresolvedFieldLocationSummary(
5083 instruction, instruction->GetFieldType(), calling_convention);
5084}
5085
5086void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5087 HUnresolvedStaticFieldGet* instruction) {
5088 FieldAccessCallingConventionARM64 calling_convention;
5089 codegen_->GenerateUnresolvedFieldAccess(instruction,
5090 instruction->GetFieldType(),
5091 instruction->GetFieldIndex(),
5092 instruction->GetDexPc(),
5093 calling_convention);
5094}
5095
5096void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5097 HUnresolvedStaticFieldSet* instruction) {
5098 FieldAccessCallingConventionARM64 calling_convention;
5099 codegen_->CreateUnresolvedFieldLocationSummary(
5100 instruction, instruction->GetFieldType(), calling_convention);
5101}
5102
5103void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5104 HUnresolvedStaticFieldSet* instruction) {
5105 FieldAccessCallingConventionARM64 calling_convention;
5106 codegen_->GenerateUnresolvedFieldAccess(instruction,
5107 instruction->GetFieldType(),
5108 instruction->GetFieldIndex(),
5109 instruction->GetDexPc(),
5110 calling_convention);
5111}
5112
Alexandre Rames5319def2014-10-23 10:03:10 +01005113void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005114 LocationSummary* locations =
5115 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005116 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005117}
5118
5119void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005120 HBasicBlock* block = instruction->GetBlock();
5121 if (block->GetLoopInformation() != nullptr) {
5122 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5123 // The back edge will generate the suspend check.
5124 return;
5125 }
5126 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5127 // The goto will generate the suspend check.
5128 return;
5129 }
5130 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005131}
5132
Alexandre Rames67555f72014-11-18 10:55:16 +00005133void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5134 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005136 InvokeRuntimeCallingConvention calling_convention;
5137 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5138}
5139
5140void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005141 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005142 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005143}
5144
5145void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5146 LocationSummary* locations =
5147 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5148 Primitive::Type input_type = conversion->GetInputType();
5149 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005150 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005151 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5152 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5153 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5154 }
5155
Alexandre Rames542361f2015-01-29 16:57:31 +00005156 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005157 locations->SetInAt(0, Location::RequiresFpuRegister());
5158 } else {
5159 locations->SetInAt(0, Location::RequiresRegister());
5160 }
5161
Alexandre Rames542361f2015-01-29 16:57:31 +00005162 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005163 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5164 } else {
5165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5166 }
5167}
5168
5169void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5170 Primitive::Type result_type = conversion->GetResultType();
5171 Primitive::Type input_type = conversion->GetInputType();
5172
5173 DCHECK_NE(input_type, result_type);
5174
Alexandre Rames542361f2015-01-29 16:57:31 +00005175 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005176 int result_size = Primitive::ComponentSize(result_type);
5177 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005178 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005179 Register output = OutputRegister(conversion);
5180 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005181 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005182 // 'int' values are used directly as W registers, discarding the top
5183 // bits, so we don't need to sign-extend and can just perform a move.
5184 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5185 // top 32 bits of the target register. We theoretically could leave those
5186 // bits unchanged, but we would have to make sure that no code uses a
5187 // 32bit input value as a 64bit value assuming that the top 32 bits are
5188 // zero.
5189 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005190 } else if (result_type == Primitive::kPrimChar ||
5191 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5192 __ Ubfx(output,
5193 output.IsX() ? source.X() : source.W(),
5194 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005195 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005196 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005197 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005198 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005199 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005200 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005201 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5202 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005203 } else if (Primitive::IsFloatingPointType(result_type) &&
5204 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005205 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5206 } else {
5207 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5208 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005209 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005210}
Alexandre Rames67555f72014-11-18 10:55:16 +00005211
Serban Constantinescu02164b32014-11-13 14:05:07 +00005212void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5213 HandleShift(ushr);
5214}
5215
5216void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5217 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005218}
5219
5220void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5221 HandleBinaryOp(instruction);
5222}
5223
5224void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5225 HandleBinaryOp(instruction);
5226}
5227
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005228void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005229 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005230 LOG(FATAL) << "Unreachable";
5231}
5232
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005233void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005234 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005235 LOG(FATAL) << "Unreachable";
5236}
5237
Mark Mendellfe57faa2015-09-18 09:26:15 -04005238// Simple implementation of packed switch - generate cascaded compare/jumps.
5239void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5240 LocationSummary* locations =
5241 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5242 locations->SetInAt(0, Location::RequiresRegister());
5243}
5244
5245void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5246 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005247 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005248 Register value_reg = InputRegisterAt(switch_instr, 0);
5249 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5250
Zheng Xu3927c8b2015-11-18 17:46:25 +08005251 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005252 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005253 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5254 // make sure we don't emit it if the target may run out of range.
5255 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5256 // ranges and emit the tables only as required.
5257 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005258
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005259 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005260 // Current instruction id is an upper bound of the number of HIRs in the graph.
5261 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5262 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005263 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5264 Register temp = temps.AcquireW();
5265 __ Subs(temp, value_reg, Operand(lower_bound));
5266
Zheng Xu3927c8b2015-11-18 17:46:25 +08005267 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005268 // Jump to successors[0] if value == lower_bound.
5269 __ B(eq, codegen_->GetLabelOf(successors[0]));
5270 int32_t last_index = 0;
5271 for (; num_entries - last_index > 2; last_index += 2) {
5272 __ Subs(temp, temp, Operand(2));
5273 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5274 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5275 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5276 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5277 }
5278 if (num_entries - last_index == 2) {
5279 // The last missing case_value.
5280 __ Cmp(temp, Operand(1));
5281 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005282 }
5283
5284 // And the default for any other value.
5285 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5286 __ B(codegen_->GetLabelOf(default_block));
5287 }
5288 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005289 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005290
5291 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5292
5293 // Below instructions should use at most one blocked register. Since there are two blocked
5294 // registers, we are free to block one.
5295 Register temp_w = temps.AcquireW();
5296 Register index;
5297 // Remove the bias.
5298 if (lower_bound != 0) {
5299 index = temp_w;
5300 __ Sub(index, value_reg, Operand(lower_bound));
5301 } else {
5302 index = value_reg;
5303 }
5304
5305 // Jump to default block if index is out of the range.
5306 __ Cmp(index, Operand(num_entries));
5307 __ B(hs, codegen_->GetLabelOf(default_block));
5308
5309 // In current VIXL implementation, it won't require any blocked registers to encode the
5310 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5311 // register pressure.
5312 Register table_base = temps.AcquireX();
5313 // Load jump offset from the table.
5314 __ Adr(table_base, jump_table->GetTableStartLabel());
5315 Register jump_offset = temp_w;
5316 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5317
5318 // Jump to target block by branching to table_base(pc related) + offset.
5319 Register target_address = table_base;
5320 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5321 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005322 }
5323}
5324
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005325void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5326 HInstruction* instruction,
5327 Location out,
5328 uint32_t offset,
5329 Location maybe_temp,
5330 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005331 Primitive::Type type = Primitive::kPrimNot;
5332 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005333 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005334 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005335 Register temp_reg = RegisterFrom(maybe_temp, type);
5336 if (kUseBakerReadBarrier) {
5337 // Load with fast path based Baker's read barrier.
5338 // /* HeapReference<Object> */ out = *(out + offset)
5339 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5340 out,
5341 out_reg,
5342 offset,
5343 temp_reg,
5344 /* needs_null_check */ false,
5345 /* use_load_acquire */ false);
5346 } else {
5347 // Load with slow path based read barrier.
5348 // Save the value of `out` into `maybe_temp` before overwriting it
5349 // in the following move operation, as we will need it for the
5350 // read barrier below.
5351 __ Mov(temp_reg, out_reg);
5352 // /* HeapReference<Object> */ out = *(out + offset)
5353 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5354 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5355 }
5356 } else {
5357 // Plain load with no read barrier.
5358 // /* HeapReference<Object> */ out = *(out + offset)
5359 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5360 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5361 }
5362}
5363
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005364void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5365 HInstruction* instruction,
5366 Location out,
5367 Location obj,
5368 uint32_t offset,
5369 Location maybe_temp,
5370 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005371 Primitive::Type type = Primitive::kPrimNot;
5372 Register out_reg = RegisterFrom(out, type);
5373 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005374 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005375 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005376 if (kUseBakerReadBarrier) {
5377 // Load with fast path based Baker's read barrier.
5378 Register temp_reg = RegisterFrom(maybe_temp, type);
5379 // /* HeapReference<Object> */ out = *(obj + offset)
5380 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5381 out,
5382 obj_reg,
5383 offset,
5384 temp_reg,
5385 /* needs_null_check */ false,
5386 /* use_load_acquire */ false);
5387 } else {
5388 // Load with slow path based read barrier.
5389 // /* HeapReference<Object> */ out = *(obj + offset)
5390 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5391 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5392 }
5393 } else {
5394 // Plain load with no read barrier.
5395 // /* HeapReference<Object> */ out = *(obj + offset)
5396 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5397 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5398 }
5399}
5400
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005401void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5402 HInstruction* instruction,
5403 Location root,
5404 Register obj,
5405 uint32_t offset,
5406 vixl::aarch64::Label* fixup_label,
5407 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005408 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005409 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005410 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005411 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005412 if (kUseBakerReadBarrier) {
5413 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5414 // Baker's read barrier are used:
5415 //
5416 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005417 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5418 // if (temp != null) {
5419 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005420 // }
5421
5422 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005423 if (fixup_label == nullptr) {
5424 __ Ldr(root_reg, MemOperand(obj, offset));
5425 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005426 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005427 }
Roland Levillain44015862016-01-22 11:47:17 +00005428 static_assert(
5429 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5430 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5431 "have different sizes.");
5432 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5433 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5434 "have different sizes.");
5435
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005436 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005437
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005438 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5439 SlowPathCodeARM64* slow_path =
5440 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5441 root,
5442 LocationFrom(temp));
5443 codegen_->AddSlowPath(slow_path);
5444 const int32_t entry_point_offset =
5445 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5446 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5447 // Loading the entrypoint does not require a load acquire since it is only changed when
5448 // threads are suspended or running a checkpoint.
5449 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5450 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5451 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005452 __ Cbnz(temp, slow_path->GetEntryLabel());
5453 __ Bind(slow_path->GetExitLabel());
5454 } else {
5455 // GC root loaded through a slow path for read barriers other
5456 // than Baker's.
5457 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005458 if (fixup_label == nullptr) {
5459 __ Add(root_reg.X(), obj.X(), offset);
5460 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005461 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005462 }
Roland Levillain44015862016-01-22 11:47:17 +00005463 // /* mirror::Object* */ root = root->Read()
5464 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5465 }
5466 } else {
5467 // Plain GC root load with no read barrier.
5468 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005469 if (fixup_label == nullptr) {
5470 __ Ldr(root_reg, MemOperand(obj, offset));
5471 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005472 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 }
Roland Levillain44015862016-01-22 11:47:17 +00005474 // Note that GC roots are not affected by heap poisoning, thus we
5475 // do not have to unpoison `root_reg` here.
5476 }
5477}
5478
5479void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5480 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005481 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005482 uint32_t offset,
5483 Register temp,
5484 bool needs_null_check,
5485 bool use_load_acquire) {
5486 DCHECK(kEmitCompilerReadBarrier);
5487 DCHECK(kUseBakerReadBarrier);
5488
5489 // /* HeapReference<Object> */ ref = *(obj + offset)
5490 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005491 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005492 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5493 ref,
5494 obj,
5495 offset,
5496 no_index,
5497 no_scale_factor,
5498 temp,
5499 needs_null_check,
5500 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005501}
5502
5503void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5504 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005505 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005506 uint32_t data_offset,
5507 Location index,
5508 Register temp,
5509 bool needs_null_check) {
5510 DCHECK(kEmitCompilerReadBarrier);
5511 DCHECK(kUseBakerReadBarrier);
5512
5513 // Array cells are never volatile variables, therefore array loads
5514 // never use Load-Acquire instructions on ARM64.
5515 const bool use_load_acquire = false;
5516
Roland Levillainbfea3352016-06-23 13:48:47 +01005517 static_assert(
5518 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5519 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005520 // /* HeapReference<Object> */ ref =
5521 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005522 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5523 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5524 ref,
5525 obj,
5526 data_offset,
5527 index,
5528 scale_factor,
5529 temp,
5530 needs_null_check,
5531 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005532}
5533
5534void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5535 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005536 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005537 uint32_t offset,
5538 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005539 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005540 Register temp,
5541 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005542 bool use_load_acquire,
5543 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005544 DCHECK(kEmitCompilerReadBarrier);
5545 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005546 // If we are emitting an array load, we should not be using a
5547 // Load Acquire instruction. In other words:
5548 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5549 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005550
5551 MacroAssembler* masm = GetVIXLAssembler();
5552 UseScratchRegisterScope temps(masm);
5553
5554 // In slow path based read barriers, the read barrier call is
5555 // inserted after the original load. However, in fast path based
5556 // Baker's read barriers, we need to perform the load of
5557 // mirror::Object::monitor_ *before* the original reference load.
5558 // This load-load ordering is required by the read barrier.
5559 // The fast path/slow path (for Baker's algorithm) should look like:
5560 //
5561 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5562 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5563 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005564 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005565 // if (is_gray) {
5566 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5567 // }
5568 //
5569 // Note: the original implementation in ReadBarrier::Barrier is
5570 // slightly more complex as it performs additional checks that we do
5571 // not do here for performance reasons.
5572
5573 Primitive::Type type = Primitive::kPrimNot;
5574 Register ref_reg = RegisterFrom(ref, type);
5575 DCHECK(obj.IsW());
5576 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5577
5578 // /* int32_t */ monitor = obj->monitor_
5579 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5580 if (needs_null_check) {
5581 MaybeRecordImplicitNullCheck(instruction);
5582 }
5583 // /* LockWord */ lock_word = LockWord(monitor)
5584 static_assert(sizeof(LockWord) == sizeof(int32_t),
5585 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005586
Vladimir Marko877a0332016-07-11 19:30:56 +01005587 // Introduce a dependency on the lock_word including rb_state,
5588 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005589 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005590 // `obj` is unchanged by this operation, but its value now depends
5591 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005592 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005593
5594 // The actual reference load.
5595 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005596 // Load types involving an "index": ArrayGet,
5597 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5598 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005599 if (use_load_acquire) {
5600 // UnsafeGetObjectVolatile intrinsic case.
5601 // Register `index` is not an index in an object array, but an
5602 // offset to an object reference field within object `obj`.
5603 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5604 DCHECK(instruction->GetLocations()->Intrinsified());
5605 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5606 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005607 DCHECK_EQ(offset, 0u);
5608 DCHECK_EQ(scale_factor, 0u);
5609 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005610 // /* HeapReference<Object> */ ref = *(obj + index)
5611 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5612 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005613 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005614 // ArrayGet and UnsafeGetObject intrinsics cases.
5615 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5616 if (index.IsConstant()) {
5617 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5618 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5619 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005620 Register temp3 = temps.AcquireW();
5621 __ Add(temp3, obj, offset);
5622 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5623 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005624 }
Roland Levillain44015862016-01-22 11:47:17 +00005625 }
Roland Levillain44015862016-01-22 11:47:17 +00005626 } else {
5627 // /* HeapReference<Object> */ ref = *(obj + offset)
5628 MemOperand field = HeapOperand(obj, offset);
5629 if (use_load_acquire) {
5630 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5631 } else {
5632 Load(type, ref_reg, field);
5633 }
5634 }
5635
5636 // Object* ref = ref_addr->AsMirrorPtr()
5637 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5638
Vladimir Marko953437b2016-08-24 08:30:46 +00005639 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005640 SlowPathCodeARM64* slow_path;
5641 if (always_update_field) {
5642 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5643 // address of the form `obj + field_offset`, where `obj` is a
5644 // register and `field_offset` is a register. Thus `offset` and
5645 // `scale_factor` above are expected to be null in this code path.
5646 DCHECK_EQ(offset, 0u);
5647 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5648 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5649 instruction, ref, obj, /* field_offset */ index, temp);
5650 } else {
5651 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5652 }
Roland Levillain44015862016-01-22 11:47:17 +00005653 AddSlowPath(slow_path);
5654
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005655 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005656 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005657 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005658 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5659 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005660 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005661 __ Bind(slow_path->GetExitLabel());
5662}
5663
5664void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5665 Location out,
5666 Location ref,
5667 Location obj,
5668 uint32_t offset,
5669 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005670 DCHECK(kEmitCompilerReadBarrier);
5671
Roland Levillain44015862016-01-22 11:47:17 +00005672 // Insert a slow path based read barrier *after* the reference load.
5673 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005674 // If heap poisoning is enabled, the unpoisoning of the loaded
5675 // reference will be carried out by the runtime within the slow
5676 // path.
5677 //
5678 // Note that `ref` currently does not get unpoisoned (when heap
5679 // poisoning is enabled), which is alright as the `ref` argument is
5680 // not used by the artReadBarrierSlow entry point.
5681 //
5682 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5683 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5684 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5685 AddSlowPath(slow_path);
5686
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005687 __ B(slow_path->GetEntryLabel());
5688 __ Bind(slow_path->GetExitLabel());
5689}
5690
Roland Levillain44015862016-01-22 11:47:17 +00005691void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5692 Location out,
5693 Location ref,
5694 Location obj,
5695 uint32_t offset,
5696 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005697 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005698 // Baker's read barriers shall be handled by the fast path
5699 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5700 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005701 // If heap poisoning is enabled, unpoisoning will be taken care of
5702 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005703 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005704 } else if (kPoisonHeapReferences) {
5705 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5706 }
5707}
5708
Roland Levillain44015862016-01-22 11:47:17 +00005709void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5710 Location out,
5711 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005712 DCHECK(kEmitCompilerReadBarrier);
5713
Roland Levillain44015862016-01-22 11:47:17 +00005714 // Insert a slow path based read barrier *after* the GC root load.
5715 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005716 // Note that GC roots are not affected by heap poisoning, so we do
5717 // not need to do anything special for this here.
5718 SlowPathCodeARM64* slow_path =
5719 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5720 AddSlowPath(slow_path);
5721
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005722 __ B(slow_path->GetEntryLabel());
5723 __ Bind(slow_path->GetExitLabel());
5724}
5725
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005726void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5727 LocationSummary* locations =
5728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5729 locations->SetInAt(0, Location::RequiresRegister());
5730 locations->SetOut(Location::RequiresRegister());
5731}
5732
5733void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5734 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005735 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005736 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005737 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005738 __ Ldr(XRegisterFrom(locations->Out()),
5739 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005740 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005741 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005742 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005743 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5744 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005745 __ Ldr(XRegisterFrom(locations->Out()),
5746 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005747 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005748}
5749
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005750static void PatchJitRootUse(uint8_t* code,
5751 const uint8_t* roots_data,
5752 vixl::aarch64::Literal<uint32_t>* literal,
5753 uint64_t index_in_table) {
5754 uint32_t literal_offset = literal->GetOffset();
5755 uintptr_t address =
5756 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5757 uint8_t* data = code + literal_offset;
5758 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5759}
5760
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005761void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5762 for (const auto& entry : jit_string_patches_) {
5763 const auto& it = jit_string_roots_.find(entry.first);
5764 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005765 PatchJitRootUse(code, roots_data, entry.second, it->second);
5766 }
5767 for (const auto& entry : jit_class_patches_) {
5768 const auto& it = jit_class_roots_.find(entry.first);
5769 DCHECK(it != jit_class_roots_.end());
5770 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005771 }
5772}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005773
Alexandre Rames67555f72014-11-18 10:55:16 +00005774#undef __
5775#undef QUICK_ENTRY_POINT
5776
Alexandre Rames5319def2014-10-23 10:03:10 +01005777} // namespace arm64
5778} // namespace art