blob: 598be4715b028fdcb7d633bef3b249d74e386f44 [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
Roland Levillain558dea12017-01-27 19:40:44 +00001455// Allocate a scratch register from the VIXL pool, querying first into
1456// the floating-point register pool, and then the the core register
1457// pool. This is essentially a reimplementation of
1458// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1459// using a different allocation strategy.
1460static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1461 vixl::aarch64::UseScratchRegisterScope* temps,
1462 int size_in_bits) {
1463 return masm->GetScratchFPRegisterList()->IsEmpty()
1464 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1465 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1466}
1467
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468void CodeGeneratorARM64::MoveLocation(Location destination,
1469 Location source,
1470 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001471 if (source.Equals(destination)) {
1472 return;
1473 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001474
1475 // A valid move can always be inferred from the destination and source
1476 // locations. When moving from and to a register, the argument type can be
1477 // used to generate 32bit instead of 64bit moves. In debug mode we also
1478 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001479 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001480
1481 if (destination.IsRegister() || destination.IsFpuRegister()) {
1482 if (unspecified_type) {
1483 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1484 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001485 (src_cst != nullptr && (src_cst->IsIntConstant()
1486 || src_cst->IsFloatConstant()
1487 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001489 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001490 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001491 // If the source is a double stack slot or a 64bit constant, a 64bit
1492 // type is appropriate. Else the source is a register, and since the
1493 // type has not been specified, we chose a 64bit type to force a 64bit
1494 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001495 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001496 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001498 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1499 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1500 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001501 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1502 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1503 __ Ldr(dst, StackOperandFrom(source));
1504 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001505 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001506 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001507 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001508 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001509 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001510 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001511 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001512 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1513 ? Primitive::kPrimLong
1514 : Primitive::kPrimInt;
1515 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1516 }
1517 } else {
1518 DCHECK(source.IsFpuRegister());
1519 if (destination.IsRegister()) {
1520 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1521 ? Primitive::kPrimDouble
1522 : Primitive::kPrimFloat;
1523 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1524 } else {
1525 DCHECK(destination.IsFpuRegister());
1526 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001527 }
1528 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001529 } else { // The destination is not a register. It must be a stack slot.
1530 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1531 if (source.IsRegister() || source.IsFpuRegister()) {
1532 if (unspecified_type) {
1533 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001534 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001536 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 }
1538 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001539 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1540 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1541 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001542 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001543 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1544 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001545 UseScratchRegisterScope temps(GetVIXLAssembler());
1546 HConstant* src_cst = source.GetConstant();
1547 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001548 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001549 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1550 ? Register(xzr)
1551 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001552 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001553 if (src_cst->IsIntConstant()) {
1554 temp = temps.AcquireW();
1555 } else if (src_cst->IsLongConstant()) {
1556 temp = temps.AcquireX();
1557 } else if (src_cst->IsFloatConstant()) {
1558 temp = temps.AcquireS();
1559 } else {
1560 DCHECK(src_cst->IsDoubleConstant());
1561 temp = temps.AcquireD();
1562 }
1563 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001564 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001565 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001566 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001567 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001568 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001569 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001570 // Use any scratch register (a core or a floating-point one)
1571 // from VIXL scratch register pools as a temporary.
1572 //
1573 // We used to only use the FP scratch register pool, but in some
1574 // rare cases the only register from this pool (D31) would
1575 // already be used (e.g. within a ParallelMove instruction, when
1576 // a move is blocked by a another move requiring a scratch FP
1577 // register, which would reserve D31). To prevent this issue, we
1578 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001579 //
1580 // Also, we start by asking for a FP scratch register first, as the
1581 // demand of scratch core registers is higher. This is why we
1582 // use AcquireFPOrCoreCPURegisterOfSize instead of
1583 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1584 // allocates core scratch registers first.
1585 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1586 GetVIXLAssembler(),
1587 &temps,
1588 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001589 __ Ldr(temp, StackOperandFrom(source));
1590 __ Str(temp, StackOperandFrom(destination));
1591 }
1592 }
1593}
1594
1595void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001596 CPURegister dst,
1597 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001598 switch (type) {
1599 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001600 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001601 break;
1602 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001603 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001604 break;
1605 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001606 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001607 break;
1608 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001609 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001610 break;
1611 case Primitive::kPrimInt:
1612 case Primitive::kPrimNot:
1613 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001614 case Primitive::kPrimFloat:
1615 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001616 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001617 __ Ldr(dst, src);
1618 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001619 case Primitive::kPrimVoid:
1620 LOG(FATAL) << "Unreachable type " << type;
1621 }
1622}
1623
Calin Juravle77520bc2015-01-12 18:45:46 +00001624void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001625 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001626 const MemOperand& src,
1627 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001628 MacroAssembler* masm = GetVIXLAssembler();
1629 BlockPoolsScope block_pools(masm);
1630 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001631 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001632 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001633
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001634 DCHECK(!src.IsPreIndex());
1635 DCHECK(!src.IsPostIndex());
1636
1637 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001638 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001639 MemOperand base = MemOperand(temp_base);
1640 switch (type) {
1641 case Primitive::kPrimBoolean:
1642 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001643 if (needs_null_check) {
1644 MaybeRecordImplicitNullCheck(instruction);
1645 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001646 break;
1647 case Primitive::kPrimByte:
1648 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001649 if (needs_null_check) {
1650 MaybeRecordImplicitNullCheck(instruction);
1651 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001652 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1653 break;
1654 case Primitive::kPrimChar:
1655 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001656 if (needs_null_check) {
1657 MaybeRecordImplicitNullCheck(instruction);
1658 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001659 break;
1660 case Primitive::kPrimShort:
1661 __ Ldarh(Register(dst), 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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1666 break;
1667 case Primitive::kPrimInt:
1668 case Primitive::kPrimNot:
1669 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001670 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001671 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001672 if (needs_null_check) {
1673 MaybeRecordImplicitNullCheck(instruction);
1674 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001675 break;
1676 case Primitive::kPrimFloat:
1677 case Primitive::kPrimDouble: {
1678 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001679 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001680
1681 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1682 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001683 if (needs_null_check) {
1684 MaybeRecordImplicitNullCheck(instruction);
1685 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001686 __ Fmov(FPRegister(dst), temp);
1687 break;
1688 }
1689 case Primitive::kPrimVoid:
1690 LOG(FATAL) << "Unreachable type " << type;
1691 }
1692}
1693
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001694void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001695 CPURegister src,
1696 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001697 switch (type) {
1698 case Primitive::kPrimBoolean:
1699 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001700 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001701 break;
1702 case Primitive::kPrimChar:
1703 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001704 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001705 break;
1706 case Primitive::kPrimInt:
1707 case Primitive::kPrimNot:
1708 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001709 case Primitive::kPrimFloat:
1710 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001711 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001712 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001713 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001714 case Primitive::kPrimVoid:
1715 LOG(FATAL) << "Unreachable type " << type;
1716 }
1717}
1718
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001719void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1720 CPURegister src,
1721 const MemOperand& dst) {
1722 UseScratchRegisterScope temps(GetVIXLAssembler());
1723 Register temp_base = temps.AcquireX();
1724
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001725 DCHECK(!dst.IsPreIndex());
1726 DCHECK(!dst.IsPostIndex());
1727
1728 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001729 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001730 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001731 MemOperand base = MemOperand(temp_base);
1732 switch (type) {
1733 case Primitive::kPrimBoolean:
1734 case Primitive::kPrimByte:
1735 __ Stlrb(Register(src), base);
1736 break;
1737 case Primitive::kPrimChar:
1738 case Primitive::kPrimShort:
1739 __ Stlrh(Register(src), base);
1740 break;
1741 case Primitive::kPrimInt:
1742 case Primitive::kPrimNot:
1743 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001744 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001745 __ Stlr(Register(src), base);
1746 break;
1747 case Primitive::kPrimFloat:
1748 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001749 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001750 Register temp_src;
1751 if (src.IsZero()) {
1752 // The zero register is used to avoid synthesizing zero constants.
1753 temp_src = Register(src);
1754 } else {
1755 DCHECK(src.IsFPRegister());
1756 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1757 __ Fmov(temp_src, FPRegister(src));
1758 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001759
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001760 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001761 break;
1762 }
1763 case Primitive::kPrimVoid:
1764 LOG(FATAL) << "Unreachable type " << type;
1765 }
1766}
1767
Calin Juravle175dc732015-08-25 15:42:32 +01001768void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1769 HInstruction* instruction,
1770 uint32_t dex_pc,
1771 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001772 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001773 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001774 if (EntrypointRequiresStackMap(entrypoint)) {
1775 RecordPcInfo(instruction, dex_pc, slow_path);
1776 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001777}
1778
Roland Levillaindec8f632016-07-22 17:10:06 +01001779void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1780 HInstruction* instruction,
1781 SlowPathCode* slow_path) {
1782 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001783 GenerateInvokeRuntime(entry_point_offset);
1784}
1785
1786void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001787 BlockPoolsScope block_pools(GetVIXLAssembler());
1788 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1789 __ Blr(lr);
1790}
1791
Alexandre Rames67555f72014-11-18 10:55:16 +00001792void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001793 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001794 UseScratchRegisterScope temps(GetVIXLAssembler());
1795 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001796 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1797
Serban Constantinescu02164b32014-11-13 14:05:07 +00001798 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001799 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1800 __ Add(temp, class_reg, status_offset);
1801 __ Ldar(temp, HeapOperand(temp));
1802 __ Cmp(temp, mirror::Class::kStatusInitialized);
1803 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001804 __ Bind(slow_path->GetExitLabel());
1805}
Alexandre Rames5319def2014-10-23 10:03:10 +01001806
Roland Levillain44015862016-01-22 11:47:17 +00001807void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001808 BarrierType type = BarrierAll;
1809
1810 switch (kind) {
1811 case MemBarrierKind::kAnyAny:
1812 case MemBarrierKind::kAnyStore: {
1813 type = BarrierAll;
1814 break;
1815 }
1816 case MemBarrierKind::kLoadAny: {
1817 type = BarrierReads;
1818 break;
1819 }
1820 case MemBarrierKind::kStoreStore: {
1821 type = BarrierWrites;
1822 break;
1823 }
1824 default:
1825 LOG(FATAL) << "Unexpected memory barrier " << kind;
1826 }
1827 __ Dmb(InnerShareable, type);
1828}
1829
Serban Constantinescu02164b32014-11-13 14:05:07 +00001830void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1831 HBasicBlock* successor) {
1832 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001833 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1834 if (slow_path == nullptr) {
1835 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1836 instruction->SetSlowPath(slow_path);
1837 codegen_->AddSlowPath(slow_path);
1838 if (successor != nullptr) {
1839 DCHECK(successor->IsLoopHeader());
1840 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1841 }
1842 } else {
1843 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1844 }
1845
Serban Constantinescu02164b32014-11-13 14:05:07 +00001846 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1847 Register temp = temps.AcquireW();
1848
Andreas Gampe542451c2016-07-26 09:02:02 -07001849 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001850 if (successor == nullptr) {
1851 __ Cbnz(temp, slow_path->GetEntryLabel());
1852 __ Bind(slow_path->GetReturnLabel());
1853 } else {
1854 __ Cbz(temp, codegen_->GetLabelOf(successor));
1855 __ B(slow_path->GetEntryLabel());
1856 // slow_path will return to GetLabelOf(successor).
1857 }
1858}
1859
Alexandre Rames5319def2014-10-23 10:03:10 +01001860InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1861 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001862 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001863 assembler_(codegen->GetAssembler()),
1864 codegen_(codegen) {}
1865
1866#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001867 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001868
1869#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1870
1871enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001872 // Using a base helps identify when we hit such breakpoints.
1873 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001874#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1875 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1876#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1877};
1878
1879#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001880 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001881 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1882 } \
1883 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1885 locations->SetOut(Location::Any()); \
1886 }
1887 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1888#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1889
1890#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001891#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001892
Alexandre Rames67555f72014-11-18 10:55:16 +00001893void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001894 DCHECK_EQ(instr->InputCount(), 2U);
1895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1896 Primitive::Type type = instr->GetResultType();
1897 switch (type) {
1898 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001899 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001900 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001901 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001903 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001904
1905 case Primitive::kPrimFloat:
1906 case Primitive::kPrimDouble:
1907 locations->SetInAt(0, Location::RequiresFpuRegister());
1908 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001910 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001911
Alexandre Rames5319def2014-10-23 10:03:10 +01001912 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001913 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001914 }
1915}
1916
Alexandre Rames09a99962015-04-15 11:47:56 +01001917void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001918 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1919
1920 bool object_field_get_with_read_barrier =
1921 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001922 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001923 new (GetGraph()->GetArena()) LocationSummary(instruction,
1924 object_field_get_with_read_barrier ?
1925 LocationSummary::kCallOnSlowPath :
1926 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001927 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001928 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00001929 // We need a temporary register for the read barrier marking slow
1930 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
1931 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01001932 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001933 locations->SetInAt(0, Location::RequiresRegister());
1934 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1935 locations->SetOut(Location::RequiresFpuRegister());
1936 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001937 // The output overlaps for an object field get when read barriers
1938 // are enabled: we do not want the load to overwrite the object's
1939 // location, as we need it to emit the read barrier.
1940 locations->SetOut(
1941 Location::RequiresRegister(),
1942 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001943 }
1944}
1945
1946void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1947 const FieldInfo& field_info) {
1948 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001949 LocationSummary* locations = instruction->GetLocations();
1950 Location base_loc = locations->InAt(0);
1951 Location out = locations->Out();
1952 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001953 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001954 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001955 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001956
Roland Levillain44015862016-01-22 11:47:17 +00001957 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1958 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001959 // /* HeapReference<Object> */ out = *(base + offset)
1960 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00001961 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00001962 // Note that potential implicit null checks are handled in this
1963 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1964 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1965 instruction,
1966 out,
1967 base,
1968 offset,
1969 temp,
1970 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001971 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001972 } else {
1973 // General case.
1974 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001975 // Note that a potential implicit null check is handled in this
1976 // CodeGeneratorARM64::LoadAcquire call.
1977 // NB: LoadAcquire will record the pc info if needed.
1978 codegen_->LoadAcquire(
1979 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001980 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001981 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001983 }
Roland Levillain44015862016-01-22 11:47:17 +00001984 if (field_type == Primitive::kPrimNot) {
1985 // If read barriers are enabled, emit read barriers other than
1986 // Baker's using a slow path (and also unpoison the loaded
1987 // reference, if heap poisoning is enabled).
1988 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1989 }
Roland Levillain4d027112015-07-01 15:41:14 +01001990 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001991}
1992
1993void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1994 LocationSummary* locations =
1995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1996 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001997 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1998 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1999 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002000 locations->SetInAt(1, Location::RequiresFpuRegister());
2001 } else {
2002 locations->SetInAt(1, Location::RequiresRegister());
2003 }
2004}
2005
2006void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002007 const FieldInfo& field_info,
2008 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002009 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01002010 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01002011
2012 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002013 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002014 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002015 Offset offset = field_info.GetFieldOffset();
2016 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002017
Roland Levillain4d027112015-07-01 15:41:14 +01002018 {
2019 // We use a block to end the scratch scope before the write barrier, thus
2020 // freeing the temporary registers so they can be used in `MarkGCCard`.
2021 UseScratchRegisterScope temps(GetVIXLAssembler());
2022
2023 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2024 DCHECK(value.IsW());
2025 Register temp = temps.AcquireW();
2026 __ Mov(temp, value.W());
2027 GetAssembler()->PoisonHeapReference(temp.W());
2028 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002029 }
Roland Levillain4d027112015-07-01 15:41:14 +01002030
2031 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002032 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
2033 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01002034 } else {
2035 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2036 codegen_->MaybeRecordImplicitNullCheck(instruction);
2037 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002038 }
2039
2040 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002041 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002042 }
2043}
2044
Alexandre Rames67555f72014-11-18 10:55:16 +00002045void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002046 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002047
2048 switch (type) {
2049 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002050 case Primitive::kPrimLong: {
2051 Register dst = OutputRegister(instr);
2052 Register lhs = InputRegisterAt(instr, 0);
2053 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002054 if (instr->IsAdd()) {
2055 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002056 } else if (instr->IsAnd()) {
2057 __ And(dst, lhs, rhs);
2058 } else if (instr->IsOr()) {
2059 __ Orr(dst, lhs, rhs);
2060 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002061 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002062 } else if (instr->IsRor()) {
2063 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002064 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002065 __ Ror(dst, lhs, shift);
2066 } else {
2067 // Ensure shift distance is in the same size register as the result. If
2068 // we are rotating a long and the shift comes in a w register originally,
2069 // we don't need to sxtw for use as an x since the shift distances are
2070 // all & reg_bits - 1.
2071 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2072 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002073 } else {
2074 DCHECK(instr->IsXor());
2075 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002076 }
2077 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002078 }
2079 case Primitive::kPrimFloat:
2080 case Primitive::kPrimDouble: {
2081 FPRegister dst = OutputFPRegister(instr);
2082 FPRegister lhs = InputFPRegisterAt(instr, 0);
2083 FPRegister rhs = InputFPRegisterAt(instr, 1);
2084 if (instr->IsAdd()) {
2085 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002086 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002087 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002088 } else {
2089 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002090 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002091 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002092 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002093 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002094 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002095 }
2096}
2097
Serban Constantinescu02164b32014-11-13 14:05:07 +00002098void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2099 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2100
2101 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2102 Primitive::Type type = instr->GetResultType();
2103 switch (type) {
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimLong: {
2106 locations->SetInAt(0, Location::RequiresRegister());
2107 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2108 locations->SetOut(Location::RequiresRegister());
2109 break;
2110 }
2111 default:
2112 LOG(FATAL) << "Unexpected shift type " << type;
2113 }
2114}
2115
2116void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2117 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2118
2119 Primitive::Type type = instr->GetType();
2120 switch (type) {
2121 case Primitive::kPrimInt:
2122 case Primitive::kPrimLong: {
2123 Register dst = OutputRegister(instr);
2124 Register lhs = InputRegisterAt(instr, 0);
2125 Operand rhs = InputOperandAt(instr, 1);
2126 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002127 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002128 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002129 if (instr->IsShl()) {
2130 __ Lsl(dst, lhs, shift_value);
2131 } else if (instr->IsShr()) {
2132 __ Asr(dst, lhs, shift_value);
2133 } else {
2134 __ Lsr(dst, lhs, shift_value);
2135 }
2136 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002137 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002138
2139 if (instr->IsShl()) {
2140 __ Lsl(dst, lhs, rhs_reg);
2141 } else if (instr->IsShr()) {
2142 __ Asr(dst, lhs, rhs_reg);
2143 } else {
2144 __ Lsr(dst, lhs, rhs_reg);
2145 }
2146 }
2147 break;
2148 }
2149 default:
2150 LOG(FATAL) << "Unexpected shift operation type " << type;
2151 }
2152}
2153
Alexandre Rames5319def2014-10-23 10:03:10 +01002154void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002155 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002156}
2157
2158void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002159 HandleBinaryOp(instruction);
2160}
2161
2162void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2163 HandleBinaryOp(instruction);
2164}
2165
2166void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2167 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002168}
2169
Artem Serov7fc63502016-02-09 17:15:29 +00002170void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002171 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2173 locations->SetInAt(0, Location::RequiresRegister());
2174 // There is no immediate variant of negated bitwise instructions in AArch64.
2175 locations->SetInAt(1, Location::RequiresRegister());
2176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2177}
2178
Artem Serov7fc63502016-02-09 17:15:29 +00002179void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002180 Register dst = OutputRegister(instr);
2181 Register lhs = InputRegisterAt(instr, 0);
2182 Register rhs = InputRegisterAt(instr, 1);
2183
2184 switch (instr->GetOpKind()) {
2185 case HInstruction::kAnd:
2186 __ Bic(dst, lhs, rhs);
2187 break;
2188 case HInstruction::kOr:
2189 __ Orn(dst, lhs, rhs);
2190 break;
2191 case HInstruction::kXor:
2192 __ Eon(dst, lhs, rhs);
2193 break;
2194 default:
2195 LOG(FATAL) << "Unreachable";
2196 }
2197}
2198
Alexandre Rames8626b742015-11-25 16:28:08 +00002199void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
2200 HArm64DataProcWithShifterOp* instruction) {
2201 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2202 instruction->GetType() == Primitive::kPrimLong);
2203 LocationSummary* locations =
2204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2205 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2206 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2207 } else {
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 }
2210 locations->SetInAt(1, Location::RequiresRegister());
2211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2212}
2213
2214void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
2215 HArm64DataProcWithShifterOp* instruction) {
2216 Primitive::Type type = instruction->GetType();
2217 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2218 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2219 Register out = OutputRegister(instruction);
2220 Register left;
2221 if (kind != HInstruction::kNeg) {
2222 left = InputRegisterAt(instruction, 0);
2223 }
2224 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
2225 // shifter operand operation, the IR generating `right_reg` (input to the type
2226 // conversion) can have a different type from the current instruction's type,
2227 // so we manually indicate the type.
2228 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00002229 int64_t shift_amount = instruction->GetShiftAmount() &
2230 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00002231
2232 Operand right_operand(0);
2233
2234 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2235 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
2236 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2237 } else {
2238 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
2239 }
2240
2241 // Logical binary operations do not support extension operations in the
2242 // operand. Note that VIXL would still manage if it was passed by generating
2243 // the extension as a separate instruction.
2244 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2245 DCHECK(!right_operand.IsExtendedRegister() ||
2246 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2247 kind != HInstruction::kNeg));
2248 switch (kind) {
2249 case HInstruction::kAdd:
2250 __ Add(out, left, right_operand);
2251 break;
2252 case HInstruction::kAnd:
2253 __ And(out, left, right_operand);
2254 break;
2255 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002256 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002257 __ Neg(out, right_operand);
2258 break;
2259 case HInstruction::kOr:
2260 __ Orr(out, left, right_operand);
2261 break;
2262 case HInstruction::kSub:
2263 __ Sub(out, left, right_operand);
2264 break;
2265 case HInstruction::kXor:
2266 __ Eor(out, left, right_operand);
2267 break;
2268 default:
2269 LOG(FATAL) << "Unexpected operation kind: " << kind;
2270 UNREACHABLE();
2271 }
2272}
2273
Artem Serov328429f2016-07-06 16:23:04 +01002274void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002275 LocationSummary* locations =
2276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2277 locations->SetInAt(0, Location::RequiresRegister());
2278 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2279 locations->SetOut(Location::RequiresRegister());
2280}
2281
Roland Levillain19c54192016-11-04 13:44:09 +00002282void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002283 __ Add(OutputRegister(instruction),
2284 InputRegisterAt(instruction, 0),
2285 Operand(InputOperandAt(instruction, 1)));
2286}
2287
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002288void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002289 LocationSummary* locations =
2290 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002291 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2292 if (instr->GetOpKind() == HInstruction::kSub &&
2293 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002294 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002295 // Don't allocate register for Mneg instruction.
2296 } else {
2297 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2298 Location::RequiresRegister());
2299 }
2300 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2301 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2303}
2304
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002305void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002306 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002307 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2308 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002309
2310 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2311 // This fixup should be carried out for all multiply-accumulate instructions:
2312 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2313 if (instr->GetType() == Primitive::kPrimLong &&
2314 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2315 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002316 vixl::aarch64::Instruction* prev =
2317 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002318 if (prev->IsLoadOrStore()) {
2319 // Make sure we emit only exactly one nop.
Scott Wakelingb77051e2016-11-21 19:46:00 +00002320 vixl::CodeBufferCheckScope scope(masm,
2321 kInstructionSize,
2322 vixl::CodeBufferCheckScope::kReserveBufferSpace,
2323 vixl::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002324 __ nop();
2325 }
2326 }
2327
2328 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002329 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002330 __ Madd(res, mul_left, mul_right, accumulator);
2331 } else {
2332 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002333 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002334 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002335 __ Mneg(res, mul_left, mul_right);
2336 } else {
2337 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2338 __ Msub(res, mul_left, mul_right, accumulator);
2339 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002340 }
2341}
2342
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002343void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002344 bool object_array_get_with_read_barrier =
2345 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002346 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002347 new (GetGraph()->GetArena()) LocationSummary(instruction,
2348 object_array_get_with_read_barrier ?
2349 LocationSummary::kCallOnSlowPath :
2350 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002351 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002352 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002353 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002354 locations->SetInAt(0, Location::RequiresRegister());
2355 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002356 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2357 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2358 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002359 // The output overlaps in the case of an object array get with
2360 // read barriers enabled: we do not want the move to overwrite the
2361 // array's location, as we need it to emit the read barrier.
2362 locations->SetOut(
2363 Location::RequiresRegister(),
2364 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002365 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002366}
2367
2368void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002369 Primitive::Type type = instruction->GetType();
2370 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002371 LocationSummary* locations = instruction->GetLocations();
2372 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002373 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002374 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002375 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2376 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002377 MacroAssembler* masm = GetVIXLAssembler();
2378 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002379 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002380 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002381
Roland Levillain19c54192016-11-04 13:44:09 +00002382 // The read barrier instrumentation of object ArrayGet instructions
2383 // does not support the HIntermediateAddress instruction.
2384 DCHECK(!((type == Primitive::kPrimNot) &&
2385 instruction->GetArray()->IsIntermediateAddress() &&
2386 kEmitCompilerReadBarrier));
2387
Roland Levillain44015862016-01-22 11:47:17 +00002388 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2389 // Object ArrayGet with Baker's read barrier case.
2390 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002391 // Note that a potential implicit null check is handled in the
2392 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2393 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2394 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002395 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002396 // General case.
2397 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002398 Register length;
2399 if (maybe_compressed_char_at) {
2400 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2401 length = temps.AcquireW();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002402 if (instruction->GetArray()->IsIntermediateAddress()) {
2403 DCHECK_LT(count_offset, offset);
2404 int64_t adjusted_offset = static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2405 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2406 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2407 } else {
2408 __ Ldr(length, HeapOperand(obj, count_offset));
2409 }
jessicahandojo05765752016-09-09 19:01:32 -07002410 codegen_->MaybeRecordImplicitNullCheck(instruction);
2411 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002412 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002413 if (maybe_compressed_char_at) {
2414 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002415 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2416 "Expecting 0=compressed, 1=uncompressed");
2417 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002418 __ Ldrb(Register(OutputCPURegister(instruction)),
2419 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2420 __ B(&done);
2421 __ Bind(&uncompressed_load);
2422 __ Ldrh(Register(OutputCPURegister(instruction)),
2423 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2424 __ Bind(&done);
2425 } else {
2426 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2427 source = HeapOperand(obj, offset);
2428 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002429 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002430 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002431 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002432 // We do not need to compute the intermediate address from the array: the
2433 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002434 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002435 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002436 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002437 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2438 }
2439 temp = obj;
2440 } else {
2441 __ Add(temp, obj, offset);
2442 }
jessicahandojo05765752016-09-09 19:01:32 -07002443 if (maybe_compressed_char_at) {
2444 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002445 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2446 "Expecting 0=compressed, 1=uncompressed");
2447 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002448 __ Ldrb(Register(OutputCPURegister(instruction)),
2449 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2450 __ B(&done);
2451 __ Bind(&uncompressed_load);
2452 __ Ldrh(Register(OutputCPURegister(instruction)),
2453 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2454 __ Bind(&done);
2455 } else {
2456 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2457 }
Roland Levillain44015862016-01-22 11:47:17 +00002458 }
jessicahandojo05765752016-09-09 19:01:32 -07002459 if (!maybe_compressed_char_at) {
2460 codegen_->Load(type, OutputCPURegister(instruction), source);
2461 codegen_->MaybeRecordImplicitNullCheck(instruction);
2462 }
Roland Levillain44015862016-01-22 11:47:17 +00002463
2464 if (type == Primitive::kPrimNot) {
2465 static_assert(
2466 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2467 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2468 Location obj_loc = locations->InAt(0);
2469 if (index.IsConstant()) {
2470 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2471 } else {
2472 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2473 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002474 }
Roland Levillain4d027112015-07-01 15:41:14 +01002475 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002476}
2477
Alexandre Rames5319def2014-10-23 10:03:10 +01002478void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2480 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002482}
2483
2484void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002485 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002486 vixl::aarch64::Register out = OutputRegister(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002487 BlockPoolsScope block_pools(GetVIXLAssembler());
jessicahandojo05765752016-09-09 19:01:32 -07002488 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002489 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002490 // Mask out compression flag from String's array length.
2491 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002492 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002493 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002494}
2495
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002496void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002497 Primitive::Type value_type = instruction->GetComponentType();
2498
2499 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2501 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002502 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002503 LocationSummary::kCallOnSlowPath :
2504 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002505 locations->SetInAt(0, Location::RequiresRegister());
2506 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002507 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2508 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2509 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002510 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002511 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002512 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002513 }
2514}
2515
2516void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2517 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002518 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002519 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002520 bool needs_write_barrier =
2521 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002522
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002523 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002524 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002525 CPURegister source = value;
2526 Location index = locations->InAt(1);
2527 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2528 MemOperand destination = HeapOperand(array);
2529 MacroAssembler* masm = GetVIXLAssembler();
2530 BlockPoolsScope block_pools(masm);
2531
2532 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002533 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002534 if (index.IsConstant()) {
2535 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2536 destination = HeapOperand(array, offset);
2537 } else {
2538 UseScratchRegisterScope temps(masm);
2539 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002540 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002541 // We do not need to compute the intermediate address from the array: the
2542 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002543 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002544 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002545 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002546 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2547 }
2548 temp = array;
2549 } else {
2550 __ Add(temp, array, offset);
2551 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002552 destination = HeapOperand(temp,
2553 XRegisterFrom(index),
2554 LSL,
2555 Primitive::ComponentSizeShift(value_type));
2556 }
2557 codegen_->Store(value_type, value, destination);
2558 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002559 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002560 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002561 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002562 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002563 {
2564 // We use a block to end the scratch scope before the write barrier, thus
2565 // freeing the temporary registers so they can be used in `MarkGCCard`.
2566 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002567 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002568 if (index.IsConstant()) {
2569 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002570 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002571 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002572 destination = HeapOperand(temp,
2573 XRegisterFrom(index),
2574 LSL,
2575 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002576 }
2577
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002578 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2579 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2580 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2581
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002582 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002583 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2584 codegen_->AddSlowPath(slow_path);
2585 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002586 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002587 __ Cbnz(Register(value), &non_zero);
2588 if (!index.IsConstant()) {
2589 __ Add(temp, array, offset);
2590 }
2591 __ Str(wzr, destination);
2592 codegen_->MaybeRecordImplicitNullCheck(instruction);
2593 __ B(&done);
2594 __ Bind(&non_zero);
2595 }
2596
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002597 // Note that when Baker read barriers are enabled, the type
2598 // checks are performed without read barriers. This is fine,
2599 // even in the case where a class object is in the from-space
2600 // after the flip, as a comparison involving such a type would
2601 // not produce a false positive; it may of course produce a
2602 // false negative, in which case we would take the ArraySet
2603 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002604
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002605 Register temp2 = temps.AcquireSameSizeAs(array);
2606 // /* HeapReference<Class> */ temp = array->klass_
2607 __ Ldr(temp, HeapOperand(array, class_offset));
2608 codegen_->MaybeRecordImplicitNullCheck(instruction);
2609 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002610
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002611 // /* HeapReference<Class> */ temp = temp->component_type_
2612 __ Ldr(temp, HeapOperand(temp, component_offset));
2613 // /* HeapReference<Class> */ temp2 = value->klass_
2614 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2615 // If heap poisoning is enabled, no need to unpoison `temp`
2616 // nor `temp2`, as we are comparing two poisoned references.
2617 __ Cmp(temp, temp2);
2618 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002619
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002620 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2621 vixl::aarch64::Label do_put;
2622 __ B(eq, &do_put);
2623 // If heap poisoning is enabled, the `temp` reference has
2624 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002625 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2626
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002627 // /* HeapReference<Class> */ temp = temp->super_class_
2628 __ Ldr(temp, HeapOperand(temp, super_offset));
2629 // If heap poisoning is enabled, no need to unpoison
2630 // `temp`, as we are comparing against null below.
2631 __ Cbnz(temp, slow_path->GetEntryLabel());
2632 __ Bind(&do_put);
2633 } else {
2634 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002635 }
2636 }
2637
2638 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002639 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002640 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002641 __ Mov(temp2, value.W());
2642 GetAssembler()->PoisonHeapReference(temp2);
2643 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002644 }
2645
2646 if (!index.IsConstant()) {
2647 __ Add(temp, array, offset);
2648 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002649 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002650
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002651 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002652 codegen_->MaybeRecordImplicitNullCheck(instruction);
2653 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002654 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002655
2656 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2657
2658 if (done.IsLinked()) {
2659 __ Bind(&done);
2660 }
2661
2662 if (slow_path != nullptr) {
2663 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002664 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002665 }
2666}
2667
Alexandre Rames67555f72014-11-18 10:55:16 +00002668void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002669 RegisterSet caller_saves = RegisterSet::Empty();
2670 InvokeRuntimeCallingConvention calling_convention;
2671 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2672 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2673 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002674 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002675 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002676}
2677
2678void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002679 BoundsCheckSlowPathARM64* slow_path =
2680 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002681 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002682 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2683 __ B(slow_path->GetEntryLabel(), hs);
2684}
2685
Alexandre Rames67555f72014-11-18 10:55:16 +00002686void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2687 LocationSummary* locations =
2688 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2689 locations->SetInAt(0, Location::RequiresRegister());
2690 if (check->HasUses()) {
2691 locations->SetOut(Location::SameAsFirstInput());
2692 }
2693}
2694
2695void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2696 // We assume the class is not null.
2697 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2698 check->GetLoadClass(), check, check->GetDexPc(), true);
2699 codegen_->AddSlowPath(slow_path);
2700 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2701}
2702
Roland Levillain1a653882016-03-18 18:05:57 +00002703static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2704 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2705 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2706}
2707
2708void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2709 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2710 Location rhs_loc = instruction->GetLocations()->InAt(1);
2711 if (rhs_loc.IsConstant()) {
2712 // 0.0 is the only immediate that can be encoded directly in
2713 // an FCMP instruction.
2714 //
2715 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2716 // specify that in a floating-point comparison, positive zero
2717 // and negative zero are considered equal, so we can use the
2718 // literal 0.0 for both cases here.
2719 //
2720 // Note however that some methods (Float.equal, Float.compare,
2721 // Float.compareTo, Double.equal, Double.compare,
2722 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2723 // StrictMath.min) consider 0.0 to be (strictly) greater than
2724 // -0.0. So if we ever translate calls to these methods into a
2725 // HCompare instruction, we must handle the -0.0 case with
2726 // care here.
2727 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2728 __ Fcmp(lhs_reg, 0.0);
2729 } else {
2730 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2731 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002732}
2733
Serban Constantinescu02164b32014-11-13 14:05:07 +00002734void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002735 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002736 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2737 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002738 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002739 case Primitive::kPrimBoolean:
2740 case Primitive::kPrimByte:
2741 case Primitive::kPrimShort:
2742 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002743 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002744 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002745 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002746 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2748 break;
2749 }
2750 case Primitive::kPrimFloat:
2751 case Primitive::kPrimDouble: {
2752 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002753 locations->SetInAt(1,
2754 IsFloatingPointZeroConstant(compare->InputAt(1))
2755 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2756 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002757 locations->SetOut(Location::RequiresRegister());
2758 break;
2759 }
2760 default:
2761 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2762 }
2763}
2764
2765void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2766 Primitive::Type in_type = compare->InputAt(0)->GetType();
2767
2768 // 0 if: left == right
2769 // 1 if: left > right
2770 // -1 if: left < right
2771 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002772 case Primitive::kPrimBoolean:
2773 case Primitive::kPrimByte:
2774 case Primitive::kPrimShort:
2775 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002776 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002777 case Primitive::kPrimLong: {
2778 Register result = OutputRegister(compare);
2779 Register left = InputRegisterAt(compare, 0);
2780 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002781 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002782 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2783 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002784 break;
2785 }
2786 case Primitive::kPrimFloat:
2787 case Primitive::kPrimDouble: {
2788 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002789 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002790 __ Cset(result, ne);
2791 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002792 break;
2793 }
2794 default:
2795 LOG(FATAL) << "Unimplemented compare type " << in_type;
2796 }
2797}
2798
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002799void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002801
2802 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2803 locations->SetInAt(0, Location::RequiresFpuRegister());
2804 locations->SetInAt(1,
2805 IsFloatingPointZeroConstant(instruction->InputAt(1))
2806 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2807 : Location::RequiresFpuRegister());
2808 } else {
2809 // Integer cases.
2810 locations->SetInAt(0, Location::RequiresRegister());
2811 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2812 }
2813
David Brazdilb3e773e2016-01-26 11:28:37 +00002814 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002816 }
2817}
2818
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002819void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002820 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002821 return;
2822 }
2823
2824 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002825 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002826 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002827
Roland Levillain7f63c522015-07-13 15:54:55 +00002828 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002829 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002830 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002831 } else {
2832 // Integer cases.
2833 Register lhs = InputRegisterAt(instruction, 0);
2834 Operand rhs = InputOperandAt(instruction, 1);
2835 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002836 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002837 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002838}
2839
2840#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2841 M(Equal) \
2842 M(NotEqual) \
2843 M(LessThan) \
2844 M(LessThanOrEqual) \
2845 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002846 M(GreaterThanOrEqual) \
2847 M(Below) \
2848 M(BelowOrEqual) \
2849 M(Above) \
2850 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002851#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002852void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2853void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002854FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002855#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002856#undef FOR_EACH_CONDITION_INSTRUCTION
2857
Zheng Xuc6667102015-05-15 16:08:45 +08002858void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2859 DCHECK(instruction->IsDiv() || instruction->IsRem());
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 Location second = locations->InAt(1);
2863 DCHECK(second.IsConstant());
2864
2865 Register out = OutputRegister(instruction);
2866 Register dividend = InputRegisterAt(instruction, 0);
2867 int64_t imm = Int64FromConstant(second.GetConstant());
2868 DCHECK(imm == 1 || imm == -1);
2869
2870 if (instruction->IsRem()) {
2871 __ Mov(out, 0);
2872 } else {
2873 if (imm == 1) {
2874 __ Mov(out, dividend);
2875 } else {
2876 __ Neg(out, dividend);
2877 }
2878 }
2879}
2880
2881void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2882 DCHECK(instruction->IsDiv() || instruction->IsRem());
2883
2884 LocationSummary* locations = instruction->GetLocations();
2885 Location second = locations->InAt(1);
2886 DCHECK(second.IsConstant());
2887
2888 Register out = OutputRegister(instruction);
2889 Register dividend = InputRegisterAt(instruction, 0);
2890 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002891 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002892 int ctz_imm = CTZ(abs_imm);
2893
2894 UseScratchRegisterScope temps(GetVIXLAssembler());
2895 Register temp = temps.AcquireSameSizeAs(out);
2896
2897 if (instruction->IsDiv()) {
2898 __ Add(temp, dividend, abs_imm - 1);
2899 __ Cmp(dividend, 0);
2900 __ Csel(out, temp, dividend, lt);
2901 if (imm > 0) {
2902 __ Asr(out, out, ctz_imm);
2903 } else {
2904 __ Neg(out, Operand(out, ASR, ctz_imm));
2905 }
2906 } else {
2907 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2908 __ Asr(temp, dividend, bits - 1);
2909 __ Lsr(temp, temp, bits - ctz_imm);
2910 __ Add(out, dividend, temp);
2911 __ And(out, out, abs_imm - 1);
2912 __ Sub(out, out, temp);
2913 }
2914}
2915
2916void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2917 DCHECK(instruction->IsDiv() || instruction->IsRem());
2918
2919 LocationSummary* locations = instruction->GetLocations();
2920 Location second = locations->InAt(1);
2921 DCHECK(second.IsConstant());
2922
2923 Register out = OutputRegister(instruction);
2924 Register dividend = InputRegisterAt(instruction, 0);
2925 int64_t imm = Int64FromConstant(second.GetConstant());
2926
2927 Primitive::Type type = instruction->GetResultType();
2928 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2929
2930 int64_t magic;
2931 int shift;
2932 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2933
2934 UseScratchRegisterScope temps(GetVIXLAssembler());
2935 Register temp = temps.AcquireSameSizeAs(out);
2936
2937 // temp = get_high(dividend * magic)
2938 __ Mov(temp, magic);
2939 if (type == Primitive::kPrimLong) {
2940 __ Smulh(temp, dividend, temp);
2941 } else {
2942 __ Smull(temp.X(), dividend, temp);
2943 __ Lsr(temp.X(), temp.X(), 32);
2944 }
2945
2946 if (imm > 0 && magic < 0) {
2947 __ Add(temp, temp, dividend);
2948 } else if (imm < 0 && magic > 0) {
2949 __ Sub(temp, temp, dividend);
2950 }
2951
2952 if (shift != 0) {
2953 __ Asr(temp, temp, shift);
2954 }
2955
2956 if (instruction->IsDiv()) {
2957 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2958 } else {
2959 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2960 // TODO: Strength reduction for msub.
2961 Register temp_imm = temps.AcquireSameSizeAs(out);
2962 __ Mov(temp_imm, imm);
2963 __ Msub(out, temp, temp_imm, dividend);
2964 }
2965}
2966
2967void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2968 DCHECK(instruction->IsDiv() || instruction->IsRem());
2969 Primitive::Type type = instruction->GetResultType();
2970 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2971
2972 LocationSummary* locations = instruction->GetLocations();
2973 Register out = OutputRegister(instruction);
2974 Location second = locations->InAt(1);
2975
2976 if (second.IsConstant()) {
2977 int64_t imm = Int64FromConstant(second.GetConstant());
2978
2979 if (imm == 0) {
2980 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2981 } else if (imm == 1 || imm == -1) {
2982 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002983 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002984 DivRemByPowerOfTwo(instruction);
2985 } else {
2986 DCHECK(imm <= -2 || imm >= 2);
2987 GenerateDivRemWithAnyConstant(instruction);
2988 }
2989 } else {
2990 Register dividend = InputRegisterAt(instruction, 0);
2991 Register divisor = InputRegisterAt(instruction, 1);
2992 if (instruction->IsDiv()) {
2993 __ Sdiv(out, dividend, divisor);
2994 } else {
2995 UseScratchRegisterScope temps(GetVIXLAssembler());
2996 Register temp = temps.AcquireSameSizeAs(out);
2997 __ Sdiv(temp, dividend, divisor);
2998 __ Msub(out, temp, divisor, dividend);
2999 }
3000 }
3001}
3002
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003003void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3004 LocationSummary* locations =
3005 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3006 switch (div->GetResultType()) {
3007 case Primitive::kPrimInt:
3008 case Primitive::kPrimLong:
3009 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003010 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003011 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3012 break;
3013
3014 case Primitive::kPrimFloat:
3015 case Primitive::kPrimDouble:
3016 locations->SetInAt(0, Location::RequiresFpuRegister());
3017 locations->SetInAt(1, Location::RequiresFpuRegister());
3018 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3019 break;
3020
3021 default:
3022 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3023 }
3024}
3025
3026void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3027 Primitive::Type type = div->GetResultType();
3028 switch (type) {
3029 case Primitive::kPrimInt:
3030 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003031 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003032 break;
3033
3034 case Primitive::kPrimFloat:
3035 case Primitive::kPrimDouble:
3036 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3037 break;
3038
3039 default:
3040 LOG(FATAL) << "Unexpected div type " << type;
3041 }
3042}
3043
Alexandre Rames67555f72014-11-18 10:55:16 +00003044void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003045 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003046 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003047}
3048
3049void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3050 SlowPathCodeARM64* slow_path =
3051 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3052 codegen_->AddSlowPath(slow_path);
3053 Location value = instruction->GetLocations()->InAt(0);
3054
Alexandre Rames3e69f162014-12-10 10:36:50 +00003055 Primitive::Type type = instruction->GetType();
3056
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003057 if (!Primitive::IsIntegralType(type)) {
3058 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003059 return;
3060 }
3061
Alexandre Rames67555f72014-11-18 10:55:16 +00003062 if (value.IsConstant()) {
3063 int64_t divisor = Int64ConstantFrom(value);
3064 if (divisor == 0) {
3065 __ B(slow_path->GetEntryLabel());
3066 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003067 // A division by a non-null constant is valid. We don't need to perform
3068 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003069 }
3070 } else {
3071 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3072 }
3073}
3074
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003075void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3076 LocationSummary* locations =
3077 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3078 locations->SetOut(Location::ConstantLocation(constant));
3079}
3080
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003081void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3082 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003083 // Will be generated at use site.
3084}
3085
Alexandre Rames5319def2014-10-23 10:03:10 +01003086void LocationsBuilderARM64::VisitExit(HExit* exit) {
3087 exit->SetLocations(nullptr);
3088}
3089
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003090void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003091}
3092
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003093void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3094 LocationSummary* locations =
3095 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3096 locations->SetOut(Location::ConstantLocation(constant));
3097}
3098
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003099void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003100 // Will be generated at use site.
3101}
3102
David Brazdilfc6a86a2015-06-26 10:33:45 +00003103void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003104 DCHECK(!successor->IsExitBlock());
3105 HBasicBlock* block = got->GetBlock();
3106 HInstruction* previous = got->GetPrevious();
3107 HLoopInformation* info = block->GetLoopInformation();
3108
David Brazdil46e2a392015-03-16 17:31:52 +00003109 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003110 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3111 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3112 return;
3113 }
3114 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3115 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3116 }
3117 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003118 __ B(codegen_->GetLabelOf(successor));
3119 }
3120}
3121
David Brazdilfc6a86a2015-06-26 10:33:45 +00003122void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3123 got->SetLocations(nullptr);
3124}
3125
3126void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3127 HandleGoto(got, got->GetSuccessor());
3128}
3129
3130void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3131 try_boundary->SetLocations(nullptr);
3132}
3133
3134void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3135 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3136 if (!successor->IsExitBlock()) {
3137 HandleGoto(try_boundary, successor);
3138 }
3139}
3140
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003141void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003142 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003143 vixl::aarch64::Label* true_target,
3144 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003145 // FP branching requires both targets to be explicit. If either of the targets
3146 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003147 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003148 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003149
David Brazdil0debae72015-11-12 18:37:00 +00003150 if (true_target == nullptr && false_target == nullptr) {
3151 // Nothing to do. The code always falls through.
3152 return;
3153 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003154 // Constant condition, statically compared against "true" (integer value 1).
3155 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003156 if (true_target != nullptr) {
3157 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003158 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003159 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003160 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003161 if (false_target != nullptr) {
3162 __ B(false_target);
3163 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003164 }
David Brazdil0debae72015-11-12 18:37:00 +00003165 return;
3166 }
3167
3168 // The following code generates these patterns:
3169 // (1) true_target == nullptr && false_target != nullptr
3170 // - opposite condition true => branch to false_target
3171 // (2) true_target != nullptr && false_target == nullptr
3172 // - condition true => branch to true_target
3173 // (3) true_target != nullptr && false_target != nullptr
3174 // - condition true => branch to true_target
3175 // - branch to false_target
3176 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003177 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003178 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003179 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003180 if (true_target == nullptr) {
3181 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3182 } else {
3183 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3184 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003185 } else {
3186 // The condition instruction has not been materialized, use its inputs as
3187 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003188 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003189
David Brazdil0debae72015-11-12 18:37:00 +00003190 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003191 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003192 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003193 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003194 IfCondition opposite_condition = condition->GetOppositeCondition();
3195 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003196 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003197 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003198 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003199 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003200 // Integer cases.
3201 Register lhs = InputRegisterAt(condition, 0);
3202 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003203
3204 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003205 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003206 if (true_target == nullptr) {
3207 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3208 non_fallthrough_target = false_target;
3209 } else {
3210 arm64_cond = ARM64Condition(condition->GetCondition());
3211 non_fallthrough_target = true_target;
3212 }
3213
Aart Bik086d27e2016-01-20 17:02:00 -08003214 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003215 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003216 switch (arm64_cond) {
3217 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003218 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003219 break;
3220 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003221 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003222 break;
3223 case lt:
3224 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003225 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003226 break;
3227 case ge:
3228 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003229 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003230 break;
3231 default:
3232 // Without the `static_cast` the compiler throws an error for
3233 // `-Werror=sign-promo`.
3234 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3235 }
3236 } else {
3237 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003238 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003239 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003240 }
3241 }
David Brazdil0debae72015-11-12 18:37:00 +00003242
3243 // If neither branch falls through (case 3), the conditional branch to `true_target`
3244 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3245 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003246 __ B(false_target);
3247 }
David Brazdil0debae72015-11-12 18:37:00 +00003248
3249 if (fallthrough_target.IsLinked()) {
3250 __ Bind(&fallthrough_target);
3251 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003252}
3253
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003254void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003256 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003257 locations->SetInAt(0, Location::RequiresRegister());
3258 }
3259}
3260
3261void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003262 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3263 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003264 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3265 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3266 true_target = nullptr;
3267 }
3268 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3269 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3270 false_target = nullptr;
3271 }
David Brazdil0debae72015-11-12 18:37:00 +00003272 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003273}
3274
3275void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3276 LocationSummary* locations = new (GetGraph()->GetArena())
3277 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003278 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003279 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003280 locations->SetInAt(0, Location::RequiresRegister());
3281 }
3282}
3283
3284void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003285 SlowPathCodeARM64* slow_path =
3286 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003287 GenerateTestAndBranch(deoptimize,
3288 /* condition_input_index */ 0,
3289 slow_path->GetEntryLabel(),
3290 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003291}
3292
Mingyao Yang063fc772016-08-02 11:02:54 -07003293void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3294 LocationSummary* locations = new (GetGraph()->GetArena())
3295 LocationSummary(flag, LocationSummary::kNoCall);
3296 locations->SetOut(Location::RequiresRegister());
3297}
3298
3299void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3300 __ Ldr(OutputRegister(flag),
3301 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3302}
3303
David Brazdilc0b601b2016-02-08 14:20:45 +00003304static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3305 return condition->IsCondition() &&
3306 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3307}
3308
Alexandre Rames880f1192016-06-13 16:04:50 +01003309static inline Condition GetConditionForSelect(HCondition* condition) {
3310 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003311 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3312 : ARM64Condition(cond);
3313}
3314
David Brazdil74eb1b22015-12-14 11:44:01 +00003315void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003317 if (Primitive::IsFloatingPointType(select->GetType())) {
3318 locations->SetInAt(0, Location::RequiresFpuRegister());
3319 locations->SetInAt(1, Location::RequiresFpuRegister());
3320 locations->SetOut(Location::RequiresFpuRegister());
3321 } else {
3322 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3323 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3324 bool is_true_value_constant = cst_true_value != nullptr;
3325 bool is_false_value_constant = cst_false_value != nullptr;
3326 // Ask VIXL whether we should synthesize constants in registers.
3327 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3328 Operand true_op = is_true_value_constant ?
3329 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3330 Operand false_op = is_false_value_constant ?
3331 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3332 bool true_value_in_register = false;
3333 bool false_value_in_register = false;
3334 MacroAssembler::GetCselSynthesisInformation(
3335 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3336 true_value_in_register |= !is_true_value_constant;
3337 false_value_in_register |= !is_false_value_constant;
3338
3339 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3340 : Location::ConstantLocation(cst_true_value));
3341 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3342 : Location::ConstantLocation(cst_false_value));
3343 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003344 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003345
David Brazdil74eb1b22015-12-14 11:44:01 +00003346 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3347 locations->SetInAt(2, Location::RequiresRegister());
3348 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003349}
3350
3351void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003352 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003353 Condition csel_cond;
3354
3355 if (IsBooleanValueOrMaterializedCondition(cond)) {
3356 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003357 // Use the condition flags set by the previous instruction.
3358 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003359 } else {
3360 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003361 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003362 }
3363 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003364 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003365 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003366 } else {
3367 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003368 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003369 }
3370
Alexandre Rames880f1192016-06-13 16:04:50 +01003371 if (Primitive::IsFloatingPointType(select->GetType())) {
3372 __ Fcsel(OutputFPRegister(select),
3373 InputFPRegisterAt(select, 1),
3374 InputFPRegisterAt(select, 0),
3375 csel_cond);
3376 } else {
3377 __ Csel(OutputRegister(select),
3378 InputOperandAt(select, 1),
3379 InputOperandAt(select, 0),
3380 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003381 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003382}
3383
David Srbecky0cf44932015-12-09 14:09:59 +00003384void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3385 new (GetGraph()->GetArena()) LocationSummary(info);
3386}
3387
David Srbeckyd28f4a02016-03-14 17:14:24 +00003388void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3389 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003390}
3391
3392void CodeGeneratorARM64::GenerateNop() {
3393 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003394}
3395
Alexandre Rames5319def2014-10-23 10:03:10 +01003396void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003397 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003398}
3399
3400void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003401 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003402}
3403
3404void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003405 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003406}
3407
3408void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003409 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003410}
3411
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003412// Temp is used for read barrier.
3413static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3414 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003415 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003416 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3417 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3418 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3419 return 1;
3420 }
3421 return 0;
3422}
3423
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003424// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003425// interface pointer, one for loading the current interface.
3426// The other checks have one temp for loading the object's class.
3427static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3428 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3429 return 3;
3430 }
3431 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003432}
3433
Alexandre Rames67555f72014-11-18 10:55:16 +00003434void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003435 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003436 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003437 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003438 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003439 case TypeCheckKind::kExactCheck:
3440 case TypeCheckKind::kAbstractClassCheck:
3441 case TypeCheckKind::kClassHierarchyCheck:
3442 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003443 call_kind =
3444 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003445 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003446 break;
3447 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448 case TypeCheckKind::kUnresolvedCheck:
3449 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003450 call_kind = LocationSummary::kCallOnSlowPath;
3451 break;
3452 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003453
Alexandre Rames67555f72014-11-18 10:55:16 +00003454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003455 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003456 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003457 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003458 locations->SetInAt(0, Location::RequiresRegister());
3459 locations->SetInAt(1, Location::RequiresRegister());
3460 // The "out" register is used as a temporary, so it overlaps with the inputs.
3461 // Note that TypeCheckSlowPathARM64 uses this register too.
3462 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003463 // Add temps if necessary for read barriers.
3464 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003465}
3466
3467void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003468 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003469 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003470 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003471 Register obj = InputRegisterAt(instruction, 0);
3472 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003473 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003474 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003475 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3476 DCHECK_LE(num_temps, 1u);
3477 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3479 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3480 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3481 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003482
Scott Wakeling97c72b72016-06-24 16:19:36 +01003483 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003484 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003485
3486 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003487 // Avoid null check if we know `obj` is not null.
3488 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003489 __ Cbz(obj, &zero);
3490 }
3491
Roland Levillain44015862016-01-22 11:47:17 +00003492 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003493 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003494 // /* HeapReference<Class> */ out = obj->klass_
3495 GenerateReferenceLoadTwoRegisters(instruction,
3496 out_loc,
3497 obj_loc,
3498 class_offset,
3499 maybe_temp_loc,
3500 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003501 __ Cmp(out, cls);
3502 __ Cset(out, eq);
3503 if (zero.IsLinked()) {
3504 __ B(&done);
3505 }
3506 break;
3507 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003509 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003510 // /* HeapReference<Class> */ out = obj->klass_
3511 GenerateReferenceLoadTwoRegisters(instruction,
3512 out_loc,
3513 obj_loc,
3514 class_offset,
3515 maybe_temp_loc,
3516 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003517 // If the class is abstract, we eagerly fetch the super class of the
3518 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003519 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003520 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003521 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003522 GenerateReferenceLoadOneRegister(instruction,
3523 out_loc,
3524 super_offset,
3525 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003526 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003527 // If `out` is null, we use it for the result, and jump to `done`.
3528 __ Cbz(out, &done);
3529 __ Cmp(out, cls);
3530 __ B(ne, &loop);
3531 __ Mov(out, 1);
3532 if (zero.IsLinked()) {
3533 __ B(&done);
3534 }
3535 break;
3536 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003537
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003538 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003539 // /* HeapReference<Class> */ out = obj->klass_
3540 GenerateReferenceLoadTwoRegisters(instruction,
3541 out_loc,
3542 obj_loc,
3543 class_offset,
3544 maybe_temp_loc,
3545 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003547 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003548 __ Bind(&loop);
3549 __ Cmp(out, cls);
3550 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003551 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003552 GenerateReferenceLoadOneRegister(instruction,
3553 out_loc,
3554 super_offset,
3555 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003556 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003557 __ Cbnz(out, &loop);
3558 // If `out` is null, we use it for the result, and jump to `done`.
3559 __ B(&done);
3560 __ Bind(&success);
3561 __ Mov(out, 1);
3562 if (zero.IsLinked()) {
3563 __ B(&done);
3564 }
3565 break;
3566 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003567
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003568 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003569 // /* HeapReference<Class> */ out = obj->klass_
3570 GenerateReferenceLoadTwoRegisters(instruction,
3571 out_loc,
3572 obj_loc,
3573 class_offset,
3574 maybe_temp_loc,
3575 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003576 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003577 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003578 __ Cmp(out, cls);
3579 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003580 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003581 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003582 GenerateReferenceLoadOneRegister(instruction,
3583 out_loc,
3584 component_offset,
3585 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003586 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003587 // If `out` is null, we use it for the result, and jump to `done`.
3588 __ Cbz(out, &done);
3589 __ Ldrh(out, HeapOperand(out, primitive_offset));
3590 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3591 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003592 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003593 __ Mov(out, 1);
3594 __ B(&done);
3595 break;
3596 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003597
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003598 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003599 // No read barrier since the slow path will retry upon failure.
3600 // /* HeapReference<Class> */ out = obj->klass_
3601 GenerateReferenceLoadTwoRegisters(instruction,
3602 out_loc,
3603 obj_loc,
3604 class_offset,
3605 maybe_temp_loc,
3606 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003607 __ Cmp(out, cls);
3608 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003609 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3610 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003611 codegen_->AddSlowPath(slow_path);
3612 __ B(ne, slow_path->GetEntryLabel());
3613 __ Mov(out, 1);
3614 if (zero.IsLinked()) {
3615 __ B(&done);
3616 }
3617 break;
3618 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003619
Calin Juravle98893e12015-10-02 21:05:03 +01003620 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003621 case TypeCheckKind::kInterfaceCheck: {
3622 // Note that we indeed only call on slow path, but we always go
3623 // into the slow path for the unresolved and interface check
3624 // cases.
3625 //
3626 // We cannot directly call the InstanceofNonTrivial runtime
3627 // entry point without resorting to a type checking slow path
3628 // here (i.e. by calling InvokeRuntime directly), as it would
3629 // require to assign fixed registers for the inputs of this
3630 // HInstanceOf instruction (following the runtime calling
3631 // convention), which might be cluttered by the potential first
3632 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003633 //
3634 // TODO: Introduce a new runtime entry point taking the object
3635 // to test (instead of its class) as argument, and let it deal
3636 // with the read barrier issues. This will let us refactor this
3637 // case of the `switch` code as it was previously (with a direct
3638 // call to the runtime not using a type checking slow path).
3639 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 DCHECK(locations->OnlyCallsOnSlowPath());
3641 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3642 /* is_fatal */ false);
3643 codegen_->AddSlowPath(slow_path);
3644 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003645 if (zero.IsLinked()) {
3646 __ B(&done);
3647 }
3648 break;
3649 }
3650 }
3651
3652 if (zero.IsLinked()) {
3653 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003654 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003655 }
3656
3657 if (done.IsLinked()) {
3658 __ Bind(&done);
3659 }
3660
3661 if (slow_path != nullptr) {
3662 __ Bind(slow_path->GetExitLabel());
3663 }
3664}
3665
3666void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3667 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3668 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3669
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003670 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3671 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003672 case TypeCheckKind::kExactCheck:
3673 case TypeCheckKind::kAbstractClassCheck:
3674 case TypeCheckKind::kClassHierarchyCheck:
3675 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003676 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3677 LocationSummary::kCallOnSlowPath :
3678 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003679 break;
3680 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003681 case TypeCheckKind::kUnresolvedCheck:
3682 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003683 call_kind = LocationSummary::kCallOnSlowPath;
3684 break;
3685 }
3686
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3688 locations->SetInAt(0, Location::RequiresRegister());
3689 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003690 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3691 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003692}
3693
3694void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003695 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003696 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003697 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003698 Register obj = InputRegisterAt(instruction, 0);
3699 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003700 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3701 DCHECK_GE(num_temps, 1u);
3702 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003703 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003704 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3705 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003707 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3708 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3709 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3710 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3711 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3712 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3713 const uint32_t object_array_data_offset =
3714 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003715
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003716 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003717 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3718 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3719 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003720 if (!kEmitCompilerReadBarrier) {
3721 is_type_check_slow_path_fatal =
3722 (type_check_kind == TypeCheckKind::kExactCheck ||
3723 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3724 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3725 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3726 !instruction->CanThrowIntoCatchBlock();
3727 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003728 SlowPathCodeARM64* type_check_slow_path =
3729 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3730 is_type_check_slow_path_fatal);
3731 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003732
Scott Wakeling97c72b72016-06-24 16:19:36 +01003733 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003734 // Avoid null check if we know obj is not null.
3735 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003736 __ Cbz(obj, &done);
3737 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003738
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003739 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003740 case TypeCheckKind::kExactCheck:
3741 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003742 // /* HeapReference<Class> */ temp = obj->klass_
3743 GenerateReferenceLoadTwoRegisters(instruction,
3744 temp_loc,
3745 obj_loc,
3746 class_offset,
3747 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003748 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003750 __ Cmp(temp, cls);
3751 // Jump to slow path for throwing the exception or doing a
3752 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003753 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003754 break;
3755 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003757 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003758 // /* HeapReference<Class> */ temp = obj->klass_
3759 GenerateReferenceLoadTwoRegisters(instruction,
3760 temp_loc,
3761 obj_loc,
3762 class_offset,
3763 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003764 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003765
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003766 // If the class is abstract, we eagerly fetch the super class of the
3767 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003768 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003769 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003770 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003771 GenerateReferenceLoadOneRegister(instruction,
3772 temp_loc,
3773 super_offset,
3774 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003775 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003776
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003777 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3778 // exception.
3779 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3780 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003781 __ Cmp(temp, cls);
3782 __ B(ne, &loop);
3783 break;
3784 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003786 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003787 // /* HeapReference<Class> */ temp = obj->klass_
3788 GenerateReferenceLoadTwoRegisters(instruction,
3789 temp_loc,
3790 obj_loc,
3791 class_offset,
3792 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003793 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003795 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003796 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003797 __ Bind(&loop);
3798 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003799 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003800
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003801 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003802 GenerateReferenceLoadOneRegister(instruction,
3803 temp_loc,
3804 super_offset,
3805 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003806 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003807
3808 // If the class reference currently in `temp` is not null, jump
3809 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003810 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003811 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003812 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003813 break;
3814 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003815
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003816 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003817 // /* HeapReference<Class> */ temp = obj->klass_
3818 GenerateReferenceLoadTwoRegisters(instruction,
3819 temp_loc,
3820 obj_loc,
3821 class_offset,
3822 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003823 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003824
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003825 // Do an exact check.
3826 __ Cmp(temp, cls);
3827 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003828
3829 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003830 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003831 GenerateReferenceLoadOneRegister(instruction,
3832 temp_loc,
3833 component_offset,
3834 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003835 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003836
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003837 // If the component type is null, jump to the slow path to throw the exception.
3838 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3839 // Otherwise, the object is indeed an array. Further check that this component type is not a
3840 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003841 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3842 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003843 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003844 break;
3845 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003846
Calin Juravle98893e12015-10-02 21:05:03 +01003847 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003848 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003849 //
3850 // We cannot directly call the CheckCast runtime entry point
3851 // without resorting to a type checking slow path here (i.e. by
3852 // calling InvokeRuntime directly), as it would require to
3853 // assign fixed registers for the inputs of this HInstanceOf
3854 // instruction (following the runtime calling convention), which
3855 // might be cluttered by the potential first read barrier
3856 // emission at the beginning of this method.
3857 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003858 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003859 case TypeCheckKind::kInterfaceCheck: {
3860 // /* HeapReference<Class> */ temp = obj->klass_
3861 GenerateReferenceLoadTwoRegisters(instruction,
3862 temp_loc,
3863 obj_loc,
3864 class_offset,
3865 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003866 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003867
3868 // /* HeapReference<Class> */ temp = temp->iftable_
3869 GenerateReferenceLoadTwoRegisters(instruction,
3870 temp_loc,
3871 temp_loc,
3872 iftable_offset,
3873 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003874 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003875 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003876 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003877 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003878 vixl::aarch64::Label start_loop;
3879 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003880 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003881 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3882 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003883 // Go to next interface.
3884 __ Add(temp, temp, 2 * kHeapReferenceSize);
3885 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003886 // Compare the classes and continue the loop if they do not match.
3887 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
3888 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003889 break;
3890 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003891 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003892 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003893
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003894 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003895}
3896
Alexandre Rames5319def2014-10-23 10:03:10 +01003897void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3899 locations->SetOut(Location::ConstantLocation(constant));
3900}
3901
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003902void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003903 // Will be generated at use site.
3904}
3905
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003906void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3908 locations->SetOut(Location::ConstantLocation(constant));
3909}
3910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003911void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003912 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003913}
3914
Calin Juravle175dc732015-08-25 15:42:32 +01003915void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3916 // The trampoline uses the same calling convention as dex calling conventions,
3917 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3918 // the method_idx.
3919 HandleInvoke(invoke);
3920}
3921
3922void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3923 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3924}
3925
Alexandre Rames5319def2014-10-23 10:03:10 +01003926void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003927 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003928 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003929}
3930
Alexandre Rames67555f72014-11-18 10:55:16 +00003931void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3932 HandleInvoke(invoke);
3933}
3934
3935void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3936 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 LocationSummary* locations = invoke->GetLocations();
3938 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003940 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003941 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003942
3943 // The register ip1 is required to be used for the hidden argument in
3944 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003945 MacroAssembler* masm = GetVIXLAssembler();
3946 UseScratchRegisterScope scratch_scope(masm);
3947 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003948 scratch_scope.Exclude(ip1);
3949 __ Mov(ip1, invoke->GetDexMethodIndex());
3950
Alexandre Rames67555f72014-11-18 10:55:16 +00003951 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003952 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003953 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003954 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003955 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003956 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003957 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003958 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003959 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003960 // Instead of simply (possibly) unpoisoning `temp` here, we should
3961 // emit a read barrier for the previous class reference load.
3962 // However this is not required in practice, as this is an
3963 // intermediate/temporary reference and because the current
3964 // concurrent copying collector keeps the from-space memory
3965 // intact/accessible until the end of the marking phase (the
3966 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003967 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003968 __ Ldr(temp,
3969 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3970 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003971 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003972 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003973 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003974 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003975 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003976 // lr();
3977 __ Blr(lr);
3978 DCHECK(!codegen_->IsLeafMethod());
3979 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3980}
3981
3982void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003983 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3984 if (intrinsic.TryDispatch(invoke)) {
3985 return;
3986 }
3987
Alexandre Rames67555f72014-11-18 10:55:16 +00003988 HandleInvoke(invoke);
3989}
3990
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003991void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003992 // Explicit clinit checks triggered by static invokes must have been pruned by
3993 // art::PrepareForRegisterAllocation.
3994 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003995
Andreas Gampe878d58c2015-01-15 23:24:00 -08003996 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3997 if (intrinsic.TryDispatch(invoke)) {
3998 return;
3999 }
4000
Alexandre Rames67555f72014-11-18 10:55:16 +00004001 HandleInvoke(invoke);
4002}
4003
Andreas Gampe878d58c2015-01-15 23:24:00 -08004004static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4005 if (invoke->GetLocations()->Intrinsified()) {
4006 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4007 intrinsic.Dispatch(invoke);
4008 return true;
4009 }
4010 return false;
4011}
4012
Vladimir Markodc151b22015-10-15 18:02:30 +01004013HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4014 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004015 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004016 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004017 return desired_dispatch_info;
4018}
4019
TatWai Chongd8c052a2016-11-02 16:12:48 +08004020Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4021 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004022 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004023 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4024 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004025 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4026 uint32_t offset =
4027 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004028 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004029 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004030 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004031 }
Vladimir Marko58155012015-08-19 12:49:41 +00004032 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004033 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004034 break;
4035 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4036 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004037 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004038 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004039 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4040 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004041 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004042 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004043 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004044 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004045 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004046 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004047 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004048 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004049 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004050 }
Vladimir Marko58155012015-08-19 12:49:41 +00004051 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004052 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004053 Register reg = XRegisterFrom(temp);
4054 Register method_reg;
4055 if (current_method.IsRegister()) {
4056 method_reg = XRegisterFrom(current_method);
4057 } else {
4058 DCHECK(invoke->GetLocations()->Intrinsified());
4059 DCHECK(!current_method.IsValid());
4060 method_reg = reg;
4061 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4062 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004063
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004065 __ Ldr(reg.X(),
4066 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004067 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004068 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004069 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4070 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004071 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4072 break;
4073 }
4074 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004075 return callee_method;
4076}
4077
4078void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4079 // All registers are assumed to be correctly set up.
4080 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004081
4082 switch (invoke->GetCodePtrLocation()) {
4083 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4084 __ Bl(&frame_entry_label_);
4085 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004086 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4087 // LR = callee_method->entry_point_from_quick_compiled_code_;
4088 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004089 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004090 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004091 // lr()
4092 __ Blr(lr);
4093 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004094 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004095
Andreas Gampe878d58c2015-01-15 23:24:00 -08004096 DCHECK(!IsLeafMethod());
4097}
4098
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004099void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004100 // Use the calling convention instead of the location of the receiver, as
4101 // intrinsics may have put the receiver in a different register. In the intrinsics
4102 // slow path, the arguments have been moved to the right place, so here we are
4103 // guaranteed that the receiver is the first register of the calling convention.
4104 InvokeDexCallingConvention calling_convention;
4105 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004106 Register temp = XRegisterFrom(temp_in);
4107 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4108 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4109 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004110 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004111
4112 BlockPoolsScope block_pools(GetVIXLAssembler());
4113
4114 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004116 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004117 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 // Instead of simply (possibly) unpoisoning `temp` here, we should
4119 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 // intermediate/temporary reference and because the current
4121 // concurrent copying collector keeps the from-space memory
4122 // intact/accessible until the end of the marking phase (the
4123 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004124 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4125 // temp = temp->GetMethodAt(method_offset);
4126 __ Ldr(temp, MemOperand(temp, method_offset));
4127 // lr = temp->GetEntryPoint();
4128 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
4129 // lr();
4130 __ Blr(lr);
4131}
4132
Orion Hodsonac141392017-01-13 11:53:47 +00004133void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4134 HandleInvoke(invoke);
4135}
4136
4137void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4138 codegen_->GenerateInvokePolymorphicCall(invoke);
4139}
4140
Scott Wakeling97c72b72016-06-24 16:19:36 +01004141vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4142 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004143 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004144 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004145 return
4146 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004147}
4148
Scott Wakeling97c72b72016-06-24 16:19:36 +01004149vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4150 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004151 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004152 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004153 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004154}
4155
Vladimir Marko1998cd02017-01-13 13:02:58 +00004156vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4157 const DexFile& dex_file,
4158 dex::TypeIndex type_index,
4159 vixl::aarch64::Label* adrp_label) {
4160 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4161}
4162
Scott Wakeling97c72b72016-06-24 16:19:36 +01004163vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4164 const DexFile& dex_file,
4165 uint32_t element_offset,
4166 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004167 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4168}
4169
Scott Wakeling97c72b72016-06-24 16:19:36 +01004170vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4171 const DexFile& dex_file,
4172 uint32_t offset_or_index,
4173 vixl::aarch64::Label* adrp_label,
4174 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004175 // Add a patch entry and return the label.
4176 patches->emplace_back(dex_file, offset_or_index);
4177 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004178 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004179 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4180 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4181 return label;
4182}
4183
Scott Wakeling97c72b72016-06-24 16:19:36 +01004184vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004185 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004186 return boot_image_string_patches_.GetOrCreate(
4187 StringReference(&dex_file, string_index),
4188 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4189}
4190
Scott Wakeling97c72b72016-06-24 16:19:36 +01004191vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004192 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004193 return boot_image_type_patches_.GetOrCreate(
4194 TypeReference(&dex_file, type_index),
4195 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4196}
4197
Scott Wakeling97c72b72016-06-24 16:19:36 +01004198vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4199 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004200 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4201 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4202 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4203}
4204
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004205vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004206 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4207 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4208 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004209 return jit_string_patches_.GetOrCreate(
4210 StringReference(&dex_file, string_index),
4211 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4212}
4213
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004214vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004215 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4216 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4217 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004218 return jit_class_patches_.GetOrCreate(
4219 TypeReference(&dex_file, type_index),
4220 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4221}
4222
Vladimir Markoaad75c62016-10-03 08:46:48 +00004223void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4224 vixl::aarch64::Register reg) {
4225 DCHECK(reg.IsX());
4226 SingleEmissionCheckScope guard(GetVIXLAssembler());
4227 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004228 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004229}
4230
4231void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4232 vixl::aarch64::Register out,
4233 vixl::aarch64::Register base) {
4234 DCHECK(out.IsX());
4235 DCHECK(base.IsX());
4236 SingleEmissionCheckScope guard(GetVIXLAssembler());
4237 __ Bind(fixup_label);
4238 __ add(out, base, Operand(/* offset placeholder */ 0));
4239}
4240
4241void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4242 vixl::aarch64::Register out,
4243 vixl::aarch64::Register base) {
4244 DCHECK(base.IsX());
4245 SingleEmissionCheckScope guard(GetVIXLAssembler());
4246 __ Bind(fixup_label);
4247 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4248}
4249
4250template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4251inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4252 const ArenaDeque<PcRelativePatchInfo>& infos,
4253 ArenaVector<LinkerPatch>* linker_patches) {
4254 for (const PcRelativePatchInfo& info : infos) {
4255 linker_patches->push_back(Factory(info.label.GetLocation(),
4256 &info.target_dex_file,
4257 info.pc_insn_label->GetLocation(),
4258 info.offset_or_index));
4259 }
4260}
4261
Vladimir Marko58155012015-08-19 12:49:41 +00004262void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4263 DCHECK(linker_patches->empty());
4264 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004265 pc_relative_dex_cache_patches_.size() +
4266 boot_image_string_patches_.size() +
4267 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004268 boot_image_type_patches_.size() +
4269 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004270 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004271 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004272 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004273 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004274 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004275 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004276 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004277 info.offset_or_index));
4278 }
4279 for (const auto& entry : boot_image_string_patches_) {
4280 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004281 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4282 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004283 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004284 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004285 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004286 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004287 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004288 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4289 linker_patches);
4290 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004291 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4292 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004293 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4294 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004295 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004296 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4297 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004298 for (const auto& entry : boot_image_type_patches_) {
4299 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004300 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4301 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004302 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004303 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004304 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004305 for (const auto& entry : boot_image_address_patches_) {
4306 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004307 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4308 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004309 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004310 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004311}
4312
Scott Wakeling97c72b72016-06-24 16:19:36 +01004313vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004314 Uint32ToLiteralMap* map) {
4315 return map->GetOrCreate(
4316 value,
4317 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4318}
4319
Scott Wakeling97c72b72016-06-24 16:19:36 +01004320vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004321 return uint64_literals_.GetOrCreate(
4322 value,
4323 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004324}
4325
Scott Wakeling97c72b72016-06-24 16:19:36 +01004326vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004327 MethodReference target_method,
4328 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004329 return map->GetOrCreate(
4330 target_method,
4331 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004332}
4333
Andreas Gampe878d58c2015-01-15 23:24:00 -08004334void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004335 // Explicit clinit checks triggered by static invokes must have been pruned by
4336 // art::PrepareForRegisterAllocation.
4337 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004338
Andreas Gampe878d58c2015-01-15 23:24:00 -08004339 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4340 return;
4341 }
4342
Alexandre Ramesd921d642015-04-16 15:07:16 +01004343 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004344 LocationSummary* locations = invoke->GetLocations();
4345 codegen_->GenerateStaticOrDirectCall(
4346 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004347 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004348}
4349
4350void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004351 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4352 return;
4353 }
4354
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004355 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004356 DCHECK(!codegen_->IsLeafMethod());
4357 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4358}
4359
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004360HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4361 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004362 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004363 case HLoadClass::LoadKind::kInvalid:
4364 LOG(FATAL) << "UNREACHABLE";
4365 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004366 case HLoadClass::LoadKind::kReferrersClass:
4367 break;
4368 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4369 DCHECK(!GetCompilerOptions().GetCompilePic());
4370 break;
4371 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4372 DCHECK(GetCompilerOptions().GetCompilePic());
4373 break;
4374 case HLoadClass::LoadKind::kBootImageAddress:
4375 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004376 case HLoadClass::LoadKind::kBssEntry:
4377 DCHECK(!Runtime::Current()->UseJitCompilation());
4378 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004379 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004380 DCHECK(Runtime::Current()->UseJitCompilation());
4381 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004382 case HLoadClass::LoadKind::kDexCacheViaMethod:
4383 break;
4384 }
4385 return desired_class_load_kind;
4386}
4387
Alexandre Rames67555f72014-11-18 10:55:16 +00004388void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004389 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4390 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004391 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004392 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004393 cls,
4394 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004395 LocationFrom(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004396 return;
4397 }
Vladimir Marko41559982017-01-06 14:04:23 +00004398 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004399
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004400 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4401 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004402 ? LocationSummary::kCallOnSlowPath
4403 : LocationSummary::kNoCall;
4404 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004405 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004406 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004407 }
4408
Vladimir Marko41559982017-01-06 14:04:23 +00004409 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004410 locations->SetInAt(0, Location::RequiresRegister());
4411 }
4412 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004413}
4414
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004415// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4416// move.
4417void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004418 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4419 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4420 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004421 return;
4422 }
Vladimir Marko41559982017-01-06 14:04:23 +00004423 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004424
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004425 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004426 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004427
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004428 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4429 ? kWithoutReadBarrier
4430 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004431 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004432 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004433 case HLoadClass::LoadKind::kReferrersClass: {
4434 DCHECK(!cls->CanCallRuntime());
4435 DCHECK(!cls->MustGenerateClinitCheck());
4436 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4437 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004438 GenerateGcRootFieldLoad(cls,
4439 out_loc,
4440 current_method,
4441 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004442 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004443 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004444 break;
4445 }
4446 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004447 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004448 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4449 cls->GetTypeIndex()));
4450 break;
4451 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004452 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004453 // Add ADRP with its PC-relative type patch.
4454 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004455 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004456 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004457 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004458 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004459 vixl::aarch64::Label* add_label =
4460 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004461 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004462 break;
4463 }
4464 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004465 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004466 uint32_t address = dchecked_integral_cast<uint32_t>(
4467 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4468 DCHECK_NE(address, 0u);
4469 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004470 break;
4471 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004472 case HLoadClass::LoadKind::kBssEntry: {
4473 // Add ADRP with its PC-relative Class .bss entry patch.
4474 const DexFile& dex_file = cls->GetDexFile();
4475 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko1998cd02017-01-13 13:02:58 +00004476 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004477 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
4478 // Add LDR with its PC-relative Class patch.
4479 vixl::aarch64::Label* ldr_label =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004480 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004481 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4482 GenerateGcRootFieldLoad(cls,
4483 cls->GetLocations()->Out(),
4484 out.X(),
4485 /* placeholder */ 0u,
4486 ldr_label,
4487 kCompilerReadBarrierOption);
4488 generate_null_check = true;
4489 break;
4490 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004491 case HLoadClass::LoadKind::kJitTableAddress: {
4492 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4493 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004494 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004495 GenerateGcRootFieldLoad(cls,
4496 out_loc,
4497 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004498 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004499 /* fixup_label */ nullptr,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004500 kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004501 break;
4502 }
Vladimir Marko41559982017-01-06 14:04:23 +00004503 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004504 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004505 LOG(FATAL) << "UNREACHABLE";
4506 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004507 }
4508
4509 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4510 DCHECK(cls->CanCallRuntime());
4511 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4512 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4513 codegen_->AddSlowPath(slow_path);
4514 if (generate_null_check) {
4515 __ Cbz(out, slow_path->GetEntryLabel());
4516 }
4517 if (cls->MustGenerateClinitCheck()) {
4518 GenerateClassInitializationCheck(slow_path, out);
4519 } else {
4520 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004521 }
4522 }
4523}
4524
David Brazdilcb1c0552015-08-04 16:22:25 +01004525static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004526 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004527}
4528
Alexandre Rames67555f72014-11-18 10:55:16 +00004529void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4530 LocationSummary* locations =
4531 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4532 locations->SetOut(Location::RequiresRegister());
4533}
4534
4535void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004536 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4537}
4538
4539void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4540 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4541}
4542
4543void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4544 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004545}
4546
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004547HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4548 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004549 switch (desired_string_load_kind) {
4550 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4551 DCHECK(!GetCompilerOptions().GetCompilePic());
4552 break;
4553 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4554 DCHECK(GetCompilerOptions().GetCompilePic());
4555 break;
4556 case HLoadString::LoadKind::kBootImageAddress:
4557 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004558 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004559 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004560 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004561 case HLoadString::LoadKind::kJitTableAddress:
4562 DCHECK(Runtime::Current()->UseJitCompilation());
4563 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004564 case HLoadString::LoadKind::kDexCacheViaMethod:
4565 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004566 }
4567 return desired_string_load_kind;
4568}
4569
Alexandre Rames67555f72014-11-18 10:55:16 +00004570void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004571 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004573 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004574 InvokeRuntimeCallingConvention calling_convention;
4575 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4576 } else {
4577 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004578 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4579 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4580 // Rely on the pResolveString and/or marking to save everything, including temps.
4581 RegisterSet caller_saves = RegisterSet::Empty();
4582 InvokeRuntimeCallingConvention calling_convention;
4583 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4584 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4585 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4586 Primitive::kPrimNot).GetCode());
4587 locations->SetCustomSlowPathCallerSaves(caller_saves);
4588 } else {
4589 // For non-Baker read barrier we have a temp-clobbering call.
4590 }
4591 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004592 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004593}
4594
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004595// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4596// move.
4597void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004598 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004599 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004600
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004601 switch (load->GetLoadKind()) {
4602 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004603 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4604 load->GetStringIndex()));
4605 return; // No dex cache slow path.
4606 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004607 // Add ADRP with its PC-relative String patch.
4608 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004609 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004610 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004611 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004612 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004613 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004614 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004615 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004616 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004617 return; // No dex cache slow path.
4618 }
4619 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004620 uint32_t address = dchecked_integral_cast<uint32_t>(
4621 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4622 DCHECK_NE(address, 0u);
4623 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004624 return; // No dex cache slow path.
4625 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004626 case HLoadString::LoadKind::kBssEntry: {
4627 // Add ADRP with its PC-relative String .bss entry patch.
4628 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004629 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004630 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004631 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4632 Register temp = temps.AcquireX();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004633 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004634 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004635 // Add LDR with its PC-relative String patch.
4636 vixl::aarch64::Label* ldr_label =
4637 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004638 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004639 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004640 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004641 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004642 /* offset placeholder */ 0u,
4643 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004644 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004645 SlowPathCodeARM64* slow_path =
4646 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004647 codegen_->AddSlowPath(slow_path);
4648 __ Cbz(out.X(), slow_path->GetEntryLabel());
4649 __ Bind(slow_path->GetExitLabel());
4650 return;
4651 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004652 case HLoadString::LoadKind::kJitTableAddress: {
4653 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004654 load->GetStringIndex(),
4655 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004656 GenerateGcRootFieldLoad(load,
4657 out_loc,
4658 out.X(),
4659 /* offset */ 0,
4660 /* fixup_label */ nullptr,
4661 kCompilerReadBarrierOption);
4662 return;
4663 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004664 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004665 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004666 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004667
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004668 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004669 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004670 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004671 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004672 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4673 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004674}
4675
Alexandre Rames5319def2014-10-23 10:03:10 +01004676void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4677 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4678 locations->SetOut(Location::ConstantLocation(constant));
4679}
4680
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004681void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004682 // Will be generated at use site.
4683}
4684
Alexandre Rames67555f72014-11-18 10:55:16 +00004685void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4686 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004687 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004688 InvokeRuntimeCallingConvention calling_convention;
4689 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4690}
4691
4692void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004693 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004694 instruction,
4695 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004696 if (instruction->IsEnter()) {
4697 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4698 } else {
4699 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4700 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004701}
4702
Alexandre Rames42d641b2014-10-27 14:00:51 +00004703void LocationsBuilderARM64::VisitMul(HMul* mul) {
4704 LocationSummary* locations =
4705 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4706 switch (mul->GetResultType()) {
4707 case Primitive::kPrimInt:
4708 case Primitive::kPrimLong:
4709 locations->SetInAt(0, Location::RequiresRegister());
4710 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004712 break;
4713
4714 case Primitive::kPrimFloat:
4715 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004716 locations->SetInAt(0, Location::RequiresFpuRegister());
4717 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004718 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004719 break;
4720
4721 default:
4722 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4723 }
4724}
4725
4726void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4727 switch (mul->GetResultType()) {
4728 case Primitive::kPrimInt:
4729 case Primitive::kPrimLong:
4730 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4731 break;
4732
4733 case Primitive::kPrimFloat:
4734 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004735 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004736 break;
4737
4738 default:
4739 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4740 }
4741}
4742
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004743void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4744 LocationSummary* locations =
4745 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4746 switch (neg->GetResultType()) {
4747 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004748 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004749 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004751 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004752
4753 case Primitive::kPrimFloat:
4754 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004755 locations->SetInAt(0, Location::RequiresFpuRegister());
4756 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004757 break;
4758
4759 default:
4760 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4761 }
4762}
4763
4764void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4765 switch (neg->GetResultType()) {
4766 case Primitive::kPrimInt:
4767 case Primitive::kPrimLong:
4768 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4769 break;
4770
4771 case Primitive::kPrimFloat:
4772 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004773 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004774 break;
4775
4776 default:
4777 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4778 }
4779}
4780
4781void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4782 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004783 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004784 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004785 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004786 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4787 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004788}
4789
4790void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004791 // Note: if heap poisoning is enabled, the entry point takes cares
4792 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004793 QuickEntrypointEnum entrypoint =
4794 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4795 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004796 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004797}
4798
Alexandre Rames5319def2014-10-23 10:03:10 +01004799void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4800 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004801 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004802 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004803 if (instruction->IsStringAlloc()) {
4804 locations->AddTemp(LocationFrom(kArtMethodRegister));
4805 } else {
4806 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004807 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004808 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4809}
4810
4811void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004812 // Note: if heap poisoning is enabled, the entry point takes cares
4813 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004814 if (instruction->IsStringAlloc()) {
4815 // String is allocated through StringFactory. Call NewEmptyString entry point.
4816 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004817 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004818 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4819 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4820 __ Blr(lr);
4821 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4822 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004823 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004824 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004825 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004826}
4827
4828void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4829 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004830 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004831 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004832}
4833
4834void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004835 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004836 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004837 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004838 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004839 break;
4840
4841 default:
4842 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4843 }
4844}
4845
David Brazdil66d126e2015-04-03 16:02:44 +01004846void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4847 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4848 locations->SetInAt(0, Location::RequiresRegister());
4849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4850}
4851
4852void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004853 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004854}
4855
Alexandre Rames5319def2014-10-23 10:03:10 +01004856void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004857 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4858 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01004859}
4860
Calin Juravle2ae48182016-03-16 14:05:09 +00004861void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4862 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004863 return;
4864 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004865
Alexandre Ramesd921d642015-04-16 15:07:16 +01004866 BlockPoolsScope block_pools(GetVIXLAssembler());
4867 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004868 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004869 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004870}
4871
Calin Juravle2ae48182016-03-16 14:05:09 +00004872void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004873 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004874 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004875
4876 LocationSummary* locations = instruction->GetLocations();
4877 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004878
4879 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004880}
4881
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004882void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004883 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004884}
4885
Alexandre Rames67555f72014-11-18 10:55:16 +00004886void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4887 HandleBinaryOp(instruction);
4888}
4889
4890void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4891 HandleBinaryOp(instruction);
4892}
4893
Alexandre Rames3e69f162014-12-10 10:36:50 +00004894void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4895 LOG(FATAL) << "Unreachable";
4896}
4897
4898void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4899 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4900}
4901
Alexandre Rames5319def2014-10-23 10:03:10 +01004902void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4903 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4904 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4905 if (location.IsStackSlot()) {
4906 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4907 } else if (location.IsDoubleStackSlot()) {
4908 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4909 }
4910 locations->SetOut(location);
4911}
4912
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004913void InstructionCodeGeneratorARM64::VisitParameterValue(
4914 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004915 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004916}
4917
4918void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4919 LocationSummary* locations =
4920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004921 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004922}
4923
4924void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4925 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4926 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004927}
4928
4929void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004931 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004932 locations->SetInAt(i, Location::Any());
4933 }
4934 locations->SetOut(Location::Any());
4935}
4936
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004937void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004938 LOG(FATAL) << "Unreachable";
4939}
4940
Serban Constantinescu02164b32014-11-13 14:05:07 +00004941void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004942 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004943 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004944 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4945 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4947
4948 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004949 case Primitive::kPrimInt:
4950 case Primitive::kPrimLong:
4951 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004952 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4954 break;
4955
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004956 case Primitive::kPrimFloat:
4957 case Primitive::kPrimDouble: {
4958 InvokeRuntimeCallingConvention calling_convention;
4959 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4960 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4961 locations->SetOut(calling_convention.GetReturnLocation(type));
4962
4963 break;
4964 }
4965
Serban Constantinescu02164b32014-11-13 14:05:07 +00004966 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004967 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004968 }
4969}
4970
4971void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4972 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004973
Serban Constantinescu02164b32014-11-13 14:05:07 +00004974 switch (type) {
4975 case Primitive::kPrimInt:
4976 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004977 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004978 break;
4979 }
4980
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004981 case Primitive::kPrimFloat:
4982 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004983 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4984 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004985 if (type == Primitive::kPrimFloat) {
4986 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4987 } else {
4988 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4989 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004990 break;
4991 }
4992
Serban Constantinescu02164b32014-11-13 14:05:07 +00004993 default:
4994 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004995 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004996 }
4997}
4998
Calin Juravle27df7582015-04-17 19:12:31 +01004999void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5000 memory_barrier->SetLocations(nullptr);
5001}
5002
5003void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005004 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005005}
5006
Alexandre Rames5319def2014-10-23 10:03:10 +01005007void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5008 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5009 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005010 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005011}
5012
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005013void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005014 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005015}
5016
5017void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5018 instruction->SetLocations(nullptr);
5019}
5020
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005021void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005022 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005023}
5024
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005025void LocationsBuilderARM64::VisitRor(HRor* ror) {
5026 HandleBinaryOp(ror);
5027}
5028
5029void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5030 HandleBinaryOp(ror);
5031}
5032
Serban Constantinescu02164b32014-11-13 14:05:07 +00005033void LocationsBuilderARM64::VisitShl(HShl* shl) {
5034 HandleShift(shl);
5035}
5036
5037void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5038 HandleShift(shl);
5039}
5040
5041void LocationsBuilderARM64::VisitShr(HShr* shr) {
5042 HandleShift(shr);
5043}
5044
5045void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5046 HandleShift(shr);
5047}
5048
Alexandre Rames5319def2014-10-23 10:03:10 +01005049void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005050 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005051}
5052
5053void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005054 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005055}
5056
Alexandre Rames67555f72014-11-18 10:55:16 +00005057void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005058 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005059}
5060
5061void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005062 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005063}
5064
5065void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005066 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005067}
5068
Alexandre Rames67555f72014-11-18 10:55:16 +00005069void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005070 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005071}
5072
Calin Juravlee460d1d2015-09-29 04:52:17 +01005073void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5074 HUnresolvedInstanceFieldGet* instruction) {
5075 FieldAccessCallingConventionARM64 calling_convention;
5076 codegen_->CreateUnresolvedFieldLocationSummary(
5077 instruction, instruction->GetFieldType(), calling_convention);
5078}
5079
5080void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5081 HUnresolvedInstanceFieldGet* instruction) {
5082 FieldAccessCallingConventionARM64 calling_convention;
5083 codegen_->GenerateUnresolvedFieldAccess(instruction,
5084 instruction->GetFieldType(),
5085 instruction->GetFieldIndex(),
5086 instruction->GetDexPc(),
5087 calling_convention);
5088}
5089
5090void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5091 HUnresolvedInstanceFieldSet* instruction) {
5092 FieldAccessCallingConventionARM64 calling_convention;
5093 codegen_->CreateUnresolvedFieldLocationSummary(
5094 instruction, instruction->GetFieldType(), calling_convention);
5095}
5096
5097void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5098 HUnresolvedInstanceFieldSet* instruction) {
5099 FieldAccessCallingConventionARM64 calling_convention;
5100 codegen_->GenerateUnresolvedFieldAccess(instruction,
5101 instruction->GetFieldType(),
5102 instruction->GetFieldIndex(),
5103 instruction->GetDexPc(),
5104 calling_convention);
5105}
5106
5107void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5108 HUnresolvedStaticFieldGet* instruction) {
5109 FieldAccessCallingConventionARM64 calling_convention;
5110 codegen_->CreateUnresolvedFieldLocationSummary(
5111 instruction, instruction->GetFieldType(), calling_convention);
5112}
5113
5114void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5115 HUnresolvedStaticFieldGet* instruction) {
5116 FieldAccessCallingConventionARM64 calling_convention;
5117 codegen_->GenerateUnresolvedFieldAccess(instruction,
5118 instruction->GetFieldType(),
5119 instruction->GetFieldIndex(),
5120 instruction->GetDexPc(),
5121 calling_convention);
5122}
5123
5124void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5125 HUnresolvedStaticFieldSet* instruction) {
5126 FieldAccessCallingConventionARM64 calling_convention;
5127 codegen_->CreateUnresolvedFieldLocationSummary(
5128 instruction, instruction->GetFieldType(), calling_convention);
5129}
5130
5131void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5132 HUnresolvedStaticFieldSet* instruction) {
5133 FieldAccessCallingConventionARM64 calling_convention;
5134 codegen_->GenerateUnresolvedFieldAccess(instruction,
5135 instruction->GetFieldType(),
5136 instruction->GetFieldIndex(),
5137 instruction->GetDexPc(),
5138 calling_convention);
5139}
5140
Alexandre Rames5319def2014-10-23 10:03:10 +01005141void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005142 LocationSummary* locations =
5143 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005144 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005145}
5146
5147void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005148 HBasicBlock* block = instruction->GetBlock();
5149 if (block->GetLoopInformation() != nullptr) {
5150 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5151 // The back edge will generate the suspend check.
5152 return;
5153 }
5154 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5155 // The goto will generate the suspend check.
5156 return;
5157 }
5158 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005159}
5160
Alexandre Rames67555f72014-11-18 10:55:16 +00005161void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5162 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005164 InvokeRuntimeCallingConvention calling_convention;
5165 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5166}
5167
5168void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005169 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005170 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005171}
5172
5173void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5174 LocationSummary* locations =
5175 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5176 Primitive::Type input_type = conversion->GetInputType();
5177 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005178 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005179 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5180 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5181 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5182 }
5183
Alexandre Rames542361f2015-01-29 16:57:31 +00005184 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005185 locations->SetInAt(0, Location::RequiresFpuRegister());
5186 } else {
5187 locations->SetInAt(0, Location::RequiresRegister());
5188 }
5189
Alexandre Rames542361f2015-01-29 16:57:31 +00005190 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005191 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5192 } else {
5193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5194 }
5195}
5196
5197void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5198 Primitive::Type result_type = conversion->GetResultType();
5199 Primitive::Type input_type = conversion->GetInputType();
5200
5201 DCHECK_NE(input_type, result_type);
5202
Alexandre Rames542361f2015-01-29 16:57:31 +00005203 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005204 int result_size = Primitive::ComponentSize(result_type);
5205 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005206 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005207 Register output = OutputRegister(conversion);
5208 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005209 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005210 // 'int' values are used directly as W registers, discarding the top
5211 // bits, so we don't need to sign-extend and can just perform a move.
5212 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5213 // top 32 bits of the target register. We theoretically could leave those
5214 // bits unchanged, but we would have to make sure that no code uses a
5215 // 32bit input value as a 64bit value assuming that the top 32 bits are
5216 // zero.
5217 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005218 } else if (result_type == Primitive::kPrimChar ||
5219 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5220 __ Ubfx(output,
5221 output.IsX() ? source.X() : source.W(),
5222 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005223 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005224 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005225 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005226 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005227 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005228 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005229 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5230 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005231 } else if (Primitive::IsFloatingPointType(result_type) &&
5232 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005233 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5234 } else {
5235 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5236 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005237 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005238}
Alexandre Rames67555f72014-11-18 10:55:16 +00005239
Serban Constantinescu02164b32014-11-13 14:05:07 +00005240void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5241 HandleShift(ushr);
5242}
5243
5244void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5245 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005246}
5247
5248void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5249 HandleBinaryOp(instruction);
5250}
5251
5252void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5253 HandleBinaryOp(instruction);
5254}
5255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005256void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005257 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005258 LOG(FATAL) << "Unreachable";
5259}
5260
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005261void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005262 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005263 LOG(FATAL) << "Unreachable";
5264}
5265
Mark Mendellfe57faa2015-09-18 09:26:15 -04005266// Simple implementation of packed switch - generate cascaded compare/jumps.
5267void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5268 LocationSummary* locations =
5269 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5270 locations->SetInAt(0, Location::RequiresRegister());
5271}
5272
5273void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5274 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005275 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005276 Register value_reg = InputRegisterAt(switch_instr, 0);
5277 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5278
Zheng Xu3927c8b2015-11-18 17:46:25 +08005279 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005280 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005281 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5282 // make sure we don't emit it if the target may run out of range.
5283 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5284 // ranges and emit the tables only as required.
5285 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005286
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005287 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005288 // Current instruction id is an upper bound of the number of HIRs in the graph.
5289 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5290 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005291 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5292 Register temp = temps.AcquireW();
5293 __ Subs(temp, value_reg, Operand(lower_bound));
5294
Zheng Xu3927c8b2015-11-18 17:46:25 +08005295 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005296 // Jump to successors[0] if value == lower_bound.
5297 __ B(eq, codegen_->GetLabelOf(successors[0]));
5298 int32_t last_index = 0;
5299 for (; num_entries - last_index > 2; last_index += 2) {
5300 __ Subs(temp, temp, Operand(2));
5301 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5302 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5303 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5304 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5305 }
5306 if (num_entries - last_index == 2) {
5307 // The last missing case_value.
5308 __ Cmp(temp, Operand(1));
5309 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005310 }
5311
5312 // And the default for any other value.
5313 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5314 __ B(codegen_->GetLabelOf(default_block));
5315 }
5316 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005317 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005318
5319 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5320
5321 // Below instructions should use at most one blocked register. Since there are two blocked
5322 // registers, we are free to block one.
5323 Register temp_w = temps.AcquireW();
5324 Register index;
5325 // Remove the bias.
5326 if (lower_bound != 0) {
5327 index = temp_w;
5328 __ Sub(index, value_reg, Operand(lower_bound));
5329 } else {
5330 index = value_reg;
5331 }
5332
5333 // Jump to default block if index is out of the range.
5334 __ Cmp(index, Operand(num_entries));
5335 __ B(hs, codegen_->GetLabelOf(default_block));
5336
5337 // In current VIXL implementation, it won't require any blocked registers to encode the
5338 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5339 // register pressure.
5340 Register table_base = temps.AcquireX();
5341 // Load jump offset from the table.
5342 __ Adr(table_base, jump_table->GetTableStartLabel());
5343 Register jump_offset = temp_w;
5344 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5345
5346 // Jump to target block by branching to table_base(pc related) + offset.
5347 Register target_address = table_base;
5348 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5349 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005350 }
5351}
5352
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005353void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5354 HInstruction* instruction,
5355 Location out,
5356 uint32_t offset,
5357 Location maybe_temp,
5358 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005359 Primitive::Type type = Primitive::kPrimNot;
5360 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005361 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005362 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005363 Register temp_reg = RegisterFrom(maybe_temp, type);
5364 if (kUseBakerReadBarrier) {
5365 // Load with fast path based Baker's read barrier.
5366 // /* HeapReference<Object> */ out = *(out + offset)
5367 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5368 out,
5369 out_reg,
5370 offset,
5371 temp_reg,
5372 /* needs_null_check */ false,
5373 /* use_load_acquire */ false);
5374 } else {
5375 // Load with slow path based read barrier.
5376 // Save the value of `out` into `maybe_temp` before overwriting it
5377 // in the following move operation, as we will need it for the
5378 // read barrier below.
5379 __ Mov(temp_reg, out_reg);
5380 // /* HeapReference<Object> */ out = *(out + offset)
5381 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5382 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5383 }
5384 } else {
5385 // Plain load with no read barrier.
5386 // /* HeapReference<Object> */ out = *(out + offset)
5387 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5388 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5389 }
5390}
5391
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005392void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5393 HInstruction* instruction,
5394 Location out,
5395 Location obj,
5396 uint32_t offset,
5397 Location maybe_temp,
5398 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005399 Primitive::Type type = Primitive::kPrimNot;
5400 Register out_reg = RegisterFrom(out, type);
5401 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005402 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005403 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005404 if (kUseBakerReadBarrier) {
5405 // Load with fast path based Baker's read barrier.
5406 Register temp_reg = RegisterFrom(maybe_temp, type);
5407 // /* HeapReference<Object> */ out = *(obj + offset)
5408 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5409 out,
5410 obj_reg,
5411 offset,
5412 temp_reg,
5413 /* needs_null_check */ false,
5414 /* use_load_acquire */ false);
5415 } else {
5416 // Load with slow path based read barrier.
5417 // /* HeapReference<Object> */ out = *(obj + offset)
5418 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5419 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5420 }
5421 } else {
5422 // Plain load with no read barrier.
5423 // /* HeapReference<Object> */ out = *(obj + offset)
5424 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5425 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5426 }
5427}
5428
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005429void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5430 HInstruction* instruction,
5431 Location root,
5432 Register obj,
5433 uint32_t offset,
5434 vixl::aarch64::Label* fixup_label,
5435 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005436 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005437 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005438 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005439 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005440 if (kUseBakerReadBarrier) {
5441 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5442 // Baker's read barrier are used:
5443 //
5444 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005445 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5446 // if (temp != null) {
5447 // root = temp(root)
Roland Levillain44015862016-01-22 11:47:17 +00005448 // }
5449
5450 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005451 if (fixup_label == nullptr) {
5452 __ Ldr(root_reg, MemOperand(obj, offset));
5453 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005454 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005455 }
Roland Levillain44015862016-01-22 11:47:17 +00005456 static_assert(
5457 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5458 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5459 "have different sizes.");
5460 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5461 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5462 "have different sizes.");
5463
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005464 Register temp = lr;
Roland Levillain44015862016-01-22 11:47:17 +00005465
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005466 // Slow path marking the GC root `root`. The entrypoint will alrady be loaded in temp.
5467 SlowPathCodeARM64* slow_path =
5468 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction,
5469 root,
5470 LocationFrom(temp));
5471 codegen_->AddSlowPath(slow_path);
5472 const int32_t entry_point_offset =
5473 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5474 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5475 // Loading the entrypoint does not require a load acquire since it is only changed when
5476 // threads are suspended or running a checkpoint.
5477 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5478 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5479 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005480 __ Cbnz(temp, slow_path->GetEntryLabel());
5481 __ Bind(slow_path->GetExitLabel());
5482 } else {
5483 // GC root loaded through a slow path for read barriers other
5484 // than Baker's.
5485 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005486 if (fixup_label == nullptr) {
5487 __ Add(root_reg.X(), obj.X(), offset);
5488 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005489 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005490 }
Roland Levillain44015862016-01-22 11:47:17 +00005491 // /* mirror::Object* */ root = root->Read()
5492 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5493 }
5494 } else {
5495 // Plain GC root load with no read barrier.
5496 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005497 if (fixup_label == nullptr) {
5498 __ Ldr(root_reg, MemOperand(obj, offset));
5499 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005500 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005501 }
Roland Levillain44015862016-01-22 11:47:17 +00005502 // Note that GC roots are not affected by heap poisoning, thus we
5503 // do not have to unpoison `root_reg` here.
5504 }
5505}
5506
5507void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5508 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005509 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005510 uint32_t offset,
5511 Register temp,
5512 bool needs_null_check,
5513 bool use_load_acquire) {
5514 DCHECK(kEmitCompilerReadBarrier);
5515 DCHECK(kUseBakerReadBarrier);
5516
5517 // /* HeapReference<Object> */ ref = *(obj + offset)
5518 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005519 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005520 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5521 ref,
5522 obj,
5523 offset,
5524 no_index,
5525 no_scale_factor,
5526 temp,
5527 needs_null_check,
5528 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005529}
5530
5531void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5532 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005533 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005534 uint32_t data_offset,
5535 Location index,
5536 Register temp,
5537 bool needs_null_check) {
5538 DCHECK(kEmitCompilerReadBarrier);
5539 DCHECK(kUseBakerReadBarrier);
5540
5541 // Array cells are never volatile variables, therefore array loads
5542 // never use Load-Acquire instructions on ARM64.
5543 const bool use_load_acquire = false;
5544
Roland Levillainbfea3352016-06-23 13:48:47 +01005545 static_assert(
5546 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5547 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005548 // /* HeapReference<Object> */ ref =
5549 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005550 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5551 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5552 ref,
5553 obj,
5554 data_offset,
5555 index,
5556 scale_factor,
5557 temp,
5558 needs_null_check,
5559 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005560}
5561
5562void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5563 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005564 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005565 uint32_t offset,
5566 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005567 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005568 Register temp,
5569 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005570 bool use_load_acquire,
5571 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005572 DCHECK(kEmitCompilerReadBarrier);
5573 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005574 // If we are emitting an array load, we should not be using a
5575 // Load Acquire instruction. In other words:
5576 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5577 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005578
5579 MacroAssembler* masm = GetVIXLAssembler();
5580 UseScratchRegisterScope temps(masm);
5581
5582 // In slow path based read barriers, the read barrier call is
5583 // inserted after the original load. However, in fast path based
5584 // Baker's read barriers, we need to perform the load of
5585 // mirror::Object::monitor_ *before* the original reference load.
5586 // This load-load ordering is required by the read barrier.
5587 // The fast path/slow path (for Baker's algorithm) should look like:
5588 //
5589 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5590 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5591 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005592 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain44015862016-01-22 11:47:17 +00005593 // if (is_gray) {
5594 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5595 // }
5596 //
5597 // Note: the original implementation in ReadBarrier::Barrier is
5598 // slightly more complex as it performs additional checks that we do
5599 // not do here for performance reasons.
5600
5601 Primitive::Type type = Primitive::kPrimNot;
5602 Register ref_reg = RegisterFrom(ref, type);
5603 DCHECK(obj.IsW());
5604 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5605
5606 // /* int32_t */ monitor = obj->monitor_
5607 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5608 if (needs_null_check) {
5609 MaybeRecordImplicitNullCheck(instruction);
5610 }
5611 // /* LockWord */ lock_word = LockWord(monitor)
5612 static_assert(sizeof(LockWord) == sizeof(int32_t),
5613 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005614
Vladimir Marko877a0332016-07-11 19:30:56 +01005615 // Introduce a dependency on the lock_word including rb_state,
5616 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005617 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005618 // `obj` is unchanged by this operation, but its value now depends
5619 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005620 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005621
5622 // The actual reference load.
5623 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005624 // Load types involving an "index": ArrayGet,
5625 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5626 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005627 if (use_load_acquire) {
5628 // UnsafeGetObjectVolatile intrinsic case.
5629 // Register `index` is not an index in an object array, but an
5630 // offset to an object reference field within object `obj`.
5631 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5632 DCHECK(instruction->GetLocations()->Intrinsified());
5633 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5634 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005635 DCHECK_EQ(offset, 0u);
5636 DCHECK_EQ(scale_factor, 0u);
5637 DCHECK_EQ(needs_null_check, 0u);
Roland Levillainbfea3352016-06-23 13:48:47 +01005638 // /* HeapReference<Object> */ ref = *(obj + index)
5639 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5640 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005641 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005642 // ArrayGet and UnsafeGetObject intrinsics cases.
5643 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5644 if (index.IsConstant()) {
5645 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5646 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5647 } else {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005648 Register temp3 = temps.AcquireW();
5649 __ Add(temp3, obj, offset);
5650 Load(type, ref_reg, HeapOperand(temp3, XRegisterFrom(index), LSL, scale_factor));
5651 temps.Release(temp3);
Roland Levillainbfea3352016-06-23 13:48:47 +01005652 }
Roland Levillain44015862016-01-22 11:47:17 +00005653 }
Roland Levillain44015862016-01-22 11:47:17 +00005654 } else {
5655 // /* HeapReference<Object> */ ref = *(obj + offset)
5656 MemOperand field = HeapOperand(obj, offset);
5657 if (use_load_acquire) {
5658 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5659 } else {
5660 Load(type, ref_reg, field);
5661 }
5662 }
5663
5664 // Object* ref = ref_addr->AsMirrorPtr()
5665 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5666
Vladimir Marko953437b2016-08-24 08:30:46 +00005667 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005668 SlowPathCodeARM64* slow_path;
5669 if (always_update_field) {
5670 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5671 // address of the form `obj + field_offset`, where `obj` is a
5672 // register and `field_offset` is a register. Thus `offset` and
5673 // `scale_factor` above are expected to be null in this code path.
5674 DCHECK_EQ(offset, 0u);
5675 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5676 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5677 instruction, ref, obj, /* field_offset */ index, temp);
5678 } else {
5679 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
5680 }
Roland Levillain44015862016-01-22 11:47:17 +00005681 AddSlowPath(slow_path);
5682
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005683 // if (rb_state == ReadBarrier::GrayState())
Roland Levillain44015862016-01-22 11:47:17 +00005684 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005685 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07005686 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5687 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko877a0332016-07-11 19:30:56 +01005688 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005689 __ Bind(slow_path->GetExitLabel());
5690}
5691
5692void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5693 Location out,
5694 Location ref,
5695 Location obj,
5696 uint32_t offset,
5697 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005698 DCHECK(kEmitCompilerReadBarrier);
5699
Roland Levillain44015862016-01-22 11:47:17 +00005700 // Insert a slow path based read barrier *after* the reference load.
5701 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005702 // If heap poisoning is enabled, the unpoisoning of the loaded
5703 // reference will be carried out by the runtime within the slow
5704 // path.
5705 //
5706 // Note that `ref` currently does not get unpoisoned (when heap
5707 // poisoning is enabled), which is alright as the `ref` argument is
5708 // not used by the artReadBarrierSlow entry point.
5709 //
5710 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5711 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5712 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5713 AddSlowPath(slow_path);
5714
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005715 __ B(slow_path->GetEntryLabel());
5716 __ Bind(slow_path->GetExitLabel());
5717}
5718
Roland Levillain44015862016-01-22 11:47:17 +00005719void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5720 Location out,
5721 Location ref,
5722 Location obj,
5723 uint32_t offset,
5724 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005725 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005726 // Baker's read barriers shall be handled by the fast path
5727 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5728 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005729 // If heap poisoning is enabled, unpoisoning will be taken care of
5730 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005731 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005732 } else if (kPoisonHeapReferences) {
5733 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5734 }
5735}
5736
Roland Levillain44015862016-01-22 11:47:17 +00005737void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5738 Location out,
5739 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005740 DCHECK(kEmitCompilerReadBarrier);
5741
Roland Levillain44015862016-01-22 11:47:17 +00005742 // Insert a slow path based read barrier *after* the GC root load.
5743 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005744 // Note that GC roots are not affected by heap poisoning, so we do
5745 // not need to do anything special for this here.
5746 SlowPathCodeARM64* slow_path =
5747 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5748 AddSlowPath(slow_path);
5749
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005750 __ B(slow_path->GetEntryLabel());
5751 __ Bind(slow_path->GetExitLabel());
5752}
5753
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005754void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5755 LocationSummary* locations =
5756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5757 locations->SetInAt(0, Location::RequiresRegister());
5758 locations->SetOut(Location::RequiresRegister());
5759}
5760
5761void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5762 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005763 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005764 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005765 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005766 __ Ldr(XRegisterFrom(locations->Out()),
5767 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005768 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005769 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005770 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005771 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5772 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005773 __ Ldr(XRegisterFrom(locations->Out()),
5774 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005775 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005776}
5777
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005778static void PatchJitRootUse(uint8_t* code,
5779 const uint8_t* roots_data,
5780 vixl::aarch64::Literal<uint32_t>* literal,
5781 uint64_t index_in_table) {
5782 uint32_t literal_offset = literal->GetOffset();
5783 uintptr_t address =
5784 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
5785 uint8_t* data = code + literal_offset;
5786 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
5787}
5788
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005789void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
5790 for (const auto& entry : jit_string_patches_) {
5791 const auto& it = jit_string_roots_.find(entry.first);
5792 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005793 PatchJitRootUse(code, roots_data, entry.second, it->second);
5794 }
5795 for (const auto& entry : jit_class_patches_) {
5796 const auto& it = jit_class_roots_.find(entry.first);
5797 DCHECK(it != jit_class_roots_.end());
5798 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005799 }
5800}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005801
Alexandre Rames67555f72014-11-18 10:55:16 +00005802#undef __
5803#undef QUICK_ENTRY_POINT
5804
Alexandre Rames5319def2014-10-23 10:03:10 +01005805} // namespace arm64
5806} // namespace art